Class JWTSecret
java.lang.Object
org.gcube.common.security.secrets.Secret
org.gcube.common.security.secrets.JWTSecret
- Direct Known Subclasses:
UmaTokenSecret
public class JWTSecret
extends org.gcube.common.security.secrets.Secret
A JSON Web Token issued by the D4Science IAM, e.g. the bearer token a service receives in the
Authorization header. Its Context is its audience, which must be a single value.
The token is valid only when it is issued by one of the trusted IAM realms
(setTrustedIssuers(Collection)) and its signature is verified with the key of that realm.
The issuer is checked before any connection, so a token naming another realm is rejected without
contacting it. Whoever hosts the code sets the trusted realms (SmartGears sets the realm of the
container); while none is set, every token is rejected.
The claims (owner, Context, roles, expiration) are read from the payload of the token without
verifying it: they can be trusted only after isValid() has returned true.
- Author:
- Luca Frosini (ISTI - CNR)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intPriority of the secrets based on the tokens of the IAM. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the Context named in the audience of the token.protected StringReturns the encoded token.org.gcube.common.security.OwnergetOwner()Returns the owner named in the token.Returns the IAM realms whose tokens are accepted.booleanbooleanisValid()Tells whether the token is issued by a trusted IAM realm and its signature is verified with the key of that realm.intpriority()static voidsetTrustedIssuers(Collection<String> issuers) Sets the IAM realms whose tokens are accepted, for the whole JVM.
-
Field Details
-
KEYCLOAK_BASED_SECRET_PRIORITY
public static final int KEYCLOAK_BASED_SECRET_PRIORITYPriority of the secrets based on the tokens of the IAM.- See Also:
-
-
Constructor Details
-
JWTSecret
Creates the secret of an encoded token.- Parameters:
encodedToken- the encoded JWT
-
-
Method Details
-
setTrustedIssuers
Sets the IAM realms whose tokens are accepted, for the whole JVM.- Parameters:
issuers- the base URLs of the realms, as in theissclaim of their tokens, e.g.https://accounts.d4science.org/auth/realms/d4science; a trailing slash is ignored
-
getTrustedIssuers
Returns the IAM realms whose tokens are accepted.- Returns:
- the base URLs of the realms, without trailing slash; empty when none is set
-
priority
public int priority()- Specified by:
priorityin classorg.gcube.common.security.secrets.Secret
-
getOwner
public org.gcube.common.security.Owner getOwner()Returns the owner named in the token. The claims are not verified here: trust them only whenisValid()returnstrue.- Specified by:
getOwnerin classorg.gcube.common.security.secrets.Secret- Returns:
- the owner of the token
-
getContext
Returns the Context named in the audience of the token. The claims are not verified here: trust them only whenisValid()returnstrue.- Specified by:
getContextin classorg.gcube.common.security.secrets.Secret- Returns:
- the Context of the token
-
getHTTPAuthorizationHeaders
- Specified by:
getHTTPAuthorizationHeadersin classorg.gcube.common.security.secrets.Secret
-
getEncodedToken
Returns the encoded token.- Returns:
- the encoded JWT
-
isExpired
public boolean isExpired()- Specified by:
isExpiredin classorg.gcube.common.security.secrets.Secret
-
isValid
public boolean isValid()Tells whether the token is issued by a trusted IAM realm and its signature is verified with the key of that realm. The expiration is not checked here: seeisExpired().- Specified by:
isValidin classorg.gcube.common.security.secrets.Secret- Returns:
truewhen the token is valid
-