-
Notifications
You must be signed in to change notification settings - Fork 2.9k
TokenManager Interface #7452
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
peternied
merged 31 commits into
opensearch-project:main
from
stephen-crawford:tokenManager
Jun 6, 2023
Merged
TokenManager Interface #7452
Changes from 6 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a73d42f
TokenManager Interface
stephen-crawford 6cd0353
Update javadocs
stephen-crawford 53f58a3
Merge branch 'opensearch-project:main' into tokenManager
stephen-crawford ba38c92
Update changelog
stephen-crawford c2d4129
Merge branch 'main' into tokenManager
stephen-crawford 3f2d228
Update code coverage
stephen-crawford 1d05fc2
add getter
stephen-crawford 54ef048
Swap to issue word
stephen-crawford 19e51cb
Merge branch 'main' into tokenManager
stephen-crawford fb7f8aa
Increase coverage
stephen-crawford b86b24e
Spotless
stephen-crawford d90e8e1
Create bearer auth token
stephen-crawford f8647d6
java doc
stephen-crawford 3fd358f
Add full token string
stephen-crawford b05964f
Add coverage for bearer token type
stephen-crawford 17e5a44
spotless
stephen-crawford 8102b23
Merge branch 'opensearch-project:main' into tokenManager
stephen-crawford 29042b5
Merge branch 'main' into tokenManager
stephen-crawford 0fc832e
Merge branch 'main' into tokenManager
stephen-crawford 1e9afe3
Implement Password generation for development
stephen-crawford ff876ab
fix audit
stephen-crawford d353f62
Update plugins/identity-shiro/src/main/java/org/opensearch/identity/s…
stephen-crawford 2b1a1fe
Update server/src/main/java/org/opensearch/identity/tokens/BearerAuth…
stephen-crawford 1bb88a6
Merge branch 'opensearch-project:main' into tokenManager
stephen-crawford 57acaca
spotless
stephen-crawford 323d6f6
Swap exception type
stephen-crawford 510ce2e
Merge branch 'main' into tokenManager
stephen-crawford 1151c49
Merge branch 'opensearch-project:main' into tokenManager
stephen-crawford 4e48c62
Fix changelog
stephen-crawford e156bfe
Update token manager
stephen-crawford fedcfd1
Merge branch 'opensearch-project:main' into tokenManager
stephen-crawford 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
37 changes: 0 additions & 37 deletions
37
plugins/identity-shiro/src/main/java/org/opensearch/identity/shiro/AuthTokenHandler.java
This file was deleted.
Oops, something went wrong.
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
97 changes: 97 additions & 0 deletions
97
plugins/identity-shiro/src/main/java/org/opensearch/identity/shiro/ShiroTokenHandler.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,97 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.identity.shiro; | ||
|
|
||
| import java.util.Base64; | ||
| import java.util.Optional; | ||
|
|
||
| import org.apache.shiro.SecurityUtils; | ||
| import org.apache.shiro.authc.AuthenticationToken; | ||
| import org.apache.shiro.authc.UsernamePasswordToken; | ||
| import org.opensearch.identity.Subject; | ||
| import org.opensearch.identity.tokens.AuthToken; | ||
| import org.opensearch.identity.tokens.BasicAuthToken; | ||
| import org.opensearch.identity.tokens.TokenManager; | ||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| /** | ||
| * Extracts Shiro's {@link AuthenticationToken} from different types of auth headers | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| class ShiroTokenHandler implements TokenManager { | ||
|
stephen-crawford marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Translates into shiro auth token from the given header token | ||
| * @param authenticationToken the token from which to translate | ||
| * @return An optional of the shiro auth token for login | ||
| */ | ||
| public Optional<AuthenticationToken> translateAuthToken(org.opensearch.identity.tokens.AuthToken authenticationToken) { | ||
| if (authenticationToken instanceof BasicAuthToken) { | ||
| final BasicAuthToken basicAuthToken = (BasicAuthToken) authenticationToken; | ||
| return Optional.of(new UsernamePasswordToken(basicAuthToken.getUser(), basicAuthToken.getPassword())); | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public AuthToken generateToken() { | ||
|
|
||
| Subject subject = new ShiroSubject(this, SecurityUtils.getSubject()); | ||
| final byte[] rawEncoded = Base64.getEncoder().encode((subject.getPrincipal().getName() + ":" + generatePassword()).getBytes(UTF_8)); | ||
| final String usernamePassword = new String(rawEncoded, UTF_8); | ||
| final String header = "Basic " + usernamePassword; | ||
|
|
||
| return new BasicAuthToken(header); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean validateToken(AuthToken token) { | ||
| if (token instanceof BasicAuthToken) { | ||
| final BasicAuthToken basicAuthToken = (BasicAuthToken) token; | ||
| if (basicAuthToken.getUser().equals(SecurityUtils.getSubject()) && basicAuthToken.getPassword().equals(generatePassword())) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String getTokenInfo(AuthToken token) { | ||
| if (token instanceof BasicAuthToken) { | ||
| final BasicAuthToken basicAuthToken = (BasicAuthToken) token; | ||
| return basicAuthToken.toString(); | ||
| } | ||
| throw new UnsupportedAuthenticationToken(); | ||
| } | ||
|
|
||
| @Override | ||
| public void revokeToken(AuthToken token) { | ||
| if (token instanceof BasicAuthToken) { | ||
| final BasicAuthToken basicAuthToken = (BasicAuthToken) token; | ||
| basicAuthToken.revoke(); | ||
| return; | ||
| } | ||
| throw new UnsupportedAuthenticationToken(); | ||
| } | ||
|
|
||
| @Override | ||
| public void resetToken(AuthToken token) { | ||
| if (token instanceof BasicAuthToken) { | ||
| final BasicAuthToken basicAuthToken = (BasicAuthToken) token; | ||
| basicAuthToken.revoke(); | ||
| } | ||
| } | ||
|
|
||
| public String generatePassword() { | ||
|
peternied marked this conversation as resolved.
|
||
| return "superSecurePassword1!"; | ||
| } | ||
|
|
||
| } | ||
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
86 changes: 86 additions & 0 deletions
86
server/src/main/java/org/opensearch/identity/noop/NoopTokenHandler.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,86 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.identity.noop; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.OpenSearchException; | ||
| import org.opensearch.identity.IdentityService; | ||
| import org.opensearch.identity.tokens.AuthToken; | ||
| import org.opensearch.identity.tokens.NoopToken; | ||
| import org.opensearch.identity.tokens.TokenManager; | ||
|
|
||
| /** | ||
| * This class represents a Noop Token Manager | ||
| */ | ||
| public class NoopTokenHandler implements TokenManager { | ||
|
stephen-crawford marked this conversation as resolved.
Outdated
|
||
|
|
||
| private static final Logger log = LogManager.getLogger(IdentityService.class); | ||
|
|
||
| /** | ||
| * Generate a new Noop Token | ||
| * @return a new Noop Token | ||
| */ | ||
| @Override | ||
| public AuthToken generateToken() { | ||
| return new NoopToken(); | ||
|
stephen-crawford marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Validate a token | ||
| * @param token The token to be validated | ||
| * @return If the token is a Noop Token, then pass with True; otherwise fail with False. | ||
| */ | ||
| @Override | ||
| public boolean validateToken(AuthToken token) { | ||
| if (token instanceof NoopToken) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Get token info, there should not be any token info so just return whether the token is a NoopToken | ||
| * @param token The auth token to be parsed | ||
| * @return A String stating the token is a NoopToken or is not a NopToken | ||
| */ | ||
| @Override | ||
| public String getTokenInfo(AuthToken token) { | ||
| if (token instanceof NoopToken) { | ||
| return "Token is NoopToken"; | ||
| } | ||
| return "Token is not a NoopToken"; | ||
| } | ||
|
|
||
| /** | ||
| * Revoking a Noop Token should not do anything | ||
| * @param token The Auth Token to be revoked | ||
| */ | ||
| @Override | ||
| public void revokeToken(AuthToken token) { | ||
| if (token instanceof NoopToken) { | ||
| log.info("Revoke operation is not supported for NoopTokens"); | ||
| return; | ||
| } | ||
| throw new OpenSearchException("Token is not a NoopToken"); | ||
| } | ||
|
|
||
| /** | ||
| * Refreshing a NoopToken also not do anything | ||
| * @param token The token to be refreshed | ||
| */ | ||
| @Override | ||
| public void resetToken(AuthToken token) { | ||
| if (token instanceof NoopToken) { | ||
| log.info("Reset operation is not supported for NoopTokens"); | ||
| return; | ||
| } | ||
| throw new OpenSearchException("Token is not a NoopToken"); | ||
| } | ||
| } | ||
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.