"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenUtils = void 0;
const tslib_1 = require("tslib");
const codiac_common_1 = require("@codiac.io/codiac-common");
const contracts_1 = require("@codiac.io/codiac-common/contracts");
const jwt_lib_1 = require("@codiac.io/jwt-lib");
const inversify_1 = require("inversify");
let TokenUtils = class TokenUtils {
    constructor(logger) {
        this.logger = logger;
    }
    /** Determines whether the given token is expired.
     * @param throwOnError throws any encountered errors, including token parsing errors.
     */
    isExpired(token, throwOnError = false) {
        try {
            const exp = this.extractTokenExpiration(token);
            return (exp == undefined) || !(0, codiac_common_1.isFuture)(exp);
        }
        catch (err) {
            let newErr = new contracts_1.CodiacError(`Error determining expiry status: ${err.message || ""}`, err);
            this.logger.detail(newErr.message);
            if (throwOnError) {
                throw newErr;
            }
            else {
                return true;
            }
        }
    }
    extractTokenExpiration(token) {
        let exp;
        try {
            const encoder = new jwt_lib_1.JwtEncoder();
            const tokenObj = encoder.tryDecode(token);
            const CLAIM_KEY_FOR_EXPIRATION = "exp"; // this is just a standard claim so it ought to come from the jwt lib
            const rawExp = tokenObj === null || tokenObj === void 0 ? void 0 : tokenObj.payload[CLAIM_KEY_FOR_EXPIRATION];
            if (rawExp) {
                try {
                    exp = new Date(rawExp);
                    this.logger.trace(`Exracted token expiration (utc): ${(exp === null || exp === void 0 ? void 0 : exp.toUTCString()) || "undefined"}`);
                }
                catch (_a) {
                    this.logger.trace(`Invalid date format in token expiration: [${rawExp}]`);
                }
            }
            return exp;
        }
        catch (err) {
            throw new Error(`Error parsing expiration from token: ${err.message}`);
        }
    }
    extractRoles(token) {
        var _a;
        let roles;
        try {
            const encoder = new jwt_lib_1.JwtEncoder();
            const tokenObj = encoder.tryDecode(token);
            const CLAIM_KEY_FOR_ROLES = "roles"; // this is just a standard claim so it ought to come from the jwt lib
            const rawValue = tokenObj === null || tokenObj === void 0 ? void 0 : tokenObj.payload[CLAIM_KEY_FOR_ROLES];
            if (rawValue) {
                try {
                    roles = rawValue.split(",");
                    this.logger.trace(`Extracted roles: ${rawValue || "undefined"}`);
                }
                catch (err) {
                    this.logger.trace(`Error parsing roles from claim: [${(_a = err.message) !== null && _a !== void 0 ? _a : "no details available"}]`);
                }
            }
            return roles;
        }
        catch (err) {
            throw new Error(`Error parsing expiration from token: ${err.message}`);
        }
    }
};
TokenUtils = tslib_1.__decorate([
    (0, inversify_1.injectable)(),
    tslib_1.__param(0, (0, inversify_1.inject)(codiac_common_1.INTERFACES.IBetterLogger)),
    tslib_1.__metadata("design:paramtypes", [Object])
], TokenUtils);
exports.TokenUtils = TokenUtils;
//# sourceMappingURL=token-utils.js.map