balena-auth
Advanced tools
Comparing version 3.1.1-balena-lint-6dbc95de3543cddab72ba6516cb998a212e63151 to 4.0.0-4-x-0e2ea945079be9e7b9cd212ad7e6556c9233a226
@@ -22,5 +22,5 @@ "use strict"; | ||
*/ | ||
var token_1 = require("./token"); | ||
var APIKey = /** @class */ (function () { | ||
function APIKey(key) { | ||
const token_1 = require("./token"); | ||
class APIKey { | ||
constructor(key) { | ||
/** | ||
@@ -48,3 +48,3 @@ * @member type | ||
*/ | ||
this.isValid = function () { return true; }; | ||
this.isValid = () => true; | ||
/** | ||
@@ -61,3 +61,3 @@ * @member getAge | ||
*/ | ||
this.getAge = function () { return 0; }; | ||
this.getAge = () => 0; | ||
/** | ||
@@ -74,3 +74,3 @@ * @member isExpired | ||
*/ | ||
this.isExpired = function () { return false; }; | ||
this.isExpired = () => false; | ||
/** | ||
@@ -87,8 +87,7 @@ * @member needs2FA | ||
*/ | ||
this.needs2FA = function () { return false; }; | ||
this.needs2FA = () => false; | ||
this.key = key; | ||
} | ||
return APIKey; | ||
}()); | ||
} | ||
exports.APIKey = APIKey; | ||
//# sourceMappingURL=api-key.js.map |
@@ -1,2 +0,1 @@ | ||
import * as Promise from 'bluebird'; | ||
import { TokenType } from './token'; | ||
@@ -3,0 +2,0 @@ interface BalenaAuthOptions { |
@@ -18,14 +18,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
/** | ||
* @module auth | ||
*/ | ||
var errors = require("balena-errors"); | ||
var getStorage = require("balena-settings-storage"); | ||
var Promise = require("bluebird"); | ||
var api_key_1 = require("./api-key"); | ||
var jwt_1 = require("./jwt"); | ||
var BalenaAuth = /** @class */ (function () { | ||
function BalenaAuth(_a) { | ||
var _this = this; | ||
var _b = _a === void 0 ? {} : _a, dataDirectory = _b.dataDirectory, _c = _b.tokenKey, tokenKey = _c === void 0 ? 'token' : _c; | ||
const errors = require("balena-errors"); | ||
const getStorage = require("balena-settings-storage"); | ||
const api_key_1 = require("./api-key"); | ||
const jwt_1 = require("./jwt"); | ||
class BalenaAuth { | ||
constructor({ dataDirectory, tokenKey = 'token' } = {}) { | ||
// Storage related methods | ||
@@ -44,12 +42,7 @@ /** | ||
*/ | ||
this.setKey = function (key) { | ||
try { | ||
_this.token = _this.createToken(key); | ||
// Do not override the current key if the new one is invalid | ||
return _this.storage.set(_this.tokenKey, key); | ||
} | ||
catch (err) { | ||
return Promise.reject(err); | ||
} | ||
}; | ||
this.setKey = (key) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this.token = this.createToken(key); | ||
// Do not override the current key if the new one is invalid | ||
return this.storage.set(this.tokenKey, key); | ||
}); | ||
/** | ||
@@ -66,3 +59,3 @@ * @member hasKey | ||
*/ | ||
this.hasKey = function () { return _this.storage.has(_this.tokenKey); }; | ||
this.hasKey = () => this.storage.has(this.tokenKey); | ||
/** | ||
@@ -82,5 +75,5 @@ * @member removeKey | ||
*/ | ||
this.removeKey = function () { | ||
_this.token = undefined; | ||
return _this.storage.remove(_this.tokenKey); | ||
this.removeKey = () => { | ||
this.token = undefined; | ||
return this.storage.remove(this.tokenKey); | ||
}; | ||
@@ -99,5 +92,6 @@ // Proxy promisified Token methods | ||
*/ | ||
this.getType = function () { | ||
return _this.getToken().then(function (token) { return token.type; }); | ||
}; | ||
this.getType = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const token = yield this.getToken(); | ||
return token.type; | ||
}); | ||
/** | ||
@@ -114,5 +108,6 @@ * @member getKey | ||
*/ | ||
this.getKey = function () { | ||
return _this.getToken().then(function (token) { return token.key; }); | ||
}; | ||
this.getKey = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const token = yield this.getToken(); | ||
return token.key; | ||
}); | ||
/** | ||
@@ -129,5 +124,6 @@ * @member getAge | ||
*/ | ||
this.getAge = function () { | ||
return _this.getToken().then(function (token) { return token.getAge(); }); | ||
}; | ||
this.getAge = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const token = yield this.getToken(); | ||
return token.getAge(); | ||
}); | ||
/** | ||
@@ -144,5 +140,6 @@ * @member isExpired | ||
*/ | ||
this.isExpired = function () { | ||
return _this.getToken().then(function (token) { return token.isExpired(); }); | ||
}; | ||
this.isExpired = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const token = yield this.getToken(); | ||
return token.isExpired(); | ||
}); | ||
/** | ||
@@ -159,5 +156,6 @@ * @member isValid | ||
*/ | ||
this.isValid = function () { | ||
return _this.getToken().then(function (token) { return token.isValid(); }); | ||
}; | ||
this.isValid = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const token = yield this.getToken(); | ||
return token.isValid(); | ||
}); | ||
/** | ||
@@ -174,8 +172,9 @@ * @member needs2FA | ||
*/ | ||
this.needs2FA = function () { | ||
return _this.getToken().then(function (token) { return token.needs2FA(); }); | ||
}; | ||
this.needs2FA = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const token = yield this.getToken(); | ||
return token.needs2FA(); | ||
}); | ||
// Utility methods | ||
this.createToken = function (key) { | ||
var token = jwt_1.JWT.isValid(key) ? new jwt_1.JWT(key) : new api_key_1.APIKey(key); | ||
this.createToken = (key) => { | ||
const token = jwt_1.JWT.isValid(key) ? new jwt_1.JWT(key) : new api_key_1.APIKey(key); | ||
if (!token.isValid()) { | ||
@@ -189,20 +188,18 @@ throw new errors.BalenaMalformedToken(key); | ||
}; | ||
this.getToken = function () { | ||
if (_this.token) { | ||
return Promise.resolve(_this.token); | ||
this.getToken = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (this.token) { | ||
return this.token; | ||
} | ||
return _this.storage.get(_this.tokenKey).then(function (key) { | ||
if (typeof key !== 'string') { | ||
throw new errors.BalenaMalformedToken(key); | ||
} | ||
_this.token = _this.createToken(key); | ||
return _this.token; | ||
}); | ||
}; | ||
this.storage = getStorage({ dataDirectory: dataDirectory }); | ||
const key = yield this.storage.get(this.tokenKey); | ||
if (typeof key !== 'string') { | ||
throw new errors.BalenaMalformedToken(key); | ||
} | ||
this.token = this.createToken(key); | ||
return this.token; | ||
}); | ||
this.storage = getStorage({ dataDirectory }); | ||
this.tokenKey = tokenKey; | ||
} | ||
return BalenaAuth; | ||
}()); | ||
} | ||
exports.default = BalenaAuth; | ||
//# sourceMappingURL=auth.js.map |
@@ -22,7 +22,6 @@ "use strict"; | ||
*/ | ||
var jwtDecode = require("jwt-decode"); | ||
var token_1 = require("./token"); | ||
var JWT = /** @class */ (function () { | ||
function JWT(key) { | ||
var _this = this; | ||
const jwtDecode = require("jwt-decode"); | ||
const token_1 = require("./token"); | ||
class JWT { | ||
constructor(key) { | ||
/** | ||
@@ -50,3 +49,3 @@ * @member type | ||
*/ | ||
this.isValid = function () { return JWT.isValid(_this.key); }; | ||
this.isValid = () => JWT.isValid(this.key); | ||
/** | ||
@@ -63,4 +62,4 @@ * @member getAge | ||
*/ | ||
this.getAge = function () { | ||
var iat = JWT.parse(_this.key).iat; | ||
this.getAge = () => { | ||
const { iat } = JWT.parse(this.key); | ||
return iat ? Date.now() - iat * 1000 : undefined; | ||
@@ -79,4 +78,4 @@ }; | ||
*/ | ||
this.isExpired = function () { | ||
var exp = JWT.parse(_this.key).exp; | ||
this.isExpired = () => { | ||
const { exp } = JWT.parse(this.key); | ||
return exp ? Date.now() > exp * 1000 : false; | ||
@@ -95,9 +94,9 @@ }; | ||
*/ | ||
this.needs2FA = function () { return !!JWT.parse(_this.key).twoFactorRequired; }; | ||
this.needs2FA = () => !!JWT.parse(this.key).twoFactorRequired; | ||
this.key = key; | ||
} | ||
JWT.parse = function (key) { | ||
static parse(key) { | ||
return jwtDecode(key.trim()); | ||
}; | ||
JWT.isValid = function (key) { | ||
} | ||
static isValid(key) { | ||
try { | ||
@@ -110,6 +109,5 @@ this.parse(key); | ||
} | ||
}; | ||
return JWT; | ||
}()); | ||
} | ||
} | ||
exports.JWT = JWT; | ||
//# sourceMappingURL=jwt.js.map |
@@ -7,5 +7,8 @@ # Change Log | ||
# v3.1.1 | ||
# v4.0.0 | ||
## (2020-07-02) | ||
* Switch to native promises [Pagan Gazzard] | ||
* Update target to es2015 [Pagan Gazzard] | ||
* Update to balena-settings-storage 6.x [Pagan Gazzard] | ||
* Switch to @balena/lint for linting [Pagan Gazzard] | ||
@@ -12,0 +15,0 @@ |
@@ -24,3 +24,2 @@ /* | ||
import { BalenaSettingsStorage } from 'balena-settings-storage/lib/types'; | ||
import * as Promise from 'bluebird'; | ||
@@ -60,10 +59,6 @@ import { APIKey } from './api-key'; | ||
*/ | ||
public setKey = (key: string): Promise<void> => { | ||
try { | ||
this.token = this.createToken(key); | ||
// Do not override the current key if the new one is invalid | ||
return this.storage.set(this.tokenKey, key); | ||
} catch (err) { | ||
return Promise.reject(err); | ||
} | ||
public setKey = async (key: string): Promise<void> => { | ||
this.token = this.createToken(key); | ||
// Do not override the current key if the new one is invalid | ||
return this.storage.set(this.tokenKey, key); | ||
}; | ||
@@ -82,3 +77,3 @@ | ||
*/ | ||
public hasKey = () => this.storage.has(this.tokenKey); | ||
public hasKey = (): Promise<boolean> => this.storage.has(this.tokenKey); | ||
@@ -99,3 +94,3 @@ /** | ||
*/ | ||
public removeKey = () => { | ||
public removeKey = (): Promise<void> => { | ||
this.token = undefined; | ||
@@ -118,4 +113,6 @@ return this.storage.remove(this.tokenKey); | ||
*/ | ||
public getType = (): Promise<TokenType> => | ||
this.getToken().then((token) => token.type); | ||
public getType = async (): Promise<TokenType> => { | ||
const token = await this.getToken(); | ||
return token.type; | ||
}; | ||
@@ -133,4 +130,6 @@ /** | ||
*/ | ||
public getKey = (): Promise<string> => | ||
this.getToken().then((token) => token.key); | ||
public getKey = async (): Promise<string> => { | ||
const token = await this.getToken(); | ||
return token.key; | ||
}; | ||
@@ -148,4 +147,6 @@ /** | ||
*/ | ||
public getAge = (): Promise<number | undefined> => | ||
this.getToken().then((token) => token.getAge()); | ||
public getAge = async (): Promise<number | undefined> => { | ||
const token = await this.getToken(); | ||
return token.getAge(); | ||
}; | ||
@@ -163,4 +164,6 @@ /** | ||
*/ | ||
public isExpired = (): Promise<boolean> => | ||
this.getToken().then((token) => token.isExpired()); | ||
public isExpired = async (): Promise<boolean> => { | ||
const token = await this.getToken(); | ||
return token.isExpired(); | ||
}; | ||
@@ -178,4 +181,6 @@ /** | ||
*/ | ||
public isValid = (): Promise<boolean> => | ||
this.getToken().then((token) => token.isValid()); | ||
public isValid = async (): Promise<boolean> => { | ||
const token = await this.getToken(); | ||
return token.isValid(); | ||
}; | ||
@@ -193,4 +198,6 @@ /** | ||
*/ | ||
public needs2FA = (): Promise<boolean> => | ||
this.getToken().then((token) => token.needs2FA()); | ||
public needs2FA = async (): Promise<boolean> => { | ||
const token = await this.getToken(); | ||
return token.needs2FA(); | ||
}; | ||
@@ -210,15 +217,14 @@ // Utility methods | ||
private getToken = (): Promise<Token> => { | ||
private getToken = async (): Promise<Token> => { | ||
if (this.token) { | ||
return Promise.resolve(this.token); | ||
return this.token; | ||
} | ||
return this.storage.get(this.tokenKey).then((key) => { | ||
if (typeof key !== 'string') { | ||
throw new errors.BalenaMalformedToken(key as any); | ||
} | ||
this.token = this.createToken(key as string); | ||
return this.token; | ||
}); | ||
const key = await this.storage.get(this.tokenKey); | ||
if (typeof key !== 'string') { | ||
throw new errors.BalenaMalformedToken(key as any); | ||
} | ||
this.token = this.createToken(key as string); | ||
return this.token; | ||
}; | ||
} |
{ | ||
"name": "balena-auth", | ||
"version": "3.1.1-balena-lint-6dbc95de3543cddab72ba6516cb998a212e63151", | ||
"version": "4.0.0-4-x-0e2ea945079be9e7b9cd212ad7e6556c9233a226", | ||
"description": "Balena session authentication utilities", | ||
@@ -64,7 +64,5 @@ "main": "build/auth.js", | ||
"dependencies": { | ||
"@types/bluebird": "^3.5.32", | ||
"@types/jwt-decode": "^2.2.1", | ||
"balena-errors": "^4.2.1", | ||
"balena-settings-storage": "^5.0.0", | ||
"bluebird": "^3.7.2", | ||
"balena-settings-storage": "^6.0.0", | ||
"jwt-decode": "^2.2.0" | ||
@@ -71,0 +69,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4
52521
1123
+ Added@types/node@10.17.60(transitive)
+ Addedbalena-settings-storage@6.0.1(transitive)
- Removed@types/bluebird@^3.5.32
- Removedbluebird@^3.7.2
- Removed@resin.io/types-node-localstorage@1.3.0(transitive)
- Removed@types/bluebird@3.5.42(transitive)
- Removed@types/node@8.10.66(transitive)
- Removedbalena-settings-storage@5.0.2(transitive)
- Removedbluebird@3.7.2(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removednode-localstorage@1.3.1(transitive)
- Removedslide@1.1.6(transitive)
- Removedwrite-file-atomic@1.3.4(transitive)