commit
34ac3a28da
6 changed files with 4654 additions and 824 deletions
3
.prettierrc.json
Normal file
3
.prettierrc.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"printWidth": 120
|
||||
}
|
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
||||
"editor.formatOnSave": true
|
||||
}
|
3786
package-lock.json
generated
Normal file
3786
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
14
package.json
14
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "lhisp-oauth-client",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.7",
|
||||
"main": "src/index",
|
||||
"types": "src/index.d.ts",
|
||||
"repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git",
|
||||
|
@ -12,13 +12,13 @@
|
|||
"test:watch": "jest --watchAll"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.4.0",
|
||||
"@types/node": "^18.11.18",
|
||||
"jest": "^29.4.1",
|
||||
"ts-jest": "^29.0.5",
|
||||
"typescript": "^4.9.4"
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/node": "^20.3.1",
|
||||
"jest": "^29.5.0",
|
||||
"ts-jest": "^29.1.0",
|
||||
"typescript": "^5.1.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.2.5"
|
||||
"axios": "^1.4.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import qs from 'querystring';
|
||||
import https from 'https';
|
||||
import axios, { AxiosRequestConfig } from 'axios';
|
||||
import qs from "querystring";
|
||||
import https from "https";
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import {
|
||||
AccessToken, ContentType, defaultAuthContentType,
|
||||
AccessToken,
|
||||
ContentType,
|
||||
defaultAuthContentType,
|
||||
defaultAuthHeaderName,
|
||||
defaultGrantType,
|
||||
defaultTokenHeaderName,
|
||||
|
@ -37,11 +39,10 @@ export class LhispOauthClient {
|
|||
});
|
||||
} else {
|
||||
this.agent = new https.Agent({
|
||||
rejectUnauthorized: false
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
this.certificado = params.certificado;
|
||||
this.headers = (params.headers ? params.headers : {}) as any as Headers;
|
||||
this.apiUrl = params.apiUrl;
|
||||
|
@ -60,105 +61,135 @@ export class LhispOauthClient {
|
|||
return `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64")}`;
|
||||
}
|
||||
|
||||
parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any, contentType: string }) {
|
||||
if(!data || Object.keys(data).length===0) return undefined;
|
||||
parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any; contentType: string }) {
|
||||
if (!data || Object.keys(data).length === 0) return undefined;
|
||||
|
||||
switch (contentType) {
|
||||
case ContentType.APPLICATION_JSON: return JSON.stringify(data);
|
||||
case ContentType.APPLICATION_X_WWW_FORM_URLENCODED: return qs.stringify(data);
|
||||
case ContentType.APPLICATION_JSON:
|
||||
return JSON.stringify(data);
|
||||
case ContentType.APPLICATION_X_WWW_FORM_URLENCODED:
|
||||
return qs.stringify(data);
|
||||
default:
|
||||
throw new Error(`Content Type Inválido: [${contentType}]`);
|
||||
}
|
||||
}
|
||||
|
||||
isTokenValid(token: AccessToken){
|
||||
if(!token) return false;
|
||||
if(!token.created_at) return false;
|
||||
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;
|
||||
}
|
||||
|
||||
async getAccessToken() : Promise<AccessToken> {
|
||||
if(this.accessToken && this.isTokenValid(this.accessToken)) {
|
||||
async getAccessToken(): Promise<AccessToken> {
|
||||
if (this.accessToken && this.isTokenValid(this.accessToken)) {
|
||||
return this.accessToken;
|
||||
}
|
||||
|
||||
// TODO: Implementar Refresh Token.
|
||||
|
||||
let authRequestOpt: AxiosRequestConfig = {
|
||||
method: 'POST',
|
||||
method: "POST",
|
||||
url: this.authUrl,
|
||||
httpsAgent: this.agent,
|
||||
headers: {
|
||||
[this.authHeaderName]: this.getAuthHeaderValue(),
|
||||
'Content-Type': this.authContentType,
|
||||
"Content-Type": this.authContentType,
|
||||
},
|
||||
data: {},
|
||||
};
|
||||
|
||||
if (this.grantType) authRequestOpt.data.grant_type = this.grantType;
|
||||
if (this.authScope) authRequestOpt.data.scope = this.authScope;
|
||||
|
||||
if (this.sendAuthCredentialsOnRequestBody) {
|
||||
if (this.clientId) authRequestOpt.data.client_id = this.clientId;
|
||||
if (this.clientSecret) authRequestOpt.data.client_secret = this.clientSecret;
|
||||
}
|
||||
|
||||
if(this.grantType) authRequestOpt.data.grant_type = this.grantType;
|
||||
if(this.authScope) authRequestOpt.data.scope = this.authScope;
|
||||
|
||||
if(this.sendAuthCredentialsOnRequestBody) {
|
||||
if(this.clientId) authRequestOpt.data.client_id = this.clientId;
|
||||
if(this.clientSecret) authRequestOpt.data.client_secret = this.clientSecret;
|
||||
}
|
||||
|
||||
authRequestOpt.data = this.parseData({ data: authRequestOpt.data, contentType: this.authContentType });
|
||||
authRequestOpt.data = this.parseData({
|
||||
data: authRequestOpt.data,
|
||||
contentType: this.authContentType,
|
||||
});
|
||||
const response = await axios.request(authRequestOpt);
|
||||
return this.buildAccessToken(response.data);
|
||||
}
|
||||
|
||||
buildAccessToken(data: Omit<AccessToken,'created_at'>): AccessToken{
|
||||
buildAccessToken(data: Omit<AccessToken, "created_at">): AccessToken {
|
||||
this.accessToken = {
|
||||
...data,
|
||||
created_at: Date.now()
|
||||
created_at: Date.now(),
|
||||
};
|
||||
|
||||
return this.accessToken;
|
||||
}
|
||||
|
||||
getAuthToken(){
|
||||
getAuthToken() {
|
||||
return `${this.accessToken?.token_type} ${this.accessToken?.access_token}`;
|
||||
}
|
||||
|
||||
async executarRequest({ method, path, data, params, contentType = ContentType.APPLICATION_JSON } : ExecutarRequestParams) {
|
||||
async executarRequest<ResponseType>({
|
||||
method,
|
||||
path,
|
||||
data,
|
||||
params,
|
||||
contentType = ContentType.APPLICATION_JSON,
|
||||
}: ExecutarRequestParams): Promise<ResponseType> {
|
||||
await this.getAccessToken();
|
||||
if (!this.accessToken?.token_type) {
|
||||
console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
|
||||
}
|
||||
|
||||
let headers = {
|
||||
'Content-Type': contentType,
|
||||
"Content-Type": contentType,
|
||||
[this.tokenHeaderName]: this.getAuthToken(),
|
||||
// ...this.headers
|
||||
};
|
||||
|
||||
const response = await axios.request({
|
||||
const response = await axios.request<ResponseType>({
|
||||
method,
|
||||
url: `${this.apiUrl}${path}`,
|
||||
httpsAgent: this.agent,
|
||||
headers,
|
||||
data,
|
||||
params
|
||||
params,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async get({ path, contentType = ContentType.APPLICATION_JSON, params }: ExecutarRequestParams) {
|
||||
return this.executarRequest({ method: 'GET', path, contentType, params });
|
||||
async get<ResponseType>({ path, contentType = ContentType.APPLICATION_JSON, params }: ExecutarRequestParams) {
|
||||
return this.executarRequest<ResponseType>({
|
||||
method: "GET",
|
||||
path,
|
||||
contentType,
|
||||
params,
|
||||
});
|
||||
}
|
||||
|
||||
async put({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||
return this.executarRequest({ method: 'PUT', path, data, contentType });
|
||||
async put<ResponseType>({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||
return this.executarRequest<ResponseType>({
|
||||
method: "PUT",
|
||||
path,
|
||||
data,
|
||||
contentType,
|
||||
});
|
||||
}
|
||||
|
||||
async post({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||
return this.executarRequest({ method: 'POST', path, data, contentType });
|
||||
async post<ResponseType>({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||
return this.executarRequest<ResponseType>({
|
||||
method: "POST",
|
||||
path,
|
||||
data,
|
||||
contentType,
|
||||
});
|
||||
}
|
||||
|
||||
async delete({ path, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||
return this.executarRequest({ method: 'DELETE', path, contentType });
|
||||
async delete<ResponseType>({ path, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||
return this.executarRequest<ResponseType>({
|
||||
method: "DELETE",
|
||||
path,
|
||||
contentType,
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue