Skip to content

Commit 5567e61

Browse files
authored
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
1 parent 5a3af4b commit 5567e61

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ Initialization requires 5 parameters, which are all string type:
4848

4949
```typescript
5050
import { SDK, Config } from 'casdoor-nodejs-sdk'
51+
import type { AxiosRequestConfig } from 'axios';
52+
import https from 'node:https';
53+
54+
// Optional param for providing a self-signed CA with requests.
55+
const axiosConfig: AxiosRequestConfig = {
56+
httpsAgent: new https.Agent({ ca: ... })
57+
}
5158

5259
const authCfg: Config = {
5360
endpoint: '',
@@ -58,6 +65,8 @@ const authCfg: Config = {
5865
}
5966

6067
const sdk = new SDK(authCfg)
68+
// or
69+
const sdk = new SDK(authCfg, axiosConfig)
6170

6271
// call sdk to handle
6372
```

src/request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class Request {
2323
baseURL: config.url,
2424
timeout: config.timeout || 60000,
2525
headers: config.headers,
26+
...config,
2627
})
2728
}
2829
get(url: string, config?: AxiosRequestConfig<any>) {

src/sdk.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { Sms, SmsSDK } from './sms'
4040
import { MfaData, MfaSDK } from './mfa'
4141
import { CasbinRequest, EnforceSDK } from './enforce'
4242
import { UrlSDK } from './url'
43+
import type { AxiosRequestConfig } from 'axios'
4344

4445
export class SDK {
4546
private readonly config: Config
@@ -71,7 +72,7 @@ export class SDK {
7172
private enforceSDK: EnforceSDK
7273
private urlSDK: UrlSDK
7374

74-
constructor(config: Config) {
75+
constructor(config: Config, axiosConfig?: AxiosRequestConfig) {
7576
this.config = config
7677
this.request = new Request({
7778
url: config.endpoint + '/api',
@@ -83,6 +84,7 @@ export class SDK {
8384
`${this.config.clientId}:${this.config.clientSecret}`,
8485
).toString('base64'),
8586
},
87+
...axiosConfig,
8688
})
8789
this.userSDK = new UserSDK(this.config, this.request)
8890
this.adapterSDK = new AdapterSDK(this.config, this.request)
@@ -526,6 +528,10 @@ export class SDK {
526528
return await this.tokenSDK.deleteToken(token)
527529
}
528530

531+
public async introspect(token: string, token_type_hint: string) {
532+
return await this.tokenSDK.introspect(token, token_type_hint)
533+
}
534+
529535
public async getWebhooks() {
530536
return await this.webhookSDK.getWebhooks()
531537
}

src/token.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,23 @@ export class TokenSDK {
9696
public async deleteToken(token: Token) {
9797
return this.modifyToken('delete-token', token)
9898
}
99+
100+
public async introspect(token: string, token_type_hint: string) {
101+
if (!this.request) {
102+
throw new Error('request init failed')
103+
}
104+
105+
return (await this.request.post(
106+
'/login/oauth/introspect',
107+
{
108+
token,
109+
token_type_hint,
110+
},
111+
{
112+
headers: {
113+
'Content-Type': 'application/x-www-form-urlencoded',
114+
},
115+
},
116+
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
117+
}
99118
}

0 commit comments

Comments
 (0)