@vonage/auth
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -1,2 +0,4 @@ | ||
import { AuthInterface, AuthOpts, AuthQueryParams, SignedHashParams, AuthSignedParams } from './types'; | ||
import { GeneratorOptions } from '@vonage/jwt'; | ||
import { AuthParams, AuthQueryParams, SignedHashParams, AuthSignedParams } from './types/index'; | ||
import { AuthInterface } from './interfaces/index'; | ||
export declare class Auth implements AuthInterface { | ||
@@ -8,7 +10,8 @@ apiKey: string; | ||
signature: SignedHashParams; | ||
constructor(opts?: AuthOpts); | ||
getQueryParams: <T>(params?: T) => Promise<AuthQueryParams & T>; | ||
jwtOptions: GeneratorOptions; | ||
constructor(opts?: AuthParams); | ||
getQueryParams: <T>(params?: AuthQueryParams & T) => Promise<AuthQueryParams & T>; | ||
createBasicHeader: () => Promise<string>; | ||
createBearerHeader: () => Promise<string>; | ||
createSignatureHash: <T>(params: T) => Promise<AuthSignedParams & T>; | ||
createSignatureHash: <T>(params: AuthSignedParams & T) => Promise<AuthSignedParams & T>; | ||
} |
109
dist/auth.js
@@ -10,3 +10,3 @@ "use strict"; | ||
const fs_1 = require("fs"); | ||
const types_1 = require("./types"); | ||
const enums_1 = require("./enums"); | ||
const debug_1 = __importDefault(require("debug")); | ||
@@ -20,2 +20,3 @@ const log = (0, debug_1.default)('vonage:auth'); | ||
signature; | ||
jwtOptions; | ||
constructor(opts) { | ||
@@ -26,64 +27,72 @@ this.apiKey = opts?.apiKey || ''; | ||
this.applicationId = opts?.applicationId || null; | ||
if (opts?.privateKey) { | ||
if ((0, fs_1.existsSync)(opts.privateKey)) { | ||
opts.privateKey = (0, fs_1.readFileSync)(opts.privateKey).toString(); | ||
} | ||
if (opts.privateKey instanceof Buffer) { | ||
this.privateKey = opts.privateKey.toString(); | ||
} | ||
else { | ||
this.privateKey = opts.privateKey; | ||
} | ||
this.jwtOptions = opts?.jwtOptions || {}; | ||
if (!opts?.privateKey) { | ||
log('No private key set'); | ||
return; | ||
} | ||
if ((0, fs_1.existsSync)(opts.privateKey)) { | ||
log('Reading private key file'); | ||
opts.privateKey = (0, fs_1.readFileSync)(opts.privateKey).toString(); | ||
} | ||
this.privateKey = opts.privateKey instanceof Buffer | ||
? opts.privateKey.toString() | ||
: opts.privateKey; | ||
} | ||
getQueryParams = async (params) => { | ||
return { api_key: this.apiKey, api_secret: this.apiSecret, ...params }; | ||
}; | ||
getQueryParams = async (params) => ({ | ||
...params, | ||
api_key: this.apiKey, | ||
api_secret: this.apiSecret, | ||
}); | ||
createBasicHeader = async () => { | ||
log('Creating basic auth header'); | ||
const buf = Buffer.from(`${this.apiKey}:${this.apiSecret}`); | ||
return 'Basic ' + buf.toString('base64'); | ||
return `Basic ${buf.toString('base64')}`; | ||
}; | ||
createBearerHeader = async () => { | ||
log('Creating bearer header'); | ||
return 'Bearer ' + (0, jwt_1.tokenGenerate)(this.applicationId, this.privateKey); | ||
return `Bearer ${(0, jwt_1.tokenGenerate)(this.applicationId, this.privateKey, this.jwtOptions)}`; | ||
}; | ||
createSignatureHash = async (params) => { | ||
log('Creating signature hash'); | ||
const returnParams = Object.assign({ api_key: this.apiKey }, params); | ||
returnParams.timestamp = Math.floor(Date.now() / 1000).toString(); | ||
const keys = Object.keys(returnParams); | ||
const stringifiedParamsforSigning = keys | ||
.sort() | ||
.map((keyName) => { | ||
return `&${keyName}=${returnParams[keyName] | ||
.toString() | ||
.replace(/(&|=)/gi, '_')}`; | ||
}, []) | ||
.join(''); | ||
if (this.signature.algorithm === types_1.AlgorithmTypes.md5hash) { | ||
returnParams.sig = (0, crypto_1.createHash)('md5') | ||
.update(stringifiedParamsforSigning + this.signature.secret) | ||
.digest('hex'); | ||
const returnParams = { | ||
...params, | ||
api_key: this.apiKey, | ||
}; | ||
if (!returnParams.timestamp) { | ||
returnParams.timestamp = Math.floor(Date.now() / 1000).toString(); | ||
} | ||
if (this.signature.algorithm === types_1.AlgorithmTypes.md5hmac) { | ||
returnParams.sig = (0, crypto_1.createHmac)('md5', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
const sortedParams = new URLSearchParams(returnParams); | ||
sortedParams.sort(); | ||
const stringifiedParamsforSigning = sortedParams | ||
.toString() | ||
.replace(/(&|=)/gi, '_'); | ||
switch (this.signature.algorithm) { | ||
case enums_1.AlgorithmTypes.md5hash: | ||
returnParams.sig = (0, crypto_1.createHash)('md5') | ||
.update(`${stringifiedParamsforSigning}${this.signature.secret}`) | ||
.digest('hex'); | ||
break; | ||
case enums_1.AlgorithmTypes.md5hmac: | ||
returnParams.sig = (0, crypto_1.createHmac)('md5', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
break; | ||
case enums_1.AlgorithmTypes.sha1hmac: | ||
returnParams.sig = (0, crypto_1.createHmac)('sha1', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
break; | ||
case enums_1.AlgorithmTypes.sha256hmac: | ||
returnParams.sig = (0, crypto_1.createHmac)('sha256', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
break; | ||
case enums_1.AlgorithmTypes.sha512hmac: | ||
returnParams.sig = (0, crypto_1.createHmac)('sha512', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
break; | ||
default: | ||
throw new Error(`Cannot sign request! Invalid algorithm: ${this.signature.algorithm}`); | ||
} | ||
if (this.signature.algorithm === types_1.AlgorithmTypes.sha1hmac) { | ||
returnParams.sig = (0, crypto_1.createHmac)('sha1', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
} | ||
if (this.signature.algorithm === types_1.AlgorithmTypes.sha256hmac) { | ||
returnParams.sig = (0, crypto_1.createHmac)('sha256', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
} | ||
if (this.signature.algorithm === types_1.AlgorithmTypes.sha512hmac) { | ||
returnParams.sig = (0, crypto_1.createHmac)('sha512', this.signature.secret) | ||
.update(stringifiedParamsforSigning) | ||
.digest('hex'); | ||
} | ||
return returnParams; | ||
@@ -90,0 +99,0 @@ }; |
@@ -1,3 +0,4 @@ | ||
import { Auth } from './auth'; | ||
export { AuthConstructor, AuthInterface, AuthQueryParams, AuthOpts, AlgorithmTypes, SignedHashParams, AuthSignedParams, } from './types'; | ||
export { Auth }; | ||
export * from './auth'; | ||
export * from './enums/index'; | ||
export * from './interfaces/index'; | ||
export * from './types/index'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Auth = exports.AlgorithmTypes = void 0; | ||
const auth_1 = require("./auth"); | ||
Object.defineProperty(exports, "Auth", { enumerable: true, get: function () { return auth_1.Auth; } }); | ||
var types_1 = require("./types"); | ||
Object.defineProperty(exports, "AlgorithmTypes", { enumerable: true, get: function () { return types_1.AlgorithmTypes; } }); | ||
__exportStar(require("./auth"), exports); | ||
__exportStar(require("./enums/index"), exports); | ||
__exportStar(require("./interfaces/index"), exports); | ||
__exportStar(require("./types/index"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,40 +0,3 @@ | ||
/// <reference types="node" /> | ||
export type AuthConstructor = new (opts?: AuthOpts) => AuthInterface; | ||
export declare enum AlgorithmTypes { | ||
md5hash = "MD5HASH", | ||
md5hmac = "MD5HMAC", | ||
sha1hmac = "SHA1HMAC", | ||
sha256hmac = "SHA256HMAC", | ||
sha512hmac = "SHA512HMAC" | ||
} | ||
export interface AuthOpts { | ||
apiKey?: string; | ||
apiSecret?: string; | ||
privateKey?: string | Buffer; | ||
applicationId?: string; | ||
signature?: SignedHashParams; | ||
} | ||
export interface SignedHashParams { | ||
secret: string; | ||
algorithm: AlgorithmTypes; | ||
} | ||
export interface AuthSignedParams { | ||
api_key: string; | ||
sig?: string; | ||
timestamp?: string; | ||
} | ||
export interface AuthQueryParams { | ||
api_key: string; | ||
api_secret: string; | ||
} | ||
export interface AuthInterface { | ||
apiKey: string; | ||
apiSecret: string; | ||
signature?: SignedHashParams; | ||
applicationId?: string; | ||
privateKey?: string; | ||
getQueryParams<T>(params?: T): Promise<AuthQueryParams & T>; | ||
createSignatureHash<T>(params: T): Promise<AuthSignedParams & T>; | ||
createBasicHeader(): Promise<string>; | ||
createBearerHeader(): Promise<string>; | ||
} | ||
export * from './enums'; | ||
export * from './interfaces'; | ||
export * from './types'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AlgorithmTypes = void 0; | ||
var AlgorithmTypes; | ||
(function (AlgorithmTypes) { | ||
AlgorithmTypes["md5hash"] = "MD5HASH"; | ||
AlgorithmTypes["md5hmac"] = "MD5HMAC"; | ||
AlgorithmTypes["sha1hmac"] = "SHA1HMAC"; | ||
AlgorithmTypes["sha256hmac"] = "SHA256HMAC"; | ||
AlgorithmTypes["sha512hmac"] = "SHA512HMAC"; | ||
})(AlgorithmTypes = exports.AlgorithmTypes || (exports.AlgorithmTypes = {})); | ||
__exportStar(require("./enums"), exports); | ||
__exportStar(require("./interfaces"), exports); | ||
__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@vonage/auth", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Vonage Auth Package adds the correct authentication headers to requests to Vonage API's", | ||
@@ -26,3 +26,3 @@ "homepage": "https://github.com/vonage/vonage-node-sdk/tree/master/packages/auth#readme", | ||
"dependencies": { | ||
"@vonage/jwt": "^1.1.2", | ||
"@vonage/jwt": "^1.2.0", | ||
"debug": "^4.3.4" | ||
@@ -29,0 +29,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
21938
41
288
1
Updated@vonage/jwt@^1.2.0