balena-auth
Version:
Balena session authentication utilities
139 lines (138 loc) • 3.25 kB
TypeScript
import type { TokenType } from './token';
export { TokenType } from './token';
interface BalenaAuthOptions {
dataDirectory?: string | false;
tokenKey?: string;
}
export default class BalenaAuth {
private readonly storage;
private readonly tokenKey;
private token?;
constructor({ dataDirectory, tokenKey }?: BalenaAuthOptions);
/**
* @member setKey
* @summary Set the key
* @function
* @public
*
* @param {String} key
* @returns {Promise<void>}
*
* @example
* auth.setKey('...').then(() => { ... });
*/
setKey: (key: string) => Promise<void>;
/**
* @member hasKey
* @summary Has a key
* @function
* @public
*
* @returns {Promise<Boolean>} has key
*
* @example
* auth.hasKey().then((hasKey) => { ... });
*/
hasKey: () => Promise<boolean>;
/**
* @member removeKey
* @summary Remove the key
* @function
* @public
*
* @description
* This promise is not rejected if there was no key at the time of removal.
*
* @returns {Promise}
*
* @example
* auth.removeKey();
*/
removeKey: () => Promise<void>;
/**
* @member getType
* @summary Gets the key type
* @function
* @public
*
* @returns {Promise<TokenType>}
*
* @example
* auth.getType().then((type) => { ... });
*/
getType: () => Promise<TokenType>;
/**
* @member getKey
* @summary Gets the key
* @function
* @public
*
* @returns {Promise<string>}
*
* @example
* auth.getKey().then((key) => { ... });
*/
getKey: () => Promise<string>;
/**
* @member getAge
* @summary Gets the token age
* @function
* @public
*
* @returns {Promise<number | undefined>}
*
* @example
* auth.getAge().then((age) => { ... });
*/
getAge: () => Promise<number | undefined>;
/**
* @member isExpired
* @summary Checks if token is expired
* @function
* @public
*
* @returns {Promise<boolean>}
*
* @example
* auth.isExpired().then((expired) => { ... });
*/
isExpired: () => Promise<boolean>;
/**
* @member isValid
* @summary Checks if token format is valid
* @function
* @public
*
* @returns {Promise<boolean>}
*
* @example
* auth.isValid().then((valid) => { ... });
*/
isValid: () => Promise<boolean>;
/**
* @member get2FAStatus
* @summary Gets whether passing a 2FA challenge is pending, passed or not required.
* @function
* @public
*
* @returns {Promise<'not_required'|'pending'|'passed'>}
*
* @example
* auth.get2FAStatus().then((get2FAStatus) => { ... });
*/
get2FAStatus: () => Promise<'not_required' | 'pending' | 'passed'>;
/**
* @member needs2FA
* @summary Checks whether passing 2FA is pending/needed
* @function
* @public
*
* @returns {Promise<boolean>}
*
* @example
* auth.needs2FA().then((needs2FA) => { ... });
*/
needs2FA: () => Promise<boolean>;
private createToken;
private getToken;
}