@arc-publishing/sdk-identity
Version:
JS Identity SDK for working with Identity API
90 lines • 4.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isOIDCToken = void 0;
var tslib_1 = require("tslib");
var constants_1 = require("./constants");
var identity_1 = tslib_1.__importDefault(require("./identity"));
var userIdentity_1 = require("./userIdentity");
var JSONResponseHandler_1 = tslib_1.__importDefault(require("../serviceHelpers/JSONResponseHandler"));
function isOIDCToken(object) {
return object.access_token !== undefined;
}
exports.isOIDCToken = isOIDCToken;
function signInWithOIDCPKCE(_a) {
var client_id = _a.client_id, code_verifier = _a.code_verifier, redirect_uri = _a.redirect_uri, code = _a.code;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var headers, formData, response, identityTokens, userIdentity, err_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
headers = tslib_1.__assign({}, constants_1.headers);
if (!(identity_1.default.userIdentity &&
identity_1.default.userIdentity.accessToken &&
identity_1.default.userIdentity.refreshToken)) return [3, 2];
return [4, identity_1.default.heartbeat()];
case 1:
_b.sent();
headers['Authorization'] = "Bearer ".concat(identity_1.default.userIdentity.accessToken);
_b.label = 2;
case 2:
formData = new FormData();
formData.append('grant_type', 'authorization_code');
formData.append('client_id', client_id);
formData.append('code_verifier', code_verifier);
formData.append('redirect_uri', redirect_uri);
formData.append('code', code);
_b.label = 3;
case 3:
_b.trys.push([3, 6, , 7]);
return [4, fetch("".concat(identity_1.default.apiOrigin, "/identity/public/v2/oidc/provider/token"), {
method: 'POST',
headers: new Headers(),
body: formData
}).then(JSONResponseHandler_1.default)];
case 4:
response = _b.sent();
if (isOIDCToken(response)) {
identity_1.default.userIdentity = {
accessToken: response.access_token,
refreshToken: response.refresh_token,
idToken: response.id_token,
expiresIn: response.expires_in
};
window.postMessage({ type: 'ARC_LOGIN_SUCCESS', grantType: 'OIDC' }, '*');
}
else {
throw response;
}
return [4, fetch("".concat(identity_1.default.apiOrigin, "/identity/public/v1/auth/token"), {
method: 'POST',
headers: headers,
body: JSON.stringify({
token: response.refresh_token,
grantType: 'refresh-token'
})
}).then(JSONResponseHandler_1.default)];
case 5:
identityTokens = _b.sent();
if ((0, userIdentity_1.isUserIdentity)(identityTokens)) {
userIdentity = tslib_1.__assign(tslib_1.__assign({}, identity_1.default.userIdentity), { uuid: identityTokens.uuid, accessToken: identityTokens.accessToken, refreshToken: identityTokens.refreshToken || identity_1.default.userIdentity.refreshToken });
identity_1.default.userIdentity = userIdentity;
return [2, identity_1.default.userIdentity];
}
else {
if (identityTokens.code) {
console.warn("".concat(constants_1.logPrefix, " code:\"").concat(identityTokens.code, "\", message:\"").concat(identityTokens.message, "\""));
}
identity_1.default.clearSession();
throw identityTokens;
}
return [3, 7];
case 6:
err_1 = _b.sent();
throw err_1;
case 7: return [2];
}
});
});
}
exports.default = signInWithOIDCPKCE;
//# sourceMappingURL=signInWithOIDCPKCE.js.map