"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var MicrosoftOAuthStrategy_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MicrosoftOAuthStrategy = exports.MicrosoftOauthGuard = void 0;
const common_1 = require("@nestjs/common");
const passport_1 = require("@nestjs/passport");
const passport_microsoft_1 = require("passport-microsoft");
const microsoft_oauth_helper_1 = require("../helpers/microsoft-oauth.helper");
const uuid_1 = require("uuid");
const user_service_1 = require("../services/user.service");
const DUMMY_CLIENT_ID = 'DUMMY_CLIENT_ID';
const DUMMY_CLIENT_SECRET = 'DUMMY_CLIENT_SECRET';
const DUMMY_TENANT = 'common';
const DUMMY_CALLBACK_URL = 'DUMMY_CALLBACK_URL';
let MicrosoftOauthGuard = class MicrosoftOauthGuard extends (0, passport_1.AuthGuard)('microsoft') {
};
exports.MicrosoftOauthGuard = MicrosoftOauthGuard;
exports.MicrosoftOauthGuard = MicrosoftOauthGuard = __decorate([
    (0, common_1.Injectable)()
], MicrosoftOauthGuard);
let MicrosoftOAuthStrategy = MicrosoftOAuthStrategy_1 = class MicrosoftOAuthStrategy extends (0, passport_1.PassportStrategy)(passport_microsoft_1.Strategy, 'microsoft') {
    constructor(userService) {
        const clientID = process.env.IAM_MICROSOFT_OAUTH_CLIENT_ID ?? DUMMY_CLIENT_ID;
        const clientSecret = process.env.IAM_MICROSOFT_OAUTH_CLIENT_SECRET ?? DUMMY_CLIENT_SECRET;
        const tenant = process.env.IAM_MICROSOFT_OAUTH_TENANT_ID ?? DUMMY_TENANT;
        const callbackURL = process.env.IAM_MICROSOFT_OAUTH_CALLBACK_URL ?? DUMMY_CALLBACK_URL;
        const redirectURL = process.env.IAM_MICROSOFT_OAUTH_REDIRECT_URL;
        super({
            clientID,
            clientSecret,
            callbackURL,
            tenant,
            scope: ['user.read'],
            addUPNAsEmail: true,
        });
        this.userService = userService;
        this.logger = new common_1.Logger(MicrosoftOAuthStrategy_1.name);
        const microsoftOauth = { clientID, clientSecret, tenant, callbackURL, redirectURL };
        if (!(0, microsoft_oauth_helper_1.isMicrosoftOAuthConfigured)(microsoftOauth)) {
            this.logger.debug('Microsoft OAuth strategy is not configured');
        }
    }
    async validate(_accessToken, _refreshToken, profile, done) {
        const { id, displayName, emails, photos } = profile;
        const loginAccessCode = (0, uuid_1.v4)();
        const email = emails?.[0]?.value || profile._json?.mail || profile._json?.userPrincipalName;
        const user = {
            provider: 'microsoft',
            providerId: id,
            email: email,
            name: displayName,
            picture: photos?.[0]?.value || null,
            accessCode: loginAccessCode,
        };
        await this.userService.resolveUserOnOauthMicrosoft({
            ...user,
            accessToken: _accessToken,
            refreshToken: null
        });
        done(null, user);
    }
};
exports.MicrosoftOAuthStrategy = MicrosoftOAuthStrategy;
exports.MicrosoftOAuthStrategy = MicrosoftOAuthStrategy = MicrosoftOAuthStrategy_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [user_service_1.UserService])
], MicrosoftOAuthStrategy);
//# sourceMappingURL=microsoft-oauth.strategy.js.map