UNPKG

casdoor-nodejs-sdk

Version:
92 lines (91 loc) 3.2 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.MfaSDK = exports.MfaType = void 0; const FormData = require("form-data"); var MfaType; (function (MfaType) { MfaType["EMAIL"] = "email"; MfaType["SMS"] = "sms"; MfaType["APP"] = "app"; })(MfaType = exports.MfaType || (exports.MfaType = {})); class MfaSDK { constructor(config, request) { this.config = config; this.request = request; } prepareMfaPayload(data) { const formData = new FormData(); formData.append('owner', data.owner); formData.append('mfaType', data.mfaType); formData.append('name', data.name); return formData; } async initiate(data) { if (!this.request) { throw new Error('request init failed'); } const payload = this.prepareMfaPayload(data); console.log(payload); return (await this.request.post('mfa/setup/initiate', payload, { headers: payload.getHeaders(), })); } async verify(data, passcode) { if (!this.request) { throw new Error('request init failed'); } const payload = this.prepareMfaPayload(data); payload.append('passcode', passcode); return (await this.request.post('mfa/setup/verify', payload, { headers: payload.getHeaders(), withCredentials: true, })); } async enable(data, cookie = null) { if (!this.request) { throw new Error('request init failed'); } const payload = this.prepareMfaPayload(data); const headers = payload.getHeaders(); if (cookie) headers.Cookie = cookie; return (await this.request.post('mfa/setup/enable', payload, { headers: headers, withCredentials: true, })); } async setPreferred(data) { if (!this.request) { throw new Error('request init failed'); } const payload = this.prepareMfaPayload(data); return (await this.request.post('set-preferred-mfa', payload, { headers: payload.getHeaders(), })); } async delete(owner, name) { if (!this.request) { throw new Error('request init failed'); } const formData = new FormData(); formData.append('owner', owner); formData.append('name', name); return (await this.request.post('delete-mfa', formData, { headers: formData.getHeaders(), })); } } exports.MfaSDK = MfaSDK;