"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IccAuthApi = exports.DigitalIdProvider = exports.OAuthThirdParty = void 0;
/**
 * iCure Data Stack API Documentation
 * The iCure Data Stack Application API is the native interface to iCure. This version is obsolete, please use v2.
 *
 * OpenAPI spec version: v1
 *
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */
const XHR_1 = require("./XHR");
const AuthenticationResponse_1 = require("../model/AuthenticationResponse");
const AuthenticationProvider_1 = require("../../icc-x-api/auth/AuthenticationProvider");
const IccRestApiPath_1 = require("./IccRestApiPath");
var OAuthThirdParty;
(function (OAuthThirdParty) {
    OAuthThirdParty["GOOGLE"] = "google";
    OAuthThirdParty["MICROSOFT"] = "microsoft";
    OAuthThirdParty["APPLE"] = "apple";
    OAuthThirdParty["LINKEDIN"] = "linkedin";
    OAuthThirdParty["GITHUB"] = "github";
})(OAuthThirdParty || (exports.OAuthThirdParty = OAuthThirdParty = {}));
var DigitalIdProvider;
(function (DigitalIdProvider) {
    DigitalIdProvider["BE_FAS"] = "be.fas";
})(DigitalIdProvider || (exports.DigitalIdProvider = DigitalIdProvider = {}));
class IccAuthApi {
    constructor(host, headers, authenticationProvider, fetchImpl) {
        this.host = (0, IccRestApiPath_1.iccRestApiPath)(host);
        this.headers = Object.keys(headers).map((k) => new XHR_1.XHR.Header(k, headers[k]));
        this.authenticationProvider = !!authenticationProvider ? authenticationProvider : new AuthenticationProvider_1.NoAuthenticationProvider();
        this.fetchImpl = fetchImpl;
    }
    setHeaders(h) {
        this.headers = h;
    }
    handleError(e) {
        throw e;
    }
    /**
     * Check login using groupId/userId and password
     * @summary check
     * @param body
     */
    check(body) {
        let _body = null;
        _body = body;
        const _url = this.host + `/auth/check` + '?ts=' + new Date().getTime();
        let headers = this.headers;
        headers = headers
            .filter((h) => { var _a; return h.header !== 'Content-Type' && ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; })
            .concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
        return XHR_1.XHR.sendCommand('POST', _url, headers, _body, this.fetchImpl)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Login using username and password
     * @summary login
     * @param body
     * @param groupId the group id where to login to.
     * @param duration the duration of the returned JWT authentication token, in seconds.
     */
    login(body, groupId, duration) {
        let _body = null;
        _body = body;
        const _url = this.host +
            `/auth/login` +
            '?ts=' +
            new Date().getTime() +
            (groupId ? `&groupId=${encodeURIComponent(String(groupId))}` : '') +
            (!!duration ? `&duration=${duration}` : '');
        let headers = this.headers;
        headers = headers
            .filter((h) => { var _a; return h.header !== 'Content-Type' && ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; })
            .concat(new XHR_1.XHR.Header('Content-Type', 'application/json'));
        return XHR_1.XHR.sendCommand('POST', _url, headers, _body, this.fetchImpl, undefined)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Login using third party OAuth provider token
     * @summary login
     * @param thirdParty The third party OAuth service used to authenticate the user
     * @param token The token returned by the third party OAuth service
     * @param groupId the group id where to login to.
     */
    loginWithThirdPartyToken(thirdParty, token, groupId) {
        let _body = null;
        _body = token;
        const _url = this.host + `/auth/login/${thirdParty}` + '?ts=' + new Date().getTime() + (groupId ? `&groupId=${encodeURIComponent(String(groupId))}` : '');
        let headers = this.headers;
        headers = headers
            .filter((h) => { var _a; return h.header !== 'Content-Type' && ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; })
            .concat(new XHR_1.XHR.Header('Content-Type', 'application/json'))
            .concat(new XHR_1.XHR.Header('token', token));
        return XHR_1.XHR.sendCommand('POST', _url, headers, null, this.fetchImpl, undefined)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Lite only
     * /auth/
     */
    loginWithCloudToken(issuer, cloudJwt) {
        let _body = null;
        const _url = this.host + `/auth/login/icureCloud` + '?ts=' + new Date().getTime() + `&issuer=${encodeURIComponent(String(issuer))}`;
        let headers = [...this.headers, new XHR_1.XHR.Header('Cloud-Auth-Token', cloudJwt)];
        return XHR_1.XHR.sendCommand('POST', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Logout
     * @summary logout
     */
    logout() {
        let _body = null;
        const _url = this.host + `/auth/logout` + '?ts=' + new Date().getTime();
        let headers = this.headers;
        return XHR_1.XHR.sendCommand('GET', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Logout
     * @summary logout
     */
    logoutPost() {
        let _body = null;
        const _url = this.host + `/auth/logout` + '?ts=' + new Date().getTime();
        let headers = this.headers;
        return XHR_1.XHR.sendCommand('POST', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Get token for subsequent operation
     * @summary token
     * @param method
     * @param path
     */
    token(method, path) {
        let _body = null;
        const _url = this.host + `/auth/token/${encodeURIComponent(String(method))}/${encodeURIComponent(String(path))}` + '?ts=' + new Date().getTime();
        let headers = this.headers;
        return XHR_1.XHR.sendCommand('GET', _url, headers, _body, this.fetchImpl, undefined, this.authenticationProvider.getAuthService())
            .then((doc) => JSON.parse(JSON.stringify(doc.body)))
            .catch((err) => this.handleError(err));
    }
    /**
     * Gets a new authentication and refresh JWT for a different group
     */
    switchGroup(refreshJWT, groupId) {
        let _body = null;
        const _url = this.host + `/auth/switch/${encodeURIComponent(String(groupId))}` + '?ts=' + new Date().getTime();
        let headers = [...this.headers, new XHR_1.XHR.Header('Refresh-Token', refreshJWT)];
        return XHR_1.XHR.sendCommand('POST', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Gets a new authentication JWT using the refresh JWT
     * @summary refresh
     */
    refreshAuthenticationJWT(refreshJWT) {
        let _body = null;
        const _url = this.host + `/auth/refresh` + '?ts=' + new Date().getTime();
        let headers = [...this.headers, new XHR_1.XHR.Header('Refresh-Token', refreshJWT)];
        return XHR_1.XHR.sendCommand('POST', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
    /**
     * Switch groups using the refresh JWT
     * @summary switch groups
     */
    switchGroupUsingRefreshJWT(groupId) {
        return this.authenticationProvider.getIcureTokens().then((tokens) => {
            let _body = null;
            const _url = this.host + `/auth/switch/${encodeURIComponent(String(groupId))}` + '?ts=' + new Date().getTime();
            const refreshToken = tokens === null || tokens === void 0 ? void 0 : tokens.refreshToken;
            if (!refreshToken) {
                throw new Error('No refresh token found');
            }
            let headers = [...this.headers, new XHR_1.XHR.Header('Refresh-Token', refreshToken)];
            return XHR_1.XHR.sendCommand('POST', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl)
                .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
                .catch((err) => this.handleError(err));
        });
    }
    /**
     * Invalidates a refresh JWT
     * @summary invalidate
     */
    invalidateRefreshJWT(refreshJWT) {
        let _body = null;
        const _url = this.host + `/auth/invalidate` + '?ts=' + new Date().getTime();
        let headers = [...this.headers, new XHR_1.XHR.Header('Refresh-Token', refreshJWT)];
        return XHR_1.XHR.sendCommand('POST', _url, headers.filter((h) => { var _a; return ((_a = h.header) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'authorization'; }), _body, this.fetchImpl)
            .then((doc) => new AuthenticationResponse_1.AuthenticationResponse(doc.body))
            .catch((err) => this.handleError(err));
    }
}
exports.IccAuthApi = IccAuthApi;
//# sourceMappingURL=IccAuthApi.js.map