
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
@syntropy/token-manager
Advanced tools
A JWT token management library for browser clients
Features:
To install:
npm install --save token-manager
To use with a module bundler (webpack, etc):
var tokenManagerModule = require('token-manager');
angular.module('myApp', [ tokenManagerModule.name ]);
To use directly in-browser, see dist/token-manager.bundle.js
.
TokenManager
is an event emitter. See tiny-emitter for detailed documentation on how to bind to or trigger events.
Fires whenever a new token is set. The token value is passed as the first argument.
Fires shortly before the token expires. The token value is passed as the first argument.
Configure the amount of time before expiration using the expirationWarning
option when constructing the instance.
Fires when the token expires. The token value is passed as the first argument.
Fires on error. The token value is passed as the first argument.
store
- an instance of TokenStore
options
- a configuration object
expirationWarning
- time in ms before token expiration to fire the expiring
event. If not set, or zero, the event will not fire.logger
- instance of a logger, such as console
or $log
.Returns the current token from the token store.
Sets a new token. If any expiration timers are running they will be reset for the new token.
Fires the auth
event when successful, or the error
event when unsuccessful.
Resets the token manager state, stops all timers, and removes the token from the token store.
Add a middleware function (or array of functions) that is to be run on the token prior to setting it.
Middleware should have the following signature:
middleware(token, next(err, newToken))
Inside the function, call next(err)
on error, and next()
or next(null, newToken)
when done. If newToken
is passed, it will replace the token that is provided in subsequent middleware.
Add a listener for an event.
Add a one-time listener for an event.
Remove a listener for an event. If no callback function is passed, removes all listeners for that event.
Fire a custom event with one or more arguments.
Decorates a token manager instance by exposing a keepAlive()
method and binding to the 'expiring' event, so that if there has been recent activity (via keepAlive()
), calls the renewFn
to renew the token.
Token middleware to detect when an authorization token should be exchanged for an access token (or some other similar token exchange scenario).
Uses fetch
to make the token exchange. (You will probably need to polyfill.)
whenFn(token)
- a function that will receive the token
as an argument, and returns true
if the token should be exchanged.settings
- a configuration object
url
(required) - URL to callmethod='POST'
- HTTP verb to useheaders={'Authorization': 'Bearer ' + token}
- HTTP headers to useThe exchanged token is then returned by the middleware to the auth()
method.
TokenManager
relies on token stores that follow a specific interface. Custom token stores should extend this class.
store
- the storage layer for storing the tokens. By default, TokenStore
expects an object or object-like interface.key
- the key in the store to use to store the token.Get the current token value from the store.
Set a new token in the store.
Remove the token from the store.
Composes TokenManager, TokenStore, keepAliveDecorator, and exchangeMiddleware into a single AngularJS service.
Service configuration can be managed in a config()
block via the tokenManagerProvider
(see below).
Returns the wrapped TokenManager instance.
Gets the current token from the token store, and then calls TokenManager.auth()
using that initial token and returns a promise that will resolve to the token value.
Alias for TokenManager.get()
.
Just like getToken()
, but returns a promise.
Returns the decoded version of the current token.
Just like getClaims()
, but returns a promise.
Alias for TokenManager.auth()
, but with the following differences:
token
is empty, calls TokenManager.reset()
insteadtoken
is invalid, calls TokenManager.reset()
insteadAlias for TokenManager.reset()
.
Updates the last activity timestamp. See the keepAliveDecorator
above for details.
Force an immediate token refresh by calling the keepAliveDecorator
's renewFn
and returning the resulting promise.
Configures the tokenManager
service in a config()
block.
Options:
authorizeUrl="/auth"
- URL to use when exchanging tokensrefreshUrl="/renew"
- URL to use to get a new token when the old one is expiringtokenTypeClaim="type"
- JWT claim to read to determine if the token should be exchangedtokenAuthValue="auth"
- JWT claim value when the token should be exchangedtokenTypeProvider=() => function(claims)
- A higher order function that returns a function to check whether the token should be exchanged or not. Use this if tokenTypeClaim
and tokenAuthValue
are not sufficient to determine when to exchange the tokenpersistenceStrategy="urlPersistenceStrategy"
- The token store to usepersistenceStrategyProvider=function
- An injectable that returns the desired token storeautoRefresh=60*1000
- How long (in milliseconds) before token expiration to fire the expiring
event and attempt renewalautoRefreshSession
- How recent (in milliseconds) the most recent activity from the user must be before attempting to refresh the tokenTokenStore
instance that uses $location
to persist the token in the URL hash.
urlTokenStore
may be configured in a config()
block using the provider.
Options:
persistenceKey="token"
- The token key to use in the token store.persistenceKey=function()
- A function that returns the desired persistenceKey
value.TokenStore
instance that uses $localStorage
to persist the token in local storage.
Requires ngstorage or compatible.
localStorageTokenStore
may be configured in a config()
block using the provider.
Options:
persistenceKey="token"
- The token key to use in the token store.persistenceKey=function()
- A function that returns the desired persistenceKey
value.TokenStore
instance that uses $sessionStorage
to persist the token in local storage.
Requires ngstorage or compatible.
sessionStorageTokenStore
may be configured in a config()
block using the provider.
Options:
persistenceKey="token"
- The token key to use in the token store.persistenceKey=function()
- A function that returns the desired persistenceKey
value.TokenStore
that composes one or more read TokenStore
instances and one or more write TokenStore
instances together such that if the first one fails, it will retry against the second, and so on.
failoverTokenStore
may be configured in a config()
block using the provider.
Options:
read=[]
- an array of one or more *TokenStore
services to attempt to read fromwrite=[]
- an array of one or more *TokenStore
services to attempt to write toFAQs
JWT token manager
We found that @syntropy/token-manager demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.