@ntegral/lulu
Advanced tools
Comparing version 1.2.5 to 1.2.6
@@ -1,2 +0,1 @@ | ||
import * as request from 'request'; | ||
import * as rp from 'request-promise'; | ||
@@ -12,3 +11,2 @@ import { LuluConfigOptions } from './common/interfaces/index'; | ||
private isAuthenticated; | ||
private initialization; | ||
private sandbox; | ||
@@ -19,5 +17,6 @@ private prod; | ||
private exp; | ||
private token; | ||
constructor(config: LuluConfigOptions); | ||
init: () => Promise<IAuthenticationResponse | undefined>; | ||
authorizeHeader(data: IAuthenticationResponse): Promise<request.Headers>; | ||
init(): Promise<IAuthenticationResponse>; | ||
authorizeHeader(data: IAuthenticationResponse): Promise<unknown>; | ||
getToken(): Promise<IAuthenticationResponse>; | ||
@@ -24,0 +23,0 @@ refreshToken(data: IAuthenticationResponse): Promise<IAuthenticationResponse>; |
@@ -26,3 +26,2 @@ "use strict"; | ||
}; | ||
this.isAuthenticated = false; | ||
this.sandbox = 'https://api.sandbox.lulu.com'; | ||
@@ -33,24 +32,7 @@ this.prod = 'https://api.lulu.com'; | ||
this.exp = 0; | ||
this.init = () => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let now = moment(); | ||
if (!this.isAuthenticated) { | ||
let result = yield this.getToken(); | ||
return result; | ||
} | ||
if (this.isAuthenticated && this.decoded && moment.unix(this.decoded.payload.exp).isAfter(now)) { | ||
let result = yield this.getToken(); | ||
return result; | ||
} | ||
if (this.isAuthenticated && this.decoded && !moment.unix(this.decoded.payload.exp).isAfter(now.add(10, 'minutes'))) { | ||
let result = yield this.refreshToken(this.decoded); | ||
} | ||
} | ||
catch (error) { | ||
throw new TypeError('Unable to initiate due to \n' + error); | ||
} | ||
}); | ||
this.isAuthenticated = false; | ||
this.client_id = config.client_key; | ||
this.client_secret = config.client_secret; | ||
this.decoded = {}; | ||
this.token = {}; | ||
if (config.environment == 'production') { | ||
@@ -64,71 +46,90 @@ this.defaultRequest.baseUrl = this.prod; | ||
} | ||
this.initialization = this.init(); | ||
} | ||
authorizeHeader(data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const headers = this.defaultHeaders; | ||
init() { | ||
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
this.decoded = jwt.decode(data.access_token, { complete: true }); | ||
this.exp = this.decoded.payload.exp; | ||
let now = moment(); | ||
if (!this.isAuthenticated) { | ||
let result = yield this.getToken(); | ||
this.token = result; | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && !moment.unix(this.decoded.payload.exp).isAfter(now.add(10, 'minutes'))) { | ||
let result = yield this.refreshToken(this.token); | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && moment.unix(this.decoded.payload.exp).isAfter(now)) { | ||
let result = yield this.getToken(); | ||
this.token = result; | ||
resolve(result); | ||
} | ||
} | ||
catch (err) { | ||
console.log('signature has expired', err); | ||
yield this.getToken(); | ||
catch (error) { | ||
reject(`Unable to initiate due to \n' + ${error}`); | ||
} | ||
if (typeof headers.Authorization === 'undefined') { | ||
headers.Authorization = 'Bearer ' + data.access_token; | ||
} | ||
return headers; | ||
}); | ||
})); | ||
} | ||
getToken() { | ||
authorizeHeader(data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let opts = { | ||
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, | ||
}; | ||
return yield rp(this.url, opts).then((result) => __awaiter(this, void 0, void 0, function* () { | ||
if (result.access_token) { | ||
yield this.authorizeHeader(result); | ||
const headers = this.defaultHeaders; | ||
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
this.decoded = jwt.decode(data.access_token, { json: true, complete: true }); | ||
this.isAuthenticated = true; | ||
if (typeof headers.Authorization === 'undefined') { | ||
headers.Authorization = 'Bearer ' + data.access_token; | ||
} | ||
} | ||
return result; | ||
})).catch(this.handleError); | ||
catch (err) { | ||
console.log('signature has expired', err); | ||
} | ||
resolve(headers); | ||
})); | ||
}); | ||
} | ||
getToken() { | ||
let opts = { | ||
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, | ||
}; | ||
return rp(this.url, opts).then((result) => __awaiter(this, void 0, void 0, function* () { | ||
if (result.access_token) { | ||
yield this.authorizeHeader(result); | ||
} | ||
return result; | ||
})).catch(this.handleError); | ||
} | ||
refreshToken(data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let opts = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
form: { | ||
grant_type: 'refresh_token', | ||
client_id: this.client_id, | ||
client_secret: this.client_secret, | ||
refresh_token: data.refresh_token, | ||
}, | ||
json: true, | ||
}; | ||
return yield rp(this.url, opts).then((result) => __awaiter(this, void 0, void 0, function* () { | ||
if (result.access_token) { | ||
yield this.authorizeHeader(result); | ||
this.isAuthenticated = true; | ||
} | ||
return result; | ||
})).catch(this.handleError); | ||
}); | ||
let opts = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
form: { | ||
grant_type: 'refresh_token', | ||
client_id: this.client_id, | ||
client_secret: this.client_secret, | ||
refresh_token: data.refresh_token, | ||
}, | ||
json: true, | ||
}; | ||
return rp(this.url, opts).then((result) => __awaiter(this, void 0, void 0, function* () { | ||
if (result.access_token) { | ||
yield this.authorizeHeader(result); | ||
} | ||
return result; | ||
})).catch(this.handleError); | ||
} | ||
request(data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.initialization; | ||
let status = yield this.init(); | ||
console.log('status of request', status); | ||
return this.createRequest(data); | ||
@@ -135,0 +136,0 @@ }); |
@@ -13,2 +13,36 @@ import * as rp from 'request-promise'; | ||
} | ||
export interface JwtDecodedResponse { | ||
header: JwtHeader; | ||
payload: JwtPayload; | ||
signature: string; | ||
} | ||
export interface JwtHeader { | ||
alg: string; | ||
typ: string; | ||
kid: string; | ||
} | ||
export interface JwtPayload { | ||
jti: string; | ||
exp: number; | ||
nbf: number; | ||
iat: number; | ||
iss: string; | ||
aud: string[]; | ||
sub: string; | ||
typ: string; | ||
azp: string; | ||
auth_time: number; | ||
session_state: string; | ||
acr: string; | ||
realm_access: any; | ||
resource_access: any; | ||
scope: string; | ||
clientHost: string; | ||
clientId: string; | ||
email_verified: boolean; | ||
groups: string[]; | ||
preferred_username: string; | ||
clientAddress: string; | ||
email: string; | ||
} | ||
export interface IList<T> { | ||
@@ -15,0 +49,0 @@ count: number; |
@@ -6,3 +6,3 @@ import * as request from 'request'; | ||
import { LuluConfigOptions } from './common/interfaces/index'; | ||
import { LuluConfigOptions, JwtDecodedResponse } from './common/interfaces/index'; | ||
import { IAuthenticationResponse } from './common/interfaces/index' | ||
@@ -13,3 +13,3 @@ | ||
private client_secret!: string; | ||
private decoded!: any; | ||
private decoded: JwtDecodedResponse; | ||
private defaultHeaders: request.Headers = { | ||
@@ -24,4 +24,4 @@ 'Cache-Control': 'no-cache', | ||
}; | ||
private isAuthenticated: boolean = false; | ||
private initialization!: Promise<any>; | ||
private isAuthenticated:boolean; | ||
// private initialization!: Promise<any>; | ||
private sandbox: string = 'https://api.sandbox.lulu.com'; | ||
@@ -32,2 +32,3 @@ private prod: string = 'https://api.lulu.com'; | ||
private exp: number = 0; | ||
private token: IAuthenticationResponse; | ||
@@ -38,2 +39,5 @@ constructor(config: LuluConfigOptions) { | ||
this.client_secret = config.client_secret; | ||
this.decoded = {} as JwtDecodedResponse; | ||
this.token = {} as IAuthenticationResponse; | ||
if (config.environment == 'production') { | ||
@@ -46,22 +50,34 @@ this.defaultRequest.baseUrl = this.prod; | ||
} | ||
this.initialization = this.init(); | ||
// this.initialization = this.init(); | ||
} | ||
init = async () => { | ||
try { | ||
let now = moment(); | ||
if (!this.isAuthenticated) { | ||
let result = await this.getToken(); | ||
return result; | ||
init(): Promise<IAuthenticationResponse> { | ||
return new Promise(async(resolve, reject) => { | ||
try { | ||
let now = moment(); | ||
if (!this.isAuthenticated) { | ||
let result = await this.getToken(); | ||
// console.log('initial use of init'); | ||
this.token = result; | ||
// return result; | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && !moment.unix(this.decoded.payload.exp).isAfter(now.add(10,'minutes'))) { // token hasn't expired renew // | ||
let result = await this.refreshToken(this.token); | ||
// console.log('using of refreshToken'); | ||
// return result; | ||
resolve(result); | ||
} | ||
if (this.isAuthenticated && this.decoded && moment.unix(this.decoded.payload.exp).isAfter(now)) { // token has expired, get a new token // | ||
let result = await this.getToken(); | ||
this.token = result; | ||
// return result; | ||
resolve(result); | ||
} | ||
} catch (error) { | ||
// throw new TypeError('Unable to initiate due to \n' + error); | ||
reject(`Unable to initiate due to \n' + ${error}`); | ||
} | ||
if (this.isAuthenticated && this.decoded && moment.unix(this.decoded.payload.exp).isAfter(now)) { // token has expired, get a new token // | ||
let result = await this.getToken(); | ||
return result; | ||
} | ||
if (this.isAuthenticated && this.decoded && !moment.unix(this.decoded.payload.exp).isAfter(now.add(10,'minutes'))) { // token hasn't expired renew // | ||
let result = await this.refreshToken(this.decoded); | ||
} | ||
} catch (error) { | ||
throw new TypeError('Unable to initiate due to \n' + error); | ||
} | ||
}) | ||
} | ||
@@ -78,18 +94,44 @@ | ||
return new Promise(async(resolve, reject) => { | ||
try { | ||
// data. | ||
this.decoded = jwt.decode(data.access_token, {json: true , complete:true}) as JwtDecodedResponse; | ||
// console.log('jwt decoded', this.decoded); | ||
// this.exp = this.decoded.payload.exp; | ||
// let expiry = moment.unix(this.exp).toDate().toLocaleString(); | ||
// console.log('expiry',expiry); | ||
this.isAuthenticated = true; | ||
if (typeof headers.Authorization === 'undefined') { | ||
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); | ||
// await this.getToken(); | ||
// reject(err); | ||
} | ||
resolve(headers); | ||
}) | ||
// let decoded: any = jwt.decode(data.access_token, { complete: true }); | ||
try { | ||
this.decoded = jwt.decode(data.access_token, { complete: true }); | ||
this.exp = this.decoded.payload.exp; | ||
// console.log('expiry',this.exp); | ||
} catch (err) { | ||
console.log('signature has expired', err); | ||
await this.getToken(); | ||
} | ||
// try { | ||
// this.decoded = jwt.decode(data.access_token, { complete: true }) as JwtDecodedResponse; | ||
// console.log('jwt decoded', this.decoded); | ||
// this.exp = this.decoded.payload.exp; | ||
// let expiry = moment.unix(this.exp).toDate().toLocaleString(); | ||
// console.log('expiry',expiry); | ||
// this.isAuthenticated = true; | ||
// } catch (err) { | ||
// console.log('signature has expired', err); | ||
// await this.getToken(); | ||
// } | ||
// console.log('expire', moment.unix(this.decoded.payload.exp)); | ||
if (typeof headers.Authorization === 'undefined') { | ||
/* if (typeof headers.Authorization === 'undefined') { | ||
headers.Authorization = 'Bearer ' + data.access_token; | ||
} | ||
} */ | ||
// add access_token, but don't overwrite if header already set | ||
return headers; | ||
// return headers; | ||
} | ||
@@ -100,3 +142,3 @@ | ||
*/ | ||
async getToken(): Promise<IAuthenticationResponse> { | ||
getToken(): Promise<IAuthenticationResponse> { | ||
let opts: rp.RequestPromiseOptions = { | ||
@@ -115,7 +157,7 @@ method: 'POST', | ||
return await rp(this.url, opts).then(async(result) => { | ||
return rp(this.url, opts).then(async(result: IAuthenticationResponse) => { | ||
if (result.access_token) { | ||
// console.log('authentication successful', result.token_type); | ||
await this.authorizeHeader(result); | ||
this.isAuthenticated = true; | ||
// this.isAuthenticated = true; | ||
} | ||
@@ -130,3 +172,3 @@ return result; | ||
*/ | ||
async refreshToken(data: IAuthenticationResponse): Promise<IAuthenticationResponse> { | ||
refreshToken(data: IAuthenticationResponse): Promise<IAuthenticationResponse> { | ||
let opts: rp.RequestPromiseOptions = { | ||
@@ -146,7 +188,7 @@ method: 'POST', | ||
return await rp(this.url, opts).then(async(result) => { | ||
return rp(this.url, opts).then(async(result:IAuthenticationResponse) => { | ||
if (result.access_token) { | ||
// console.log('authentication successful', result.token_type); | ||
await this.authorizeHeader(result); | ||
this.isAuthenticated = true; | ||
// this.isAuthenticated = true; | ||
} | ||
@@ -164,3 +206,5 @@ return result; | ||
async request(data: rp.OptionsWithUri) { | ||
await this.initialization; | ||
// await this.initialization; | ||
let status = await this.init(); | ||
console.log('status of request', status); | ||
return this.createRequest(data); | ||
@@ -167,0 +211,0 @@ } |
@@ -15,2 +15,40 @@ import * as rp from 'request-promise'; | ||
export interface JwtDecodedResponse { | ||
header: JwtHeader; | ||
payload: JwtPayload; | ||
signature: string; | ||
} | ||
export interface JwtHeader { | ||
alg: string; | ||
typ: string; | ||
kid: string; | ||
} | ||
export interface JwtPayload { | ||
jti: string; | ||
exp: number; | ||
nbf: number; | ||
iat: number; | ||
iss: string; | ||
aud: string[]; | ||
sub: string; | ||
typ: string; | ||
azp: string; | ||
auth_time: number; | ||
session_state: string; | ||
acr: string; | ||
realm_access: any; | ||
resource_access: any; | ||
scope: string; | ||
clientHost: string; | ||
clientId: string; | ||
email_verified: boolean; | ||
groups: string[]; | ||
preferred_username: string; | ||
clientAddress: string; | ||
email: string; | ||
} | ||
export interface IList<T> { | ||
@@ -17,0 +55,0 @@ count: number; |
@@ -10,3 +10,3 @@ { | ||
"name": "@ntegral/lulu", | ||
"version": "1.2.5", | ||
"version": "1.2.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
54510
1449