Skip to content

Commit 05f0fc8

Browse files
dtodtJordan-Nelson
andauthored
fix(auth): forget local device only if matches (#4060)
* fix(auth): forget local device only if matches * chore: add unit tests for remove local device --------- Co-authored-by: Jordan Nelson <nejrd@amazon.com> Co-authored-by: Jordan Nelson <Jordanryannelson@gmail.com>
1 parent ec2ddbf commit 05f0fc8

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,9 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
981981
if (deviceKey == null) {
982982
throw const DeviceNotTrackedException();
983983
}
984-
await _deviceRepo.remove(username);
984+
if (device == null || device.id == deviceSecrets?.deviceKey) {
985+
await _deviceRepo.remove(username);
986+
}
985987
await _cognitoIdp
986988
.forgetDevice(
987989
cognito.ForgetDeviceRequest(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
5+
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
6+
import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart';
7+
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
8+
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
9+
import 'package:amplify_auth_cognito_test/common/mock_clients.dart';
10+
import 'package:amplify_auth_cognito_test/common/mock_config.dart';
11+
import 'package:amplify_auth_cognito_test/common/mock_secure_storage.dart';
12+
import 'package:amplify_core/amplify_core.dart';
13+
import 'package:test/test.dart';
14+
15+
void main() {
16+
AmplifyLogger().logLevel = LogLevel.verbose;
17+
18+
final userPoolKeys = CognitoUserPoolKeys(userPoolConfig);
19+
final identityPoolKeys = CognitoIdentityPoolKeys(identityPoolConfig);
20+
final testAuthRepo = AmplifyAuthProviderRepository();
21+
22+
late DeviceMetadataRepository repo;
23+
late AmplifyAuthCognitoDart plugin;
24+
late CognitoAuthStateMachine stateMachine;
25+
late MockSecureStorage secureStorage;
26+
27+
group('forgetDevice', () {
28+
setUp(() async {
29+
secureStorage = MockSecureStorage();
30+
seedStorage(
31+
secureStorage,
32+
userPoolKeys: userPoolKeys,
33+
identityPoolKeys: identityPoolKeys,
34+
deviceKeys: CognitoDeviceKeys(userPoolConfig, username),
35+
);
36+
plugin = AmplifyAuthCognitoDart(
37+
secureStorageFactory: (_) => secureStorage,
38+
);
39+
stateMachine = plugin.stateMachine;
40+
await plugin.configure(
41+
config: mockConfig,
42+
authProviderRepo: testAuthRepo,
43+
);
44+
final mockIdp = MockCognitoIdentityProviderClient(
45+
forgetDevice: () async {},
46+
);
47+
stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
48+
repo = stateMachine.getOrCreate<DeviceMetadataRepository>();
49+
});
50+
51+
test('should remove the local device when called with no device ID',
52+
() async {
53+
expect(await repo.get(username), isNotNull);
54+
await plugin.forgetDevice();
55+
expect(await repo.get(username), isNull);
56+
});
57+
58+
test(
59+
'should remove the local device when the device ID matches the local device ID',
60+
() async {
61+
expect(await repo.get(username), isNotNull);
62+
await plugin.forgetDevice(const CognitoDevice(id: deviceKey));
63+
expect(await repo.get(username), isNull);
64+
});
65+
66+
test(
67+
'should not remove the local device when the device ID does not match the local device ID',
68+
() async {
69+
expect(await repo.get(username), isNotNull);
70+
await plugin.forgetDevice(const CognitoDevice(id: 'other-device-id'));
71+
expect(await repo.get(username), isNotNull);
72+
});
73+
74+
tearDown(() async {
75+
await plugin.close();
76+
});
77+
});
78+
}

0 commit comments

Comments
 (0)