-
Notifications
You must be signed in to change notification settings - Fork 25.6k
HLRC: Add security Create Token API #34791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
36df032
[HLRC] Add security Create Token API
tvernum f8d7b11
Address feedback
tvernum 8aa40f9
Make request class final
tvernum 7359bf9
Merge branch 'master' into hlrc/create-token
tvernum eb431c9
Merge branch 'master' into hlrc/create-token
tvernum ebb4233
address additional feedback
tvernum 7f7c82f
Merge branch 'master' into hlrc/create-token
tvernum 04f0936
Merge branch 'master' into hlrc/create-token
tvernum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...t/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client.security; | ||
|
|
||
| import org.elasticsearch.client.Validatable; | ||
| import org.elasticsearch.common.CharArrays; | ||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Request to create a new OAuth2 token from the Elasticsearch cluster. | ||
| */ | ||
| public final class CreateTokenRequest implements Validatable, ToXContentObject { | ||
|
|
||
| private final String grantType; | ||
| private final String scope; | ||
| private final String username; | ||
| private final char[] password; | ||
| private final String refreshToken; | ||
|
|
||
| /** | ||
| * General purpose constructor. This constructor is typically not useful, and one of the following factory methods should be used | ||
| * instead: | ||
| * <ul> | ||
| * <li>{@link #passwordGrant(String, char[])}</li> | ||
| * <li>{@link #refreshTokenGrant(String)}</li> | ||
| * <li>{@link #clientCredentialsGrant()}</li> | ||
| * </ul> | ||
| */ | ||
| public CreateTokenRequest(String grantType, @Nullable String scope, @Nullable String username, @Nullable char[] password, | ||
| @Nullable String refreshToken) { | ||
| if (Strings.isNullOrEmpty(grantType)) { | ||
| throw new IllegalArgumentException("grant_type is required"); | ||
| } | ||
| this.grantType = grantType; | ||
| this.username = username; | ||
| this.password = password; | ||
| this.scope = scope; | ||
| this.refreshToken = refreshToken; | ||
| } | ||
|
|
||
| public static CreateTokenRequest passwordGrant(String username, char[] password) { | ||
| if (Strings.isNullOrEmpty(username)) { | ||
| throw new IllegalArgumentException("username is required"); | ||
| } | ||
| if (password == null || password.length == 0) { | ||
| throw new IllegalArgumentException("password is required"); | ||
| } | ||
| return new CreateTokenRequest("password", null, username, password, null); | ||
| } | ||
|
|
||
| public static CreateTokenRequest refreshTokenGrant(String refreshToken) { | ||
| if (Strings.isNullOrEmpty(refreshToken)) { | ||
| throw new IllegalArgumentException("refresh_token is required"); | ||
| } | ||
| return new CreateTokenRequest("refresh_token", null, null, null, refreshToken); | ||
bizybot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public static CreateTokenRequest clientCredentialsGrant() { | ||
| return new CreateTokenRequest("client_credentials", null, null, null, null); | ||
| } | ||
|
|
||
| public String getGrantType() { | ||
| return grantType; | ||
| } | ||
|
|
||
| public String getScope() { | ||
| return scope; | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return username; | ||
| } | ||
|
|
||
| public char[] getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public String getRefreshToken() { | ||
| return refreshToken; | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject() | ||
| .field("grant_type", grantType); | ||
| if (scope != null) { | ||
| builder.field("scope", scope); | ||
| } | ||
| if (username != null) { | ||
| builder.field("username", username); | ||
| } | ||
| if (password != null) { | ||
| byte[] passwordBytes = CharArrays.toUtf8Bytes(password); | ||
| try { | ||
| builder.field("password").utf8Value(passwordBytes, 0, passwordBytes.length); | ||
| } finally { | ||
| Arrays.fill(passwordBytes, (byte) 0); | ||
| } | ||
| } | ||
| if (refreshToken != null) { | ||
| builder.field("refresh_token", refreshToken); | ||
| } | ||
| return builder.endObject(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| final CreateTokenRequest that = (CreateTokenRequest) o; | ||
| return Objects.equals(grantType, that.grantType) && | ||
| Objects.equals(scope, that.scope) && | ||
| Objects.equals(username, that.username) && | ||
| Arrays.equals(password, that.password) && | ||
| Objects.equals(refreshToken, that.refreshToken); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = Objects.hash(grantType, scope, username, refreshToken); | ||
| result = 31 * result + Arrays.hashCode(password); | ||
| return result; | ||
| } | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
.../rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client.security; | ||
|
|
||
| import org.elasticsearch.common.ParseField; | ||
| import org.elasticsearch.common.unit.TimeValue; | ||
| import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
| import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
|
||
| /** | ||
| * Response when creating a new OAuth2 token in the Elasticsearch cluster. Contains an access token, the token's expiry, and an optional | ||
| * refresh token. | ||
| */ | ||
| public final class CreateTokenResponse { | ||
|
|
||
| private final String accessToken; | ||
| private final String type; | ||
| private final TimeValue expiresIn; | ||
| private final String scope; | ||
| private final String refreshToken; | ||
|
|
||
| public CreateTokenResponse(String accessToken, String type, TimeValue expiresIn, String scope, String refreshToken) { | ||
| this.accessToken = accessToken; | ||
| this.type = type; | ||
| this.expiresIn = expiresIn; | ||
| this.scope = scope; | ||
| this.refreshToken = refreshToken; | ||
| } | ||
|
|
||
| public String getAccessToken() { | ||
| return accessToken; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public TimeValue getExpiresIn() { | ||
| return expiresIn; | ||
| } | ||
|
|
||
| public String getScope() { | ||
| return scope; | ||
| } | ||
|
|
||
| public String getRefreshToken() { | ||
| return refreshToken; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| final CreateTokenResponse that = (CreateTokenResponse) o; | ||
| return Objects.equals(accessToken, that.accessToken) && | ||
| Objects.equals(type, that.type) && | ||
| Objects.equals(expiresIn, that.expiresIn) && | ||
| Objects.equals(scope, that.scope) && | ||
| Objects.equals(refreshToken, that.refreshToken); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(accessToken, type, expiresIn, scope, refreshToken); | ||
| } | ||
|
|
||
| private static final ConstructingObjectParser<CreateTokenResponse, Void> PARSER = new ConstructingObjectParser<>( | ||
| "create_token_response", true, args -> new CreateTokenResponse( | ||
| (String) args[0], (String) args[1], TimeValue.timeValueSeconds((Long) args[2]), (String) args[3], (String) args[4])); | ||
|
|
||
| static { | ||
| PARSER.declareString(constructorArg(), new ParseField("access_token")); | ||
| PARSER.declareString(constructorArg(), new ParseField("type")); | ||
| PARSER.declareLong(constructorArg(), new ParseField("expires_in")); | ||
| PARSER.declareStringOrNull(optionalConstructorArg(), new ParseField("scope")); | ||
| PARSER.declareStringOrNull(optionalConstructorArg(), new ParseField("refresh_token")); | ||
| } | ||
|
|
||
| public static CreateTokenResponse fromXContent(XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, null); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.