Skip to content

Commit 4b49f85

Browse files
authored
Return realm name in SAML Authenticate API (#52188)
This is useful in cases where the caller of the API needs to know the name of the realm that consumed the SAML Response and authenticated the user and this is not self evident (i.e. because there are many saml realms defined in ES). Currently, the way to learn the realm name would be to make a subsequent request to the `_authenticate` API.
1 parent 4ace062 commit 4b49f85

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ clients. See also
5050
(Required, array) A json array with all the valid SAML Request Ids that the caller of
5151
the API has for the current user.
5252

53+
`realm`::
54+
(Optional, string) The name of the realm that should authenticate the SAML response.
55+
Useful in cases where many SAML realms are defined.
56+
5357
[[security-api-saml-authenticate-response-body]]
54-
==== {api-response-body-title}
58+
==== {api-response-body-title}
5559

5660
`access_token`::
5761
(string) The access token that was generated by {es}.
@@ -61,6 +65,8 @@ clients. See also
6165
(integer) The amount of time (in seconds) left until the token expires.
6266
`refresh_token`::
6367
(string) The refresh token that was generated by {es}.
68+
`realm`::
69+
(string) The name of the realm that the user was authenticated by.
6470

6571
[[security-api-saml-authenticate-example]]
6672
==== {api-examples-title}
@@ -87,7 +93,8 @@ The API returns the following response:
8793
"access_token" : "46ToAxZVaXVVZTVKOVF5YU04ZFJVUDVSZlV3",
8894
"username" : "Bearer",
8995
"expires_in" : 1200,
90-
"refresh_token": "mJdXLtmvTUSpoLwMvdBt_w"
96+
"refresh_token": "mJdXLtmvTUSpoLwMvdBt_w",
97+
"realm": "saml1"
9198
}
9299
--------------------------------------------------
93100
// NOTCONSOLE

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateResponse.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.saml;
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;
@@ -21,18 +22,23 @@ public final class SamlAuthenticateResponse extends ActionResponse {
2122
private String principal;
2223
private String tokenString;
2324
private String refreshToken;
25+
private String realm;
2426
private TimeValue expiresIn;
2527

2628
public SamlAuthenticateResponse(StreamInput in) throws IOException {
2729
super(in);
2830
principal = in.readString();
31+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
32+
realm = in.readString();
33+
}
2934
tokenString = in.readString();
3035
refreshToken = in.readString();
3136
expiresIn = in.readTimeValue();
3237
}
3338

34-
public SamlAuthenticateResponse(String principal, String tokenString, String refreshToken, TimeValue expiresIn) {
39+
public SamlAuthenticateResponse(String principal, String realm, String tokenString, String refreshToken, TimeValue expiresIn) {
3540
this.principal = principal;
41+
this.realm = realm;
3642
this.tokenString = tokenString;
3743
this.refreshToken = refreshToken;
3844
this.expiresIn = expiresIn;
@@ -42,6 +48,10 @@ public String getPrincipal() {
4248
return principal;
4349
}
4450

51+
public String getRealm() {
52+
return realm;
53+
}
54+
4555
public String getTokenString() {
4656
return tokenString;
4757
}
@@ -57,6 +67,9 @@ public TimeValue getExpiresIn() {
5767
@Override
5868
public void writeTo(StreamOutput out) throws IOException {
5969
out.writeString(principal);
70+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
71+
out.writeString(realm);
72+
}
6073
out.writeString(tokenString);
6174
out.writeString(refreshToken);
6275
out.writeTimeValue(expiresIn);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ protected void doExecute(Task task, SamlAuthenticateRequest request, ActionListe
6868
tokenMeta, true, ActionListener.wrap(tuple -> {
6969
final TimeValue expiresIn = tokenService.getExpirationDelay();
7070
listener.onResponse(
71-
new SamlAuthenticateResponse(authentication.getUser().principal(), tuple.v1(), tuple.v2(), expiresIn));
71+
new SamlAuthenticateResponse(authentication.getUser().principal(),
72+
authentication.getAuthenticatedBy().getName(), tuple.v1(), tuple.v2(), expiresIn));
7273
}, listener::onFailure));
7374
}, e -> {
7475
logger.debug(() -> new ParameterizedMessage("SamlToken [{}] could not be authenticated", saml), e);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c
9797
public RestResponse buildResponse(SamlAuthenticateResponse response, XContentBuilder builder) throws Exception {
9898
builder.startObject()
9999
.field("username", response.getPrincipal())
100+
.field("realm", response.getRealm())
100101
.field("access_token", response.getTokenString())
101102
.field("refresh_token", response.getRefreshToken())
102103
.field("expires_in", response.getExpiresIn().seconds())

0 commit comments

Comments
 (0)