Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit b2599ce

Browse files
author
Dominik Frantisek Bucik
committed
refactor: 💡 Updated some DB entities, added missing cascades
Updated AuthenticationHolder, AuthorizationCode, DeviceCode, AccessToken, RefreshToken, UserAuthN representations of DB entries. Added missing cascades for some elements, to prevent leaving orphaned records in the DB (i.e. for UserAuthN entries).
1 parent 04c36cc commit b2599ce

15 files changed

+352
-485
lines changed

perun-oidc-server/src/main/java/cz/muni/ics/oauth2/model/AuthenticationHolderEntity.java

Lines changed: 83 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*******************************************************************************/
1818
package cz.muni.ics.oauth2.model;
1919

20+
import static cz.muni.ics.oauth2.model.AuthenticationHolderEntity.QUERY_ALL;
21+
import static cz.muni.ics.oauth2.model.AuthenticationHolderEntity.QUERY_GET_UNUSED;
22+
2023
import cz.muni.ics.oauth2.model.convert.SerializableStringConverter;
2124
import cz.muni.ics.oauth2.model.convert.SimpleGrantedAuthorityStringConverter;
2225
import java.io.Serializable;
@@ -25,7 +28,6 @@
2528
import java.util.HashSet;
2629
import java.util.Map;
2730
import java.util.Set;
28-
import javax.persistence.Basic;
2931
import javax.persistence.CascadeType;
3032
import javax.persistence.CollectionTable;
3133
import javax.persistence.Column;
@@ -43,70 +45,116 @@
4345
import javax.persistence.OneToOne;
4446
import javax.persistence.Table;
4547
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;
4655
import org.springframework.security.core.GrantedAuthority;
4756
import org.springframework.security.oauth2.provider.OAuth2Authentication;
4857
import org.springframework.security.oauth2.provider.OAuth2Request;
4958

59+
@Getter
60+
@Setter
61+
@ToString
62+
@EqualsAndHashCode
63+
@NoArgsConstructor
64+
@AllArgsConstructor
65+
// DB ANNOTATIONS
5066
@Entity
5167
@Table(name = "authentication_holder")
5268
@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)")
5876
})
5977
public class AuthenticationHolderEntity {
6078

6179
public static final String QUERY_GET_UNUSED = "AuthenticationHolderEntity.getUnusedAuthenticationHolders";
6280
public static final String QUERY_ALL = "AuthenticationHolderEntity.getAll";
6381

82+
@Id
83+
@GeneratedValue(strategy = GenerationType.IDENTITY)
84+
@Column(name = "id")
6485
private Long id;
86+
87+
@OneToOne(cascade=CascadeType.ALL)
88+
@JoinColumn(name = "user_auth_id")
89+
@CascadeOnDelete
6590
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
6697
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
67103
private Set<String> resourceIds;
104+
105+
@Column(name = "approved")
68106
private boolean approved;
107+
108+
@Column(name = "redirect_uri")
69109
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
70115
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
71123
private Map<String, Serializable> extensions;
72-
private String clientId;
73-
private Set<String> scope;
74-
private Map<String, String> requestParameters;
75124

76-
public AuthenticationHolderEntity() { }
125+
@Column(name = "client_id")
126+
private String clientId;
77127

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;
84133

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;
88140

89141
@Transient
90142
public OAuth2Authentication getAuthentication() {
91143
// TODO: memoize this
92144
return new OAuth2Authentication(createOAuth2Request(), getUserAuth());
93145
}
94146

95-
private OAuth2Request createOAuth2Request() {
96-
return new OAuth2Request(requestParameters, clientId, authorities, approved, scope, resourceIds, redirectUri, responseTypes, extensions);
97-
}
98-
99147
public void setAuthentication(OAuth2Authentication authentication) {
100148
// pull apart the request and save its bits
101149
OAuth2Request o2Request = authentication.getOAuth2Request();
102-
setAuthorities(o2Request.getAuthorities() == null ? null : new HashSet<>(o2Request.getAuthorities()));
150+
setAuthorities(convertToSetOrNull((Set<GrantedAuthority>) o2Request.getAuthorities()));
103151
setClientId(o2Request.getClientId());
104-
setExtensions(o2Request.getExtensions() == null ? null : new HashMap<>(o2Request.getExtensions()));
152+
setExtensions(convertToMapOrNull(o2Request.getExtensions()));
105153
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()));
110158
setApproved(o2Request.isApproved());
111159

112160
if (authentication.getUserAuthentication() != null) {
@@ -116,114 +164,16 @@ public void setAuthentication(OAuth2Authentication authentication) {
116164
}
117165
}
118166

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);
200169
}
201170

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);
204173
}
205174

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);
227177
}
228178

229179
}

0 commit comments

Comments
 (0)