@openzeppelin/defender-base-client
Advanced tools
Comparing version 1.54.4 to 1.54.5
/// <reference types="node" /> | ||
import { CognitoUserSession } from 'amazon-cognito-identity-js'; | ||
import { AxiosError, AxiosInstance } from 'axios'; | ||
import https from 'https'; | ||
export declare function rejectWithDefenderApiError(axiosError: AxiosError): Promise<never>; | ||
export declare function createApi(key: string, token: string, apiUrl: string, httpsAgent?: https.Agent): AxiosInstance; | ||
export declare function createAuthenticatedApi(username: string, session: CognitoUserSession, apiUrl: string, httpsAgent?: https.Agent): AxiosInstance; | ||
export declare function createApi(apiUrl: string, key?: string, token?: string, httpsAgent?: https.Agent, headers?: Record<string, string>): AxiosInstance; | ||
export declare function createAuthenticatedApi(username: string, accessToken: string, apiUrl: string, httpsAgent?: https.Agent): AxiosInstance; | ||
export declare function createUnauthorizedApi(apiUrl: string, httpsAgent?: https.Agent, headers?: Record<string, string>): AxiosInstance; | ||
//# sourceMappingURL=api.d.ts.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createAuthenticatedApi = exports.createApi = exports.rejectWithDefenderApiError = void 0; | ||
exports.createUnauthorizedApi = exports.createAuthenticatedApi = exports.createApi = exports.rejectWithDefenderApiError = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
@@ -14,9 +14,15 @@ const api_error_1 = require("./api-error"); | ||
exports.rejectWithDefenderApiError = rejectWithDefenderApiError; | ||
function createApi(key, token, apiUrl, httpsAgent) { | ||
function createApi(apiUrl, key, token, httpsAgent, headers) { | ||
const authHeaders = key && token | ||
? { | ||
'X-Api-Key': key, | ||
Authorization: `Bearer ${token}`, | ||
} | ||
: {}; | ||
const instance = axios_1.default.create({ | ||
baseURL: apiUrl, | ||
headers: { | ||
'X-Api-Key': key, | ||
'Authorization': `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
...authHeaders, | ||
...headers, | ||
}, | ||
@@ -29,6 +35,9 @@ httpsAgent, | ||
exports.createApi = createApi; | ||
function createAuthenticatedApi(username, session, apiUrl, httpsAgent) { | ||
const accessToken = session.getAccessToken().getJwtToken(); | ||
return createApi(username, accessToken, apiUrl, httpsAgent); | ||
function createAuthenticatedApi(username, accessToken, apiUrl, httpsAgent) { | ||
return createApi(apiUrl, username, accessToken, httpsAgent); | ||
} | ||
exports.createAuthenticatedApi = createAuthenticatedApi; | ||
function createUnauthorizedApi(apiUrl, httpsAgent, headers) { | ||
return createApi(apiUrl, undefined, undefined, httpsAgent, headers); | ||
} | ||
exports.createUnauthorizedApi = createUnauthorizedApi; |
/// <reference types="node" /> | ||
import { AxiosInstance } from 'axios'; | ||
import https from 'https'; | ||
import { AuthType } from './auth-v2'; | ||
export type ApiVersion = 'v1' | 'v2'; | ||
export type AuthConfig = { | ||
useCredentialsCaching: boolean; | ||
type: AuthType; | ||
}; | ||
export declare abstract class BaseApiClient { | ||
@@ -10,7 +15,9 @@ private api; | ||
private session; | ||
private sessionV2; | ||
private apiSecret; | ||
private httpsAgent?; | ||
private authConfig; | ||
protected abstract getPoolId(): string; | ||
protected abstract getPoolClientId(): string; | ||
protected abstract getApiUrl(v: ApiVersion): string; | ||
protected abstract getApiUrl(v: ApiVersion, type?: AuthType): string; | ||
constructor(params: { | ||
@@ -20,3 +27,8 @@ apiKey: string; | ||
httpsAgent?: https.Agent; | ||
authConfig?: AuthConfig; | ||
}); | ||
private getAccessToken; | ||
private getAccessTokenV2; | ||
private refreshSession; | ||
private refreshSessionV2; | ||
protected init(v?: ApiVersion): Promise<AxiosInstance>; | ||
@@ -23,0 +35,0 @@ protected refresh(v?: ApiVersion): Promise<AxiosInstance>; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const auth_1 = require("./auth"); | ||
const auth_v2_1 = require("./auth-v2"); | ||
class BaseApiClient { | ||
@@ -16,9 +17,49 @@ constructor(params) { | ||
this.httpsAgent = params.httpsAgent; | ||
this.authConfig = params.authConfig ?? { useCredentialsCaching: false, type: 'admin' }; | ||
} | ||
async getAccessToken() { | ||
const userPass = { Username: this.apiKey, Password: this.apiSecret }; | ||
const poolData = { UserPoolId: this.getPoolId(), ClientId: this.getPoolClientId() }; | ||
this.session = await (0, auth_1.authenticate)(userPass, poolData); | ||
return this.session.getAccessToken().getJwtToken(); | ||
} | ||
async getAccessTokenV2() { | ||
if (!this.authConfig.type) | ||
throw new Error('Auth type is required to authenticate in auth v2'); | ||
const credentials = { | ||
apiKey: this.apiKey, | ||
secretKey: this.apiSecret, | ||
type: this.authConfig.type, | ||
}; | ||
this.sessionV2 = await (0, auth_v2_1.authenticateV2)(credentials, this.getApiUrl('v1', 'admin')); | ||
return this.sessionV2.accessToken; | ||
} | ||
async refreshSession() { | ||
if (!this.session) | ||
return this.getAccessToken(); | ||
const userPass = { Username: this.apiKey, Password: this.apiSecret }; | ||
const poolData = { UserPoolId: this.getPoolId(), ClientId: this.getPoolClientId() }; | ||
this.session = await (0, auth_1.refreshSession)(userPass, poolData, this.session); | ||
return this.session.getAccessToken().getJwtToken(); | ||
} | ||
async refreshSessionV2() { | ||
if (!this.authConfig.type) | ||
throw new Error('Auth type is required to refresh session in auth v2'); | ||
if (!this.sessionV2) | ||
return this.getAccessTokenV2(); | ||
const credentials = { | ||
apiKey: this.apiKey, | ||
secretKey: this.apiSecret, | ||
refreshToken: this.sessionV2.refreshToken, | ||
type: this.authConfig.type, | ||
}; | ||
this.sessionV2 = await (0, auth_v2_1.refreshSessionV2)(credentials, this.getApiUrl('v1', 'admin')); | ||
return this.sessionV2.accessToken; | ||
} | ||
async init(v = 'v1') { | ||
if (!this.api || this.version !== v) { | ||
const userPass = { Username: this.apiKey, Password: this.apiSecret }; | ||
const poolData = { UserPoolId: this.getPoolId(), ClientId: this.getPoolClientId() }; | ||
this.session = await (0, auth_1.authenticate)(userPass, poolData); | ||
this.api = (0, api_1.createAuthenticatedApi)(userPass.Username, this.session, this.getApiUrl(v), this.httpsAgent); | ||
const accessToken = this.authConfig.useCredentialsCaching | ||
? await this.getAccessTokenV2() | ||
: await this.getAccessToken(); | ||
this.api = (0, api_1.createAuthenticatedApi)(this.apiKey, accessToken, this.getApiUrl(v, 'admin'), this.httpsAgent); | ||
this.version = v; | ||
@@ -29,10 +70,10 @@ } | ||
async refresh(v = 'v1') { | ||
if (!this.session) { | ||
if (!this.session && !this.sessionV2) { | ||
return this.init(v); | ||
} | ||
try { | ||
const userPass = { Username: this.apiKey, Password: this.apiSecret }; | ||
const poolData = { UserPoolId: this.getPoolId(), ClientId: this.getPoolClientId() }; | ||
this.session = await (0, auth_1.refreshSession)(userPass, poolData, this.session); | ||
this.api = (0, api_1.createAuthenticatedApi)(userPass.Username, this.session, this.getApiUrl(v), this.httpsAgent); | ||
const accessToken = this.authConfig.useCredentialsCaching | ||
? await this.refreshSessionV2() | ||
: await this.refreshSession(); | ||
this.api = (0, api_1.createAuthenticatedApi)(this.apiKey, accessToken, this.getApiUrl(v, 'admin'), this.httpsAgent); | ||
return this.api; | ||
@@ -39,0 +80,0 @@ } |
export { createApi, createAuthenticatedApi } from './api/api'; | ||
export { authenticate } from './api/auth'; | ||
export { BaseApiClient, ApiVersion } from './api/client'; | ||
export { BaseApiClient, ApiVersion, AuthConfig } from './api/client'; | ||
export { AuthType } from './api/auth-v2'; | ||
export * from './utils/network'; | ||
@@ -5,0 +6,0 @@ export declare const VERSION: any; |
{ | ||
"name": "@openzeppelin/defender-base-client", | ||
"version": "1.54.4", | ||
"version": "1.54.5", | ||
"description": "", | ||
@@ -35,3 +35,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "a7c4808dd11e708df42d110de230c264061c72c3", | ||
"gitHead": "0fc43da796268b8c75d70179c7ea649b160dfd06", | ||
"repository": { | ||
@@ -38,0 +38,0 @@ "type": "git", |
@@ -6,1 +6,5 @@ # Defender Base Client | ||
Certain components of Defender, such as Admin or Relay, can be interacted with programmatically via an API. This library provides the base class for the clients. | ||
# End Of Support Notice | ||
We will no longer be maintaining or supporting any additional releases for defender-client. Please migrate to defender-sdk as soon as possible to get all the benefits of defender 2.0 and more. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
10
74196
37
685