UNPKG

@openpass/openpass-js-sdk

Version:
177 lines 7.68 kB
import { joinPaths } from "../utils/path"; import { config, getParamGrantTypeValue, getApiDefaultTimeoutMs } from "../../config"; import { fetchRequest } from "../utils/fetch"; import { ERROR_CODE_JWT_DECODE, ERROR_CODE_OIDC_ID_TOKEN_REQUEST_FAILED, ERROR_CODE_NO_ACCESS_TOKEN, ERROR_CODE_AUTHORIZE_DEVICE_REQUEST_FAILED, ERROR_CODE_DEVICE_TOKEN_REQUEST_FAILED, } from "../error/codes"; import { AuthError, SdkError } from "../error/errors"; import { createFormBody } from "../utils/fetch"; import { decodeIdTokenJwt } from "../utils/idTokenJwtDecode"; import { version as sdkVersion } from "../../../package.json"; import { HEADER_SDK_NAME, HEADER_SDK_VERSION, SDK_NAME } from "../constants"; /** * Holds all methods that call the OpenPass API */ export class OpenPassApiClient { constructor(options) { this.options = options; this.validateOptions(options); } async exchangeAuthCodeForTokens(authCode, authSession) { var _a, _b, _c; const reqBody = { grant_type: getParamGrantTypeValue(), client_id: authSession.clientId, redirect_uri: authSession.redirectUrl, code: authCode, code_verifier: authSession.codeVerifier, }; const headers = {}; headers[HEADER_SDK_NAME] = SDK_NAME; headers[HEADER_SDK_VERSION] = sdkVersion; headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"; const response = await fetchRequest(this.resolveUri(config.SSO_TOKEN_PATH), { method: "POST", headers: headers, body: createFormBody(reqBody), timeout: getApiDefaultTimeoutMs(), }); const responseJson = response.json; if (this.isErrorResponse(responseJson)) { throw new AuthError((_a = responseJson.error) !== null && _a !== void 0 ? _a : 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 = decodeIdTokenJwt(rawIdToken); // this should not typically happen, but just in case... if (!idToken) { throw new AuthError(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 AuthError(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[HEADER_SDK_NAME] = SDK_NAME; headers[HEADER_SDK_VERSION] = sdkVersion; headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"; const response = await fetchRequest(this.resolveUri(config.SSO_AUTHORIZE_DEVICE_PATH), { method: "POST", headers: headers, body: createFormBody(reqBody), timeout: getApiDefaultTimeoutMs(), }); const responseJson = response.json; if (this.isErrorResponse(responseJson)) { throw new AuthError((_a = responseJson.error) !== null && _a !== void 0 ? _a : 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[HEADER_SDK_NAME] = SDK_NAME; headers[HEADER_SDK_VERSION] = sdkVersion; headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"; const response = await fetchRequest(this.resolveUri(config.SSO_DEVICE_TOKEN_PATH), { method: "POST", headers: headers, body: createFormBody(reqBody), timeout: 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 AuthError((_a = responseJson.error) !== null && _a !== void 0 ? _a : 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[HEADER_SDK_NAME] = SDK_NAME; headers[HEADER_SDK_VERSION] = sdkVersion; headers["Content-Type"] = "application/json"; const payload = { client_id: this.options.clientId, event_type: eventType, }; await fetchRequest(this.resolveUri(config.SSO_CLIENT_TELEMETRY_EVENT_PATH), { method: "POST", headers: headers, body: JSON.stringify(payload), timeout: getApiDefaultTimeoutMs(), }); } async sendSdkTelemetryEvent(eventType, eventName, message, stackTrace) { const headers = {}; headers[HEADER_SDK_NAME] = SDK_NAME; headers[HEADER_SDK_VERSION] = sdkVersion; headers["Content-Type"] = "application/json"; const payload = { client_id: this.options.clientId, event_type: eventType, event_name: eventName, message: message, stack_trace: stackTrace, }; await fetchRequest(this.resolveUri(config.SSO_SDK_TELEMETRY_EVENT_PATH), { method: "POST", headers: headers, body: JSON.stringify(payload), timeout: getApiDefaultTimeoutMs(), }); } resolveUri(uri) { const baseUri = this.options.baseUrl || config.SSO_BASE_URL; return joinPaths([baseUri, uri]); } isErrorResponse(response) { return response.error !== undefined; } validateOptions(options) { if (!options.clientId) { throw new SdkError("Error clientId is invalid. Please use a valid clientId"); } } } //# sourceMappingURL=openPassApiClient.js.map