Skip to content

feat: add optional AxiosRequestConfig parameter in constructor, add /introspect endpoint #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -58,6 +65,8 @@ const authCfg: Config = {
}

const sdk = new SDK(authCfg)
// or
const sdk = new SDK(authCfg, axiosConfig)

// call sdk to handle
```
Expand Down
1 change: 1 addition & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class Request {
baseURL: config.url,
timeout: config.timeout || 60000,
headers: config.headers,
...config,
})
}
get(url: string, config?: AxiosRequestConfig<any>) {
Expand Down
8 changes: 7 additions & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down
19 changes: 19 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AxiosResponse<Record<string, unknown>>>
}
}
Loading