Skip to content

Commit

Permalink
feat(auth): add a default deviceName when remembering device
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Kumar committed Feb 17, 2024
1 parent 5255143 commit fad7499
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/auth/src/providers/cognito/apis/fetchDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ const parseDevicesResponse = async (
DeviceLastModifiedDate,
DeviceLastAuthenticatedDate,
}) => {
let deviceName: string | undefined;
const attributes = DeviceAttributes.reduce(
(attrs: any, { Name, Value }) => {
if (Name && Value) {
if (Name === 'device_name') deviceName = Value;
attrs[Name] = Value;
}
return attrs;
Expand All @@ -68,6 +70,7 @@ const parseDevicesResponse = async (
);
return {
id,
name: deviceName,
attributes,
createDate: DeviceCreateDate
? new Date(DeviceCreateDate * 1000)
Expand Down
2 changes: 2 additions & 0 deletions packages/auth/src/providers/cognito/utils/signInHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
assertTokenProviderConfig,
base64Encoder,
AmplifyUrl,
getDeviceName
} from '@aws-amplify/core/internals/utils';
import { AuthenticationHelper } from './srp/AuthenticationHelper';
import { BigInteger } from './srp/BigInteger';
Expand Down Expand Up @@ -1060,6 +1061,7 @@ export async function getNewDeviceMetatada(
{ region: getRegion(userPoolId) },
{
AccessToken: accessToken,
DeviceName: await getDeviceName(),
DeviceKey: newDeviceMetadata?.DeviceKey,
DeviceSecretVerifierConfig: deviceSecretVerifierConfig,
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/libraryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { LegacyConfig } from './singleton/types';
export { ADD_OAUTH_LISTENER } from './singleton/constants';
export { amplifyUuid } from './utils/amplifyUuid';
export { AmplifyUrl, AmplifyUrlSearchParams } from './utils/amplifyUrl';
export { getDeviceName } from './utils/deviceName';

// Auth utilities
export {
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/utils/deviceName/getDeviceName.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getDeviceName as getDeviceNameNative } from '@aws-amplify/react-native';

/**
* Retrieves the device name using name in ios and model in android,
*
* @returns {Promise<string>} A promise that resolves with a string representing the device name.
*
* Example Output:
* ios: 'iPhone' / 'user's iPhone'
* android: 'sdk_gphone64_arm64'
*/
export const getDeviceName = async (): Promise<string> => getDeviceNameNative();
53 changes: 53 additions & 0 deletions packages/core/src/utils/deviceName/getDeviceName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/**
* Retrieves the device name using the User-Agent Client Hints API if available,
* falling back to the traditional userAgent string if not.
*
* @returns {Promise<string>} A promise that resolves with a string representing the device name.
*
* Example Output:
* navigator.userAgentData:
* 'macOS 14.2.1 arm macOS Not A(Brand/99.0.0.0;Google Chrome/121.0.6167.160;Chromium/121.0.6167.160'
* navigator.userAgent:
* 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0'
*/
export const getDeviceName = async (): Promise<string> => {
// userAgentData is an experimental API and hence isn't typed
// @ts-expect-error

Check failure on line 18 in packages/core/src/utils/deviceName/getDeviceName.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/core

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
if (navigator.userAgentData) {
const {
platform = '',
platformVersion = '',
architecture = '',
model = '',
fullVersionList = '',
// @ts-expect-error

Check failure on line 26 in packages/core/src/utils/deviceName/getDeviceName.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/core

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
} = await navigator.userAgentData.getHighEntropyValues([
'platform',
'platformVersion',
'architecture',
'model',
'fullVersionList',
]);

const versionList = fullVersionList
.filter((v: { brand: string }) => v.brand !== 'Not_A Brand')
.map((v: { brand: string; version: string }) => `${v.brand}/${v.version}`)
.join(';');

const deviceName = [
platform,
platformVersion,
architecture,
model,
platform,
versionList,
]
.filter(value => value)
.join(' ');
return deviceName;

Check failure on line 50 in packages/core/src/utils/deviceName/getDeviceName.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/core

Expected blank line before this statement
}
return navigator.userAgent;

Check failure on line 52 in packages/core/src/utils/deviceName/getDeviceName.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/core

Expected blank line before this statement
};
4 changes: 4 additions & 0 deletions packages/core/src/utils/deviceName/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export { getDeviceName } from './getDeviceName';

0 comments on commit fad7499

Please sign in to comment.