From 5567e6111cc29e730712a6430f915c57151c7c1a Mon Sep 17 00:00:00 2001 From: John Matczak <50644033+cidrmill@users.noreply.github.com> Date: Mon, 3 Jun 2024 01:58:19 +0000 Subject: [PATCH] feat: add optional AxiosRequestConfig parameter in constructor, add /introspect endpoint (#60) * feat: add ability for custom AxiosRequestConfig in Sdk * feat: add token introspect endpoint * feat: add token /introspect method in Sdk * fix: send correct content type to introspect endpoint * chore: add new axiosConfig param to README --- README.md | 9 +++++++++ src/request.ts | 1 + src/sdk.ts | 8 +++++++- src/token.ts | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 95cdce2..57097df 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,13 @@ Initialization requires 5 parameters, which are all string type: ```typescript import { SDK, Config } from 'casdoor-nodejs-sdk' +import type { AxiosRequestConfig } from 'axios'; +import https from 'node:https'; + +// Optional param for providing a self-signed CA with requests. +const axiosConfig: AxiosRequestConfig = { + httpsAgent: new https.Agent({ ca: ... }) +} const authCfg: Config = { endpoint: '', @@ -58,6 +65,8 @@ const authCfg: Config = { } const sdk = new SDK(authCfg) +// or +const sdk = new SDK(authCfg, axiosConfig) // call sdk to handle ``` diff --git a/src/request.ts b/src/request.ts index 710ac75..9528634 100644 --- a/src/request.ts +++ b/src/request.ts @@ -23,6 +23,7 @@ export default class Request { baseURL: config.url, timeout: config.timeout || 60000, headers: config.headers, + ...config, }) } get(url: string, config?: AxiosRequestConfig) { diff --git a/src/sdk.ts b/src/sdk.ts index be0b250..2fdf088 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -40,6 +40,7 @@ import { Sms, SmsSDK } from './sms' import { MfaData, MfaSDK } from './mfa' import { CasbinRequest, EnforceSDK } from './enforce' import { UrlSDK } from './url' +import type { AxiosRequestConfig } from 'axios' export class SDK { private readonly config: Config @@ -71,7 +72,7 @@ export class SDK { private enforceSDK: EnforceSDK private urlSDK: UrlSDK - constructor(config: Config) { + constructor(config: Config, axiosConfig?: AxiosRequestConfig) { this.config = config this.request = new Request({ url: config.endpoint + '/api', @@ -83,6 +84,7 @@ export class SDK { `${this.config.clientId}:${this.config.clientSecret}`, ).toString('base64'), }, + ...axiosConfig, }) this.userSDK = new UserSDK(this.config, this.request) this.adapterSDK = new AdapterSDK(this.config, this.request) @@ -526,6 +528,10 @@ export class SDK { return await this.tokenSDK.deleteToken(token) } + public async introspect(token: string, token_type_hint: string) { + return await this.tokenSDK.introspect(token, token_type_hint) + } + public async getWebhooks() { return await this.webhookSDK.getWebhooks() } diff --git a/src/token.ts b/src/token.ts index 7062495..3512de5 100644 --- a/src/token.ts +++ b/src/token.ts @@ -96,4 +96,23 @@ export class TokenSDK { public async deleteToken(token: Token) { return this.modifyToken('delete-token', token) } + + public async introspect(token: string, token_type_hint: string) { + if (!this.request) { + throw new Error('request init failed') + } + + return (await this.request.post( + '/login/oauth/introspect', + { + token, + token_type_hint, + }, + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }, + )) as unknown as Promise>> + } }