Skip to content

Commit 003bbd4

Browse files
Adds realm name OIDC _security/oidc/prepare and _security/oidc/authenticate APIs responses (#64966) (#65164)
* This change adds realm name of the realm used to perform authentication to the responses of _security/oidc/authenticate and _security/oidc/authenticate APIs Resolves #53161 * This change adds realm name of the realm used to perform authentication to the responses of _security/oidc/authenticate and _security/oidc/authenticate APIs Resolves #53161 * This change adds realm name of the realm used to perform authentication to the responses of _security/oidc/authenticate and _security/oidc/authenticate APIs Resolves #53161 * This change adds realm name of the realm used to perform authentication to the responses of _security/oidc/authenticate and _security/oidc/authenticate APIs Resolves #53161 Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent dc4dc94 commit 003bbd4

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

x-pack/docs/en/rest-api/security/oidc-authenticate-api.asciidoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ be used to authenticate this. Useful when multiple realms have been defined.
5858

5959
The following example request exchanges the response that was returned from the
6060
OpenID Connect Provider after a successful authentication, for an {es} access
61-
token and refresh token to be used in subsequent requests. This example is from
61+
token and refresh token to be used in subsequent requests. This example is from
6262
an authentication that uses the authorization code grant flow.
6363

6464
[source,console]

x-pack/docs/en/rest-api/security/oidc-prepare-authentication-api.asciidoc

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== OpenID Connect Prepare Authentication API
44

55
Creates an oAuth 2.0 authentication request as a URL string based on the
6-
configuration of the respective OpenID Connect authentication realm in {es}.
6+
configuration of the respective OpenID Connect authentication realm in {es}.
77

88
[[security-api-oidc-prepare-authentication-request]]
99
==== {api-request-title}
@@ -16,13 +16,13 @@ configuration of the respective OpenID Connect authentication realm in {es}.
1616
[[security-api-oidc-prepare-authentication-desc]]
1717
==== {api-description-title}
1818

19-
The response of this API is a URL pointing to the Authorization Endpoint of the
20-
configured OpenID Connect Provider and can be used to redirect the browser of
19+
The response of this API is a URL pointing to the Authorization Endpoint of the
20+
configured OpenID Connect Provider and can be used to redirect the browser of
2121
the user in order to continue the authentication process.
2222

2323
{es} exposes all the necessary OpenID Connect related functionality via the
2424
OpenID Connect APIs. These APIs are used internally by {kib} in order to provide
25-
OpenID Connect based authentication, but can also be used by other, custom web
25+
OpenID Connect based authentication, but can also be used by other, custom web
2626
applications or other clients. See also
2727
<<security-api-oidc-authenticate,OpenID Connect authenticate API>>
2828
and <<security-api-oidc-logout,OpenID Connect logout API>>.
@@ -81,7 +81,8 @@ the Authentication Request, as HTTP GET parameters:
8181
{
8282
"redirect" : "http://127.0.0.1:8080/c2id-login?scope=openid&response_type=id_token&redirect_uri=https%3A%2F%2Fmy.fantastic.rp%2Fcb&state=4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I&nonce=WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM&client_id=elasticsearch-rp",
8383
"state" : "4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
84-
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM"
84+
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM",
85+
"realm" : "oidc1"
8586
}
8687
--------------------------------------------------
8788
// TESTRESPONSE[s/4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I/\$\{body.state\}/]
@@ -109,7 +110,8 @@ the Authentication Request, as HTTP GET parameters:
109110
{
110111
"redirect" : "http://127.0.0.1:8080/c2id-login?scope=openid&response_type=id_token&redirect_uri=https%3A%2F%2Fmy.fantastic.rp%2Fcb&state=lGYK0EcSLjqH6pkT5EVZjC6eIW5YCGgywj2sxROO&nonce=zOBXLJGUooRrbLbQk5YCcyC8AXw3iloynvluYhZ5&client_id=elasticsearch-rp",
111112
"state" : "lGYK0EcSLjqH6pkT5EVZjC6eIW5YCGgywj2sxROO",
112-
"nonce" : "zOBXLJGUooRrbLbQk5YCcyC8AXw3iloynvluYhZ5"
113+
"nonce" : "zOBXLJGUooRrbLbQk5YCcyC8AXw3iloynvluYhZ5",
114+
"realm" : "oidc1"
113115
}
114116
--------------------------------------------------
115117

@@ -134,8 +136,9 @@ the Authentication Request, as HTTP GET parameters:
134136
{
135137
"redirect" : "http://127.0.0.1:8080/c2id-login?login_hint=this_is_an_opaque_string&scope=openid&response_type=id_token&redirect_uri=https%3A%2F%2Fmy.fantastic.rp%2Fcb&state=4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I&nonce=WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM&client_id=elasticsearch-rp",
136138
"state" : "4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
137-
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM"
139+
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM",
140+
"realm" : "oidc1"
138141
}
139142
--------------------------------------------------
140143
// TESTRESPONSE[s/4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I/\$\{body.state\}/]
141-
// TESTRESPONSE[s/WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM/\$\{body.nonce\}/]
144+
// TESTRESPONSE[s/WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM/\$\{body.nonce\}/]

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationResponse.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.oidc;
77

8+
import org.elasticsearch.Version;
89
import org.elasticsearch.action.ActionResponse;
910
import org.elasticsearch.common.io.stream.StreamInput;
1011
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -28,18 +29,26 @@ public class OpenIdConnectPrepareAuthenticationResponse extends ActionResponse i
2829
* String value used to associate a Client session with an ID Token, and to mitigate replay attacks.
2930
*/
3031
private String nonce;
32+
/*
33+
* String value: name of the realm used to perform authentication.
34+
*/
35+
private String realmName;
3136

32-
public OpenIdConnectPrepareAuthenticationResponse(String authorizationEndpointUrl, String state, String nonce) {
37+
public OpenIdConnectPrepareAuthenticationResponse(String authorizationEndpointUrl, String state, String nonce, String realmName) {
3338
this.authenticationRequestUrl = authorizationEndpointUrl;
3439
this.state = state;
3540
this.nonce = nonce;
41+
this.realmName = realmName;
3642
}
3743

3844
public OpenIdConnectPrepareAuthenticationResponse(StreamInput in) throws IOException {
3945
super(in);
4046
authenticationRequestUrl = in.readString();
4147
state = in.readString();
4248
nonce = in.readString();
49+
if (in.getVersion().onOrAfter(Version.V_7_11_0)) {
50+
realmName = in.readString();
51+
}
4352
}
4453

4554
public String getAuthenticationRequestUrl() {
@@ -54,15 +63,23 @@ public String getNonce() {
5463
return nonce;
5564
}
5665

66+
public String getRealmName() {
67+
return realmName;
68+
}
69+
5770
@Override
5871
public void writeTo(StreamOutput out) throws IOException {
5972
out.writeString(authenticationRequestUrl);
6073
out.writeString(state);
6174
out.writeString(nonce);
75+
if (out.getVersion().onOrAfter(Version.V_7_11_0)) {
76+
out.writeString(realmName);
77+
}
6278
}
6379

6480
public String toString() {
65-
return "{authenticationRequestUrl=" + authenticationRequestUrl + ", state=" + state + ", nonce=" + nonce + "}";
81+
return "{authenticationRequestUrl=" + authenticationRequestUrl + ", state=" + state + ", nonce="
82+
+ nonce + ", realmName" + realmName + "}";
6683
}
6784

6885
@Override
@@ -71,6 +88,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7188
builder.field("redirect", authenticationRequestUrl);
7289
builder.field("state", state);
7390
builder.field("nonce", nonce);
91+
if(realmName != null){
92+
builder.field("realm", realmName);
93+
}
7494
builder.endObject();
7595
return builder;
7696
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public OpenIdConnectPrepareAuthenticationResponse buildAuthenticationRequestUri(
365365
builder.loginHint(loginHint);
366366
}
367367
return new OpenIdConnectPrepareAuthenticationResponse(builder.build().toURI().toString(),
368-
state.getValue(), nonce.getValue());
368+
state.getValue(), nonce.getValue(), this.name());
369369
}
370370

371371
public boolean isIssuerValid(String issuer) {

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ public void testBuildRelyingPartyConfigWithoutOpenIdScope() {
272272
assertThat(response.getAuthenticationRequestUrl(),
273273
equalTo("https://op.example.com/login?scope=scope1+scope2+openid&response_type=code" +
274274
"&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my"));
275+
assertThat(response.getRealmName(), equalTo(REALM_NAME));
275276
}
276277

277278
public void testBuildingAuthenticationRequest() {
@@ -295,6 +296,7 @@ public void testBuildingAuthenticationRequest() {
295296
assertThat(response.getAuthenticationRequestUrl(),
296297
equalTo("https://op.example.com/login?scope=openid+scope1+scope2&response_type=code" +
297298
"&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my"));
299+
assertThat(response.getRealmName(), equalTo(REALM_NAME));
298300
}
299301

300302
public void testBuilidingAuthenticationRequestWithDefaultScope() {
@@ -316,6 +318,7 @@ public void testBuilidingAuthenticationRequestWithDefaultScope() {
316318
final String nonce = response.getNonce();
317319
assertThat(response.getAuthenticationRequestUrl(), equalTo("https://op.example.com/login?scope=openid&response_type=code" +
318320
"&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my"));
321+
assertThat(response.getRealmName(), equalTo(REALM_NAME));
319322
}
320323

321324
public void testBuildLogoutResponse() throws Exception {
@@ -373,6 +376,7 @@ public void testBuildingAuthenticationRequestWithExistingStateAndNonce() {
373376

374377
assertThat(response.getAuthenticationRequestUrl(), equalTo("https://op.example.com/login?scope=openid&response_type=code" +
375378
"&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my"));
379+
assertThat(response.getRealmName(), equalTo(REALM_NAME));
376380
}
377381

378382
public void testBuildingAuthenticationRequestWithLoginHint() {
@@ -397,6 +401,7 @@ public void testBuildingAuthenticationRequestWithLoginHint() {
397401
assertThat(response.getAuthenticationRequestUrl(), equalTo("https://op.example.com/login?login_hint=" + thehint +
398402
"&scope=openid&response_type=code&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" +
399403
state + "&nonce=" + nonce + "&client_id=rp-my"));
404+
assertThat(response.getRealmName(), equalTo(REALM_NAME));
400405
}
401406

402407
private AuthenticationResult authenticateWithOidc(String principal, UserRoleMapper roleMapper, boolean notPopulateMetadata,

0 commit comments

Comments
 (0)