-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Enforce SDK, Url SDK (#49)
* feat: Create Enforce SDK * feat: Url SDK
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}> | ||
> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} | ||
} |