feat: access token generic para permitir customização
This commit is contained in:
parent
4b61c118ad
commit
477d9fc6a7
3 changed files with 46 additions and 52 deletions
|
@ -1,2 +1,2 @@
|
|||
export * from './lhisp-oauth-client';
|
||||
export * from './lhisp-oauth-client.t';
|
||||
export * from "./lhisp-oauth-client";
|
||||
export * from "./lhisp-oauth-client.t";
|
||||
|
|
|
@ -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';
|
||||
export const defaultAuthHeaderName = "Authorization";
|
||||
export const defaultTokenHeaderName = "Authorization";
|
||||
|
|
|
@ -13,7 +13,7 @@ import {
|
|||
} from "./lhisp-oauth-client.t";
|
||||
import logger from "lhisp-logger";
|
||||
|
||||
export class LhispOauthClient {
|
||||
export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
|
||||
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<AccessToken> {
|
||||
async getAccessToken(): Promise<iAccessToken> {
|
||||
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<iAccessToken>(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, "created_at">): 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<ResponseType> {
|
||||
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<ResponseType>({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue