1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .springframework .security .ldap .jackson2 ;
17
16
18
- import org .springframework .ldap .core .DirContextAdapter ;
19
- import org .springframework .ldap .core .DistinguishedName ;
20
- import org .springframework .security .jackson2 .SecurityJackson2Modules ;
21
- import org .springframework .security .ldap .userdetails .InetOrgPerson ;
22
- import org .springframework .security .ldap .userdetails .Person ;
17
+ package org .springframework .security .ldap .jackson2 ;
23
18
19
+ import com .fasterxml .jackson .core .JsonProcessingException ;
24
20
import com .fasterxml .jackson .databind .ObjectMapper ;
21
+ import org .json .JSONException ;
25
22
import org .junit .jupiter .api .BeforeEach ;
26
- import org .junit .jupiter .api .Disabled ;
27
23
import org .junit .jupiter .api .Test ;
28
24
import org .skyscreamer .jsonassert .JSONAssert ;
29
25
26
+ import org .springframework .ldap .core .DirContextAdapter ;
27
+ import org .springframework .ldap .core .DistinguishedName ;
28
+ import org .springframework .security .core .authority .AuthorityUtils ;
29
+ import org .springframework .security .jackson2 .SecurityJackson2Modules ;
30
+ import org .springframework .security .ldap .userdetails .InetOrgPerson ;
31
+ import org .springframework .security .ldap .userdetails .InetOrgPersonContextMapper ;
32
+
30
33
import static org .assertj .core .api .Assertions .assertThat ;
31
- import static org .junit . jupiter .api .Assertions .* ;
34
+ import static org .assertj . core .api .Assertions .assertThatExceptionOfType ;
32
35
33
36
/**
34
37
* Tests for {@link InetOrgPersonMixin}.
35
38
*/
36
- class InetOrgPersonMixinTests {
39
+ public class InetOrgPersonMixinTests {
40
+
41
+ private static final String USER_PASSWORD = "Password1234" ;
42
+
43
+ private static final String AUTHORITIES_ARRAYLIST_JSON = "[\" java.util.Collections$UnmodifiableRandomAccessList\" , []]" ;
44
+
45
+ // @formatter:off
46
+ private static final String INET_ORG_PERSON_JSON = "{\n "
47
+ + "\" @class\" : \" org.springframework.security.ldap.userdetails.InetOrgPerson\" ,"
48
+ + "\" dn\" : \" ignored=ignored\" ,"
49
+ + "\" uid\" : \" ghengis\" ,"
50
+ + "\" username\" : \" ghengis\" ,"
51
+ + "\" password\" : \" " + USER_PASSWORD + "\" ,"
52
+ + "\" carLicense\" : \" HORS1\" ,"
53
+ + "\" givenName\" : \" Ghengis\" ,"
54
+ + "\" destinationIndicator\" : \" West\" ,"
55
+ + "\" displayName\" : \" Ghengis McCann\" ,"
56
+ + "\" givenName\" : \" Ghengis\" ,"
57
+ + "\" homePhone\" : \" +467575436521\" ,"
58
+ + "\" initials\" : \" G\" ,"
59
+ + "\" employeeNumber\" : \" 00001\" ,"
60
+ + "\" homePostalAddress\" : \" Steppes\" ,"
61
+ + "\" mail\" : \" ghengis@mongolia\" ,"
62
+ + "\" mobile\" : \" always\" ,"
63
+ + "\" o\" : \" Hordes\" ,"
64
+ + "\" ou\" : \" Horde1\" ,"
65
+ + "\" postalAddress\" : \" On the Move\" ,"
66
+ + "\" postalCode\" : \" Changes Frequently\" ,"
67
+ + "\" roomNumber\" : \" Yurt 1\" ,"
68
+ + "\" sn\" : \" Khan\" ,"
69
+ + "\" street\" : \" Westward Avenue\" ,"
70
+ + "\" telephoneNumber\" : \" +442075436521\" ,"
71
+ + "\" departmentNumber\" : \" 5679\" ,"
72
+ + "\" title\" : \" T\" ,"
73
+ + "\" cn\" : [\" java.util.Arrays$ArrayList\" ,[\" Ghengis Khan\" ]],"
74
+ + "\" description\" : \" Scary\" ,"
75
+ + "\" accountNonExpired\" : true, "
76
+ + "\" accountNonLocked\" : true, "
77
+ + "\" credentialsNonExpired\" : true, "
78
+ + "\" enabled\" : true, "
79
+ + "\" authorities\" : " + AUTHORITIES_ARRAYLIST_JSON + ","
80
+ + "\" graceLoginsRemaining\" : " + Integer .MAX_VALUE + ","
81
+ + "\" timeBeforeExpiration\" : " + Integer .MAX_VALUE
82
+ + "}" ;
83
+ // @formatter:on
37
84
38
85
private ObjectMapper mapper ;
39
86
@@ -44,22 +91,83 @@ public void setup() {
44
91
this .mapper .registerModules (SecurityJackson2Modules .getModules (loader ));
45
92
}
46
93
47
- @ Disabled
48
94
@ Test
49
95
public void serializeWhenMixinRegisteredThenSerializes () throws Exception {
50
- InetOrgPerson .Essence essence = new InetOrgPerson .Essence (createUserContext ());
51
- InetOrgPerson p = (InetOrgPerson ) essence .createUserDetails ();
96
+ InetOrgPersonContextMapper mapper = new InetOrgPersonContextMapper ();
97
+ InetOrgPerson p = (InetOrgPerson ) mapper .mapUserFromContext (createUserContext (), "ghengis" ,
98
+ AuthorityUtils .NO_AUTHORITIES );
52
99
53
- String expectedJson = asJson (p );
54
100
String json = this .mapper .writeValueAsString (p );
55
- JSONAssert .assertEquals (expectedJson , json , true );
101
+ JSONAssert .assertEquals (INET_ORG_PERSON_JSON , json , true );
102
+ }
103
+
104
+ @ Test
105
+ public void serializeWhenEraseCredentialInvokedThenUserPasswordIsNull ()
106
+ throws JsonProcessingException , JSONException {
107
+ InetOrgPersonContextMapper mapper = new InetOrgPersonContextMapper ();
108
+ InetOrgPerson p = (InetOrgPerson ) mapper .mapUserFromContext (createUserContext (), "ghengis" ,
109
+ AuthorityUtils .NO_AUTHORITIES );
110
+ p .eraseCredentials ();
111
+ String actualJson = this .mapper .writeValueAsString (p );
112
+ JSONAssert .assertEquals (INET_ORG_PERSON_JSON .replaceAll ("\" " + USER_PASSWORD + "\" " , "null" ), actualJson , true );
113
+ }
114
+
115
+ @ Test
116
+ public void deserializeWhenMixinNotRegisteredThenThrowJsonProcessingException () {
117
+ assertThatExceptionOfType (JsonProcessingException .class )
118
+ .isThrownBy (() -> new ObjectMapper ().readValue (INET_ORG_PERSON_JSON , InetOrgPerson .class ));
119
+ }
120
+
121
+ @ Test
122
+ public void deserializeWhenMixinRegisteredThenDeserializes () throws Exception {
123
+ InetOrgPersonContextMapper mapper = new InetOrgPersonContextMapper ();
124
+ InetOrgPerson expectedAuthentication = (InetOrgPerson ) mapper .mapUserFromContext (createUserContext (), "ghengis" ,
125
+ AuthorityUtils .NO_AUTHORITIES );
126
+
127
+ InetOrgPerson authentication = this .mapper .readValue (INET_ORG_PERSON_JSON , InetOrgPerson .class );
128
+ assertThat (authentication .getAuthorities ()).containsExactlyElementsOf (expectedAuthentication .getAuthorities ());
129
+ assertThat (authentication .getCarLicense ()).isEqualTo (expectedAuthentication .getCarLicense ());
130
+ assertThat (authentication .getDepartmentNumber ()).isEqualTo (expectedAuthentication .getDepartmentNumber ());
131
+ assertThat (authentication .getDestinationIndicator ())
132
+ .isEqualTo (expectedAuthentication .getDestinationIndicator ());
133
+ assertThat (authentication .getDn ()).isEqualTo (expectedAuthentication .getDn ());
134
+ assertThat (authentication .getDescription ()).isEqualTo (expectedAuthentication .getDescription ());
135
+ assertThat (authentication .getDisplayName ()).isEqualTo (expectedAuthentication .getDisplayName ());
136
+ assertThat (authentication .getUid ()).isEqualTo (expectedAuthentication .getUid ());
137
+ assertThat (authentication .getUsername ()).isEqualTo (expectedAuthentication .getUsername ());
138
+ assertThat (authentication .getPassword ()).isEqualTo (expectedAuthentication .getPassword ());
139
+ assertThat (authentication .getHomePhone ()).isEqualTo (expectedAuthentication .getHomePhone ());
140
+ assertThat (authentication .getEmployeeNumber ()).isEqualTo (expectedAuthentication .getEmployeeNumber ());
141
+ assertThat (authentication .getHomePostalAddress ()).isEqualTo (expectedAuthentication .getHomePostalAddress ());
142
+ assertThat (authentication .getInitials ()).isEqualTo (expectedAuthentication .getInitials ());
143
+ assertThat (authentication .getMail ()).isEqualTo (expectedAuthentication .getMail ());
144
+ assertThat (authentication .getMobile ()).isEqualTo (expectedAuthentication .getMobile ());
145
+ assertThat (authentication .getO ()).isEqualTo (expectedAuthentication .getO ());
146
+ assertThat (authentication .getOu ()).isEqualTo (expectedAuthentication .getOu ());
147
+ assertThat (authentication .getPostalAddress ()).isEqualTo (expectedAuthentication .getPostalAddress ());
148
+ assertThat (authentication .getPostalCode ()).isEqualTo (expectedAuthentication .getPostalCode ());
149
+ assertThat (authentication .getRoomNumber ()).isEqualTo (expectedAuthentication .getRoomNumber ());
150
+ assertThat (authentication .getStreet ()).isEqualTo (expectedAuthentication .getStreet ());
151
+ assertThat (authentication .getSn ()).isEqualTo (expectedAuthentication .getSn ());
152
+ assertThat (authentication .getTitle ()).isEqualTo (expectedAuthentication .getTitle ());
153
+ assertThat (authentication .getGivenName ()).isEqualTo (expectedAuthentication .getGivenName ());
154
+ assertThat (authentication .getTelephoneNumber ()).isEqualTo (expectedAuthentication .getTelephoneNumber ());
155
+ assertThat (authentication .getGraceLoginsRemaining ())
156
+ .isEqualTo (expectedAuthentication .getGraceLoginsRemaining ());
157
+ assertThat (authentication .getTimeBeforeExpiration ())
158
+ .isEqualTo (expectedAuthentication .getTimeBeforeExpiration ());
159
+ assertThat (authentication .isAccountNonExpired ()).isEqualTo (expectedAuthentication .isAccountNonExpired ());
160
+ assertThat (authentication .isAccountNonLocked ()).isEqualTo (expectedAuthentication .isAccountNonLocked ());
161
+ assertThat (authentication .isEnabled ()).isEqualTo (expectedAuthentication .isEnabled ());
162
+ assertThat (authentication .isCredentialsNonExpired ())
163
+ .isEqualTo (expectedAuthentication .isCredentialsNonExpired ());
56
164
}
57
165
58
166
private DirContextAdapter createUserContext () {
59
167
DirContextAdapter ctx = new DirContextAdapter ();
60
168
ctx .setDn (new DistinguishedName ("ignored=ignored" ));
61
169
ctx .setAttributeValue ("uid" , "ghengis" );
62
- ctx .setAttributeValue ("userPassword" , "pillage" );
170
+ ctx .setAttributeValue ("userPassword" , USER_PASSWORD );
63
171
ctx .setAttributeValue ("carLicense" , "HORS1" );
64
172
ctx .setAttributeValue ("cn" , "Ghengis Khan" );
65
173
ctx .setAttributeValue ("description" , "Scary" );
@@ -77,19 +185,12 @@ private DirContextAdapter createUserContext() {
77
185
ctx .setAttributeValue ("postalAddress" , "On the Move" );
78
186
ctx .setAttributeValue ("postalCode" , "Changes Frequently" );
79
187
ctx .setAttributeValue ("roomNumber" , "Yurt 1" );
80
- ctx .setAttributeValue ("roomNumber" , "Yurt 1" );
81
188
ctx .setAttributeValue ("sn" , "Khan" );
82
189
ctx .setAttributeValue ("street" , "Westward Avenue" );
83
190
ctx .setAttributeValue ("telephoneNumber" , "+442075436521" );
191
+ ctx .setAttributeValue ("departmentNumber" , "5679" );
192
+ ctx .setAttributeValue ("title" , "T" );
84
193
return ctx ;
85
194
}
86
195
87
- private String asJson (Person person ) {
88
- // @formatter:off
89
- return "{\n " +
90
- " \" @class\" : \" org.springframework.security.ldap.userdetails.InetOrgPerson\" \n " +
91
- "}" ;
92
- // @formatter:on
93
- }
94
-
95
196
}
0 commit comments