@openpass/openpass-js-sdk
Version:
OpenPass SSO JavaScript SDK
181 lines • 8.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenPassApiClient = void 0;
const path_1 = require("../utils/path");
const config_1 = require("../../config");
const fetch_1 = require("../utils/fetch");
const codes_1 = require("../error/codes");
const errors_1 = require("../error/errors");
const fetch_2 = require("../utils/fetch");
const idTokenJwtDecode_1 = require("../utils/idTokenJwtDecode");
const package_json_1 = require("../../../package.json");
const constants_1 = require("../constants");
/**
* Holds all methods that call the OpenPass API
*/
class OpenPassApiClient {
constructor(options) {
this.options = options;
this.validateOptions(options);
}
async exchangeAuthCodeForTokens(authCode, authSession) {
var _a, _b, _c;
const reqBody = {
grant_type: (0, config_1.getParamGrantTypeValue)(),
client_id: authSession.clientId,
redirect_uri: authSession.redirectUrl,
code: authCode,
code_verifier: authSession.codeVerifier,
};
const headers = {};
headers[constants_1.HEADER_SDK_NAME] = constants_1.SDK_NAME;
headers[constants_1.HEADER_SDK_VERSION] = package_json_1.version;
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
const response = await (0, fetch_1.fetchRequest)(this.resolveUri(config_1.config.SSO_TOKEN_PATH), {
method: "POST",
headers: headers,
body: (0, fetch_2.createFormBody)(reqBody),
timeout: (0, config_1.getApiDefaultTimeoutMs)(),
});
const responseJson = response.json;
if (this.isErrorResponse(responseJson)) {
throw new errors_1.AuthError((_a = responseJson.error) !== null && _a !== void 0 ? _a : codes_1.ERROR_CODE_OIDC_ID_TOKEN_REQUEST_FAILED, (_b = responseJson.error_description) !== null && _b !== void 0 ? _b : "Error retrieving token", (_c = responseJson.error_uri) !== null && _c !== void 0 ? _c : "", authSession.clientState);
}
const rawIdToken = responseJson.id_token;
const idToken = (0, idTokenJwtDecode_1.decodeIdTokenJwt)(rawIdToken);
// this should not typically happen, but just in case...
if (!idToken) {
throw new errors_1.AuthError(codes_1.ERROR_CODE_JWT_DECODE, "Unable to decode jwt", "", authSession.clientState);
}
const accessToken = responseJson.access_token;
const refreshToken = responseJson.refresh_token;
// this should not typically happen, but just in case...
if (!accessToken) {
throw new errors_1.AuthError(codes_1.ERROR_CODE_NO_ACCESS_TOKEN, "No access token was returned", "", authSession.clientState);
}
return {
idToken,
rawIdToken,
accessToken,
refreshToken,
rawAccessToken: accessToken,
tokenType: responseJson.token_type,
expiresIn: responseJson.expires_in,
};
}
async authorizeDevice(clientId, loginHint, disableLoginHintEditing) {
var _a, _b, _c;
const reqBody = {
scope: "openid",
client_id: clientId,
};
if (loginHint) {
reqBody.login_hint = loginHint;
}
if (disableLoginHintEditing) {
reqBody.disable_login_hint_editing = disableLoginHintEditing;
}
const headers = {};
headers[constants_1.HEADER_SDK_NAME] = constants_1.SDK_NAME;
headers[constants_1.HEADER_SDK_VERSION] = package_json_1.version;
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
const response = await (0, fetch_1.fetchRequest)(this.resolveUri(config_1.config.SSO_AUTHORIZE_DEVICE_PATH), {
method: "POST",
headers: headers,
body: (0, fetch_2.createFormBody)(reqBody),
timeout: (0, config_1.getApiDefaultTimeoutMs)(),
});
const responseJson = response.json;
if (this.isErrorResponse(responseJson)) {
throw new errors_1.AuthError((_a = responseJson.error) !== null && _a !== void 0 ? _a : codes_1.ERROR_CODE_AUTHORIZE_DEVICE_REQUEST_FAILED, (_b = responseJson.error_description) !== null && _b !== void 0 ? _b : "Error authorizing device", (_c = responseJson.error_uri) !== null && _c !== void 0 ? _c : "");
}
return responseJson;
}
async deviceToken(clientId, deviceCode) {
var _a, _b, _c;
const reqBody = {
client_id: clientId,
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
device_code: deviceCode,
};
const headers = {};
headers[constants_1.HEADER_SDK_NAME] = constants_1.SDK_NAME;
headers[constants_1.HEADER_SDK_VERSION] = package_json_1.version;
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
const response = await (0, fetch_1.fetchRequest)(this.resolveUri(config_1.config.SSO_DEVICE_TOKEN_PATH), {
method: "POST",
headers: headers,
body: (0, fetch_2.createFormBody)(reqBody),
timeout: (0, config_1.getApiDefaultTimeoutMs)(),
});
const responseJson = response.json;
if (this.isErrorResponse(responseJson)) {
if (responseJson.error === "authorization_pending") {
return {
status: "authorization_pending",
};
}
else if (responseJson.error === "slow_down") {
return {
status: "slow_down",
};
}
else {
throw new errors_1.AuthError((_a = responseJson.error) !== null && _a !== void 0 ? _a : codes_1.ERROR_CODE_DEVICE_TOKEN_REQUEST_FAILED, (_b = responseJson.error_description) !== null && _b !== void 0 ? _b : "Error getting device token", (_c = responseJson.error_uri) !== null && _c !== void 0 ? _c : "");
}
}
return {
status: "ok",
tokensResponse: responseJson,
};
}
async sendClientTelemetryEvent(eventType) {
const headers = {};
headers[constants_1.HEADER_SDK_NAME] = constants_1.SDK_NAME;
headers[constants_1.HEADER_SDK_VERSION] = package_json_1.version;
headers["Content-Type"] = "application/json";
const payload = {
client_id: this.options.clientId,
event_type: eventType,
};
await (0, fetch_1.fetchRequest)(this.resolveUri(config_1.config.SSO_CLIENT_TELEMETRY_EVENT_PATH), {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
timeout: (0, config_1.getApiDefaultTimeoutMs)(),
});
}
async sendSdkTelemetryEvent(eventType, eventName, message, stackTrace) {
const headers = {};
headers[constants_1.HEADER_SDK_NAME] = constants_1.SDK_NAME;
headers[constants_1.HEADER_SDK_VERSION] = package_json_1.version;
headers["Content-Type"] = "application/json";
const payload = {
client_id: this.options.clientId,
event_type: eventType,
event_name: eventName,
message: message,
stack_trace: stackTrace,
};
await (0, fetch_1.fetchRequest)(this.resolveUri(config_1.config.SSO_SDK_TELEMETRY_EVENT_PATH), {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
timeout: (0, config_1.getApiDefaultTimeoutMs)(),
});
}
resolveUri(uri) {
const baseUri = this.options.baseUrl || config_1.config.SSO_BASE_URL;
return (0, path_1.joinPaths)([baseUri, uri]);
}
isErrorResponse(response) {
return response.error !== undefined;
}
validateOptions(options) {
if (!options.clientId) {
throw new errors_1.SdkError("Error clientId is invalid. Please use a valid clientId");
}
}
}
exports.OpenPassApiClient = OpenPassApiClient;
//# sourceMappingURL=openPassApiClient.js.map