Skip to content

Commit 6452ae7

Browse files
committed
chore(auth): Adds device helper method to clear devicemeta data info (#2691)
- Adds a method to clear devicemetadata from keychain, this will be used for device meta data cleanup during signIn. - Change the getDeviceMetaData property to accurately say that it is fetching deviceMetaData for a user.
1 parent 040647f commit 6452ae7

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/RefreshAuthorizationSession/UserPool/RefreshUserPoolTokens.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ struct RefreshUserPoolTokens: Action {
3434
let existingTokens = existingSignedIndata.cognitoUserPoolTokens
3535

3636
let deviceMetadata = await DeviceMetadataHelper.getDeviceMetadata(
37-
for: environment,
38-
with: existingSignedIndata.username)
37+
for: existingSignedIndata.username,
38+
with: environment)
3939

4040
let asfDeviceId = try await CognitoUserPoolASF.asfDeviceID(
4141
for: existingSignedIndata.username,

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/SignIn/DeviceSRPAuth/InitiateAuthDeviceSRP.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct InitiateAuthDeviceSRP: Action {
3030

3131
// Get device metadata
3232
let deviceMetadata = await DeviceMetadataHelper.getDeviceMetadata(
33-
for: environment,
34-
with: username)
33+
for: username,
34+
with: environment)
3535

3636
let srpStateData = SRPStateData(
3737
username: username,

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/SignIn/IntializeSignInFlow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ struct InitializeSignInFlow: Action {
3434
var deviceMetadata = DeviceMetadata.noData
3535
if let username = signInEventData.username {
3636
deviceMetadata = await DeviceMetadataHelper.getDeviceMetadata(
37-
for: environment,
38-
with: username)
37+
for: username,
38+
with: environment)
3939
}
4040

4141
let event: SignInEvent

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/DeviceMetadataHelper.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import AWSPluginsCore
1010
struct DeviceMetadataHelper {
1111

1212
static func getDeviceMetadata(
13-
for environment: Environment,
14-
with username: String) async -> DeviceMetadata {
13+
for username: String,
14+
with environment: Environment) async -> DeviceMetadata {
1515
let credentialStoreClient = (environment as? AuthEnvironment)?.credentialsClient
1616
do {
17-
let data = try await credentialStoreClient?.fetchData(type: .deviceMetadata(username: username))
18-
17+
let data = try await credentialStoreClient?.fetchData(
18+
type: .deviceMetadata(username: username)
19+
)
1920
if case .deviceMetadata(let fetchedMetadata, _) = data {
2021
return fetchedMetadata
2122
}
@@ -31,4 +32,17 @@ struct DeviceMetadataHelper {
3132
return .noData
3233
}
3334

35+
static func removeDeviceMetaData(
36+
for username: String,
37+
with environment: Environment) async {
38+
let credentialStoreClient = (environment as? AuthEnvironment)?.credentialsClient
39+
do {
40+
try await credentialStoreClient?.deleteData(
41+
type: .deviceMetadata(username: username)
42+
)
43+
} catch {
44+
let logger = (environment as? LoggerProvider)?.logger
45+
logger?.error("Unable to remove device metadata with error: \(error)")
46+
}
47+
}
3448
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/DeviceTasks/AWSAuthForgetDeviceTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class AWSAuthForgetDeviceTask: AuthForgetDeviceTask {
6262
let userPoolService = try environment.cognitoUserPoolFactory()
6363
guard let device = request.device else {
6464
let deviceMetadata = await DeviceMetadataHelper.getDeviceMetadata(
65-
for: environment,
66-
with: username)
65+
for: username,
66+
with: environment)
6767
if case .metadata(let data) = deviceMetadata {
6868
let input = ForgetDeviceInput(accessToken: accessToken, deviceKey: data.deviceKey)
6969
_ = try await userPoolService.forgetDevice(input: input)

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/DeviceTasks/AWSAuthRememberDeviceTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class AWSAuthRememberDeviceTask: AuthRememberDeviceTask {
6060
private func rememberDevice(with accessToken: String, username: String) async throws {
6161
let userPoolService = try environment.cognitoUserPoolFactory()
6262
let deviceMetadata = await DeviceMetadataHelper.getDeviceMetadata(
63-
for: environment,
64-
with: username)
63+
for: username,
64+
with: environment)
6565
if case .metadata(let data) = deviceMetadata {
6666
let input = UpdateDeviceStatusInput(accessToken: accessToken,
6767
deviceKey: data.deviceKey,

0 commit comments

Comments
 (0)