Class ServiceProviderToken
- java.lang.Object
 - 
- com.atlassian.bitbucket.jenkins.internal.applink.oauth.Token
 - 
- com.atlassian.bitbucket.jenkins.internal.applink.oauth.serviceprovider.token.ServiceProviderToken
 
 
 
- 
@Immutable public final class ServiceProviderToken extends Token
Representation of an OAuth token for use by service providers. A token can be either a request token or an access token. Tokens always have a token value, a token secret and theConsumerthe token belongs to. A request token that has been authorized will also contain the user that authorized the request. An access token will always contain user that gave permission to theConsumerto make requests on their behalf.Tokens instances are immutable. To create a new
ServiceProviderTokeninstance, use builder. To build an unauthorized request token, use thenewRequestToken(String)as followsServiceProviderToken unauthorizedRequestToken = ServiceProviderToken.newRequestToken("bb6dd1391ce33b5bd3ecad1175139a39") .tokenSecret("29c3005cc5fbe5d431f27b29d6191ea3") .consumer(consumer) .build();An authorized request token can be built by calling
authorize(String, String)method on an unauthorized request tokenServiceProviderToken authorizedRequestToken = unauthorizedRequestToken.authorize(fred);
or from scratch in a similar way to unauthorized request tokens, but also setting the authorizedBy attribute by callingServiceProviderToken.ServiceProviderTokenBuilder.authorizedBy(String)before calling build()ServiceProviderToken authorizedRequestToken = ServiceProviderToken.newRequestToken("bb6dd1391ce33b5bd3ecad1175139a39") .tokenSecret("29c3005cc5fbe5d431f27b29d6191ea3") .consumer(consumer) .authorizedBy(fred) .build();To build an access token, use the
newAccessToken(String)method as the starting pointServiceProviderToken accessToken = ServiceProviderToken.newAccessToken("bb6dd1391ce33b5bd3ecad1175139a39") .tokenSecret("29c3005cc5fbe5d431f27b29d6191ea3") .consumer(consumer) .authorizedBy(fred) .build();ServiceProviderTokens also have two additional attributes that control when they expire: thecreationTimeandtimeToLive. If these values are not specified when building a token, the defaults are used. The default value forcreationTimeis when theServiceProviderToken.ServiceProviderTokenBuilder.build()method is called and the token constructed. The default value fortimeToLivedepends on the type of token being constructed. For request tokens, the default value is 10 minutes. For access tokens, the default value is 1 week. When a token has been around for longer than itstimeToLive, any attempts to use it should result in an OAuth problem oftoken_expired, as described in the- See Also:
 - OAuth problem reporting spec
 
 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static classServiceProviderToken.AuthorizationDefines the status of request tokens.static classServiceProviderToken.ServiceProviderTokenBuilderstatic classServiceProviderToken.SessionRepresentation of an OAuth session.- 
Nested classes/interfaces inherited from class com.atlassian.bitbucket.jenkins.internal.applink.oauth.Token
Token.TokenBuilder<T,B extends Token.TokenBuilder<T,B>>, Token.Type 
 - 
 
- 
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_ACCESS_TOKEN_TTLThe default value for access token time to live.static longDEFAULT_REQUEST_TOKEN_TTLThe default value for request token time to live.static longDEFAULT_SESSION_TTLThe default value for session time to live. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ServiceProviderTokenauthorize(String user, String verifier)If this is an unauthorized request token, this method will return a request token that has been authorized by theuser.ServiceProviderTokendeny(String user)If this is an unauthorized request token, this method will return a request token that has been denied by theuser.ServiceProviderToken.AuthorizationgetAuthorization()Returns the authorization status of this token.URIgetCallback()Returns theURIthe consumer should be redirected to after the user has completed authorization.longgetCreationTime()Returns the time the token was originally created for the user, in milliseconds.ServiceProviderToken.SessiongetSession()Returns theSessionassociated with the token.longgetTimeToLive()Returns the maximum amount of time the token is considered valid, in milliseconds.StringgetUser()If this is an authorized request token, returns the user that authorized the token.StringgetVerifier()If this is an authorized request token, returns the verification code that is used to verify the user that authorized the token is the same one that is swapping it for an access token.booleanhasBeenAuthorized()Returnstrueif this token has been authorized,falseotherwise.booleanhasBeenDenied()Returnstrueif this token has been denied,falseotherwise.booleanhasExpired(Clock clock)Returnstrueif the time to live has been exceeded,falseotherwise.booleanhasSession()Returnstrueif there is aSessionassociated with the token.static booleanisValidCallback(URI callback)static ServiceProviderToken.ServiceProviderTokenBuildernewAccessToken(String token)Static factory method that starts the process of building an accessServiceProviderTokeninstance.static ServiceProviderToken.ServiceProviderTokenBuildernewRequestToken(String token)Static factory method that starts the process of building a requestServiceProviderTokeninstance.- 
Methods inherited from class com.atlassian.bitbucket.jenkins.internal.applink.oauth.Token
getConsumer, getProperties, getProperty, getPropertyNames, getToken, getTokenSecret, hasProperty, isAccessToken, isRequestToken, toString 
 - 
 
 - 
 
- 
- 
Field Detail
- 
DEFAULT_REQUEST_TOKEN_TTL
public static final long DEFAULT_REQUEST_TOKEN_TTL
The default value for request token time to live. Value corresponds to 10 minutes in ms. 
- 
DEFAULT_ACCESS_TOKEN_TTL
public static final long DEFAULT_ACCESS_TOKEN_TTL
The default value for access token time to live. Value corresponds to 5 years in ms. 
- 
DEFAULT_SESSION_TTL
public static final long DEFAULT_SESSION_TTL
The default value for session time to live. Value corresponds to 5 years + 30 days in ms. This value is supposed to be longer thanDEFAULT_ACCESS_TOKEN_TTLso that the session is still live while the access token has just expired. 
 - 
 
- 
Method Detail
- 
newRequestToken
public static ServiceProviderToken.ServiceProviderTokenBuilder newRequestToken(String token)
Static factory method that starts the process of building a requestServiceProviderTokeninstance. Returns aServiceProviderTokenBuilderso the additional attributes of the token can be set.- Parameters:
 token- unique token used to theServiceProviderTokento be used in OAuth operations- Returns:
 - builder to set additional attributes and build the 
ServiceProviderToken 
 
- 
newAccessToken
public static ServiceProviderToken.ServiceProviderTokenBuilder newAccessToken(String token)
Static factory method that starts the process of building an accessServiceProviderTokeninstance. Returns aServiceProviderTokenBuilderso the additional attributes of the token can be set.- Parameters:
 token- unique token used to theServiceProviderTokento be used in OAuth operations- Returns:
 - builder to set additional attributes and build the 
ServiceProviderToken 
 
- 
authorize
public ServiceProviderToken authorize(String user, String verifier)
If this is an unauthorized request token, this method will return a request token that has been authorized by theuser.- Parameters:
 user- name of the user that has authorized the request tokenverifier- value used to prove the user authorizing the token is the same as the one swapping it for an access token- Returns:
 - authorized request token
 - Throws:
 IllegalStateException- thrown if the token is not a request token or has already been authorized or denied
 
- 
hasBeenAuthorized
public boolean hasBeenAuthorized()
Returnstrueif this token has been authorized,falseotherwise. This is a short-cut for callinggetAuthorization()and checking the return type. As such, it has the same condition that it will always returntrueif the token is an access token.- Returns:
 trueif this token has been authorized,falseotherwise
 
- 
deny
public ServiceProviderToken deny(String user)
If this is an unauthorized request token, this method will return a request token that has been denied by theuser.- Parameters:
 user- name of the user that has denied the request token- Returns:
 - denied request token
 - Throws:
 IllegalStateException- thrown if the token is not a request token or has already been authorized or denied
 
- 
hasBeenDenied
public boolean hasBeenDenied()
Returnstrueif this token has been denied,falseotherwise. This is a short-cut for callinggetAuthorization()and checking the return type. As such, it has the same condition that it will always returnfalseif the token is an access token.- Returns:
 trueif this token has been denied,falseotherwise
 
- 
getAuthorization
public ServiceProviderToken.Authorization getAuthorization()
Returns the authorization status of this token. If the token is a request token, it will returnAuthorization.NONEif it the user has not yet approved or denied the request,Authorization.APPROVEDif the user approved the access request, orAuthorization.DENIEDif the user denied the access request. For access tokens,Authorized.APPROVEDwill always be returned.- Returns:
 - authorization status of this token
 
 
- 
getUser
@Nullable public String getUser()
If this is an authorized request token, returns the user that authorized the token. If this is an access token, it's the user theConsumeris making requests on behalf of. Returnsnullotherwise.- Returns:
 - name of the user that authorized the 
Consumerto make requests on behalf of themselves 
 
- 
getVerifier
@Nullable public String getVerifier()
If this is an authorized request token, returns the verification code that is used to verify the user that authorized the token is the same one that is swapping it for an access token. Returnsnullotherwise.- Returns:
 - verification code that is used to verify the user that authorized the token is the same one that is swapping it for an access token
 
 
- 
getCreationTime
public long getCreationTime()
Returns the time the token was originally created for the user, in milliseconds.- Returns:
 - time the token was originally created for the user, in milliseconds
 
 
- 
getTimeToLive
public long getTimeToLive()
Returns the maximum amount of time the token is considered valid, in milliseconds.- Returns:
 - maximum amount of time the token is considered valid, in milliseconds
 
 
- 
hasExpired
public boolean hasExpired(Clock clock)
Returnstrueif the time to live has been exceeded,falseotherwise.- Parameters:
 clock- a way to determine the current time- Returns:
 trueif the time to live has been exceeded,falseotherwise
 
- 
getCallback
public URI getCallback()
Returns theURIthe consumer should be redirected to after the user has completed authorization. It will benullif theURIwas communicated out-of-band via another form of communication between the service provider and consumer. It will also benullif the token is a version 1.0 request token.- Returns:
 URIthe consumer should be redirected to after the user has completed authorization
 
- 
isValidCallback
public static boolean isValidCallback(URI callback)
 
- 
getSession
public ServiceProviderToken.Session getSession()
Returns theSessionassociated with the token. 
- 
hasSession
public boolean hasSession()
Returnstrueif there is aSessionassociated with the token. 
 - 
 
 -