001/******************************************************************************* 002 * Copyright 2014 The MITRE Corporation 003 * and the MIT Kerberos and Internet Trust Consortium 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 ******************************************************************************/ 017/** 018 * 019 */ 020package org.mitre.oauth2.model; 021 022import java.text.ParseException; 023import java.util.Date; 024 025import javax.persistence.Basic; 026import javax.persistence.Column; 027import javax.persistence.Entity; 028import javax.persistence.FetchType; 029import javax.persistence.GeneratedValue; 030import javax.persistence.GenerationType; 031import javax.persistence.Id; 032import javax.persistence.JoinColumn; 033import javax.persistence.ManyToOne; 034import javax.persistence.NamedQueries; 035import javax.persistence.NamedQuery; 036import javax.persistence.Table; 037import javax.persistence.Temporal; 038import javax.persistence.Transient; 039 040import org.springframework.security.oauth2.common.OAuth2RefreshToken; 041 042import com.nimbusds.jwt.JWT; 043import com.nimbusds.jwt.JWTParser; 044 045/** 046 * @author jricher 047 * 048 */ 049@Entity 050@Table(name = "refresh_token") 051@NamedQueries({ 052 @NamedQuery(name = "OAuth2RefreshTokenEntity.getAll", query = "select r from OAuth2RefreshTokenEntity r"), 053 @NamedQuery(name = "OAuth2RefreshTokenEntity.getAllExpiredByDate", query = "select r from OAuth2RefreshTokenEntity r where r.expiration <= :date"), 054 @NamedQuery(name = "OAuth2RefreshTokenEntity.getByClient", query = "select r from OAuth2RefreshTokenEntity r where r.client = :client"), 055 @NamedQuery(name = "OAuth2RefreshTokenEntity.getByTokenValue", query = "select r from OAuth2RefreshTokenEntity r where r.value = :tokenValue"), 056 @NamedQuery(name = "OAuth2RefreshTokenEntity.getByAuthentication", query = "select r from OAuth2RefreshTokenEntity r where r.authenticationHolder.authentication = :authentication") 057}) 058public class OAuth2RefreshTokenEntity implements OAuth2RefreshToken { 059 060 private Long id; 061 062 private AuthenticationHolderEntity authenticationHolder; 063 064 private ClientDetailsEntity client; 065 066 //JWT-encoded representation of this access token entity 067 private JWT jwt; 068 069 // our refresh tokens might expire 070 private Date expiration; 071 072 /** 073 * 074 */ 075 public OAuth2RefreshTokenEntity() { 076 077 } 078 079 /** 080 * @return the id 081 */ 082 @Id 083 @GeneratedValue(strategy = GenerationType.IDENTITY) 084 @Column(name = "id") 085 public Long getId() { 086 return id; 087 } 088 089 /** 090 * @param id the id to set 091 */ 092 public void setId(Long id) { 093 this.id = id; 094 } 095 096 /** 097 * The authentication in place when the original access token was 098 * created 099 * 100 * @return the authentication 101 */ 102 @ManyToOne 103 @JoinColumn(name = "auth_holder_id") 104 public AuthenticationHolderEntity getAuthenticationHolder() { 105 return authenticationHolder; 106 } 107 108 /** 109 * @param authentication the authentication to set 110 */ 111 public void setAuthenticationHolder(AuthenticationHolderEntity authenticationHolder) { 112 this.authenticationHolder = authenticationHolder; 113 } 114 115 /** 116 * Get the JWT-encoded value of this token 117 */ 118 @Override 119 @Basic 120 @Column(name="token_value") 121 public String getValue() { 122 return jwt.serialize(); 123 } 124 125 /** 126 * Set the value of this token as a string. Parses the string into a JWT. 127 * @param value 128 * @throws ParseException if the value is not a valid JWT string 129 */ 130 public void setValue(String value) throws ParseException { 131 setJwt(JWTParser.parse(value)); 132 } 133 134 @Basic 135 @Temporal(javax.persistence.TemporalType.TIMESTAMP) 136 @Column(name = "expiration") 137 public Date getExpiration() { 138 return expiration; 139 } 140 141 /* (non-Javadoc) 142 * @see org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken#setExpiration(java.util.Date) 143 */ 144 145 public void setExpiration(Date expiration) { 146 this.expiration = expiration; 147 } 148 149 /** 150 * Has this token expired? 151 * @return true if it has a timeout set and the timeout has passed 152 */ 153 @Transient 154 public boolean isExpired() { 155 return getExpiration() == null ? false : System.currentTimeMillis() > getExpiration().getTime(); 156 } 157 158 /** 159 * @return the client 160 */ 161 @ManyToOne(fetch = FetchType.EAGER) 162 @JoinColumn(name = "client_id") 163 public ClientDetailsEntity getClient() { 164 return client; 165 } 166 167 /** 168 * @param client the client to set 169 */ 170 public void setClient(ClientDetailsEntity client) { 171 this.client = client; 172 } 173 174 /** 175 * Get the JWT object directly 176 * @return the jwt 177 */ 178 @Transient 179 public JWT getJwt() { 180 return jwt; 181 } 182 183 /** 184 * @param jwt the jwt to set 185 */ 186 public void setJwt(JWT jwt) { 187 this.jwt = jwt; 188 } 189 190}