diff --git a/src/index.ts b/src/index.ts index f980e70..144a45c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export * from './lhisp-oauth-client'; -export * from './lhisp-oauth-client.t'; \ No newline at end of file +export * from "./lhisp-oauth-client"; +export * from "./lhisp-oauth-client.t"; diff --git a/src/lhisp-oauth-client.t.ts b/src/lhisp-oauth-client.t.ts index d945320..cacb4f8 100644 --- a/src/lhisp-oauth-client.t.ts +++ b/src/lhisp-oauth-client.t.ts @@ -1,45 +1,44 @@ import { AxiosRequestConfig } from "axios"; export interface Headers { - [name: string]: any; + [name: string]: any; } export interface LhispOauthClientConstructorParams { - authUrl: string; - apiUrl: string; - clientId: string; - clientSecret: string; - certificado?: string; - senhaCertificado?: string; - authScope?: string; - authHeaderName?: string; - tokenHeaderName?: string; - headers?: Headers; - grantType?: string; - authContentType?: ContentType; - sendAuthCredentialsOnRequestBody?: boolean; - debug?: boolean; + authUrl: string; + apiUrl: string; + clientId: string; + clientSecret: string; + certificado?: string; + senhaCertificado?: string; + authScope?: string; + authHeaderName?: string; + tokenHeaderName?: string; + headers?: Headers; + grantType?: string; + authContentType?: ContentType; + sendAuthCredentialsOnRequestBody?: boolean; + debug?: boolean; } export interface ExecutarRequestParams extends AxiosRequestConfig { - path: string; - contentType?: ContentType, + path: string; + contentType?: ContentType; } export interface AccessToken { - token_type: string; - access_token: string; - expires_in: number; - scope?: string; - created_at?: number; + token_type: string; + access_token: string; + expires_in: number; + scope?: string; } export enum ContentType { - APPLICATION_JSON='application/json', - APPLICATION_X_WWW_FORM_URLENCODED='application/x-www-form-urlencoded', + APPLICATION_JSON = "application/json", + APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded", } -export const defaultGrantType = 'client_credentials'; +export const defaultGrantType = "client_credentials"; export const defaultAuthContentType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED; -export const defaultAuthHeaderName = 'Authorization'; -export const defaultTokenHeaderName = 'Authorization'; \ No newline at end of file +export const defaultAuthHeaderName = "Authorization"; +export const defaultTokenHeaderName = "Authorization"; diff --git a/src/lhisp-oauth-client.ts b/src/lhisp-oauth-client.ts index f7f0e9e..6295d6e 100644 --- a/src/lhisp-oauth-client.ts +++ b/src/lhisp-oauth-client.ts @@ -13,7 +13,7 @@ import { } from "./lhisp-oauth-client.t"; import logger from "lhisp-logger"; -export class LhispOauthClient { +export class LhispOauthClient { protected authUrl: string; protected apiUrl: string; protected clientId: string; @@ -27,8 +27,10 @@ export class LhispOauthClient { protected headers?: Headers; protected grantType?: string; protected agent: https.Agent; - protected accessToken?: AccessToken; - protected refreshToken?: AccessToken; + protected accessToken?: iAccessToken; + protected refreshToken?: iAccessToken; + protected tokenCreatedAt = 0; + protected tokenExpiresIn = 0; protected sendAuthCredentialsOnRequestBody?: boolean; constructor(params: LhispOauthClientConstructorParams) { @@ -75,16 +77,16 @@ export class LhispOauthClient { } } - isTokenValid(token: AccessToken) { - if (!token) return false; - if (!token.created_at) return false; - const timeDiff = (Date.now() - token.created_at) / 1000; - return timeDiff < token.expires_in - 10; + isTokenValid() { + if (!this.accessToken) return false; + if (!this.tokenCreatedAt) return false; + const timeDiff = (Date.now() - this.tokenCreatedAt) / 1000; + return timeDiff < this.tokenExpiresIn - 10; } - async getAccessToken(): Promise { + async getAccessToken(): Promise { try { - if (this.accessToken && this.isTokenValid(this.accessToken)) { + if (this.accessToken && this.isTokenValid()) { return this.accessToken; } @@ -113,21 +115,18 @@ export class LhispOauthClient { data: authRequestOpt.data, contentType: this.authContentType, }); - const response = await axios.request(authRequestOpt); - return this.buildAccessToken(response.data); + const resp = await axios.request(authRequestOpt); + this.accessToken = this.buildAccessToken(resp.data); + this.tokenCreatedAt = new Date().getTime(); + return this.accessToken; } catch (error) { logger.error({ message: "LhispOauthClient.getAccessToken", error }); throw error; } } - buildAccessToken(data: Omit): AccessToken { - this.accessToken = { - ...data, - created_at: Date.now(), - }; - - return this.accessToken; + buildAccessToken(data: any) { + return data as iAccessToken; } getAuthToken() { @@ -143,14 +142,10 @@ export class LhispOauthClient { }: ExecutarRequestParams): Promise { try { await this.getAccessToken(); - if (!this.accessToken?.token_type) { - console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken); - } let headers = { "Content-Type": contentType, [this.tokenHeaderName]: this.getAuthToken(), - // ...this.headers }; const response = await axios.request({