Skip to content

Commit

Permalink
feat: general implement (#2)
Browse files Browse the repository at this point in the history
* feat: general implement

ref to:
https://github.com/casbin/casbin-oa/blob/master/web/src/auth/Auth.js

Signed-off-by: Zxilly <[email protected]>

feat: export correspond interface

Signed-off-by: Zxilly <[email protected]>

* feat: add option for redirect url

Signed-off-by: Zxilly <[email protected]>

* refactor: a higher degree of customization

Signed-off-by: Zxilly <[email protected]>

* feat: adjust for new API

Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly authored Jul 29, 2021
1 parent f8e2a0c commit 798f919
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

.idea/
lib/
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "casdoor-js-sdk",
"version": "1.0.0",
"main": "lib/cjs/index.js",
"typings": "lib/cjs/index.d.ts",
"module": "lib/esm/index.js",
"license": "Apache-2.0",
"scripts": {
"build": "run-s clean && run-p build:*",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.esm.json",
"clean": "rimraf lib"
},
"devDependencies": {
"npm-run-all": "^4.1.5",
"typescript": "^4.3.5"
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sdk from './sdk';

export default sdk;
59 changes: 59 additions & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export interface sdkConfig {
serverUrl: string, // your Casdoor URL, like the official one: https://door.casbin.com
clientId: string, // your Casdoor OAuth Client ID
appName: string, // your Casdoor application name, like: "app-built-in"
organizationName: string // your Casdoor organization name, like: "built-in"
}

// reference: https://github.com/casdoor/casdoor-go-sdk/blob/90fcd5646ec63d733472c5e7ce526f3447f99f1f/auth/jwt.go#L19-L32
export interface accountSession {
organization: string,
username: string,
type: string,
name: string,
avatar: string,
email: string,
phone: string,
affiliation: string,
tag: string,
language: string,
score: number,
isAdmin: boolean,
accessToken: string
}

class Sdk {
private config: sdkConfig

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

public getSignupUrl() {
return `${this.config.serverUrl.trim()}/signup/${this.config.appName}`;
}

public getSigninUrl(redirectUri: string = `${window.location.origin}/callback`): string {
const scope = "read";
const state = this.config.appName;
return `${this.config.serverUrl.trim()}/login/oauth/authorize?client_id=${this.config.clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}&state=${state}`;
}

public getUserProfileUrl(userName: string, account: accountSession) {
let param = "";
if (account !== undefined && account !== null) {
param = `?access_token=${account.accessToken}`;
}
return `${this.config.serverUrl.trim()}/users/${this.config.organizationName}/${userName}${param}`;
}

public getMyProfileUrl(account: accountSession) {
let param = "";
if (account !== undefined && account !== null) {
param = `?access_token=${account.accessToken}`;
}
return `${this.config.serverUrl.trim()}/account${param}`;
}
}

export default Sdk;
8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"target": "ES2017",
"module": "CommonJS",
"outDir": "lib/cjs"
}
}
8 changes: 8 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"target": "ES2017",
"module": "ESNext",
"outDir": "lib/esm"
}
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"moduleResolution": "Node",
"strict": true,
"strictPropertyInitialization": false,
"declaration": true,
"downlevelIteration": true,
"allowSyntheticDefaultImports": true
},
"include": ["src/**/*"]
}
Loading

0 comments on commit 798f919

Please sign in to comment.