@targetprocess/tokiny
v3.10.0-alpha.2
Published
Client for easy retrieving access_tokens from Targetprocess Auth service.
Downloads
3,724
Maintainers
Readme
Node.js compatibility
Node.js 12 or higher is required. For older Node.js versions use tokiny@^2.0.0.
Targetprocess Tokiny
JavaScript client for retrieving auth access_tokens by OAuth client credentials flow.
Features
- Allow easily retrieve access_token from Targetprocess Auth service
- Cache token if specified
- Does not send multiple requests to auth in case of multiple token retrieve attempts occurred in one time. For token requests with same access parameters only on request will be actually made to auth.
Quick start
To install tokiny use:
npm install @targetprocess/tokiny
To get new access token from Targetprocess auth:
import {createTokenFactory} from "@targetprocess/tokiny";
const tokenFactory = createTokenFactory({
authUrl: env.AUTH_SERVICE_URI,
clientId: env.CLIENT_ID,
clientSecret: env.CLIENT_SECRET
});
const token = await tokenFactory.getToken("scope1 scope2");
createTokenFsactory as a parameter expects object of type IAuthOptions:
interface IAuthOptions {
authUrl: string; // Auth service url
clientId: string; // Your service client id
clientSecret: string; // Your service client secret
expirationLimitSeconds?: number; // if useCashe = true, defines a treshold between cached token expiraion, when new token will be retieved. 120 by default
useCache?: boolean; // Sets if token caching is turned on. true by default
httpRequestTimeout: number; // Sets http timeout for requests
retryOptions: IAuthRetryOptions; // Allow to specify auth retry options
logger?: IAuthLogger; // Allow to enable logging by specifying logger that would be used for logging internal diagnostics messages. Logging is disabled by default
loggerMessagePrefix?: string; // Allow to specify custom logger messages prefix. "[tokiny] " by default
}
interface IAuthRetryOptions {
retry: boolean // Should retry. Default false.
retries?: number // The maximum amount of times to retry the operation. Default is 3.
factor?: number // The exponential factor to use. Default is 2.
minTimeout?: number // The number of milliseconds before starting the first retry. Default is 1000.
maxTimeout?: number // The maximum number of milliseconds between two retries. Default is Infinity.
}
interface IAuthLogger {
error: LogMethod
warn: LogMethod
info: LogMethod
debug: LogMethod
}
type LogMethod = (msg: string | Error, err?: Error) => void
tokenFactory.getToken returns object of type ITokenSet:
export interface ITokenSet {
access_token: string; //access_token itself
expires_at: number; // token expiration time
type: string; // token type
}
Tokiny also exports wrapped over http-fetch object, which expected by generated by nswag client for example. So you had not to set token to Authroization header manually. You can just write smth like this:
import {createTokenFactory, httpClientFactory} from "@targetprocess/tokiny";
const tokenFactory = createTokenFactory({
authUrl: env.AUTH_SERVICE_URI,
clientId: env.CLIENT_ID,
clientSecret: env.CLIENT_SECRET
});
const token = await tokenFactory.getToken("scope1 scope2");
const scopes = "scope1 scope2";
const someClient = new SomeClient(
`http://some_url`,
httpClientFactory(scopes, tokenFactory, fetch)
);