Skip to content

Commit 2a20bd8

Browse files
authored
Nit fixes and formatting following #62490 comments (#63797)
* Nit fixes and formatting following #62490 comments Resolves: #63792 * Nit fixes and formatting following #62490 comments Resolves: #63792 * Nit fixes and formatting following #62490 comments Fixing username * Nit fixes and formatting following #62490 comments Fixing formatting
1 parent 8237c06 commit 2a20bd8

File tree

12 files changed

+95
-87
lines changed

12 files changed

+95
-87
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenResponse.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public final class CreateTokenResponse {
4242
private final String scope;
4343
private final String refreshToken;
4444
private final String kerberosAuthenticationResponseToken;
45-
private final AuthenticateResponse authenticationResponse;
45+
private final AuthenticateResponse authentication;
4646

4747
public CreateTokenResponse(String accessToken, String type, TimeValue expiresIn, String scope, String refreshToken,
48-
String kerberosAuthenticationResponseToken, AuthenticateResponse authenticationResponse) {
48+
String kerberosAuthenticationResponseToken, AuthenticateResponse authentication) {
4949
this.accessToken = accessToken;
5050
this.type = type;
5151
this.expiresIn = expiresIn;
5252
this.scope = scope;
5353
this.refreshToken = refreshToken;
5454
this.kerberosAuthenticationResponseToken = kerberosAuthenticationResponseToken;
55-
this.authenticationResponse = authenticationResponse;
55+
this.authentication = authentication;
5656
}
5757

5858
public String getAccessToken() {
@@ -79,7 +79,7 @@ public String getKerberosAuthenticationResponseToken() {
7979
return kerberosAuthenticationResponseToken;
8080
}
8181

82-
public AuthenticateResponse getAuthenticationResponse() { return authenticationResponse; }
82+
public AuthenticateResponse getAuthentication() { return authentication; }
8383

8484
@Override
8585
public boolean equals(Object o) {
@@ -96,12 +96,12 @@ public boolean equals(Object o) {
9696
Objects.equals(scope, that.scope) &&
9797
Objects.equals(refreshToken, that.refreshToken) &&
9898
Objects.equals(kerberosAuthenticationResponseToken, that.kerberosAuthenticationResponseToken)&&
99-
Objects.equals(authenticationResponse, that.authenticationResponse);
99+
Objects.equals(authentication, that.authentication);
100100
}
101101

102102
@Override
103103
public int hashCode() {
104-
return Objects.hash(accessToken, type, expiresIn, scope, refreshToken, kerberosAuthenticationResponseToken, authenticationResponse);
104+
return Objects.hash(accessToken, type, expiresIn, scope, refreshToken, kerberosAuthenticationResponseToken, authentication);
105105
}
106106

107107
private static final ConstructingObjectParser<CreateTokenResponse, Void> PARSER = new ConstructingObjectParser<>(

client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponse.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public final class DelegatePkiAuthenticationResponse {
3434
private final String accessToken;
3535
private final String type;
3636
private final TimeValue expiresIn;
37-
private final AuthenticateResponse authenticationResponse;
37+
private final AuthenticateResponse authentication;
3838

3939
public DelegatePkiAuthenticationResponse(String accessToken, String type, TimeValue expiresIn,
40-
AuthenticateResponse authenticationResponse) {
40+
AuthenticateResponse authentication) {
4141
this.accessToken = accessToken;
4242
this.type = type;
4343
this.expiresIn = expiresIn;
44-
this.authenticationResponse = authenticationResponse;
44+
this.authentication = authentication;
4545
}
4646

4747
public String getAccessToken() {
@@ -56,7 +56,7 @@ public TimeValue getExpiresIn() {
5656
return expiresIn;
5757
}
5858

59-
public AuthenticateResponse getAuthenticationResponse() { return authenticationResponse; }
59+
public AuthenticateResponse getAuthentication() { return authentication; }
6060

6161
@Override
6262
public boolean equals(Object o) {
@@ -70,12 +70,12 @@ public boolean equals(Object o) {
7070
return Objects.equals(accessToken, that.accessToken) &&
7171
Objects.equals(type, that.type) &&
7272
Objects.equals(expiresIn, that.expiresIn) &&
73-
Objects.equals(authenticationResponse, that.authenticationResponse);
73+
Objects.equals(authentication, that.authentication);
7474
}
7575

7676
@Override
7777
public int hashCode() {
78-
return Objects.hash(accessToken, type, expiresIn, authenticationResponse);
78+
return Objects.hash(accessToken, type, expiresIn, authentication);
7979
}
8080

8181
@SuppressWarnings("unchecked")

client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenResponseTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testFromXContent() throws IOException {
4040
final String scope = randomBoolean() ? null : randomAlphaOfLength(4);
4141
final String type = randomAlphaOfLength(6);
4242
final String kerberosAuthenticationResponseToken = randomBoolean() ? null : randomAlphaOfLength(7);
43-
final AuthenticateResponse authenticateResponse = new AuthenticateResponse(new User(randomAlphaOfLength(7),
43+
final AuthenticateResponse authentication = new AuthenticateResponse(new User(randomAlphaOfLength(7),
4444
Arrays.asList( randomAlphaOfLength(9) )),
4545
true, new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(7) ),
4646
new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(5) ), "realm");
@@ -60,7 +60,7 @@ public void testFromXContent() throws IOException {
6060
if (kerberosAuthenticationResponseToken != null) {
6161
builder.field("kerberos_authentication_response_token", kerberosAuthenticationResponseToken);
6262
}
63-
builder.field("authentication", authenticateResponse);
63+
builder.field("authentication", authentication);
6464
builder.endObject();
6565
BytesReference xContent = BytesReference.bytes(builder);
6666

@@ -71,6 +71,6 @@ public void testFromXContent() throws IOException {
7171
assertThat(response.getType(), equalTo(type));
7272
assertThat(response.getExpiresIn(), equalTo(expiresIn));
7373
assertThat(response.getKerberosAuthenticationResponseToken(), equalTo(kerberosAuthenticationResponseToken));
74-
assertThat(response.getAuthenticationResponse(), equalTo(authenticateResponse));
74+
assertThat(response.getAuthentication(), equalTo(authentication));
7575
}
7676
}

client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponseTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void assertInstances(org.elasticsearch.xpack.core.security.action.Dele
6161
assertThat(clientInstance.getType(), is("Bearer"));
6262
AuthenticateResponse serverAuthenticationResponse = createServerAuthenticationResponse(serverTestInstance.getAuthentication());
6363
User user = serverTestInstance.getAuthentication().getUser();
64-
assertThat(serverAuthenticationResponse, equalTo(clientInstance.getAuthenticationResponse()));
64+
assertThat(serverAuthenticationResponse, equalTo(clientInstance.getAuthentication()));
6565
}
6666

6767
protected Authentication createAuthentication() {

x-pack/docs/en/rest-api/security/delegate-pki-authentication.asciidoc

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ Which returns the following response:
9999
"pki_dn" : "O=org, OU=Elasticsearch, CN=Elasticsearch Test Client",
100100
"pki_delegated_by_user" : "test_admin",
101101
"pki_delegated_by_realm" : "file"
102-
},
102+
},
103103
"enabled" : true,
104104
"authentication_realm" : {
105105
"name" : "pki1",
106106
"type" : "pki"
107-
},
107+
},
108108
"lookup_realm" : {
109109
"name" : "pki1",
110110
"type" : "pki"
111-
},
111+
},
112112
"authentication_type" : "realm"
113113
}
114114
}

x-pack/docs/en/rest-api/security/get-tokens.asciidoc

+61-61
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,23 @@ seconds) that the token expires in, and the type:
125125
"type" : "Bearer",
126126
"expires_in" : 1200,
127127
"authentication" : {
128-
"username" : "test_admin",
129-
"roles" : [
130-
"superuser"
131-
],
132-
"full_name" : null,
133-
"email" : null,
134-
"metadata" : { },
135-
"enabled" : true,
136-
"authentication_realm" : {
137-
"name" : "file",
138-
"type" : "file"
139-
},
140-
"lookup_realm" : {
141-
"name" : "file",
142-
"type" : "file"
143-
},
144-
"authentication_type" : "realm"
128+
"username" : "test_admin",
129+
"roles" : [
130+
"superuser"
131+
],
132+
"full_name" : null,
133+
"email" : null,
134+
"metadata" : { },
135+
"enabled" : true,
136+
"authentication_realm" : {
137+
"name" : "file",
138+
"type" : "file"
139+
},
140+
"lookup_realm" : {
141+
"name" : "file",
142+
"type" : "file"
143+
},
144+
"authentication_type" : "realm"
145145
}
146146
}
147147
--------------------------------------------------
@@ -182,23 +182,23 @@ seconds) that the token expires in, the type, and the refresh token:
182182
"expires_in" : 1200,
183183
"refresh_token": "vLBPvmAB6KvwvJZr27cS",
184184
"authentication" : {
185-
"username" : "test_admin",
186-
"roles" : [
187-
"superuser"
185+
"username" : "test_admin",
186+
"roles" : [
187+
"superuser"
188188
],
189-
"full_name" : null,
190-
"email" : null,
191-
"metadata" : { },
192-
"enabled" : true,
193-
"authentication_realm" : {
194-
"name" : "file",
195-
"type" : "file"
189+
"full_name" : null,
190+
"email" : null,
191+
"metadata" : { },
192+
"enabled" : true,
193+
"authentication_realm" : {
194+
"name" : "file",
195+
"type" : "file"
196196
},
197-
"lookup_realm" : {
198-
"name" : "file",
199-
"type" : "file"
200-
},
201-
"authentication_type" : "realm"
197+
"lookup_realm" : {
198+
"name" : "file",
199+
"type" : "file"
200+
},
201+
"authentication_type" : "realm"
202202
}
203203
}
204204
--------------------------------------------------
@@ -232,23 +232,23 @@ be used one time.
232232
"expires_in" : 1200,
233233
"refresh_token": "vLBPvmAB6KvwvJZr27cS",
234234
"authentication" : {
235-
"username" : "test_admin",
236-
"roles" : [
237-
"superuser"
235+
"username" : "test_admin",
236+
"roles" : [
237+
"superuser"
238238
],
239-
"full_name" : null,
240-
"email" : null,
241-
"metadata" : { },
242-
"enabled" : true,
243-
"authentication_realm" : {
244-
"name" : "file",
245-
"type" : "file"
239+
"full_name" : null,
240+
"email" : null,
241+
"metadata" : { },
242+
"enabled" : true,
243+
"authentication_realm" : {
244+
"name" : "file",
245+
"type" : "file"
246246
},
247-
"lookup_realm" : {
248-
"name" : "file",
249-
"type" : "file"
247+
"lookup_realm" : {
248+
"name" : "file",
249+
"type" : "file"
250250
},
251-
"authentication_type" : "token"
251+
"authentication_type" : "token"
252252
}
253253
}
254254
--------------------------------------------------
@@ -282,23 +282,23 @@ Each refresh token may only be used one time. When the mutual authentication is
282282
"refresh_token": "vLBPvmAB6KvwvJZr27cS"
283283
"kerberos_authentication_response_token": "YIIB6wYJKoZIhvcSAQICAQBuggHaMIIB1qADAg",
284284
"authentication" : {
285-
"username" : "test_admin",
286-
"roles" : [
287-
"superuser"
285+
"username" : "test_admin",
286+
"roles" : [
287+
"superuser"
288288
],
289-
"full_name" : null,
290-
"email" : null,
291-
"metadata" : { },
292-
"enabled" : true,
293-
"authentication_realm" : {
294-
"name" : "file",
295-
"type" : "file"
289+
"full_name" : null,
290+
"email" : null,
291+
"metadata" : { },
292+
"enabled" : true,
293+
"authentication_realm" : {
294+
"name" : "file",
295+
"type" : "file"
296+
},
297+
"lookup_realm" : {
298+
"name" : "file",
299+
"type" : "file"
296300
},
297-
"lookup_realm" : {
298-
"name" : "file",
299-
"type" : "file"
300-
},
301-
"authentication_type" : "realm"
301+
"authentication_type" : "realm"
302302
}
303303
}
304304
--------------------------------------------------

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void testTokenServiceBootstrapOnNodeJoin() throws Exception {
8787
final RestHighLevelClient restClient = new TestRestHighLevelClient();
8888
CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant(
8989
SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
90+
assertNotNull(response.getAuthentication());
9091
for (TokenService tokenService : internalCluster().getInstances(TokenService.class)) {
9192
PlainActionFuture<UserToken> userTokenFuture = new PlainActionFuture<>();
9293
tokenService.decodeToken(response.getAccessToken(), userTokenFuture);
@@ -104,7 +105,6 @@ public void testTokenServiceBootstrapOnNodeJoin() throws Exception {
104105
PlainActionFuture<UserToken> userTokenFuture = new PlainActionFuture<>();
105106
tokenService.decodeToken(response.getAccessToken(), userTokenFuture);
106107
assertNotNull(userTokenFuture.actionGet());
107-
assertNotNull(response.getAuthenticationResponse());
108108
}
109109

110110

@@ -134,7 +134,8 @@ public void testTokenServiceCanRotateKeys() throws Exception {
134134
assertNotNull(userTokenFuture.actionGet());
135135
assertNotEquals(activeKeyHash, tokenService.getActiveKeyHash());
136136
}
137-
assertNotNull(response.getAuthenticationResponse());
137+
assertNotNull(response.getAuthentication());
138+
assertEquals(SecuritySettingsSource.TEST_USER_NAME, response.getAuthentication().getUser().getUsername());
138139
}
139140

140141
public void testExpiredTokensDeletedAfterExpiration() throws Exception {
@@ -367,7 +368,7 @@ public void testRefreshingToken() throws IOException {
367368

368369
// Assert that we can authenticate with the refreshed access token
369370
assertAuthenticateWithToken(refreshResponse.getAccessToken(), SecuritySettingsSource.TEST_USER_NAME);
370-
assertNotNull(refreshResponse.getAuthenticationResponse());
371+
assertNotNull(refreshResponse.getAuthentication());
371372
}
372373

373374
public void testRefreshingInvalidatedToken() throws IOException {

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public void testDelegateThenAuthenticate() throws Exception {
149149
optionsBuilder.build());
150150
String token = delegatePkiResponse.getAccessToken();
151151
assertThat(token, is(notNullValue()));
152-
assertNotNull(delegatePkiResponse.getAuthenticationResponse());
152+
assertNotNull(delegatePkiResponse.getAuthentication());
153+
assertEquals("Elasticsearch Test Client", delegatePkiResponse.getAuthentication().getUser().getUsername());
153154

154155
// authenticate
155156
optionsBuilder = RequestOptions.DEFAULT.toBuilder();
@@ -189,7 +190,7 @@ public void testTokenInvalidate() throws Exception {
189190
optionsBuilder.build());
190191
String token = delegatePkiResponse.getAccessToken();
191192
assertThat(token, is(notNullValue()));
192-
assertNotNull(delegatePkiResponse.getAuthenticationResponse());
193+
assertNotNull(delegatePkiResponse.getAuthentication());
193194
// authenticate
194195
optionsBuilder = RequestOptions.DEFAULT.toBuilder();
195196
optionsBuilder.addHeader("Authorization", "Bearer " + token);

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectAuthenticateAction.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public RestResponse buildResponse(OpenIdConnectAuthenticateResponse response, XC
7070
builder.field("access_token", response.getAccessTokenString());
7171
builder.field("refresh_token", response.getRefreshTokenString());
7272
builder.field("expires_in", response.getExpiresIn().seconds());
73-
builder.field("authentication", response.getAuthentication());
73+
if(response.getAuthentication() != null) {
74+
builder.field("authentication", response.getAuthentication());
75+
}
7476
builder.endObject();
7577
return new BytesRestResponse(RestStatus.OK, builder);
7678
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlAuthenticateAction.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public RestResponse buildResponse(SamlAuthenticateResponse response, XContentBui
103103
builder.field("access_token", response.getTokenString());
104104
builder.field("refresh_token", response.getRefreshToken());
105105
builder.field("expires_in", response.getExpiresIn().seconds());
106-
builder.field("authentication", response.getAuthentication());
106+
if(response.getAuthentication() != null) {
107+
builder.field("authentication", response.getAuthentication());
108+
}
107109
builder.endObject();
108110
return new BytesRestResponse(RestStatus.OK, builder);
109111
}

x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ private Tuple<String, String> completeAuthentication(String redirectUri, String
421421
assertNotNull(responseBody.get("access_token"));
422422
assertNotNull(responseBody.get("refresh_token"));
423423
assertNotNull(responseBody.get("authentication"));
424+
assertEquals("alice", ((Map)responseBody.get("authentication")).get("username"));
424425
return Tuple.tuple(responseBody.get("access_token").toString(), responseBody.get("refresh_token").toString());
425426
}
426427
}

x-pack/qa/saml-idp-tests/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticationIT.java

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ private Tuple<String, String> loginViaSaml(String realmName) throws Exception {
207207
final Object authentication = result.get("authentication");
208208
assertThat(authentication, notNullValue());
209209
assertThat(authentication, instanceOf(Map.class));
210+
assertEquals("thor", ((Map)authentication).get("username"));
210211

211212
return new Tuple<>((String) accessToken, (String) refreshToken);
212213
}

0 commit comments

Comments
 (0)