17
17
package cz .muni .ics .oauth2 .model ;
18
18
19
19
import cz .muni .ics .oauth2 .model .convert .SimpleGrantedAuthorityStringConverter ;
20
+ import cz .muni .ics .oidc .saml .SamlPrincipal ;
20
21
import java .util .Collection ;
21
22
import java .util .HashSet ;
23
+ import java .util .stream .Collectors ;
22
24
import javax .persistence .Basic ;
23
25
import javax .persistence .CollectionTable ;
24
26
import javax .persistence .Column ;
32
34
import javax .persistence .JoinColumn ;
33
35
import javax .persistence .Table ;
34
36
import javax .persistence .Transient ;
37
+ import lombok .ToString ;
38
+ import lombok .extern .slf4j .Slf4j ;
39
+ import org .opensaml .saml2 .core .AuthnContext ;
40
+ import org .opensaml .saml2 .core .AuthnContextClassRef ;
41
+ import org .opensaml .saml2 .core .AuthnStatement ;
35
42
import org .springframework .security .core .Authentication ;
36
43
import org .springframework .security .core .GrantedAuthority ;
44
+ import org .springframework .security .providers .ExpiringUsernameAuthenticationToken ;
37
45
38
46
/**
39
47
* This class stands in for an original Authentication object.
42
50
*/
43
51
@ Entity
44
52
@ Table (name ="saved_user_auth" )
53
+ @ Slf4j
54
+ @ ToString
45
55
public class SavedUserAuthentication implements Authentication {
46
56
47
57
private static final long serialVersionUID = -1804249963940323488L ;
@@ -50,18 +60,21 @@ public class SavedUserAuthentication implements Authentication {
50
60
private String name ;
51
61
private Collection <GrantedAuthority > authorities ;
52
62
private boolean authenticated ;
53
- private String sourceClass ;
63
+ private String acr ;
54
64
55
65
public SavedUserAuthentication (Authentication src ) {
56
66
setName (src .getName ());
57
67
setAuthorities (new HashSet <>(src .getAuthorities ()));
58
68
setAuthenticated (src .isAuthenticated ());
59
-
60
- if (src instanceof SavedUserAuthentication ) {
61
- // if we're copying in a saved auth, carry over the original class name
62
- setSourceClass (((SavedUserAuthentication ) src ).getSourceClass ());
63
- } else {
64
- setSourceClass (src .getClass ().getName ());
69
+ if (src instanceof ExpiringUsernameAuthenticationToken ) {
70
+ ExpiringUsernameAuthenticationToken token = (ExpiringUsernameAuthenticationToken ) src ;
71
+ this .acr = ((SamlPrincipal ) token .getPrincipal ()).getSamlCredential ()
72
+ .getAuthenticationAssertion ()
73
+ .getAuthnStatements ().stream ()
74
+ .map (AuthnStatement ::getAuthnContext )
75
+ .map (AuthnContext ::getAuthnContextClassRef )
76
+ .map (AuthnContextClassRef ::getAuthnContextClassRef )
77
+ .collect (Collectors .joining ());
65
78
}
66
79
}
67
80
@@ -85,6 +98,10 @@ public String getName() {
85
98
return name ;
86
99
}
87
100
101
+ public void setName (String name ) {
102
+ this .name = name ;
103
+ }
104
+
88
105
@ Override
89
106
@ ElementCollection (fetch = FetchType .EAGER )
90
107
@ CollectionTable (name ="saved_user_auth_authority" , joinColumns =@ JoinColumn (name ="owner_id" ))
@@ -94,22 +111,18 @@ public Collection<GrantedAuthority> getAuthorities() {
94
111
return authorities ;
95
112
}
96
113
97
- @ Override
98
- @ Transient
99
- public Object getCredentials () {
100
- return "" ;
114
+ public void setAuthorities (Collection <GrantedAuthority > authorities ) {
115
+ this .authorities = authorities ;
101
116
}
102
117
103
- @ Override
104
- @ Transient
105
- public Object getDetails () {
106
- return null ;
118
+ @ Basic
119
+ @ Column ( name = "acr" )
120
+ public String getAcr () {
121
+ return acr ;
107
122
}
108
123
109
- @ Override
110
- @ Transient
111
- public Object getPrincipal () {
112
- return getName ();
124
+ public void setAcr (String acr ) {
125
+ this .acr = acr ;
113
126
}
114
127
115
128
@ Override
@@ -124,22 +137,22 @@ public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentExce
124
137
this .authenticated = isAuthenticated ;
125
138
}
126
139
127
- @ Basic
128
- @ Column (name ="source_class" )
129
- public String getSourceClass () {
130
- return sourceClass ;
131
- }
132
-
133
- public void setSourceClass (String sourceClass ) {
134
- this .sourceClass = sourceClass ;
140
+ @ Override
141
+ @ Transient
142
+ public Object getCredentials () {
143
+ return "" ;
135
144
}
136
145
137
- public void setName (String name ) {
138
- this .name = name ;
146
+ @ Override
147
+ @ Transient
148
+ public Object getDetails () {
149
+ return null ;
139
150
}
140
151
141
- public void setAuthorities (Collection <GrantedAuthority > authorities ) {
142
- this .authorities = authorities ;
152
+ @ Override
153
+ @ Transient
154
+ public Object getPrincipal () {
155
+ return getName ();
143
156
}
144
157
145
158
}
0 commit comments