Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including balena-auth with all npm packages installed. Try it out:

var balenaAuth = require("balena-auth")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

balena-auth v4.0.2

Balena session authentication utilities

balena-auth

npm version dependencies Build Status

Join our online chat at Gitter chat

Balena session authentication utilities

Role

The intention of this module is to provide low level access to how a balena authentication tokens are parsed and persisted.

THIS MODULE IS LOW LEVEL AND IS NOT MEANT TO BE USED BY END USERS DIRECTLY.

Unless you know what you're doing, use the balena SDK instead.

Installation

Install balena-auth by running:

$ npm install --save balena-auth

Documentation

The module returns a class that you use to get an instance of the auth module.

It accepts the following params:

ParamTypeDescription
optionsObjectoptions
options.dataDirectorystringthe directory to use for storage in Node.js. Ignored in the browser.
options.tokenKeystringthe key used to store the last token in the storage. token by default.

Example

import BalenaAuth from 'balena-auth';
const auth = new BalenaAuth({
    dataDirectory: '/opt/cache/balena',
    tokenKey: 'token'
});

auth~setKey(key) ⇒ Promise.<void>

Kind: inner method of auth
Summary: Set the key
Access: public

ParamType
keyString

Example

auth.setKey('...').then(() => { ... });

auth~hasKey() ⇒ Promise.<Boolean>

Kind: inner method of auth
Summary: Has a key
Returns: Promise.<Boolean> - has key
Access: public
Example

auth.hasKey().then((hasKey) => { ... });

auth~removeKey() ⇒ Promise

This promise is not rejected if there was no key at the time of removal.

Kind: inner method of auth
Summary: Remove the key
Access: public
Example

auth.removeKey();

auth~getType() ⇒ Promise.<TokenType>

Kind: inner method of auth
Summary: Gets the key type
Access: public
Example

auth.getType().then((type) => { ... });

auth~getKey() ⇒ Promise.<string>

Kind: inner method of auth
Summary: Gets the key
Access: public
Example

auth.getKey().then((key) => { ... });

auth~getAge() ⇒ Promise.<(number|undefined)>

Kind: inner method of auth
Summary: Gets the token age
Access: public
Example

auth.getAge().then((age) => { ... });

auth~isExpired() ⇒ Promise.<boolean>

Kind: inner method of auth
Summary: Checks if token is expired
Access: public
Example

auth.isExpired().then((expired) => { ... });

auth~isValid() ⇒ Promise.<boolean>

Kind: inner method of auth
Summary: Checks if token format is valid
Access: public
Example

auth.isValid().then((valid) => { ... });

auth~needs2FA() ⇒ Promise.<boolean>

Kind: inner method of auth
Summary: Checks whether 2FA is needed
Access: public
Example

auth.needs2FA().then((needs2FA) => { ... });

Support

If you're having any problem, please raise an issue on GitHub and the balena team will be happy to help.

Tests

Run the test suite by doing:

$ npm test

Contribute

Before submitting a PR, please make sure that you include tests, and that tslint runs without any warning:

$ npm run lint

License

The project is licensed under the Apache 2.0 license.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free