Overview
The Token Service manages access tokens, refresh tokens, API keys, and two-factor tokens. It handles secure storage of authentication credentials across different vault timeout configurations and provides utilities for token validation and decoding.TokenService
Abstract service for managing authentication tokens.Observables
hasAccessToken$()
userId- The user ID to check for an access token
Observable<boolean> - Observable emitting token presence status
Token Management Methods
setTokens()
accessToken- The access token to setvaultTimeoutAction- The action to take when the vault times outvaultTimeout- The timeout for the vaultrefreshToken- Optional refresh token (undefined for CLI Login via API Key flow)clientIdClientSecret- Optional tuple of API Key Client ID and Client Secret
Promise<SetTokensResult> - Result containing the tokens that were set
Notes:
- For platforms that support secure storage, tokens are stored in secure storage instead of on disk
- This method enforces setting access token and refresh token together for efficiency
clearTokens()
userId- Optional user ID to clear tokens for; if not provided, the active user ID is used
Promise<void>
setAccessToken()
accessToken- The access token to setvaultTimeoutAction- The action to take when the vault times outvaultTimeout- The timeout for the vault
Promise<string> - The access token that has been set
Note: For platforms that support secure storage, the access token is stored in secure storage instead of on disk.
clearAccessToken()
userId- Optional user ID to clear the access token for; if not provided, the active user is used
Promise<void>
getAccessToken()
userId- The user ID to get the access token for
Promise<string | null> - The access token or null
getRefreshToken()
userId- The user ID to get the refresh token for
Promise<string | null> - The refresh token or null
API Key Methods
setClientId()
clientId- The API Key Client ID to setvaultTimeoutAction- The action to take when the vault times outvaultTimeout- The timeout for the vaultuserId- Optional user ID; uses active user if not provided
Promise<string> - The API Key Client ID that has been set
getClientId()
userId- The user ID
Promise<string | undefined> - The API Key Client ID or undefined
setClientSecret()
clientSecret- The API Key Client Secret to setvaultTimeoutAction- The action to take when the vault times outvaultTimeout- The timeout for the vaultuserId- Optional user ID; uses active user if not provided
Promise<string> - The client secret that has been set
getClientSecret()
userId- The user ID
Promise<string | undefined> - The API Key Client Secret or undefined
Two-Factor Token Methods
setTwoFactorToken()
email- The email to set the two-factor token fortwoFactorToken- The two-factor token to set
Promise<void>
getTwoFactorToken()
email- The email to get the two-factor token for
Promise<string | null> - The two-factor token or null if not found
clearTwoFactorToken()
email- The email to clear the two-factor token for
Promise<void>
Token Utility Methods
decodeAccessToken()
tokenOrUserId- The access token to decode or the user ID to retrieve the access token for; if null, the active user’s token is used
Promise<DecodedAccessToken> - The decoded access token containing user claims
getTokenExpirationDate()
userId- The user ID
Promise<Date | null> - The expiration date or null if token can’t be decoded or has no expiration
tokenSecondsRemaining()
userId- The user IDoffsetSeconds- Optional seconds to subtract from the remaining time (default: 0). Creates a buffer before actual expiration, useful for preemptive actions
Promise<number> - The adjusted seconds remaining
tokenNeedsRefresh()
userId- The user IDminutes- Optional number of minutes before expiration to consider refreshing (default: 5)
Promise<boolean> - True if the token needs to be refreshed
User Information Methods
getUserId()
Promise<UserId> - The user ID
Deprecated: Use AccountService.activeAccount$ instead
getEmail()
Promise<string> - The email address
Deprecated: Use AccountService.activeAccount$ instead
getEmailVerified()
Promise<boolean> - True if email is verified
getName()
Promise<string> - The user’s name
Deprecated: Use AccountService.activeAccount$ instead
getIssuer()
Promise<string> - The token issuer
getIsExternal()
userId- The user ID to check
Promise<boolean> - True if external authentication was used
Security Stamp Methods
getSecurityStamp()
userId- Optional user ID; uses active user if not provided
Promise<string | null> - The security stamp or null
setSecurityStamp()
securityStamp- The security stamp to setuserId- Optional user ID; uses active user if not provided
Promise<void>