Access tokens represent a secure and credible verification of wallet and/or token ownership that can be leveraged repeatedly without additional user interaction until expiration.
Anatomy of an Access Token
Access tokens represent a secure and credible verification of wallet and/or token ownership that can be leveraged repeatedly without additional user interaction until expiration.
The ENS name for the wallet address if it exists; otherwise, the wallet address
contractAddress
The token contract address associated with this access token (Optional). This will only be part of the token if it is a requirement in the auth request
tokenBalance
The token balance held by the wallet address for the contract address at the time the token was issued (Optional). This will only be part of the token if it is a requirement in the auth request.
tokenIds
The token IDs address associated with this access token (Optional). This will only be part of the token if it is a requirement in the auth request.
iat
Issued at timestamp (UTC in seconds).
ext
Expiration timestamp (UTC in seconds). The default expiration is 12 hours.
iss
sub
The "subject" of the request. This field is populated with the blockchain associated with this token and the user's wallet address
aud
Identifies the project space. This field is populated with the Picket project ID
ver
The version of Picket API used to generate the token
tid
Unique token identifier
email
An auto-generated web3 email for the user based off their wallet address. There is no guarantee the user will check this inbox.
If you want an email for your users, you should collect it manually.
Get an Access Token
Access tokens are returned after a successful auth flow, which is usually initiated by picket.login. An access token is valid until its expiration timestamp (exp). Once an access token expires, the user will have to re-login to get a new one.
Remember, an access token is a secret. Never share an access token with anyone!
Once a user is logged in, you'll get their access token as part of the response from picket.login. If you are using a Picket client-side SDK, the user's access token will be saved to the browser's local storage and can be access as part of the SDK's auth state.
Examples
Get the access token from the auth state once the user is logged in
Access tokens are used in JWT-based authentication to allow an application to access an API. To include an access token (JWT) in a request, send it as a Bearer Token in the HTTP Authorization header.
Once a backend receives an access token in the authorization header, it should parse the header and validate the access token. After validating, the backend can trust that the request came from the associated wallet address!
Validate an Access Token
The simplest way to validate an access token is to leverage Picket's /auth/validate endpoint; however, you can also validate access token manually.
import Picket from"@picketapi/picket-node";constpicket=newPicket("YOUR_PROJECT_SECRET_KEY");const accessToken = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3YWxsZXRBZGRyZXNzIjoiMHhBNTQ4YWM5YzZmNDFCMTkxNzUwMjNCNWYwOWM2RTcwZjk5Y0ZlZWM4IiwiY29udHJhY3RBZGRyZXNzIjoiMHhiYzRjYTBlZGE3NjQ3YThhYjdjMjA2MWMyZTExOGExOGE5MzZmMTNkIiwibnVtVG9rZW5zIjoiMC4xMzM3IiwibmJmIjoxNjQ5MTkxMjU4LCJpYXQiOjE2NDkxOTEyNTgsImV4dCI6MTY0OTE5MTg1OCwiaXNzIjoicGlja2V0YXBpLmNvbSIsInN1YiI6ImV0aGVyZXVtL21haW5uZXQ6MHhBNTQ4YWM5YzZmNDFCMTkxNzUwMjNCNWYwOWM2RTcwZjk5Y0ZlZWM4IiwiYXVkIjoiNTNlNzRiZGYtNmU2ZS00ZjMxLWIwZDEtY2VkYTE4OWE2YTI1IiwidGlkIjoiNThkYjRhMjYtZmJjMS00YzUzLTg3NDEtNWQzOWUyNWY1NGY1IiwidmVyIjoxfQ.nI6E5yzn3gHvw7ego37A_PkoLOLfkdrIPCjYi3XVMC6zTPSjD8-aZOB-auIW92Rgsf_NggAGiT2XXRTaEq_8hA";
try {// optionally include token ownership requirements as the second parameterconstdecodedAccessToken=awaitpicket.validate(accessToken)} catch (err) {console.error("invalid access token:", err);}
import { Buffer } from"buffer";// if using node >= 17.5, fetch is included and globally available// else, install node-fetchimport fetch from"node-fetch";constbase64SecretKey=Buffer.from("YOUR_PROJECT_SECRET_KEY").toString("base64");const accessToken = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3YWxsZXRBZGRyZXNzIjoiMHhBNTQ4YWM5YzZmNDFCMTkxNzUwMjNCNWYwOWM2RTcwZjk5Y0ZlZWM4IiwiY29udHJhY3RBZGRyZXNzIjoiMHhiYzRjYTBlZGE3NjQ3YThhYjdjMjA2MWMyZTExOGExOGE5MzZmMTNkIiwibnVtVG9rZW5zIjoiMC4xMzM3IiwibmJmIjoxNjQ5MTkxMjU4LCJpYXQiOjE2NDkxOTEyNTgsImV4dCI6MTY0OTE5MTg1OCwiaXNzIjoicGlja2V0YXBpLmNvbSIsInN1YiI6ImV0aGVyZXVtL21haW5uZXQ6MHhBNTQ4YWM5YzZmNDFCMTkxNzUwMjNCNWYwOWM2RTcwZjk5Y0ZlZWM4IiwiYXVkIjoiNTNlNzRiZGYtNmU2ZS00ZjMxLWIwZDEtY2VkYTE4OWE2YTI1IiwidGlkIjoiNThkYjRhMjYtZmJjMS00YzUzLTg3NDEtNWQzOWUyNWY1NGY1IiwidmVyIjoxfQ.nI6E5yzn3gHvw7ego37A_PkoLOLfkdrIPCjYi3XVMC6zTPSjD8-aZOB-auIW92Rgsf_NggAGiT2XXRTaEq_8hA";
constresp=awaitfetch("https://picketapi.com/api/v1/auth/validate", { method:"POST", headers: {"Content-Type":"application/json", Authorization:`Basic ${base64SecretKey}`, }, data:JSON.stringify({ accessToken,// optionally include token ownership requirements// requirements: {} })});// invalid acccess tokenif (resp.status !==200) {// do somethingconsole.error("invalid access token!");}// else use decoded JWT data const { walletAddress } =awaitresp.json();console.log("valid access token for wallet address", walletAddress);
Steps to Manually Validate an Access Token
This section gets into the weeds for those who want it. For the easiest and fastest way to validate JWTs, we recommend using our validate endpoint to validate JWTs server-side which handles this all for you in a single api call.
1. Perform standard JWT validation
The process for validating any JWT is as follows
Check the JWT is well-formed. This can be done without knowing the encryption method or signing key
Check the signature. This requires fetching the corresponding public key from Pickets' JSON Web Keys (JWKs) endpoint (/.well-known/jwks.json).
Check the standard claims
The issuer iss equals picketapi.com
Check that the JWT hasn't expired. The expiration time exp must be later than (>) the current unix timestamp
For more details on how to do this, we recommend Auth0's documentation on this standard process.
2. Verify the audience (aud) claim
Once you performed the standard JWT validation, you'll have the decoded JWT. With the decoded JWT, you'll want to verify that the aud equals your Picket project ID. This prevents other people from using a different an access token from another Picket-powered application.
// replace with a real IDconstPICKET_PROJECT_ID="53e74bdf-6e6e-4f31-b0d1-ceda189a6a25";constcheckClaims= (jwt) => {// deconstruction can be done in parameter declaration, // moved here for readabilityconst { iss,aud } = jwt;// check both fields existif (!(iss && aud)) returnfalse;return iss ==="picketapi.com"&& aud ===PICKET_PROJECT_ID;}
3. (Optional) Verify token ownership requirements
If this is a token-gated API, you'll need to verify the access token meets the requirements.
import BigNumber from"bignumber.js";constisBAYCOwner= (jwt) => {const { contractAddress,numTokens } = jwt;// check both fields existif (!(contractAddress && numTokens)) returnfalse;return (// Bored Ape Yacht Club contract address contractAddress ==="0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"// remember numTokens is a string!&&BigNumber(numTokens).isGreaterThan(0) );}