17
17
*******************************************************************************/
18
18
package cz .muni .ics .oauth2 .model ;
19
19
20
+ import static cz .muni .ics .oauth2 .model .AuthenticationHolderEntity .QUERY_ALL ;
21
+ import static cz .muni .ics .oauth2 .model .AuthenticationHolderEntity .QUERY_GET_UNUSED ;
22
+
20
23
import cz .muni .ics .oauth2 .model .convert .SerializableStringConverter ;
21
24
import cz .muni .ics .oauth2 .model .convert .SimpleGrantedAuthorityStringConverter ;
22
25
import java .io .Serializable ;
25
28
import java .util .HashSet ;
26
29
import java .util .Map ;
27
30
import java .util .Set ;
28
- import javax .persistence .Basic ;
29
31
import javax .persistence .CascadeType ;
30
32
import javax .persistence .CollectionTable ;
31
33
import javax .persistence .Column ;
43
45
import javax .persistence .OneToOne ;
44
46
import javax .persistence .Table ;
45
47
import javax .persistence .Transient ;
48
+ import lombok .AllArgsConstructor ;
49
+ import lombok .EqualsAndHashCode ;
50
+ import lombok .Getter ;
51
+ import lombok .NoArgsConstructor ;
52
+ import lombok .Setter ;
53
+ import lombok .ToString ;
54
+ import org .eclipse .persistence .annotations .CascadeOnDelete ;
46
55
import org .springframework .security .core .GrantedAuthority ;
47
56
import org .springframework .security .oauth2 .provider .OAuth2Authentication ;
48
57
import org .springframework .security .oauth2 .provider .OAuth2Request ;
49
58
59
+ @ Getter
60
+ @ Setter
61
+ @ ToString
62
+ @ EqualsAndHashCode
63
+ @ NoArgsConstructor
64
+ @ AllArgsConstructor
65
+ // DB ANNOTATIONS
50
66
@ Entity
51
67
@ Table (name = "authentication_holder" )
52
68
@ NamedQueries ({
53
- @ NamedQuery (name = AuthenticationHolderEntity .QUERY_ALL , query = "select a from AuthenticationHolderEntity a" ),
54
- @ NamedQuery (name = AuthenticationHolderEntity .QUERY_GET_UNUSED , query = "select a from AuthenticationHolderEntity a where " +
55
- "a.id not in (select t.authenticationHolder.id from OAuth2AccessTokenEntity t) and " +
56
- "a.id not in (select r.authenticationHolder.id from OAuth2RefreshTokenEntity r) and " +
57
- "a.id not in (select c.authenticationHolder.id from AuthorizationCodeEntity c)" )
69
+ @ NamedQuery (name = QUERY_ALL ,
70
+ query = "SELECT a FROM AuthenticationHolderEntity a" ),
71
+ @ NamedQuery (name = QUERY_GET_UNUSED ,
72
+ query = "SELECT a FROM AuthenticationHolderEntity a " +
73
+ "WHERE a.id NOT IN (SELECT t.authenticationHolder.id FROM OAuth2AccessTokenEntity t) " +
74
+ "AND a.id NOT IN (SELECT r.authenticationHolder.id FROM OAuth2RefreshTokenEntity r) " +
75
+ "AND a.id NOT IN (SELECT c.authenticationHolder.id FROM AuthorizationCodeEntity c)" )
58
76
})
59
77
public class AuthenticationHolderEntity {
60
78
61
79
public static final String QUERY_GET_UNUSED = "AuthenticationHolderEntity.getUnusedAuthenticationHolders" ;
62
80
public static final String QUERY_ALL = "AuthenticationHolderEntity.getAll" ;
63
81
82
+ @ Id
83
+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
84
+ @ Column (name = "id" )
64
85
private Long id ;
86
+
87
+ @ OneToOne (cascade =CascadeType .ALL )
88
+ @ JoinColumn (name = "user_auth_id" )
89
+ @ CascadeOnDelete
65
90
private SavedUserAuthentication userAuth ;
91
+
92
+ @ ElementCollection (fetch = FetchType .EAGER )
93
+ @ CollectionTable (name = "authentication_holder_authority" , joinColumns = @ JoinColumn (name = "owner_id" ))
94
+ @ Convert (converter = SimpleGrantedAuthorityStringConverter .class )
95
+ @ Column (name = "authority" )
96
+ @ CascadeOnDelete
66
97
private Collection <GrantedAuthority > authorities ;
98
+
99
+ @ ElementCollection (fetch = FetchType .EAGER )
100
+ @ CollectionTable (name = "authentication_holder_resource_id" , joinColumns = @ JoinColumn (name = "owner_id" ))
101
+ @ Column (name = "resource_id" )
102
+ @ CascadeOnDelete
67
103
private Set <String > resourceIds ;
104
+
105
+ @ Column (name = "approved" )
68
106
private boolean approved ;
107
+
108
+ @ Column (name = "redirect_uri" )
69
109
private String redirectUri ;
110
+
111
+ @ ElementCollection (fetch = FetchType .EAGER )
112
+ @ CollectionTable (name = "authentication_holder_response_type" , joinColumns = @ JoinColumn (name = "owner_id" ))
113
+ @ Column (name = "response_type" )
114
+ @ CascadeOnDelete
70
115
private Set <String > responseTypes ;
116
+
117
+ @ ElementCollection (fetch = FetchType .EAGER )
118
+ @ CollectionTable (name = "authentication_holder_extension" , joinColumns = @ JoinColumn (name = "owner_id" ))
119
+ @ Column (name = "val" )
120
+ @ MapKeyColumn (name = "extension" )
121
+ @ Convert (converter = SerializableStringConverter .class )
122
+ @ CascadeOnDelete
71
123
private Map <String , Serializable > extensions ;
72
- private String clientId ;
73
- private Set <String > scope ;
74
- private Map <String , String > requestParameters ;
75
124
76
- public AuthenticationHolderEntity () { }
125
+ @ Column (name = "client_id" )
126
+ private String clientId ;
77
127
78
- @ Id
79
- @ GeneratedValue (strategy = GenerationType .IDENTITY )
80
- @ Column (name = "id" )
81
- public Long getId () {
82
- return id ;
83
- }
128
+ @ ElementCollection (fetch = FetchType .EAGER )
129
+ @ CollectionTable (name = "authentication_holder_scope" , joinColumns = @ JoinColumn (name = "owner_id" ))
130
+ @ Column (name = "scope" )
131
+ @ CascadeOnDelete
132
+ private Set <String > scope ;
84
133
85
- public void setId (Long id ) {
86
- this .id = id ;
87
- }
134
+ @ ElementCollection (fetch = FetchType .EAGER )
135
+ @ CollectionTable (name = "authentication_holder_request_parameter" , joinColumns = @ JoinColumn (name = "owner_id" ))
136
+ @ Column (name = "val" )
137
+ @ MapKeyColumn (name = "param" )
138
+ @ CascadeOnDelete
139
+ private Map <String , String > requestParameters ;
88
140
89
141
@ Transient
90
142
public OAuth2Authentication getAuthentication () {
91
143
// TODO: memoize this
92
144
return new OAuth2Authentication (createOAuth2Request (), getUserAuth ());
93
145
}
94
146
95
- private OAuth2Request createOAuth2Request () {
96
- return new OAuth2Request (requestParameters , clientId , authorities , approved , scope , resourceIds , redirectUri , responseTypes , extensions );
97
- }
98
-
99
147
public void setAuthentication (OAuth2Authentication authentication ) {
100
148
// pull apart the request and save its bits
101
149
OAuth2Request o2Request = authentication .getOAuth2Request ();
102
- setAuthorities (o2Request . getAuthorities () == null ? null : new HashSet <>( o2Request .getAuthorities ()));
150
+ setAuthorities (convertToSetOrNull (( Set < GrantedAuthority >) o2Request .getAuthorities ()));
103
151
setClientId (o2Request .getClientId ());
104
- setExtensions (o2Request . getExtensions () == null ? null : new HashMap <> (o2Request .getExtensions ()));
152
+ setExtensions (convertToMapOrNull (o2Request .getExtensions ()));
105
153
setRedirectUri (o2Request .getRedirectUri ());
106
- setRequestParameters (o2Request . getRequestParameters () == null ? null : new HashMap <> (o2Request .getRequestParameters ()));
107
- setResourceIds (o2Request . getResourceIds () == null ? null : new HashSet <> (o2Request .getResourceIds ()));
108
- setResponseTypes (o2Request . getResponseTypes () == null ? null : new HashSet <> (o2Request .getResponseTypes ()));
109
- setScope (o2Request . getScope () == null ? null : new HashSet <> (o2Request .getScope ()));
154
+ setRequestParameters (convertToMapOrNull (o2Request .getRequestParameters ()));
155
+ setResourceIds (convertToSetOrNull (o2Request .getResourceIds ()));
156
+ setResponseTypes (convertToSetOrNull (o2Request .getResponseTypes ()));
157
+ setScope (convertToSetOrNull (o2Request .getScope ()));
110
158
setApproved (o2Request .isApproved ());
111
159
112
160
if (authentication .getUserAuthentication () != null ) {
@@ -116,114 +164,16 @@ public void setAuthentication(OAuth2Authentication authentication) {
116
164
}
117
165
}
118
166
119
- @ OneToOne (cascade =CascadeType .ALL )
120
- @ JoinColumn (name = "user_auth_id" )
121
- public SavedUserAuthentication getUserAuth () {
122
- return userAuth ;
123
- }
124
-
125
- public void setUserAuth (SavedUserAuthentication userAuth ) {
126
- this .userAuth = userAuth ;
127
- }
128
-
129
- @ ElementCollection (fetch = FetchType .EAGER )
130
- @ CollectionTable (name ="authentication_holder_authority" , joinColumns =@ JoinColumn (name ="owner_id" ))
131
- @ Convert (converter = SimpleGrantedAuthorityStringConverter .class )
132
- @ Column (name ="authority" )
133
- public Collection <GrantedAuthority > getAuthorities () {
134
- return authorities ;
135
- }
136
-
137
- public void setAuthorities (Collection <GrantedAuthority > authorities ) {
138
- this .authorities = authorities ;
139
- }
140
-
141
- @ ElementCollection (fetch = FetchType .EAGER )
142
- @ CollectionTable (name ="authentication_holder_resource_id" , joinColumns =@ JoinColumn (name ="owner_id" ))
143
- @ Column (name ="resource_id" )
144
- public Set <String > getResourceIds () {
145
- return resourceIds ;
146
- }
147
-
148
- public void setResourceIds (Set <String > resourceIds ) {
149
- this .resourceIds = resourceIds ;
150
- }
151
-
152
- @ Basic
153
- @ Column (name ="approved" )
154
- public boolean isApproved () {
155
- return approved ;
156
- }
157
-
158
- public void setApproved (boolean approved ) {
159
- this .approved = approved ;
160
- }
161
-
162
- @ Basic
163
- @ Column (name ="redirect_uri" )
164
- public String getRedirectUri () {
165
- return redirectUri ;
166
- }
167
-
168
- public void setRedirectUri (String redirectUri ) {
169
- this .redirectUri = redirectUri ;
170
- }
171
-
172
- @ ElementCollection (fetch = FetchType .EAGER )
173
- @ CollectionTable (name ="authentication_holder_response_type" , joinColumns =@ JoinColumn (name ="owner_id" ))
174
- @ Column (name ="response_type" )
175
- public Set <String > getResponseTypes () {
176
- return responseTypes ;
177
- }
178
-
179
- public void setResponseTypes (Set <String > responseTypes ) {
180
- this .responseTypes = responseTypes ;
181
- }
182
-
183
- @ ElementCollection (fetch = FetchType .EAGER )
184
- @ CollectionTable (name ="authentication_holder_extension" , joinColumns =@ JoinColumn (name ="owner_id" ))
185
- @ Column (name ="val" )
186
- @ MapKeyColumn (name ="extension" )
187
- @ Convert (converter = SerializableStringConverter .class )
188
- public Map <String , Serializable > getExtensions () {
189
- return extensions ;
190
- }
191
-
192
- public void setExtensions (Map <String , Serializable > extensions ) {
193
- this .extensions = extensions ;
194
- }
195
-
196
- @ Basic
197
- @ Column (name ="client_id" )
198
- public String getClientId () {
199
- return clientId ;
167
+ private <T > Set <T > convertToSetOrNull (Collection <T > obj ) {
168
+ return obj == null ? null : new HashSet <>(obj );
200
169
}
201
170
202
- public void setClientId ( String clientId ) {
203
- this . clientId = clientId ;
171
+ private < T , S > Map < T , S > convertToMapOrNull ( Map < T , S > obj ) {
172
+ return obj == null ? null : new HashMap <>( obj ) ;
204
173
}
205
174
206
- @ ElementCollection (fetch = FetchType .EAGER )
207
- @ CollectionTable (name ="authentication_holder_scope" , joinColumns =@ JoinColumn (name ="owner_id" ))
208
- @ Column (name ="scope" )
209
- public Set <String > getScope () {
210
- return scope ;
211
- }
212
-
213
- public void setScope (Set <String > scope ) {
214
- this .scope = scope ;
215
- }
216
-
217
- @ ElementCollection (fetch = FetchType .EAGER )
218
- @ CollectionTable (name ="authentication_holder_request_parameter" , joinColumns =@ JoinColumn (name ="owner_id" ))
219
- @ Column (name ="val" )
220
- @ MapKeyColumn (name ="param" )
221
- public Map <String , String > getRequestParameters () {
222
- return requestParameters ;
223
- }
224
-
225
- public void setRequestParameters (Map <String , String > requestParameters ) {
226
- this .requestParameters = requestParameters ;
175
+ private OAuth2Request createOAuth2Request () {
176
+ return new OAuth2Request (requestParameters , clientId , authorities , approved , scope , resourceIds , redirectUri , responseTypes , extensions );
227
177
}
228
178
229
179
}
0 commit comments