UNPKG

nocobase-plugin-casdoor

Version:

Integrated Casdoor authentication system

115 lines (113 loc) 3.8 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var casdoorOauth_exports = {}; __export(casdoorOauth_exports, { CasdoorOAuthSdk: () => CasdoorOAuthSdk }); module.exports = __toCommonJS(casdoorOauth_exports); class CasdoorOAuthSdk { client_id; client_secret; endpoint; authorization_path; access_token_path; user_info_path; constructor({ client_id, client_secret, endpoint, authorization_path, access_token_path, user_info_path }) { this.client_id = client_id; this.client_secret = client_secret; this.endpoint = endpoint.replace(/\/$/, ""); this.authorization_path = authorization_path || "/login/oauth/authorize"; this.access_token_path = access_token_path || "/api/login/oauth/access_token"; this.user_info_path = user_info_path || "/api/userinfo"; } getAuthorizationUrl(redirect_uri, invite_token) { const params = { client_id: this.client_id, redirect_uri, response_type: "code", scope: "profile email" }; if (invite_token) { params["state"] = invite_token; } const queryString = Object.entries(params).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("&"); return `${this.endpoint}${this.authorization_path}?${queryString}`; } async getAccessToken(code) { const queryString = Object.entries({ client_id: this.client_id, client_secret: this.client_secret, code, grant_type: "authorization_code" }).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("&"); const tokenUrl = `${this.endpoint}${this.access_token_path}?${queryString}`; try { const response = await fetch(tokenUrl, { method: "POST" }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}, text: ${await response.text()}`); } const data = await response.json(); return data.access_token; } catch (error) { throw new Error(`Failed to get access token: ${error}`); } } async getUserInfo(access_token) { const userInfoUrl = `${this.endpoint}${this.user_info_path}`; try { const response = await fetch(userInfoUrl, { method: "GET", headers: { Authorization: `Bearer ${access_token}` } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}, text: ${await response.text()}`); } const data = await response.json(); return { id: data.sub, name: data.name, email: data.email }; } catch (error) { throw new Error(`Failed to get user info: ${error}`); } } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CasdoorOAuthSdk });