-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathDSUserDetails.java
More file actions
214 lines (189 loc) · 5.62 KB
/
Copy pathDSUserDetails.java
File metadata and controls
214 lines (189 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.digitalsanctuary.spring.user.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import com.digitalsanctuary.spring.user.persistence.model.User;
import lombok.Builder;
import lombok.ToString;
/**
* The {@code DSUserDetails} class is an extension of the default Spring Security {@code UserDetails} interface that uses a custom {@code User} object
* and email address as the username. This class provides implementations of the {@code UserDetails} and {@code OAuth2User} interfaces for use in
* Spring Security authentication and authorization workflows.
*
* <p>
* Instances of this class are created with a {@code User} object and an optional collection of {@code GrantedAuthority} objects that define the
* user's authorization permissions. The class provides methods for accessing the user's information, such as the username (email address), password,
* enabled status, and full name.
*
* <p>
* This class also implements the {@code OAuth2User} interface, which allows it to be used in conjunction with OAuth2 authentication providers. The
* class provides methods for retrieving the user's attributes and name from the OAuth2 provider, which can be useful for applications that need to
* customize the user experience based on the provider or the user's attributes.
*
* <p>
* Example usage:
*
* <pre>{@code
* // Create a new DSUserDetails object for a user
* User user = userRepository.findByEmail("user@example.com");
* Collection<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
* DSUserDetails userDetails = new DSUserDetails(user, authorities);
* }</pre>
*/
@ToString
public class DSUserDetails implements UserDetails, OidcUser {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 5286810064622508389L;
/** The user. */
private final User user;
/** The granted authorities. */
private final Collection<? extends GrantedAuthority> grantedAuthorities;
/** The attributes. */
private Map<String, Object> attributes;
/** The Oidc user properties. */
private OidcUserInfo oidcUserInfo;
/** The Oidc user token. */
private OidcIdToken oidcIdToken;
/**
* Instantiates a new DS user details.
*
* @param user the user
* @param grantedAuthorities the granted authorities (optional, default = empty list)
*/
public DSUserDetails(User user, Collection<? extends GrantedAuthority> grantedAuthorities) {
this.user = user;
this.grantedAuthorities = grantedAuthorities != null ? grantedAuthorities : new ArrayList<>();
this.attributes = new HashMap<>();
}
/**
* Instantiates a new DS user details with no granted authorities.
*
* @param user the user
*/
public DSUserDetails(User user) {
this(user, null);
}
/**
* Instantiates a new DS user details.
*
* @param user the user
* @param oidcUserInfo containing claims about the user
* @param oidcIdToken containing claims about the user
* @param grantedAuthorities the granted authorities (optional, default = empty list)
*/
@Builder
public DSUserDetails(User user, OidcUserInfo oidcUserInfo, OidcIdToken oidcIdToken, Collection<? extends GrantedAuthority> grantedAuthorities) {
this.user = user;
this.oidcUserInfo = oidcUserInfo;
this.oidcIdToken = oidcIdToken;
this.grantedAuthorities = grantedAuthorities != null ? grantedAuthorities : new ArrayList<>();
}
/**
* Instantiates a new DS user details.
*
* @param user the user
* @param oidcUserInfo containing claims about the user
* @param oidcIdToken containing claims about the user
*/
@Builder
public DSUserDetails(User user, OidcUserInfo oidcUserInfo, OidcIdToken oidcIdToken) {
this(user, oidcUserInfo, oidcIdToken, null);
}
/**
* Gets the authorities.
*
* @return the authorities
*/
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return grantedAuthorities;
}
/**
* Gets the password.
*
* @return the password
*/
@Override
public String getPassword() {
return user.getPassword();
}
/**
* Gets the username.
*
* @return the username
*/
@Override
public String getUsername() {
return user.getEmail();
}
/**
* Checks if is account non expired.
*
* @return true, if is account non expired
*/
@Override
public boolean isAccountNonExpired() {
return true;
}
/**
* Checks if is account non locked.
*
* @return true, if is account non locked
*/
@Override
public boolean isAccountNonLocked() {
return !user.isLocked();
}
/**
* Checks if is credentials non expired.
*
* @return true, if is credentials non expired
*/
@Override
public boolean isCredentialsNonExpired() {
return true;
}
/**
* Checks if is enabled.
*
* @return true, if is enabled
*/
@Override
public boolean isEnabled() {
return user.isEnabled();
}
/**
* Gets the user.
*
* @return the user
*/
public User getUser() {
return user;
}
@Override
public Map<String, Object> getAttributes() {
return attributes;
}
@Override
public String getName() {
return user.getFullName();
}
@Override
public Map<String, Object> getClaims() {
return oidcUserInfo != null ? oidcUserInfo.getClaims() : Map.of();
}
@Override
public OidcUserInfo getUserInfo() {
return oidcUserInfo;
}
@Override
public OidcIdToken getIdToken() {
return oidcIdToken;
}
}