-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from peternied/basic-auth-via-internal-idp
Add token authentication flow
- Loading branch information
Showing
7 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
39 changes: 39 additions & 0 deletions
39
...er/src/main/java/org/opensearch/identity/internal/noop/InternalAuthenticationManager.java
This file contains 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,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.identity.noop; | ||
|
||
import org.opensearch.identity.AccessTokenManager; | ||
import org.opensearch.identity.AuthenticationManager; | ||
import org.opensearch.identity.noop.InternalSubject; | ||
import org.opensearch.authn.Subject; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.mgt.DefaultSecurityManager; | ||
import org.apache.shiro.mgt.SecurityManager; | ||
|
||
/** | ||
* Implementation of authentication manager that does not enforce authentication | ||
* | ||
* This class and related classes in this package will not return nulls or fail permissions checks | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class InternalAuthenticationManager implements AuthenticationManager { | ||
|
||
public InternalAuthenticationManager() { | ||
final SecurityManager securityManager = new DefaultSecurityManager(InternalRealm.INSTANCE); | ||
SecurityUtils.setSecurityManager(securityManager); | ||
} | ||
|
||
@Override | ||
public Subject getSubject() { | ||
return new InternalSubject(SecurityUtils.getSubject()); | ||
} | ||
|
||
@Override | ||
public AccessTokenManager getAccessTokenManager() { | ||
return null; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
server/src/main/java/org/opensearch/identity/internal/noop/InternalSubject.java
This file contains 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,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.identity.noop; | ||
|
||
import java.security.Principal; | ||
import java.util.Objects; | ||
|
||
import org.opensearch.authn.Subject; | ||
import org.opensearch.authn.Principals; | ||
import org.apache.shiro.authc.UsernamePasswordToken; | ||
|
||
/** | ||
* Implementation of subject that is always authenticated | ||
* | ||
* This class and related classes in this package will not return nulls or fail permissions checks | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class InternalSubject implements Subject { | ||
|
||
private final org.apache.shiro.subject.Subject shiroSubject; | ||
|
||
public InternalSubject(org.apache.shiro.subject.Subject subject) { | ||
shiroSubject = subject; | ||
} | ||
|
||
@Override | ||
public Principal getPrincipal() { | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null || getClass() != obj.getClass()) return false; | ||
Subject that = (Subject) obj; | ||
return Objects.equals(getPrincipal(), that.getPrincipal()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getPrincipal()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "InternalSubject(principal=" + getPrincipal() + ")"; | ||
} | ||
|
||
/** | ||
* Logs the user in | ||
*/ | ||
void login(AuthenticationToken authenticationToken) { | ||
|
||
AuthenticationToken authToken; | ||
|
||
if (authenticationToken instanceof HttpHeaderToken) { | ||
final HttpHeaderToken headerToken = (HttpHeaderToken) authenticationToken; | ||
|
||
if (token.getHeaderValue().contains("Basic")) { | ||
final byte[] decodedAuthHeader = Base64.getDecoder().decode(token.getHeaderValue().substring("Basic".length()).trim()); | ||
final String[] decodedUserNamePassword = decodedAuthHeader.toString().split(":"); | ||
|
||
authToken = new UsernamePasswordToken(decodedUserNamePassword[0], decodedUserNamePassword[1]); | ||
} | ||
} | ||
|
||
|
||
shiroSubject.login(authToken); | ||
|
||
return; // Do nothing we are already logged in to nothing | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
server/src/main/java/org/opensearch/identity/internal/noop/NoopAccessTokenManager.java
This file contains 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,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.identity.noop; | ||
|
||
import org.opensearch.authn.AccessToken; | ||
import org.opensearch.identity.AccessTokenManager; | ||
|
||
/** | ||
* Implementation of access token manager that does not enforce authentication | ||
* | ||
* This class and related classes in this package will not return nulls or fail permissions checks | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class NoopAccessTokenManager implements AccessTokenManager { | ||
|
||
@Override | ||
public void expireAllTokens() { | ||
// Tokens cannot be expired | ||
} | ||
|
||
@Override | ||
public AccessToken generate() { | ||
return new AccessToken(); | ||
} | ||
|
||
@Override | ||
public AccessToken refresh(final AccessToken token) { | ||
return new AccessToken(); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
server/src/main/java/org/opensearch/identity/internal/noop/package-info.java
This file contains 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,7 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** Classes for the internal authentication in OpenSearch */ | ||
package org.opensearch.identity.internal; |
This file contains 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 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