UNPKG

@aws-amplify/auth

Version:
64 lines (62 loc) 2.75 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.getNewDeviceMetadata = getNewDeviceMetadata; const utils_1 = require("@aws-amplify/core/internals/utils"); const cognitoIdentityProvider_1 = require("../../../foundation/factories/serviceClients/cognitoIdentityProvider"); const factories_1 = require("../factories"); const parsers_1 = require("../../../foundation/parsers"); const srp_1 = require("./srp"); /** * This function is used to kick off the device management flow. * * If an error is thrown while generating a hash device or calling the `ConfirmDevice` * client, then this API will ignore the error and return undefined. Otherwise the authentication * flow will not complete and the user won't be able to be signed in. * * @returns DeviceMetadata | undefined */ async function getNewDeviceMetadata({ userPoolId, userPoolEndpoint, newDeviceMetadata, accessToken, }) { if (!newDeviceMetadata) return undefined; const userPoolName = userPoolId.split('_')[1] || ''; const authenticationHelper = await (0, srp_1.getAuthenticationHelper)(userPoolName); const deviceKey = newDeviceMetadata?.DeviceKey; const deviceGroupKey = newDeviceMetadata?.DeviceGroupKey; try { await authenticationHelper.generateHashDevice(deviceGroupKey ?? '', deviceKey ?? ''); } catch (errGenHash) { // TODO: log error here return undefined; } const deviceSecretVerifierConfig = { Salt: utils_1.base64Encoder.convert((0, srp_1.getBytesFromHex)(authenticationHelper.getSaltToHashDevices())), PasswordVerifier: utils_1.base64Encoder.convert((0, srp_1.getBytesFromHex)(authenticationHelper.getVerifierDevices())), }; const randomPassword = authenticationHelper.getRandomPassword(); try { const confirmDevice = (0, cognitoIdentityProvider_1.createConfirmDeviceClient)({ endpointResolver: (0, factories_1.createCognitoUserPoolEndpointResolver)({ endpointOverride: userPoolEndpoint, }), }); await confirmDevice({ region: (0, parsers_1.getRegionFromUserPoolId)(userPoolId) }, { AccessToken: accessToken, DeviceName: await (0, utils_1.getDeviceName)(), DeviceKey: newDeviceMetadata?.DeviceKey, DeviceSecretVerifierConfig: deviceSecretVerifierConfig, }); return { deviceKey, deviceGroupKey, randomPassword, }; } catch (error) { // TODO: log error here return undefined; } } //# sourceMappingURL=getNewDeviceMetadata.js.map