UNPKG

node-socialite

Version:

Socialite is an OAuth2 authentication tool for Node.js

176 lines (175 loc) 6.51 kB
'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WeWork = void 0; const BaseProvider_1 = require("../Core/BaseProvider"); const User_1 = require("../Core/User"); const Utils_1 = require("../Core/Utils"); /** * @see [网页授权登录](https://developer.work.weixin.qq.com/document/path/91335) */ class WeWork extends BaseProvider_1.BaseProvider { constructor(config) { super(config); this._detailed = false; this._asQrcode = false; this._agentId = null; this._apiAccessToken = ''; this._baseUrl = 'https://qyapi.weixin.qq.com'; if (!this._config.has('base_url')) { this._baseUrl = this._config.get('base_url'); } if (!this._config.has('agent_id')) { this._baseUrl = this._config.get('agent_id'); } } getBaseUrl() { return this._baseUrl; } setAgentId(agentId) { this._agentId = agentId; return this; } withAgentId(agentId) { return this.setAgentId(agentId); } detailed() { this._detailed = true; return this; } asQrcode() { this._asQrcode = true; return this; } withApiAccessToken(apiAccessToken) { this._apiAccessToken = apiAccessToken; return this; } userFromCode(code) { return __awaiter(this, void 0, void 0, function* () { let token = yield this.getApiAccessToken(); let user = yield this.getUser(token, code); if (this._detailed) { user = yield this.getUserById(user['UserId']); } return this.mapUserToObject(user) .setProvider(WeWork.NAME) .setRaw(user); }); } getApiAccessToken() { return __awaiter(this, void 0, void 0, function* () { if (!this._apiAccessToken) { this._apiAccessToken = yield this.createApiAccessToken(); } return this._apiAccessToken; }); } createApiAccessToken() { return __awaiter(this, void 0, void 0, function* () { let response = yield this.doRequest({ url: this._baseUrl + '/cgi-bin/gettoken', method: 'get', params: { corpid: this._config.get('corp_id') || this._config.get('corpid') || this.getClientId(), corpsecret: this._config.get('corp_secret') || this._config.get('corpsecret') || this.getClientSecret(), } }); let data = response.data; if (!data || data['errcode']) { throw new Error(`Failed to get api access_token: ${data['errmsg'] || 'Unknown'}`); } return data['access_token']; }); } getUser(token, code) { return __awaiter(this, void 0, void 0, function* () { let response = yield this.doRequest({ url: this._baseUrl + '/cgi-bin/user/getuserinfo', method: 'get', params: { access_token: token, code, } }); let data = response.data; if (!data || data['errcode'] || (!data['UserId'] && !data['OpenId'])) { throw new Error(`Failed to get user openid: ${data['errmsg'] || 'Unknown'}`); } else if (!data['UserId']) { this._detailed = false; } return data; }); } getUserById(userId) { return __awaiter(this, void 0, void 0, function* () { let response = yield this.doRequest({ url: this._baseUrl + '/cgi-bin/user/get', method: 'get', params: { access_token: this.getApiAccessToken(), userid: userId, } }); let data = response.data; if (!data || data['errcode'] || !data['userid']) { throw new Error(`Failed to get user: ${data['errmsg'] || 'Unknown'}`); } return data; }); } getAuthUrl() { let query = { appid: this.getClientId(), redirect_uri: this._redirectUrl, response_type: 'code', scope: this.formatScopes(this._scopes, this._scopeSeparator), }; if (this._agentId) { query['agentid'] = this._agentId; } if (this._state) { query['state'] = this._state; } if (!this._agentId && (query.scope.indexOf('snsapi_privateinfo') > -1 || this._asQrcode)) { throw new Error(`'agent_id' is require when qrcode mode or scopes is 'snsapi_privateinfo'`); } if (this._asQrcode) { delete query.scope; return `https://open.work.weixin.qq.com/wwopen/sso/qrConnect?` + (0, Utils_1.buildQueryString)(query); } return 'https://open.weixin.qq.com/connect/oauth2/authorize?' + (0, Utils_1.buildQueryString)(query) + '#wechat_redirect'; } getTokenUrl() { return ''; } getUserByToken(token) { return __awaiter(this, void 0, void 0, function* () { throw new Error(`WeWork doesn't support access_token mode.`); }); } mapUserToObject(user) { if (this._detailed) { return new User_1.User({ id: user['userid'] || null, name: user['name'] || null, email: user['email'] || null, avatar: user['avatar'] || null, }); } return new User_1.User({ id: user['UserId'] || user['OpenId'] || null, }); } } exports.WeWork = WeWork; WeWork.NAME = 'wework';