30
30
import org .springframework .security .core .GrantedAuthority ;
31
31
import org .springframework .security .core .authority .AuthorityUtils ;
32
32
import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
33
+ import org .springframework .security .oauth2 .core .OAuth2TokenAttributes ;
33
34
34
35
import static org .assertj .core .api .Assertions .assertThat ;
35
36
import static org .assertj .core .api .Assertions .assertThatCode ;
@@ -46,14 +47,15 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
46
47
private final OAuth2AccessToken token =
47
48
new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER ,
48
49
"token" , Instant .now (), Instant .now ().plusSeconds (3600 ));
49
- private final Map <String , Object > attributes = new HashMap <>();
50
50
private final String name = "sub" ;
51
+ private Map <String , Object > attributesMap = new HashMap <>();
52
+ private final OAuth2TokenAttributes attributes = new OAuth2TokenAttributes (attributesMap );
51
53
52
54
@ Before
53
55
public void setUp () {
54
- this .attributes .put (SUBJECT , this .name );
55
- this .attributes .put (CLIENT_ID , "client_id" );
56
- this .attributes .put (USERNAME , "username" );
56
+ this .attributesMap .put (SUBJECT , this .name );
57
+ this .attributesMap .put (CLIENT_ID , "client_id" );
58
+ this .attributesMap .put (USERNAME , "username" );
57
59
}
58
60
59
61
@ Test
@@ -67,7 +69,8 @@ public void getNameWhenConfiguredInConstructorThenReturnsName() {
67
69
@ Test
68
70
public void getNameWhenHasNoSubjectThenReturnsNull () {
69
71
OAuth2IntrospectionAuthenticationToken authenticated =
70
- new OAuth2IntrospectionAuthenticationToken (this .token , Collections .singletonMap ("claim" , "value" ),
72
+ new OAuth2IntrospectionAuthenticationToken (this .token ,
73
+ new OAuth2TokenAttributes (Collections .singletonMap ("claim" , "value" )),
71
74
Collections .emptyList ());
72
75
assertThat (authenticated .getName ()).isNull ();
73
76
}
@@ -76,7 +79,7 @@ public void getNameWhenHasNoSubjectThenReturnsNull() {
76
79
public void getNameWhenTokenHasUsernameThenReturnsUsernameAttribute () {
77
80
OAuth2IntrospectionAuthenticationToken authenticated =
78
81
new OAuth2IntrospectionAuthenticationToken (this .token , this .attributes , Collections .emptyList ());
79
- assertThat (authenticated .getName ()).isEqualTo (this .attributes .get (SUBJECT ));
82
+ assertThat (authenticated .getName ()).isEqualTo (this .attributes .getAttribute (SUBJECT ));
80
83
}
81
84
82
85
@ Test
@@ -92,15 +95,17 @@ public void constructorWhenAttributesAreNullOrEmptyThenThrowsException() {
92
95
.isInstanceOf (IllegalArgumentException .class )
93
96
.hasMessageContaining ("attributes cannot be empty" );
94
97
95
- assertThatCode (() -> new OAuth2IntrospectionAuthenticationToken (this .token , Collections .emptyMap (), null ))
98
+ assertThatCode (() -> new OAuth2IntrospectionAuthenticationToken (this .token ,
99
+ new OAuth2TokenAttributes (Collections .emptyMap ()), null ))
96
100
.isInstanceOf (IllegalArgumentException .class )
97
101
.hasMessageContaining ("attributes cannot be empty" );
98
102
}
99
103
100
104
@ Test
101
105
public void constructorWhenPassingAllAttributesThenTokenIsAuthenticated () {
102
106
OAuth2IntrospectionAuthenticationToken authenticated =
103
- new OAuth2IntrospectionAuthenticationToken (this .token , Collections .singletonMap ("claim" , "value" ),
107
+ new OAuth2IntrospectionAuthenticationToken (this .token ,
108
+ new OAuth2TokenAttributes (Collections .singletonMap ("claim" , "value" )),
104
109
Collections .emptyList (), "harris" );
105
110
assertThat (authenticated .isAuthenticated ()).isTrue ();
106
111
}
@@ -109,7 +114,7 @@ public void constructorWhenPassingAllAttributesThenTokenIsAuthenticated() {
109
114
public void getTokenAttributesWhenHasTokenThenReturnsThem () {
110
115
OAuth2IntrospectionAuthenticationToken authenticated =
111
116
new OAuth2IntrospectionAuthenticationToken (this .token , this .attributes , Collections .emptyList ());
112
- assertThat (authenticated .getTokenAttributes ()).isEqualTo (this .attributes );
117
+ assertThat (authenticated .getTokenAttributes ()).isEqualTo (this .attributes . getAttributes () );
113
118
}
114
119
115
120
@ Test
@@ -126,7 +131,8 @@ public void constructorWhenDefaultParametersThenSetsPrincipalToAttributesCopy()
126
131
JSONObject attributes = new JSONObject ();
127
132
attributes .put ("active" , true );
128
133
OAuth2IntrospectionAuthenticationToken token =
129
- new OAuth2IntrospectionAuthenticationToken (this .token , attributes , Collections .emptyList ());
134
+ new OAuth2IntrospectionAuthenticationToken (this .token , new OAuth2TokenAttributes (attributes ),
135
+ Collections .emptyList ());
130
136
assertThat (token .getPrincipal ()).isNotSameAs (attributes );
131
137
assertThat (token .getTokenAttributes ()).isNotSameAs (attributes );
132
138
}
@@ -136,7 +142,8 @@ public void constructorWhenDefaultParametersThenSetsPrincipalToAttributesCopy()
136
142
public void toStringWhenAttributesContainsURLThenDoesNotFail () throws Exception {
137
143
JSONObject attributes = new JSONObject (Collections .singletonMap ("iss" , new URL ("https://idp.example.com" )));
138
144
OAuth2IntrospectionAuthenticationToken token =
139
- new OAuth2IntrospectionAuthenticationToken (this .token , attributes , Collections .emptyList ());
145
+ new OAuth2IntrospectionAuthenticationToken (this .token , new OAuth2TokenAttributes (attributes ),
146
+ Collections .emptyList ());
140
147
assertThatCode (token ::toString )
141
148
.doesNotThrowAnyException ();
142
149
}
0 commit comments