UNPKG

@openpass/openpass-js-sdk

Version:
53 lines 2.68 kB
import { decodeIdTokenJwt } from "./utils/idTokenJwtDecode"; export default class DeviceAuthGrant { /** * Constructs a new instance of the DeviceAuthGrant class. * @param OpenPassOptions - The options for OpenPass. * @param openPassAuthClient - The client for OpenPass API. */ constructor(openPassOptions, openPassAuthClient) { this.openPassOptions = openPassOptions; this.openPassApiClient = openPassAuthClient; } /** * Authorizes a device via the device authorization grant flow. * @param options - The options for device authorization. * @returns A promise that resolves to the data of the authorized device. */ async authorizeDevice(options) { const authorizeDeviceResponse = await this.openPassApiClient.authorizeDevice(this.openPassOptions.clientId, options.loginHint, options.disableLoginHintEditing); return { deviceCode: authorizeDeviceResponse.device_code, userCode: authorizeDeviceResponse.user_code, verificationUri: authorizeDeviceResponse.verification_uri, verificationUriComplete: authorizeDeviceResponse.verification_uri_complete, expiresIn: authorizeDeviceResponse.expires_in, interval: authorizeDeviceResponse.interval, }; } /** * Retrieves the device token for the provided device code, or a status if the authentication is still pending. * @param deviceCode - The code of the device (which is returned by the authorizeDevice method) * @returns A promise that resolves to the device token with its status. */ async deviceToken(deviceCode) { const deviceTokenResponse = await this.openPassApiClient.deviceToken(this.openPassOptions.clientId, deviceCode); const deviceTokenWithStatus = { status: deviceTokenResponse.status, }; if (deviceTokenResponse.tokensResponse) { const idToken = decodeIdTokenJwt(deviceTokenResponse.tokensResponse.id_token); deviceTokenWithStatus.tokens = { idToken: idToken, rawIdToken: deviceTokenResponse.tokensResponse.id_token, accessToken: deviceTokenResponse.tokensResponse.access_token, rawAccessToken: deviceTokenResponse.tokensResponse.access_token, refreshToken: deviceTokenResponse.tokensResponse.refresh_token, tokenType: deviceTokenResponse.tokensResponse.token_type, expiresIn: deviceTokenResponse.tokensResponse.expires_in, }; } return deviceTokenWithStatus; } } //# sourceMappingURL=deviceAuthGrant.js.map