@expressive-analytics/deep-thought-authentication
Version:
Typescript conversion of Deep Thought Authentication
37 lines (36 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DTOAuth2AuthenticationService = void 0;
const deep_thought_service_1 = require("@expressive-analytics/deep-thought-service");
const DTAuthenticationService_1 = require("./DTAuthenticationService");
const DTUser_1 = require("./DTUser");
const DTOAuthToken_1 = require("./DTOAuthToken");
const appendQuery = require('append-query');
class DTOAuth2AuthenticationService extends DTAuthenticationService_1.DTAuthenticationService {
constructor() {
super(...arguments);
this.$model = DTUser_1.DTUserModel;
}
/** performs standard authentication, authorizing the relevant token if necessary */
actionAuthenticate() {
console.log("authenticating...");
const u = super.actionAuthenticate();
if (u !== undefined) {
try { // update oauth token
const [token] = DTOAuthToken_1.DTOAuthTokenModel.upsert(this.db.qb().fail(), { type: 0, status: 1, user_id: u.$get("id") });
this.authorizeToken(token, u);
}
catch (e) {
console.error("auth error: " + e.message); // the user failed to authenticate (maybe bad user/pass)
}
}
return u;
}
authorizeToken(token, u) {
const redirect_uri = decodeURI(this.params.stringParam("redirect_uri"));
const state = this.params.stringParam("state");
const url = appendQuery(redirect_uri, { code: token.$get("token"), state: state });
throw new deep_thought_service_1.DTHTTPError(302, `Location: ${url}`);
}
}
exports.DTOAuth2AuthenticationService = DTOAuth2AuthenticationService;