login-auth-services
Version:
Authentication services for Google, GitHub, Microsoft, okta and multi-factor authentication using OTP.
65 lines (64 loc) • 3.42 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleMicrosoftCallback = exports.getUserInfoMicrosoft = exports.getTokenMicrosoft = exports.getAuthURLMicrosoft = void 0;
const axios_1 = __importDefault(require("axios"));
const query_string_1 = __importDefault(require("query-string"));
const databaseService_1 = require("../services/databaseService");
const dbService = databaseService_1.DatabaseService.getInstance();
const getAuthURLMicrosoft = (options) => {
const params = query_string_1.default.stringify({
client_id: options.clientId,
redirect_uri: options.redirectUri,
response_type: "code",
scope: options.scope || 'openid email profile',
state: Math.random().toString(36).substring(7),
});
return `https://login.microsoftonline.com/${options.tenantId}/oauth2/v2.0/authorize?${params}`;
};
exports.getAuthURLMicrosoft = getAuthURLMicrosoft;
const getTokenMicrosoft = (code, options) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield axios_1.default.post(`https://login.microsoftonline.com/${options.tenantId}/oauth2/v2.0/token`, query_string_1.default.stringify({
client_id: options.clientId,
client_secret: options.clientSecret,
code,
redirect_uri: options.redirectUri,
grant_type: "authorization_code",
}), { headers: { "Content-Type": "application/x-www-form-urlencoded" } });
return response.data.access_token;
});
exports.getTokenMicrosoft = getTokenMicrosoft;
const getUserInfoMicrosoft = (accessToken) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield axios_1.default.get("https://graph.microsoft.com/v1.0/me", {
headers: { Authorization: `Bearer ${accessToken}` },
});
return response.data;
});
exports.getUserInfoMicrosoft = getUserInfoMicrosoft;
const handleMicrosoftCallback = (code, options) => __awaiter(void 0, void 0, void 0, function* () {
try {
const accessToken = yield (0, exports.getTokenMicrosoft)(code, options);
const profile = yield (0, exports.getUserInfoMicrosoft)(accessToken);
// Now update the user's Microsoft ID in the database
const user = yield dbService.findUserByEmail(profile.userPrincipalName);
if (user) {
yield dbService.updateUser(user.id, { microsoftId: profile.id });
}
return profile;
}
catch (err) {
throw err;
}
});
exports.handleMicrosoftCallback = handleMicrosoftCallback;