Skip to content

Commit

Permalink
feat: add optional AxiosRequestConfig parameter in constructor, add /…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
cidrmill authored Jun 3, 2024
1 parent 5a3af4b commit 5567e61
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
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>>>
}
}

0 comments on commit 5567e61

Please sign in to comment.