@ntegral/lulu
Advanced tools
Comparing version 1.3.5 to 1.3.6
import * as rp from 'request-promise'; | ||
import { LuluConfigOptions } from './common/interfaces/index'; | ||
import { LuluConfigOptions, JwtPayload } from './common/interfaces/index'; | ||
import { IAuthenticationResponse } from './common/interfaces/index'; | ||
export declare class Client { | ||
private config; | ||
private clock; | ||
private tokenKey; | ||
private client_id; | ||
@@ -20,4 +22,5 @@ private client_secret; | ||
init(): Promise<IAuthenticationResponse>; | ||
authorizeHeader(data: IAuthenticationResponse): Promise<unknown>; | ||
getToken(): Promise<IAuthenticationResponse>; | ||
authorizeHeader(data: IAuthenticationResponse, refresh?: boolean): Promise<unknown>; | ||
getToken(refresh?: boolean): Promise<IAuthenticationResponse>; | ||
decode(data: IAuthenticationResponse): Promise<JwtPayload>; | ||
refreshToken(data: IAuthenticationResponse): Promise<IAuthenticationResponse>; | ||
@@ -24,0 +27,0 @@ request(data: rp.OptionsWithUri): Promise<any>; |
@@ -17,2 +17,3 @@ "use strict"; | ||
constructor(config) { | ||
this.config = config; | ||
this.defaultHeaders = { | ||
@@ -32,5 +33,12 @@ 'Cache-Control': 'no-cache', | ||
this.exp = 0; | ||
if (config.hasOwnProperty('token')) { | ||
const ctx = config; | ||
this.tokenKey = ctx.token; | ||
} | ||
else { | ||
const ctx = config; | ||
this.client_id = ctx.client_key; | ||
this.client_secret = ctx.client_secret; | ||
} | ||
this.isAuthenticated = false; | ||
this.client_id = config.client_key; | ||
this.client_secret = config.client_secret; | ||
this.decoded = {}; | ||
@@ -55,11 +63,20 @@ this.token = {}; | ||
let result = yield this.getToken(); | ||
console.log('roll on 1'); | ||
this.token = result; | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && now.isSameOrBefore(moment.unix(+this.decoded.payload.exp).subtract(15, 'minutes'))) { | ||
let expiry = moment.unix(+this.decoded.payload.exp).toLocaleString(); | ||
else if (this.isAuthenticated && this.decoded && now.isSameOrBefore(moment.unix(+this.decoded.exp).subtract(60, 'minutes'))) { | ||
console.log('roll on 2'); | ||
let expiry = moment.unix(+this.decoded.exp).toLocaleString(); | ||
let result = this.token; | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && now.isSameOrAfter(moment.unix(+this.decoded.payload.exp).subtract(15, 'minutes'))) { | ||
else if (this.isAuthenticated && this.decoded && now.isSameOrAfter(moment.unix(+this.decoded.exp).subtract(15, 'minutes'))) { | ||
console.log('roll on 3'); | ||
let result = yield this.getToken(true); | ||
this.token = result; | ||
resolve(result); | ||
} | ||
else { | ||
console.log('roll on 4'); | ||
let result = yield this.getToken(); | ||
@@ -76,3 +93,3 @@ this.token = result; | ||
} | ||
authorizeHeader(data) { | ||
authorizeHeader(data, refresh) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -82,11 +99,16 @@ const headers = this.defaultHeaders; | ||
try { | ||
this.decoded = jwt.decode(data.access_token, { json: true, complete: true }); | ||
this.exp = this.decoded.payload.exp; | ||
this.isAuthenticated = true; | ||
if (typeof headers.Authorization === 'undefined') { | ||
console.log('typeof authorization', typeof headers.Authorization); | ||
if (this.config.hasOwnProperty('token')) { | ||
console.log('using tokenKey...'); | ||
} | ||
if (typeof headers.Authorization === 'undefined' && this.isAuthenticated === false) { | ||
console.log('using intializing header for the first use..'); | ||
headers.Authorization = 'Bearer ' + data.access_token; | ||
} | ||
if (typeof headers.Authorization === 'string' && this.isAuthenticated && refresh && this.config.hasOwnProperty('client_key')) { | ||
headers.Authorization = `Bearer ` + data.access_token; | ||
} | ||
} | ||
catch (err) { | ||
console.log('signature has expired', err); | ||
console.log('header configuration error', err); | ||
} | ||
@@ -97,19 +119,40 @@ resolve(headers); | ||
} | ||
getToken() { | ||
getToken(refresh) { | ||
let opts = { | ||
method: 'POST', | ||
headers: { | ||
json: true, | ||
}; | ||
if (this.config.hasOwnProperty('token')) { | ||
const ctx = this.config; | ||
opts.headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
form: { | ||
'Authorization': `Basic ${ctx.token}` | ||
}; | ||
opts.form = { | ||
grant_type: 'client_credentials' | ||
}; | ||
} | ||
else { | ||
const ctx = this.config; | ||
opts.headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}; | ||
opts.form = { | ||
grant_type: 'client_credentials', | ||
client_id: this.client_id, | ||
client_secret: this.client_secret, | ||
}, | ||
json: true, | ||
}; | ||
client_id: ctx.client_key, | ||
client_secret: ctx.client_secret | ||
}; | ||
} | ||
console.log('opts', opts); | ||
return new Promise((resolve, reject) => { | ||
rp(this.url, opts).then((result) => __awaiter(this, void 0, void 0, function* () { | ||
if (result.access_token) { | ||
yield this.authorizeHeader(result); | ||
if (refresh) { | ||
yield this.authorizeHeader(result, refresh); | ||
} | ||
else { | ||
yield this.authorizeHeader(result); | ||
} | ||
this.token = result; | ||
this.isAuthenticated = true; | ||
resolve(result); | ||
@@ -122,2 +165,15 @@ } | ||
} | ||
decode(data) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const _decoded = jwt.decode(data.access_token, { json: true }); | ||
this.exp = +_decoded.exp; | ||
console.log("_decoded", _decoded); | ||
resolve(_decoded); | ||
} | ||
catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} | ||
refreshToken(data) { | ||
@@ -140,2 +196,5 @@ let opts = { | ||
if (result.access_token) { | ||
this.decoded = jwt.decode(result.access_token, { json: true, complete: true }); | ||
this.exp = this.decoded.exp; | ||
this.isAuthenticated = true; | ||
yield this.authorizeHeader(result); | ||
@@ -152,3 +211,3 @@ resolve(result); | ||
let status = yield this.init(); | ||
return this.createRequest(data); | ||
return yield this.createRequest(data); | ||
}); | ||
@@ -166,2 +225,3 @@ } | ||
request.headers = this.createHeaders(request.headers); | ||
console.log('client request', request); | ||
return new Promise((resolve, reject) => { | ||
@@ -168,0 +228,0 @@ rp(request).then((result) => { |
export declare type LuluConfigEnvironment = 'development' | 'production'; | ||
export interface LuluConfigOptions { | ||
export interface LuluApiKeyConfigOption { | ||
token: string; | ||
environment: LuluConfigEnvironment; | ||
} | ||
export interface LuluApiCredConfigOption { | ||
client_key: string; | ||
@@ -7,1 +11,2 @@ client_secret: string; | ||
} | ||
export declare type LuluConfigOptions = LuluApiKeyConfigOption | LuluApiCredConfigOption; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
; |
@@ -6,10 +6,12 @@ import * as request from 'request'; | ||
import { LuluConfigOptions, JwtDecodedResponse } from './common/interfaces/index'; | ||
import { IAuthenticationResponse } from './common/interfaces/index' | ||
import { LuluConfigOptions, JwtDecodedResponse, LuluApiCredConfigOption, JwtPayload } from './common/interfaces/index'; | ||
import { IAuthenticationResponse, LuluApiKeyConfigOption } from './common/interfaces/index'; | ||
export class Client { | ||
private clock!: moment.Moment; | ||
private tokenKey!: string; | ||
private client_id!: string; | ||
private client_secret!: string; | ||
private decoded: JwtDecodedResponse; | ||
// private decoded: JwtDecodedResponse; | ||
private decoded: JwtPayload; | ||
private defaultHeaders: request.Headers = { | ||
@@ -32,7 +34,14 @@ 'Cache-Control': 'no-cache', | ||
constructor(config: LuluConfigOptions) { | ||
constructor(private config: LuluConfigOptions) { | ||
if (config.hasOwnProperty('token')) { | ||
const ctx = config as (LuluApiKeyConfigOption); | ||
this.tokenKey = ctx.token; | ||
} else { | ||
const ctx = config as (LuluApiCredConfigOption); | ||
this.client_id = ctx.client_key; | ||
this.client_secret = ctx.client_secret; | ||
} | ||
this.isAuthenticated = false; | ||
this.client_id = config.client_key; | ||
this.client_secret = config.client_secret; | ||
this.decoded = {} as JwtDecodedResponse; | ||
// this.decoded = {} as JwtDecodedResponse; | ||
this.decoded = {} as JwtPayload; | ||
this.token = {} as IAuthenticationResponse; | ||
@@ -57,14 +66,24 @@ | ||
let result = await this.getToken(); | ||
console.log('roll on 1'); | ||
this.token = result; | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && now.isSameOrBefore(moment.unix(+this.decoded.payload.exp).subtract(15,'minutes'))) { | ||
let expiry = moment.unix(+this.decoded.payload.exp).toLocaleString(); | ||
else if (this.isAuthenticated && this.decoded && now.isSameOrBefore(moment.unix(+this.decoded.exp).subtract(60,'minutes'))) { | ||
console.log('roll on 2'); | ||
// let expiry = moment.unix(+this.decoded.payload.exp).toLocaleString(); | ||
let expiry = moment.unix(+this.decoded.exp).toLocaleString(); | ||
let result = this.token;// await this.refreshToken(this.token); | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && now.isSameOrAfter(moment.unix(+this.decoded.payload.exp).subtract(15,'minutes'))) { | ||
else if (this.isAuthenticated && this.decoded && now.isSameOrAfter(moment.unix(+this.decoded.exp).subtract(15,'minutes'))) { | ||
console.log('roll on 3') | ||
let result = await this.getToken(true); | ||
this.token = result; | ||
resolve(result); | ||
} | ||
else { | ||
console.log('roll on 4') | ||
let result = await this.getToken(); | ||
this.token = result; | ||
resolve(result); | ||
@@ -84,3 +103,3 @@ } | ||
*/ | ||
async authorizeHeader(data: IAuthenticationResponse) { | ||
async authorizeHeader(data: IAuthenticationResponse, refresh?: boolean) { | ||
// merge access token with default headers | ||
@@ -91,14 +110,19 @@ const headers = this.defaultHeaders; | ||
try { | ||
this.decoded = jwt.decode(data.access_token, {json: true , complete:true}) as JwtDecodedResponse; | ||
this.exp = this.decoded.payload.exp; | ||
this.isAuthenticated = true; | ||
if (typeof headers.Authorization === 'undefined') { | ||
console.log('typeof authorization',typeof headers.Authorization); | ||
if (this.config.hasOwnProperty('token')) { | ||
console.log('using tokenKey...'); | ||
} | ||
if (typeof headers.Authorization === 'undefined' && this.isAuthenticated === false) { | ||
console.log('using intializing header for the first use..'); | ||
headers.Authorization = 'Bearer ' + data.access_token; | ||
} | ||
if (typeof headers.Authorization === 'string' && this.isAuthenticated && refresh && this.config.hasOwnProperty('client_key')) { | ||
// update the headers // | ||
headers.Authorization = `Bearer ` + data.access_token; | ||
} | ||
// add access_token, but don't overwrite if header already set | ||
} catch (err) { | ||
console.log('signature has expired', err); | ||
// console.log('signature has expired', err); | ||
console.log('header configuration error', err); | ||
// await this.getToken(); | ||
@@ -114,20 +138,42 @@ // reject(err); | ||
*/ | ||
getToken(): Promise<IAuthenticationResponse> { | ||
getToken(refresh?:boolean): Promise<IAuthenticationResponse> { | ||
let opts: rp.RequestPromiseOptions = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
form: { | ||
grant_type: 'client_credentials', | ||
client_id: this.client_id, | ||
client_secret: this.client_secret, | ||
}, | ||
json: true, | ||
}; | ||
if (this.config.hasOwnProperty('token')) { | ||
const ctx = this.config as (LuluApiKeyConfigOption); | ||
opts.headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': `Basic ${ctx.token}` | ||
}; | ||
opts.form = { | ||
grant_type: 'client_credentials' | ||
}; | ||
} else { | ||
const ctx = this.config as (LuluApiCredConfigOption); | ||
opts.headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}; | ||
opts.form = { | ||
grant_type: 'client_credentials', | ||
client_id: ctx.client_key, | ||
client_secret: ctx.client_secret | ||
}; | ||
} | ||
console.log('opts', opts); | ||
return new Promise((resolve, reject) => { | ||
rp(this.url, opts).then(async(result) => { | ||
rp(this.url, opts).then(async(result: IAuthenticationResponse) => { | ||
if (result.access_token) { | ||
await this.authorizeHeader(result); | ||
if (refresh) { | ||
await this.authorizeHeader(result,refresh); | ||
} else { | ||
await this.authorizeHeader(result); | ||
} | ||
this.token = result; | ||
this.isAuthenticated = true; | ||
resolve(result); | ||
@@ -141,2 +187,16 @@ } | ||
decode(data: IAuthenticationResponse): Promise<JwtPayload> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const _decoded: JwtPayload = jwt.decode(data.access_token, { json: true }) as JwtPayload; | ||
//const _decoded = jwt.verify(data.access_token, (this.config as LuluApiCredConfigOption).client_secret) as JwtDecodedResponse; | ||
this.exp = +_decoded.exp; | ||
console.log("_decoded", _decoded); | ||
resolve(_decoded); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} | ||
/** | ||
@@ -161,4 +221,9 @@ * @returns IAuthenticationResponse | ||
return new Promise((resolve, reject) => { | ||
rp(this.url, opts).then(async(result) => { | ||
rp(this.url, opts).then(async(result:IAuthenticationResponse) => { | ||
if (result.access_token) { | ||
this.decoded = jwt.decode(result.access_token, {json: true , complete:true}) as JwtPayload; | ||
this.exp = this.decoded.exp; | ||
// console.log('decoding token', this.decoded); | ||
this.isAuthenticated = true; | ||
await this.authorizeHeader(result); | ||
@@ -179,4 +244,5 @@ resolve(result); | ||
async request(data: rp.OptionsWithUri): Promise<any> { | ||
// console.log('what am i sending here', data); | ||
let status = await this.init(); | ||
return this.createRequest(data); | ||
return await this.createRequest(data); | ||
} | ||
@@ -189,2 +255,3 @@ | ||
// add API key, but don't overwrite if header already set | ||
// handled in the authorization and of getToken or refreshToken | ||
if (typeof (<request.Headers>headers).Authorization === 'undefined') { | ||
@@ -203,2 +270,4 @@ (<request.Headers>headers).Authorization = 'Bearer ' + data.access_token; | ||
console.log('client request', request); | ||
return new Promise((resolve, reject) => { | ||
@@ -205,0 +274,0 @@ rp(request).then((result) => { |
export type LuluConfigEnvironment = 'development' | 'production'; | ||
export interface LuluConfigOptions { | ||
export interface LuluApiKeyConfigOption { | ||
token: string; | ||
environment: LuluConfigEnvironment | ||
}; | ||
export interface LuluApiCredConfigOption { | ||
client_key: string; | ||
client_secret: string; | ||
environment:LuluConfigEnvironment; | ||
} | ||
} | ||
export type LuluConfigOptions = LuluApiKeyConfigOption | LuluApiCredConfigOption; |
@@ -10,3 +10,3 @@ { | ||
"name": "@ntegral/lulu", | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"description": "Lulu Print API Client", | ||
@@ -13,0 +13,0 @@ "main": "./dist/index.js", |
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
60065
1587