public class AccessTokenGenerator extends Object
This is a helper class to take care of all the generating and verifying of access tokens. Since tokens are salted, encoded and stored in the database (with the salt as part of the encoded token) we need to be able to retrieve the encoded token from the database uniquely in order to validate it against the given token. Because of the possibility of bare tokens (tokens used in the header to authenticate without providing a username) we cannot use the username as this ID. As a solution, we generate an ID and embed it as part of the token given to the user.
The token will be in the form base64.encode(id + ':' + secret)
, where the id a 12-digit random long and the
secret is 20 bytes of random data.
The caller of this class should not have to worry about the form of the token. The caller can extract the token
ID of a token generated by generateToken()
using getId(String)
and can encode the token for
storage using hashToken(String)
.
Expected flow for generating new tokens:
generateToken()
to generate a tokengetId(String)
to get the ID of the tokenhashToken(String)
to get the hashed tokenExpected flow for authenticating tokens
getId(String)
and use the ID to retrieve the hashed token from the databaseauthenticateToken(String, String)
to determine whether the
provide token is validConstructor and Description |
---|
AccessTokenGenerator() |
Modifier and Type | Method and Description |
---|---|
boolean |
authenticateToken(String token,
String hashedToken)
Given a token (such as that provided by the user) and its hashed token, return whether the given token matches
the hashed token.
|
String |
generateToken()
Generate an access token which encapsulates its ID and only uses base64 characters.
|
String |
getId(String token)
Since tokens generated with
generateToken() encapsulate the token ID, it can be extracted from
a token using this method. |
String |
hashToken(String token)
Given a token, encode it so that it can be safely stored in a database.
|
boolean |
isValidToken(String token)
Validates whether the given token is in the form of a valid access token.
|
public boolean authenticateToken(@NotNull String token, @NotNull String hashedToken)
token
- the token (as provided by the userhashedToken
- the hashed token that was saved for the given token's IDtrue
if the token is valid for the given hashed token. false
otherwise.@NotNull public String generateToken()
In order to save the generated token, pass its value into hashToken(String)
and store
the resulting hashed token. To extract the token's ID from the token, call getId(String)
.
@NotNull public String getId(@NotNull String token) throws IllegalArgumentException
generateToken()
encapsulate the token ID, it can be extracted from
a token using this method.
The ID of a token will always be 12 chars long and contain only integer characters.
token
- The token (such as that provided by the user when authenticating or by generateToken()
)IllegalArgumentException
- if the provided token is not in the expected form (as validated by isValidToken(String)
@NotNull public String hashToken(@NotNull String token)
PasswordEncoder
so that the unencoded token cannot
be derived (in any practical time) by an attacker who obtains the Bitbucket database.
Note that this is not a repeatable action; calling this method with the same input more than once will not return
the same hashed token. As such it is intended to be called once on initial token creation and then stored in the
database alongside the token's IDtoken
- The token, as generated by generateToken()
IllegalArgumentException
- if the provided token is not in the expected form (as validated by isValidToken(String)
public boolean isValidToken(@NotNull String token)
token
- the raw value of the token to validatetrue
if the given token will not produce exceptions when calling other methods, false
otherwiseCopyright © 2020 Atlassian Software Systems Pty Ltd. All rights reserved.