UNPKG

@expressive-analytics/deep-thought-authentication

Version:

Typescript conversion of Deep Thought Authentication

69 lines (68 loc) 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DTOAuth2Verifier = void 0; const deep_thought_js_1 = require("@expressive-analytics/deep-thought-js"); const deep_thought_service_1 = require("@expressive-analytics/deep-thought-service"); const DTOAuthToken_1 = require("./DTOAuthToken"); class DTOAuth2Verifier { constructor(db) { if (db === undefined) this._db = deep_thought_js_1.DTStorage.defaultStore(); else this._db = db; } verify(action, token) { if (action == "actionAccessToken") { //provide the access token const client_id = this.provider.params.stringParam("client_id"); const client_secret = this.provider.params.stringParam("client_secret"); const code = this.provider.params.stringParam("code"); const redirect_uri = this.provider.params.stringParam("redirect_uri"); const grant_type = this.provider.params.stringParam("grant_type"); // should be "authorization code" // validate the client const api = this.lookupConsumer(client_id, client_secret); if (api) { const token = DTOAuthToken_1.DTOAuthTokenModel.query(this._db.filter({ type: 0, token: code, status: 1 })); token.$set("consumer_id", api.$get("id")); token.updateToAccessToken(this._db); } this.provider.response.respond(`access_token=${token.$get["token"]}`); } return false; } lookupConsumer(key, secret) { try { const api = deep_thought_service_1.DTRequestor.query(this._db.filter({ consumer_key: key })); if (api.$get("status") == 0) this.provider.response.error(deep_thought_service_1.DTErr.OAUTH_CONSUMER_KEY_REFUSED); return api; } catch (e) { console.log(e.message); } return undefined; } token() { try { if (this._token === undefined) { this._token = deep_thought_js_1.DTKeystore.setShared(this.castToken(this._db.where(`token='${this.access_token}'`))); return this._token; } } catch (e) { console.error("Could not find token: " + e.message); } } castToken(qb) { return DTOAuthToken_1.DTOAuthTokenModel.query(qb); } userID() { const token = this.token(); return token.$get("user_id"); } db() { return this._db; } } exports.DTOAuth2Verifier = DTOAuth2Verifier;