"use strict";
(() => {
const status = document.getElementById("oauth-status");
const query = new URLSearchParams(window.location.search);
const fragment = new URLSearchParams(window.location.hash.slice(1));
const fields = ["state", "code", "access_token", "token_type", "error", "error_description"];
const queryResponse = fields.some((name) => query.has(name));
const fragmentResponse = fields.some((name) => fragment.has(name));
const parameters = fragmentResponse ? fragment : query;
window.history.replaceState(null, "", window.location.pathname);
const single = (name) => parameters.getAll(name).length === 1 ? parameters.get(name) : null;
const duplicate = fields.some((name) => parameters.getAll(name).length > 1);
const state = single("state");
const code = single("code");
const token = single("access_token");
const tokenType = single("token_type");
const error = single("error");
let message;
if (!(queryResponse && fragmentResponse) && !duplicate && state && state.length <= 512) {
if (error && !code && !token) {
message = { state, error, error_description: single("error_description") || "Authorization was denied" };
} else if (code && !token && !error) {
message = { state, code, responseType: "code" };
} else if (token && tokenType && !code && !error) {
message = { state, access_token: token, token_type: tokenType, responseType: "token" };
}
}
if (!message || !window.opener) {
status.textContent = "Unable to complete authorization. Return to the documentation and try again.";
return;
}
window.opener.postMessage(message, window.location.origin);
status.textContent = "Authorization response sent. You may close this window.";
})();