"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var OAuthController_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OAuthController = void 0;
const api_1 = require("@hed-hog/api");
const api_locale_1 = require("@hed-hog/api-locale");
const common_1 = require("@nestjs/common");
const setting_service_1 = require("../setting/setting.service");
const oauth_service_1 = require("./oauth.service");
let OAuthController = OAuthController_1 = class OAuthController {
    constructor(service, settingService) {
        this.service = service;
        this.settingService = settingService;
        this.logger = new common_1.Logger(OAuthController_1.name);
    }
    logOAuthRequest(action, provider, { locale, origin, referer, host, ipAddress, userAgent, code, }) {
        var _a;
        const summarizedUserAgent = userAgent
            ? userAgent.length > 120
                ? `${userAgent.slice(0, 117)}...`
                : userAgent
            : 'n/a';
        this.logger.log(`OAuth ${action}: provider=${provider} locale=${locale !== null && locale !== void 0 ? locale : 'n/a'} origin=${origin !== null && origin !== void 0 ? origin : 'n/a'} referer=${referer !== null && referer !== void 0 ? referer : 'n/a'} host=${host !== null && host !== void 0 ? host : 'n/a'} ip=${ipAddress !== null && ipAddress !== void 0 ? ipAddress : 'n/a'} hasCode=${code ? 'yes' : 'no'} codeLength=${(_a = code === null || code === void 0 ? void 0 : code.length) !== null && _a !== void 0 ? _a : 0} userAgent="${summarizedUserAgent}"`);
    }
    async githubCallback(code, state, res) {
        const type = state || 'login';
        const { url: frontendURL = '' } = await this.settingService.getSettingValues(['url']);
        const redirectURL = `${frontendURL}/callback/github/${type}?code=${encodeURIComponent(code)}`;
        return res.redirect(redirectURL);
    }
    async login(origin, referer, host, provider, res) {
        this.logOAuthRequest('login-init', provider, { origin, referer, host });
        const url = await this.service.getAuthUrl(provider, `/callback/${provider}/login`);
        return res.redirect(url);
    }
    async register(provider, res) {
        const url = await this.service.getAuthUrl(provider, `/callback/${provider}/register`);
        return res.redirect(url);
    }
    async connect(provider, res) {
        const url = this.service.getAuthUrl(provider, `/callback/${provider}/connect`);
        return res.redirect(url);
    }
    async callbackLogin(locale, ipAddress, userAgent, origin, referer, host, forwardedHost, provider, code, res) {
        this.logOAuthRequest('callback-login', provider, {
            locale,
            origin,
            referer,
            host: forwardedHost || host,
            ipAddress,
            userAgent,
            code,
        });
        return this.service.handleCallback({ res, locale, ipAddress, userAgent, provider: provider, code, type: 'login' });
    }
    async callbackRegister(locale, ipAddress, userAgent, provider, code, res) {
        return this.service.handleCallback({ res, locale, ipAddress, userAgent, provider: provider, code, type: 'register' });
    }
    async callbackConnect(locale, ipAddress, userAgent, { id }, provider, code, res) {
        return this.service.handleCallback({ res, locale, ipAddress, userAgent, provider: provider, code, type: 'connect', userId: id });
    }
    async disconnect(locale, ipAddress, userAgent, provider, email, res) {
        return this.service.handleCallback({ res, locale, ipAddress, userAgent, provider: provider, email, type: 'disconnect' });
    }
};
exports.OAuthController = OAuthController;
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Get)('github/callback'),
    __param(0, (0, common_1.Query)('code')),
    __param(1, (0, common_1.Query)('state')),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "githubCallback", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Get)(':provider/login'),
    __param(0, (0, common_1.Headers)('origin')),
    __param(1, (0, common_1.Headers)('referer')),
    __param(2, (0, common_1.Headers)('host')),
    __param(3, (0, common_1.Param)('provider')),
    __param(4, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "login", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Get)(':provider/register'),
    __param(0, (0, common_1.Param)('provider')),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "register", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Get)(':provider/connect'),
    __param(0, (0, common_1.Param)('provider')),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "connect", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Get)(':provider/callback/login'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Ip)()),
    __param(2, (0, common_1.Headers)('user-agent')),
    __param(3, (0, common_1.Headers)('origin')),
    __param(4, (0, common_1.Headers)('referer')),
    __param(5, (0, common_1.Headers)('host')),
    __param(6, (0, common_1.Headers)('x-forwarded-host')),
    __param(7, (0, common_1.Param)('provider')),
    __param(8, (0, common_1.Query)('code')),
    __param(9, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, String, String, String, String, String, String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "callbackLogin", null);
__decorate([
    (0, api_1.Public)(),
    (0, common_1.Get)(':provider/callback/register'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Ip)()),
    __param(2, (0, common_1.Headers)('user-agent')),
    __param(3, (0, common_1.Param)('provider')),
    __param(4, (0, common_1.Query)('code')),
    __param(5, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, String, String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "callbackRegister", null);
__decorate([
    (0, common_1.Get)(':provider/callback/connect'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Ip)()),
    __param(2, (0, common_1.Headers)('user-agent')),
    __param(3, (0, api_1.User)()),
    __param(4, (0, common_1.Param)('provider')),
    __param(5, (0, common_1.Query)('code')),
    __param(6, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, Object, String, String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "callbackConnect", null);
__decorate([
    (0, common_1.Delete)(':provider'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Ip)()),
    __param(2, (0, common_1.Headers)('user-agent')),
    __param(3, (0, common_1.Param)('provider')),
    __param(4, (0, common_1.Body)('email')),
    __param(5, (0, common_1.Res)({ passthrough: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, String, String, Object]),
    __metadata("design:returntype", Promise)
], OAuthController.prototype, "disconnect", null);
exports.OAuthController = OAuthController = OAuthController_1 = __decorate([
    (0, common_1.Controller)('oauth'),
    __metadata("design:paramtypes", [oauth_service_1.OAuthService, setting_service_1.SettingService])
], OAuthController);
//# sourceMappingURL=oauth.controller.js.map