@@ -42,6 +42,8 @@ public class ResourceServerPropertiesTests {
4242 private ResourceServerProperties properties = new ResourceServerProperties ("client" ,
4343 "secret" );
4444
45+ private Errors errors = mock (Errors .class );
46+
4547 @ Test
4648 @ SuppressWarnings ("unchecked" )
4749 public void json () throws Exception {
@@ -54,39 +56,109 @@ public void json() throws Exception {
5456 }
5557
5658 @ Test
57- public void tokenKeyDerivedFromUserInfoUri () throws Exception {
58- this .properties .setUserInfoUri ("http://example.com/userinfo" );
59- assertThat (this .properties .getJwt ().getKeyUri ())
60- .isEqualTo ("http://example.com/token_key" );
59+ public void validateWhenBothJwtAndJwkKeyUrisPresentShouldFail () throws Exception {
60+ this .properties .getJwk ().setKeySetUri ("http://my-auth-server/token_keys" );
61+ this .properties .getJwt ().setKeyUri ("http://my-auth-server/token_key" );
62+ setListableBeanFactory ();
63+ this .properties .validate (this .properties , this .errors );
64+ verify (this .errors ).reject ("ambiguous.keyUri" ,
65+ "Only one of jwt.keyUri (or jwt.keyValue) and jwk.keySetUri should be configured." );
6166 }
6267
6368 @ Test
64- public void tokenKeyDerivedFromTokenInfoUri () throws Exception {
65- this .properties .setTokenInfoUri ("http://example.com/check_token" );
66- assertThat (this .properties .getJwt ().getKeyUri ())
67- .isEqualTo ("http://example.com/token_key" );
69+ public void validateWhenBothJwtKeyValueAndJwkKeyUriPresentShouldFail ()
70+ throws Exception {
71+ this .properties .getJwk ().setKeySetUri ("http://my-auth-server/token_keys" );
72+ this .properties .getJwt ().setKeyValue ("my-key" );
73+ setListableBeanFactory ();
74+ this .properties .validate (this .properties , this .errors );
75+ verify (this .errors ).reject ("ambiguous.keyUri" ,
76+ "Only one of jwt.keyUri (or jwt.keyValue) and jwk.keySetUri should be configured." );
6877 }
6978
7079 @ Test
71- public void validateWhenBothJwtAndJwtKeyConfigurationPresentShouldFail ()
72- throws Exception {
80+ public void validateWhenJwkKeySetUriProvidedShouldSucceed () throws Exception {
7381 this .properties .getJwk ().setKeySetUri ("http://my-auth-server/token_keys" );
74- this .properties .getJwt ().setKeyUri ("http://my-auth-server/token_key" );
7582 setListableBeanFactory ();
76- Errors errors = mock (Errors .class );
77- this .properties .validate (this .properties , errors );
78- verify (errors ).reject ("ambiguous.keyUri" ,
79- "Only one of jwt.keyUri (or jwt.keyValue) and jwk.keySetUri should be configured." );
83+ this .properties .validate (this .properties , this .errors );
84+ verifyZeroInteractions (this .errors );
85+ }
8086
87+ @ Test
88+ public void validateWhenKeyValuePresentShouldSucceed () throws Exception {
89+ this .properties .getJwt ().setKeyValue ("my-key" );
90+ setListableBeanFactory ();
91+ this .properties .validate (this .properties , this .errors );
92+ verifyZeroInteractions (this .errors );
8193 }
8294
8395 @ Test
84- public void validateWhenKeySetUriProvidedShouldSucceed () throws Exception {
96+ public void validateWhenKeysUriOrValuePresentAndUserInfoAbsentShouldNotFail ()
97+ throws Exception {
98+ this .properties = new ResourceServerProperties ("client" , "" );
8599 this .properties .getJwk ().setKeySetUri ("http://my-auth-server/token_keys" );
86100 setListableBeanFactory ();
87- Errors errors = mock (Errors .class );
88- this .properties .validate (this .properties , errors );
89- verifyZeroInteractions (errors );
101+ this .properties .validate (this .properties , this .errors );
102+ verifyZeroInteractions (this .errors );
103+ }
104+
105+ @ Test
106+ public void validateWhenKeyConfigAbsentAndInfoUrisNotConfiguredShouldFail ()
107+ throws Exception {
108+ setListableBeanFactory ();
109+ this .properties .validate (this .properties , this .errors );
110+ verify (this .errors ).rejectValue ("tokenInfoUri" , "missing.tokenInfoUri" ,
111+ "Missing tokenInfoUri and userInfoUri and there is no JWT verifier key" );
112+ }
113+
114+ @ Test
115+ public void validateWhenTokenUriConfiguredShouldNotFail () throws Exception {
116+ this .properties .setTokenInfoUri ("http://my-auth-server/userinfo" );
117+ setListableBeanFactory ();
118+ this .properties .validate (this .properties , this .errors );
119+ verifyZeroInteractions (this .errors );
120+ }
121+
122+ @ Test
123+ public void validateWhenUserInfoUriConfiguredShouldNotFail () throws Exception {
124+ this .properties .setUserInfoUri ("http://my-auth-server/userinfo" );
125+ setListableBeanFactory ();
126+ this .properties .validate (this .properties , this .errors );
127+ verifyZeroInteractions (this .errors );
128+ }
129+
130+ @ Test
131+ public void validateWhenTokenUriPreferredAndClientSecretAbsentShouldFail ()
132+ throws Exception {
133+ this .properties = new ResourceServerProperties ("client" , "" );
134+ this .properties .setTokenInfoUri ("http://my-auth-server/check_token" );
135+ this .properties .setUserInfoUri ("http://my-auth-server/userinfo" );
136+ setListableBeanFactory ();
137+ this .properties .validate (this .properties , this .errors );
138+ verify (this .errors ).rejectValue ("clientSecret" , "missing.clientSecret" ,
139+ "Missing client secret" );
140+ }
141+
142+ @ Test
143+ public void validateWhenTokenUriAbsentAndClientSecretAbsentShouldNotFail ()
144+ throws Exception {
145+ this .properties = new ResourceServerProperties ("client" , "" );
146+ this .properties .setUserInfoUri ("http://my-auth-server/userinfo" );
147+ setListableBeanFactory ();
148+ this .properties .validate (this .properties , this .errors );
149+ verifyZeroInteractions (this .errors );
150+ }
151+
152+ @ Test
153+ public void validateWhenTokenUriNotPreferredAndClientSecretAbsentShouldNotFail ()
154+ throws Exception {
155+ this .properties = new ResourceServerProperties ("client" , "" );
156+ this .properties .setPreferTokenInfo (false );
157+ this .properties .setTokenInfoUri ("http://my-auth-server/check_token" );
158+ this .properties .setUserInfoUri ("http://my-auth-server/userinfo" );
159+ setListableBeanFactory ();
160+ this .properties .validate (this .properties , this .errors );
161+ verifyZeroInteractions (this .errors );
90162 }
91163
92164 private void setListableBeanFactory () {
0 commit comments