|
@@ -24,6 +24,7 @@ import org.elasticsearch.xpack.core.security.user.User;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.time.Instant;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -35,12 +36,13 @@ public record ProfileDocument(
|
|
|
boolean enabled,
|
|
|
long lastSynchronized,
|
|
|
ProfileDocumentUser user,
|
|
|
- Access access,
|
|
|
+ Map<String, Object> access,
|
|
|
BytesReference applicationData
|
|
|
) implements ToXContentObject {
|
|
|
|
|
|
public record ProfileDocumentUser(
|
|
|
String username,
|
|
|
+ List<String> roles,
|
|
|
Authentication.RealmRef realm,
|
|
|
String email,
|
|
|
String fullName,
|
|
@@ -52,6 +54,7 @@ public record ProfileDocument(
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
builder.startObject("user");
|
|
|
builder.field("username", username);
|
|
|
+ builder.field("roles", roles);
|
|
|
builder.startObject("realm");
|
|
|
builder.field("name", realm.getName());
|
|
|
builder.field("type", realm.getType());
|
|
@@ -72,23 +75,7 @@ public record ProfileDocument(
|
|
|
}
|
|
|
|
|
|
public Profile.ProfileUser toProfileUser(@Nullable String realmDomain) {
|
|
|
- return new Profile.ProfileUser(username, realm.getName(), realmDomain, email, fullName, displayName, active);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public record Access(List<String> roles, Map<String, Object> applications) implements ToXContent {
|
|
|
-
|
|
|
- @Override
|
|
|
- public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
- builder.startObject("access");
|
|
|
- builder.field("roles", roles);
|
|
|
- builder.field("applications", applications);
|
|
|
- builder.endObject();
|
|
|
- return builder;
|
|
|
- }
|
|
|
-
|
|
|
- public Profile.Access toProfileAccess() {
|
|
|
- return new Profile.Access(roles, applications);
|
|
|
+ return new Profile.ProfileUser(username, roles, realm.getName(), realmDomain, email, fullName, displayName, active);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -99,8 +86,13 @@ public record ProfileDocument(
|
|
|
builder.field("enabled", enabled);
|
|
|
builder.field("last_synchronized", lastSynchronized);
|
|
|
user.toXContent(builder, params);
|
|
|
- access.toXContent(builder, params);
|
|
|
- if (applicationData != null) {
|
|
|
+
|
|
|
+ if (params.paramAsBoolean("include_access", true) && access != null) {
|
|
|
+ builder.field("access", access);
|
|
|
+ } else {
|
|
|
+ builder.startObject("access").endObject();
|
|
|
+ }
|
|
|
+ if (params.paramAsBoolean("include_data", true) && applicationData != null) {
|
|
|
builder.field("application_data", applicationData);
|
|
|
} else {
|
|
|
builder.startObject("application_data").endObject();
|
|
@@ -118,13 +110,14 @@ public record ProfileDocument(
|
|
|
Instant.now().toEpochMilli(),
|
|
|
new ProfileDocumentUser(
|
|
|
subjectUser.principal(),
|
|
|
+ Arrays.asList(subjectUser.roles()),
|
|
|
subject.getRealm(),
|
|
|
subjectUser.email(),
|
|
|
subjectUser.fullName(),
|
|
|
null,
|
|
|
subjectUser.enabled()
|
|
|
),
|
|
|
- new Access(List.of(subjectUser.roles()), Map.of()),
|
|
|
+ Map.of(),
|
|
|
null
|
|
|
);
|
|
|
}
|
|
@@ -133,27 +126,23 @@ public record ProfileDocument(
|
|
|
return PARSER.apply(parser, null);
|
|
|
}
|
|
|
|
|
|
- static final ConstructingObjectParser<ProfileDocumentUser, Void> PROFILE_USER_PARSER = new ConstructingObjectParser<>(
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ static final ConstructingObjectParser<ProfileDocumentUser, Void> PROFILE_DOC_USER_PARSER = new ConstructingObjectParser<>(
|
|
|
"user_profile_document_user",
|
|
|
false,
|
|
|
(args, v) -> new ProfileDocumentUser(
|
|
|
(String) args[0],
|
|
|
- (Authentication.RealmRef) args[1],
|
|
|
- (String) args[2],
|
|
|
+ (List<String>) args[1],
|
|
|
+ (Authentication.RealmRef) args[2],
|
|
|
(String) args[3],
|
|
|
(String) args[4],
|
|
|
- (Boolean) args[5]
|
|
|
+ (String) args[5],
|
|
|
+ (Boolean) args[6]
|
|
|
)
|
|
|
);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- static final ConstructingObjectParser<Access, Void> ACCESS_PARSER = new ConstructingObjectParser<>(
|
|
|
- "user_profile_document_access",
|
|
|
- false,
|
|
|
- (args, v) -> new Access((List<String>) args[0], (Map<String, Object>) args[1])
|
|
|
- );
|
|
|
-
|
|
|
- static final ConstructingObjectParser<ProfileDocument, Void> PROFILE_PARSER = new ConstructingObjectParser<>(
|
|
|
+ static final ConstructingObjectParser<ProfileDocument, Void> PROFILE_DOC_PARSER = new ConstructingObjectParser<>(
|
|
|
"user_profile_document",
|
|
|
false,
|
|
|
(args, v) -> new ProfileDocument(
|
|
@@ -161,7 +150,7 @@ public record ProfileDocument(
|
|
|
(boolean) args[1],
|
|
|
(long) args[2],
|
|
|
(ProfileDocumentUser) args[3],
|
|
|
- (Access) args[4],
|
|
|
+ (Map<String, Object>) args[4],
|
|
|
(BytesReference) args[5]
|
|
|
)
|
|
|
);
|
|
@@ -172,28 +161,37 @@ public record ProfileDocument(
|
|
|
(args, v) -> (ProfileDocument) args[0]
|
|
|
);
|
|
|
|
|
|
+ // TODO:This is a copy from Authentication class. This version ignores unknown fields so that it currently ignores the domain field
|
|
|
+ // The support will be added later when authentication update is finalised.
|
|
|
+ public static ConstructingObjectParser<Authentication.RealmRef, Void> REALM_REF_PARSER = new ConstructingObjectParser<>(
|
|
|
+ "realm_ref",
|
|
|
+ true,
|
|
|
+ (args, v) -> new Authentication.RealmRef((String) args[0], (String) args[1], (String) args[2])
|
|
|
+ );
|
|
|
+
|
|
|
static {
|
|
|
- PROFILE_USER_PARSER.declareString(constructorArg(), new ParseField("username"));
|
|
|
- PROFILE_USER_PARSER.declareObject(
|
|
|
- constructorArg(),
|
|
|
- (p, c) -> Authentication.REALM_REF_PARSER.parse(p, null),
|
|
|
- new ParseField("realm")
|
|
|
- );
|
|
|
- PROFILE_USER_PARSER.declareString(optionalConstructorArg(), new ParseField("email"));
|
|
|
- PROFILE_USER_PARSER.declareString(optionalConstructorArg(), new ParseField("full_name"));
|
|
|
- PROFILE_USER_PARSER.declareString(optionalConstructorArg(), new ParseField("display_name"));
|
|
|
- PROFILE_USER_PARSER.declareBoolean(constructorArg(), new ParseField("active"));
|
|
|
- ACCESS_PARSER.declareStringArray(constructorArg(), new ParseField("roles"));
|
|
|
- ACCESS_PARSER.declareObject(constructorArg(), (p, c) -> p.map(), new ParseField("applications"));
|
|
|
-
|
|
|
- PROFILE_PARSER.declareString(constructorArg(), new ParseField("uid"));
|
|
|
- PROFILE_PARSER.declareBoolean(constructorArg(), new ParseField("enabled"));
|
|
|
- PROFILE_PARSER.declareLong(constructorArg(), new ParseField("last_synchronized"));
|
|
|
- PROFILE_PARSER.declareObject(constructorArg(), (p, c) -> PROFILE_USER_PARSER.parse(p, null), new ParseField("user"));
|
|
|
- PROFILE_PARSER.declareObject(constructorArg(), (p, c) -> ACCESS_PARSER.parse(p, null), new ParseField("access"));
|
|
|
+ REALM_REF_PARSER.declareString(constructorArg(), new ParseField("name"));
|
|
|
+ REALM_REF_PARSER.declareString(constructorArg(), new ParseField("type"));
|
|
|
+ REALM_REF_PARSER.declareString(constructorArg(), new ParseField("node_name"));
|
|
|
+ }
|
|
|
+
|
|
|
+ static {
|
|
|
+ PROFILE_DOC_USER_PARSER.declareString(constructorArg(), new ParseField("username"));
|
|
|
+ PROFILE_DOC_USER_PARSER.declareStringArray(constructorArg(), new ParseField("roles"));
|
|
|
+ PROFILE_DOC_USER_PARSER.declareObject(constructorArg(), (p, c) -> REALM_REF_PARSER.parse(p, null), new ParseField("realm"));
|
|
|
+ PROFILE_DOC_USER_PARSER.declareString(optionalConstructorArg(), new ParseField("email"));
|
|
|
+ PROFILE_DOC_USER_PARSER.declareString(optionalConstructorArg(), new ParseField("full_name"));
|
|
|
+ PROFILE_DOC_USER_PARSER.declareString(optionalConstructorArg(), new ParseField("display_name"));
|
|
|
+ PROFILE_DOC_USER_PARSER.declareBoolean(constructorArg(), new ParseField("active"));
|
|
|
+
|
|
|
+ PROFILE_DOC_PARSER.declareString(constructorArg(), new ParseField("uid"));
|
|
|
+ PROFILE_DOC_PARSER.declareBoolean(constructorArg(), new ParseField("enabled"));
|
|
|
+ PROFILE_DOC_PARSER.declareLong(constructorArg(), new ParseField("last_synchronized"));
|
|
|
+ PROFILE_DOC_PARSER.declareObject(constructorArg(), (p, c) -> PROFILE_DOC_USER_PARSER.parse(p, null), new ParseField("user"));
|
|
|
+ PROFILE_DOC_PARSER.declareObject(constructorArg(), (p, c) -> p.map(), new ParseField("access"));
|
|
|
ObjectParserHelper<ProfileDocument, Void> parserHelper = new ObjectParserHelper<>();
|
|
|
- parserHelper.declareRawObject(PROFILE_PARSER, constructorArg(), new ParseField("application_data"));
|
|
|
+ parserHelper.declareRawObject(PROFILE_DOC_PARSER, constructorArg(), new ParseField("application_data"));
|
|
|
|
|
|
- PARSER.declareObject(constructorArg(), (p, c) -> PROFILE_PARSER.parse(p, null), new ParseField("user_profile"));
|
|
|
+ PARSER.declareObject(constructorArg(), (p, c) -> PROFILE_DOC_PARSER.parse(p, null), new ParseField("user_profile"));
|
|
|
}
|
|
|
}
|