UNPKG

casdoor-nodejs-sdk

Version:
118 lines (117 loc) 4.05 kB
"use strict"; // 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. Object.defineProperty(exports, "__esModule", { value: true }); exports.UserSDK = void 0; const jwt = require("jsonwebtoken"); const FormData = require("form-data"); class UserSDK { constructor(config, request) { this.config = config; this.request = request; } async getAuthToken(code) { if (!this.request) { throw new Error('request init failed'); } const { data: { access_token, refresh_token }, } = (await this.request.post('login/oauth/access_token', { client_id: this.config.clientId, client_secret: this.config.clientSecret, grant_type: 'authorization_code', code, })); return { access_token: access_token, refresh_token: refresh_token }; } async refreshToken(refreshToken) { if (!this.request) { throw new Error('request init failed'); } const { data: { access_token, refresh_token }, } = (await this.request.post('login/oauth/refresh_token', { client_id: this.config.clientId, client_secret: this.config.clientSecret, grant_type: 'refresh_token', refresh_token: refreshToken, })); return { access_token: access_token, refresh_token: refresh_token }; } parseJwtToken(token) { return jwt.verify(token, this.config.certificate, { algorithms: ['RS256'], }); } async getUsers() { if (!this.request) { throw new Error('request init failed'); } return (await this.request.get('/get-users', { params: { owner: this.config.orgName, }, })); } async getUser(id) { if (!this.request) { throw new Error('request init failed'); } return (await this.request.get('/get-user', { params: { id: `${this.config.orgName}/${id}`, }, })); } async getUserCount(isOnline) { if (!this.request) { throw new Error('request init failed'); } return (await this.request.get('/get-user-count', { params: { isOnline, owner: this.config.orgName, }, })); } async modifyUser(method, user) { if (!this.request) { throw new Error('request init failed'); } const url = `/${method}`; user.owner = this.config.orgName; return (await this.request.post(url, user, { params: { id: `${user.owner}/${user.name}`, }, })); } async addUser(user) { return this.modifyUser('add-user', user); } async updateUser(user) { return this.modifyUser('update-user', user); } async deleteUser(user) { return this.modifyUser('delete-user', user); } async setPassword(data) { var _a; const formData = new FormData(); formData.append('userOwner', data.owner); formData.append('userName', data.name); formData.append('oldPassword', (_a = data.oldPassword) !== null && _a !== void 0 ? _a : ''); formData.append('newPassword', data.newPassword); return (await this.request.post('set-password', formData, { headers: formData.getHeaders(), })); } } exports.UserSDK = UserSDK;