Package com.nimbusds.oauth2.sdk.token
Class AccessToken
- java.lang.Object
-
- com.nimbusds.oauth2.sdk.id.Identifier
-
- com.nimbusds.oauth2.sdk.token.Token
-
- com.nimbusds.oauth2.sdk.token.AccessToken
-
- All Implemented Interfaces:
Serializable,Comparable<Identifier>,net.minidev.json.JSONAware
- Direct Known Subclasses:
BearerAccessToken,TypelessAccessToken
public abstract class AccessToken extends Token
The base abstract class for access tokens. Concrete extending classes should be immutable.Related specifications:
- OAuth 2.0 (RFC 6749), sections 1.4 and 5.1.
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class com.nimbusds.oauth2.sdk.id.Identifier
DEFAULT_BYTE_LENGTH, secureRandom
-
-
Constructor Summary
Constructors Constructor Description AccessToken(AccessTokenType type)Creates a new minimal access token with a randomly generated 256-bit (32-byte) value, Base64URL-encoded.AccessToken(AccessTokenType type, int byteLength)Creates a new minimal access token with a randomly generated value of the specified byte length, Base64URL-encoded.AccessToken(AccessTokenType type, int byteLength, long lifetime, Scope scope)Creates a new access token with a randomly generated value of the specified byte length, Base64URL-encoded, and optional lifetime and scope.AccessToken(AccessTokenType type, long lifetime, Scope scope)Creates a new access token with a randomly generated 256-bit (32-byte) value, Base64URL-encoded.AccessToken(AccessTokenType type, String value)Creates a new minimal access token with the specified value.AccessToken(AccessTokenType type, String value, long lifetime, Scope scope)Creates a new access token with the specified value and optional lifetime and scope.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description longgetLifetime()Returns the lifetime of this access token.Set<String>getParameterNames()Returns the token parameter names included in the JSON object, as required for the composition of an access token response.ScopegetScope()Returns the scope of this access token.AccessTokenTypegetType()Returns the access token type.static AccessTokenparse(String header)Parses anAuthorizationHTTP request header value for an access token.static AccessTokenparse(net.minidev.json.JSONObject jsonObject)Parses an access token from a JSON object access token response.abstract StringtoAuthorizationHeader()Returns theAuthorizationHTTP request header value for this access token.net.minidev.json.JSONObjecttoJSONObject()Returns the token parameters as a JSON object, as required for the composition of an access token response.StringtoJSONString()Returns the JSON string representation of this identifier.-
Methods inherited from class com.nimbusds.oauth2.sdk.id.Identifier
compareTo, equals, getValue, hashCode, toString, toStringList
-
-
-
-
Constructor Detail
-
AccessToken
public AccessToken(AccessTokenType type)
Creates a new minimal access token with a randomly generated 256-bit (32-byte) value, Base64URL-encoded. The optional lifetime and scope are left undefined.- Parameters:
type- The access token type. Must not benull.
-
AccessToken
public AccessToken(AccessTokenType type, int byteLength)
Creates a new minimal access token with a randomly generated value of the specified byte length, Base64URL-encoded. The optional lifetime and scope are left undefined.- Parameters:
type- The access token type. Must not benull.byteLength- The byte length of the value to generate. Must be greater than one.
-
AccessToken
public AccessToken(AccessTokenType type, long lifetime, Scope scope)
Creates a new access token with a randomly generated 256-bit (32-byte) value, Base64URL-encoded.- Parameters:
type- The access token type. Must not benull.lifetime- The lifetime in seconds, 0 if not specified.scope- The scope,nullif not specified.
-
AccessToken
public AccessToken(AccessTokenType type, int byteLength, long lifetime, Scope scope)
Creates a new access token with a randomly generated value of the specified byte length, Base64URL-encoded, and optional lifetime and scope.- Parameters:
type- The access token type. Must not benull.byteLength- The byte length of the value to generate. Must be greater than one.lifetime- The lifetime in seconds, 0 if not specified.scope- The scope,nullif not specified.
-
AccessToken
public AccessToken(AccessTokenType type, String value)
Creates a new minimal access token with the specified value. The optional lifetime and scope are left undefined.- Parameters:
type- The access token type. Must not benull.value- The access token value. Must not benullor empty string.
-
AccessToken
public AccessToken(AccessTokenType type, String value, long lifetime, Scope scope)
Creates a new access token with the specified value and optional lifetime and scope.- Parameters:
type- The access token type. Must not benull.value- The access token value. Must not benullor empty string.lifetime- The lifetime in seconds, 0 if not specified.scope- The scope,nullif not specified.
-
-
Method Detail
-
getType
public AccessTokenType getType()
Returns the access token type.- Returns:
- The access token type.
-
getLifetime
public long getLifetime()
Returns the lifetime of this access token.- Returns:
- The lifetime in seconds, 0 if not specified.
-
getScope
public Scope getScope()
Returns the scope of this access token.- Returns:
- The scope,
nullif not specified.
-
getParameterNames
public Set<String> getParameterNames()
Description copied from class:TokenReturns the token parameter names included in the JSON object, as required for the composition of an access token response. See OAuth 2.0 (RFC 6749), section 5.1.- Specified by:
getParameterNamesin classToken- Returns:
- The token parameter names.
-
toJSONObject
public net.minidev.json.JSONObject toJSONObject()
Description copied from class:TokenReturns the token parameters as a JSON object, as required for the composition of an access token response. See OAuth 2.0 (RFC 6749), section 5.1.Note that JSONObject implements Map<String,Object>.
Example:
{ "access_token" : "2YotnFZFEjr1zCsicMWpAA", "token_type" : "example", "expires_in" : 3600, "example_parameter" : "example_value" }- Specified by:
toJSONObjectin classToken- Returns:
- The token parameters as a JSON object.
-
toJSONString
public String toJSONString()
Description copied from class:IdentifierReturns the JSON string representation of this identifier.- Specified by:
toJSONStringin interfacenet.minidev.json.JSONAware- Overrides:
toJSONStringin classIdentifier- Returns:
- The JSON string.
-
toAuthorizationHeader
public abstract String toAuthorizationHeader()
Returns theAuthorizationHTTP request header value for this access token.- Returns:
- The
Authorizationheader value.
-
parse
public static AccessToken parse(net.minidev.json.JSONObject jsonObject) throws ParseException
Parses an access token from a JSON object access token response. Only bearer access tokens are supported.- Parameters:
jsonObject- The JSON object to parse. Must not benull.- Returns:
- The access token.
- Throws:
ParseException- If the JSON object couldn't be parsed to an access token.
-
parse
public static AccessToken parse(String header) throws ParseException
Parses anAuthorizationHTTP request header value for an access token. Only bearer access token are supported.- Parameters:
header- TheAuthorizationheader value to parse. Must not benull.- Returns:
- The access token.
- Throws:
ParseException- If theAuthorizationheader value couldn't be parsed to an access token.
-
-