Skip to content

Commit

Permalink
feat: add Enforce SDK, Url SDK (#49)
Browse files Browse the repository at this point in the history
* feat: Create Enforce SDK

* feat: Url SDK
  • Loading branch information
mogu4iy authored Feb 15, 2024
1 parent db0c701 commit 8ae17f8
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/enforce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { AxiosResponse } from 'axios'
import { Config } from './config'
import Request from './request'

export type CasbinRequest = string[]
export type CasbinResponse = boolean[]

export class EnforceSDK {
private config: Config
private readonly request: Request

constructor(config: Config, request: Request) {
this.config = config
this.request = request
}

public async enforce(
permissionId: string,
modelId: string,
resourceId: string,
casbinRequest: CasbinRequest,
): Promise<boolean> {
const response = await this.doEnforce<CasbinResponse>(
'enforce',
permissionId,
modelId,
resourceId,
casbinRequest,
)
const { data } = response.data
for (const isAllow of data) {
if (isAllow) {
return isAllow
}
}
return false
}

public async batchEnforce(
permissionId: string,
modelId: string,
resourceId: string,
casbinRequest: CasbinRequest[],
): Promise<boolean[]> {
const response = await this.doEnforce<CasbinResponse[]>(
'batch-enforce',
permissionId,
modelId,
resourceId,
casbinRequest,
)
const { data } = response.data
return data.flat(2)
}

private async doEnforce<T>(
action: string,
permissionId: string,
modelId: string,
resourceId: string,
casbinRequest: CasbinRequest | CasbinRequest[],
) {
if (!this.request) {
throw new Error('request init failed')
}

const url = `/${action}`
return (await this.request.post(url, casbinRequest, {
params: {
permissionId: permissionId,
modelId: modelId,
resourceId: resourceId,
},
})) as unknown as Promise<
AxiosResponse<{
data: T
}>
>
}
}
50 changes: 50 additions & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { Product, ProductSDK } from './product'
import { Email, EmailSDK } from './email'
import { Sms, SmsSDK } from './sms'
import { MfaData, MfaSDK } from './mfa'
import { CasbinRequest, EnforceSDK } from './enforce'
import { UrlSDK } from './url'

export class SDK {
private readonly config: Config
Expand Down Expand Up @@ -66,6 +68,8 @@ export class SDK {
private emailSDK: EmailSDK
private smsSDK: SmsSDK
private mfaSDK: MfaSDK
private enforceSDK: EnforceSDK
private urlSDK: UrlSDK

constructor(config: Config) {
this.config = config
Expand Down Expand Up @@ -104,6 +108,8 @@ export class SDK {
this.emailSDK = new EmailSDK(this.config, this.request)
this.smsSDK = new SmsSDK(this.config, this.request)
this.mfaSDK = new MfaSDK(this.config, this.request)
this.enforceSDK = new EnforceSDK(this.config, this.request)
this.urlSDK = new UrlSDK(this.config)
}

public async getAuthToken(code: string) {
Expand Down Expand Up @@ -591,4 +597,48 @@ export class SDK {
public async deleteMfa(owner: string, name: string) {
return await this.mfaSDK.delete(owner, name)
}

public async enforce(
permissionId: string,
modelId: string,
resourceId: string,
casbinRequest: CasbinRequest,
) {
return await this.enforceSDK.enforce(
permissionId,
modelId,
resourceId,
casbinRequest,
)
}

public async batchEnforce(
permissionId: string,
modelId: string,
resourceId: string,
casbinRequest: CasbinRequest[],
) {
return await this.enforceSDK.batchEnforce(
permissionId,
modelId,
resourceId,
casbinRequest,
)
}

public getSignUpUrl(enablePassword: boolean, redirectUri: string) {
return this.urlSDK.getSignUpUrl(enablePassword, redirectUri)
}

public getSignInUrl(redirectUri: string) {
return this.urlSDK.getSignInUrl(redirectUri)
}

public getUserProfileUrl(userName: string, accessToken: string) {
return this.urlSDK.getUserProfileUrl(userName, accessToken)
}

public getMyProfileUrl(accessToken: string) {
return this.urlSDK.getMyProfileUrl(accessToken)
}
}
57 changes: 57 additions & 0 deletions src/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Config } from './config'

export type SignAction = 'signup' | 'login'

export class UrlSDK {
private config: Config

constructor(config: Config) {
this.config = config
}

private getSignUrl(action: SignAction, redirectUri: string): string {
const scope = 'read'
const state = this.config.appName
return `${this.config.endpoint}/${action}/oauth/authorize?client_id=${
this.config.clientId
}&response_type=code&redirect_uri=${
redirectUri.split(/[?#]/)[0]
}&scope=${scope}&state=${state}`
}

public getSignUpUrl(enablePassword: boolean, redirectUri: string): string {
if (enablePassword) {
return `${this.config.endpoint}/signup/${this.config.appName}}`
} else {
return this.getSignUrl('signup', redirectUri)
}
}

public getSignInUrl(redirectUri: string): string {
return this.getSignUrl('login', redirectUri)
}

public getUserProfileUrl(userName: string, accessToken: string): string {
const param = accessToken ? `?access_token=${accessToken}}` : ''
return `${this.config.endpoint}/users/${this.config.orgName}/${userName}${param}`
}

public getMyProfileUrl(accessToken: string): string {
const param = accessToken ? `?access_token=${accessToken}}` : ''
return `${this.config.endpoint}/account${param}`
}
}

0 comments on commit 8ae17f8

Please sign in to comment.