forked from eclipse-ditto/ditto
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-ditto#926] extend authentication result to include JWT.
Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
- Loading branch information
Showing
17 changed files
with
354 additions
and
99 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
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
115 changes: 115 additions & 0 deletions
115
.../eclipse/ditto/services/gateway/security/authentication/AbstractAuthenticationResult.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,115 @@ | ||
/* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.services.gateway.security.authentication; | ||
|
||
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.NotThreadSafe; | ||
|
||
import org.eclipse.ditto.model.base.auth.AuthorizationContext; | ||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
|
||
/** | ||
* Abstract implementation of an authentication result that bears an {@link org.eclipse.ditto.model.base.auth.AuthorizationContext}. | ||
*/ | ||
@NotThreadSafe | ||
public abstract class AbstractAuthenticationResult implements AuthenticationResult { | ||
|
||
private final DittoHeaders dittoHeaders; | ||
@Nullable private final AuthorizationContext authorizationContext; | ||
@Nullable private final Throwable reasonOfFailure; | ||
|
||
/** | ||
* Construct an authentication result. | ||
* | ||
* @param dittoHeaders the Ditto headers without authorization context. | ||
* @param authorizationContext the authorization context. | ||
* @param reasonOfFailure the reason for authentication failure or null if authentication is successful. | ||
*/ | ||
protected AbstractAuthenticationResult(final DittoHeaders dittoHeaders, | ||
@Nullable final AuthorizationContext authorizationContext, @Nullable final Throwable reasonOfFailure) { | ||
|
||
checkNotNull(dittoHeaders, "dittoHeaders"); | ||
if (null != authorizationContext) { | ||
// merge present authorizationContext into dittoHeaders: | ||
this.dittoHeaders = dittoHeaders.toBuilder() | ||
.authorizationContext(authorizationContext) | ||
.build(); | ||
} else { | ||
this.dittoHeaders = dittoHeaders; | ||
} | ||
this.authorizationContext = authorizationContext; | ||
this.reasonOfFailure = reasonOfFailure; | ||
} | ||
|
||
@Override | ||
public boolean isSuccess() { | ||
return null != authorizationContext; | ||
} | ||
|
||
@Override | ||
public AuthorizationContext getAuthorizationContext() { | ||
if (null == authorizationContext) { | ||
if (reasonOfFailure instanceof RuntimeException) { | ||
throw (RuntimeException) reasonOfFailure; | ||
} | ||
throw new IllegalStateException(reasonOfFailure); | ||
} | ||
|
||
return authorizationContext; | ||
} | ||
|
||
@Override | ||
public DittoHeaders getDittoHeaders() { | ||
return dittoHeaders; | ||
} | ||
|
||
@Override | ||
public Throwable getReasonOfFailure() { | ||
if (null == reasonOfFailure) { | ||
throw new IllegalStateException("Authentication was successful!"); | ||
} | ||
|
||
return reasonOfFailure; | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final AbstractAuthenticationResult that = (AbstractAuthenticationResult) o; | ||
return Objects.equals(dittoHeaders, that.dittoHeaders) && | ||
Objects.equals(authorizationContext, that.authorizationContext) && | ||
Objects.equals(reasonOfFailure, that.reasonOfFailure); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(dittoHeaders, authorizationContext, reasonOfFailure); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "dittoHeaders=" + dittoHeaders + | ||
", authorizationContext=" + authorizationContext + | ||
", reasonOfFailure=" + reasonOfFailure; | ||
} | ||
|
||
} |
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
50 changes: 50 additions & 0 deletions
50
...se/ditto/services/gateway/security/authentication/jwt/DefaultJwtAuthenticationResult.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,50 @@ | ||
/* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.services.gateway.security.authentication.jwt; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.eclipse.ditto.model.base.auth.AuthorizationContext; | ||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.jwt.JsonWebToken; | ||
import org.eclipse.ditto.services.gateway.security.authentication.AbstractAuthenticationResult; | ||
|
||
/** | ||
* Implementation of JwtAuthenticationResult. | ||
*/ | ||
final class DefaultJwtAuthenticationResult extends AbstractAuthenticationResult implements JwtAuthenticationResult { | ||
|
||
@Nullable | ||
private final JsonWebToken jwt; | ||
|
||
DefaultJwtAuthenticationResult(final DittoHeaders dittoHeaders, | ||
@Nullable final AuthorizationContext authorizationContext, | ||
@Nullable final Throwable reasonOfFailure, | ||
@Nullable final JsonWebToken jwt) { | ||
super(dittoHeaders, authorizationContext, reasonOfFailure); | ||
this.jwt = jwt; | ||
} | ||
|
||
@Override | ||
public Optional<JsonWebToken> getJwt() { | ||
return Optional.ofNullable(jwt); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object that) { | ||
return super.equals(that) && Objects.equals(jwt, ((DefaultJwtAuthenticationResult) that).jwt); | ||
} | ||
} |
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
Oops, something went wrong.