"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthorizationManager = exports.RevokeAccessTokenHeaders = exports.RefreshAccessTokenHeaders = exports.RequestAccessTokenHeaders = exports.AuthorizeUserHeaders = exports.RevokeAccessTokenOptionals = exports.RefreshAccessTokenOptionals = exports.RequestAccessTokenOptionals = exports.AuthorizeUserOptionals = void 0;
exports.serializeAuthorizeUserQueryParamsResponseTypeField = serializeAuthorizeUserQueryParamsResponseTypeField;
exports.deserializeAuthorizeUserQueryParamsResponseTypeField = deserializeAuthorizeUserQueryParamsResponseTypeField;
const accessToken_1 = require("../schemas/accessToken");
const postOAuth2Token_1 = require("../schemas/postOAuth2Token");
const postOAuth2TokenRefreshAccessToken_1 = require("../schemas/postOAuth2TokenRefreshAccessToken");
const postOAuth2Revoke_1 = require("../schemas/postOAuth2Revoke");
const postOAuth2TokenRefreshAccessToken_2 = require("../schemas/postOAuth2TokenRefreshAccessToken");
const errors_1 = require("../box/errors");
const network_1 = require("../networking/network");
const fetchOptions_1 = require("../networking/fetchOptions");
const utils_1 = require("../internal/utils");
const utils_2 = require("../internal/utils");
const json_1 = require("../serialization/json");
class AuthorizeUserOptionals {
    headers = new AuthorizeUserHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.AuthorizeUserOptionals = AuthorizeUserOptionals;
class RequestAccessTokenOptionals {
    headers = new RequestAccessTokenHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.RequestAccessTokenOptionals = RequestAccessTokenOptionals;
class RefreshAccessTokenOptionals {
    headers = new RefreshAccessTokenHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.RefreshAccessTokenOptionals = RefreshAccessTokenOptionals;
class RevokeAccessTokenOptionals {
    headers = new RevokeAccessTokenHeaders({});
    cancellationToken = void 0;
    constructor(fields) {
        if (fields.headers !== undefined) {
            this.headers = fields.headers;
        }
        if (fields.cancellationToken !== undefined) {
            this.cancellationToken = fields.cancellationToken;
        }
    }
}
exports.RevokeAccessTokenOptionals = RevokeAccessTokenOptionals;
class AuthorizeUserHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.AuthorizeUserHeaders = AuthorizeUserHeaders;
class RequestAccessTokenHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.RequestAccessTokenHeaders = RequestAccessTokenHeaders;
class RefreshAccessTokenHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.RefreshAccessTokenHeaders = RefreshAccessTokenHeaders;
class RevokeAccessTokenHeaders {
    /**
     * Extra headers that will be included in the HTTP request. */
    extraHeaders = {};
    constructor(fields) {
        if (fields.extraHeaders !== undefined) {
            this.extraHeaders = fields.extraHeaders;
        }
    }
}
exports.RevokeAccessTokenHeaders = RevokeAccessTokenHeaders;
class AuthorizationManager {
    auth;
    networkSession = new network_1.NetworkSession({});
    constructor(fields) {
        if (fields.auth !== undefined) {
            this.auth = fields.auth;
        }
        if (fields.networkSession !== undefined) {
            this.networkSession = fields.networkSession;
        }
    }
    /**
     * Authorize a user by sending them through the [Box](https://box.com)
     * website and request their permission to act on their behalf.
     *
     * This is the first step when authenticating a user using
     * OAuth 2.0. To request a user's authorization to use the Box APIs
     * on their behalf you will need to send a user to the URL with this
     * format.
     * @param {AuthorizeUserQueryParams} queryParams Query parameters of authorizeUser method
     * @param {AuthorizeUserOptionalsInput} optionalsInput
     * @returns {Promise<undefined>}
     */
    async authorizeUser(queryParams, optionalsInput = {}) {
        const optionals = new AuthorizeUserOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const queryParamsMap = (0, utils_1.prepareParams)({
            ['response_type']: (0, utils_2.toString)(queryParams.responseType),
            ['client_id']: (0, utils_2.toString)(queryParams.clientId),
            ['redirect_uri']: (0, utils_2.toString)(queryParams.redirectUri),
            ['state']: (0, utils_2.toString)(queryParams.state),
            ['scope']: (0, utils_2.toString)(queryParams.scope),
        });
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.oauth2Url, '/authorize'),
            method: 'GET',
            params: queryParamsMap,
            headers: headersMap,
            responseFormat: 'no_content',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return void 0;
    }
    /**
     * Request an Access Token using either a client-side obtained OAuth 2.0
     * authorization code or a server-side JWT assertion.
     *
     * An Access Token is a string that enables Box to verify that a
     * request belongs to an authorized session. In the normal order of
     * operations you will begin by requesting authentication from the
     * [authorize](#get-authorize) endpoint and Box will send you an
     * authorization code.
     *
     * You will then send this code to this endpoint to exchange it for
     * an Access Token. The returned Access Token can then be used to to make
     * Box API calls.
     * @param {PostOAuth2Token} requestBody Request body of requestAccessToken method
     * @param {RequestAccessTokenOptionalsInput} optionalsInput
     * @returns {Promise<AccessToken>}
     */
    async requestAccessToken(requestBody, optionalsInput = {}) {
        const optionals = new RequestAccessTokenOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.baseUrl, '/oauth2/token'),
            method: 'POST',
            headers: headersMap,
            data: (0, postOAuth2Token_1.serializePostOAuth2Token)(requestBody),
            contentType: 'application/x-www-form-urlencoded',
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, accessToken_1.deserializeAccessToken)(response.data),
            rawData: response.data,
        };
    }
    /**
     * Refresh an Access Token using its client ID, secret, and refresh token.
     * @param {PostOAuth2TokenRefreshAccessTokenInput} requestBodyInput Request body of refreshAccessToken method
     * @param {RefreshAccessTokenOptionalsInput} optionalsInput
     * @returns {Promise<AccessToken>}
     */
    async refreshAccessToken(requestBodyInput, optionalsInput = {}) {
        const requestBody = new postOAuth2TokenRefreshAccessToken_2.PostOAuth2TokenRefreshAccessToken({
            grantType: requestBodyInput.grantType,
            clientId: requestBodyInput.clientId,
            clientSecret: requestBodyInput.clientSecret,
            refreshToken: requestBodyInput.refreshToken,
        });
        const optionals = new RefreshAccessTokenOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.baseUrl, '/oauth2/token#refresh'),
            method: 'POST',
            headers: headersMap,
            data: (0, postOAuth2TokenRefreshAccessToken_1.serializePostOAuth2TokenRefreshAccessToken)(requestBody),
            contentType: 'application/x-www-form-urlencoded',
            responseFormat: 'json',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return {
            ...(0, accessToken_1.deserializeAccessToken)(response.data),
            rawData: response.data,
        };
    }
    /**
     * Revoke an active Access Token, effectively logging a user out
     * that has been previously authenticated.
     * @param {PostOAuth2Revoke} requestBody Request body of revokeAccessToken method
     * @param {RevokeAccessTokenOptionalsInput} optionalsInput
     * @returns {Promise<undefined>}
     */
    async revokeAccessToken(requestBody, optionalsInput = {}) {
        const optionals = new RevokeAccessTokenOptionals({
            headers: optionalsInput.headers,
            cancellationToken: optionalsInput.cancellationToken,
        });
        const headers = optionals.headers;
        const cancellationToken = optionals.cancellationToken;
        const headersMap = (0, utils_1.prepareParams)({ ...{}, ...headers.extraHeaders });
        const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
            url: ''.concat(this.networkSession.baseUrls.baseUrl, '/oauth2/revoke'),
            method: 'POST',
            headers: headersMap,
            data: (0, postOAuth2Revoke_1.serializePostOAuth2Revoke)(requestBody),
            contentType: 'application/x-www-form-urlencoded',
            responseFormat: 'no_content',
            auth: this.auth,
            networkSession: this.networkSession,
            cancellationToken: cancellationToken,
        }));
        return void 0;
    }
}
exports.AuthorizationManager = AuthorizationManager;
function serializeAuthorizeUserQueryParamsResponseTypeField(val) {
    return val;
}
function deserializeAuthorizeUserQueryParamsResponseTypeField(val) {
    if (val == 'code') {
        return val;
    }
    if ((0, json_1.sdIsString)(val)) {
        return val;
    }
    throw new errors_1.BoxSdkError({
        message: "Can't deserialize AuthorizeUserQueryParamsResponseTypeField",
    });
}
//# sourceMappingURL=authorization.js.map