Skip to content

Commit

Permalink
Reconfigure when fetching auth session if sharing keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroluchko committed Aug 12, 2024
1 parent 799fc0d commit af6f9c0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
8 changes: 6 additions & 2 deletions Amplify/Categories/Auth/Request/AuthFetchSessionRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ public struct AuthFetchSessionRequest: AmplifyOperationRequest {

/// Extra request options defined in `AuthFetchSessionRequest.Options`
public var options: Options

Check warning on line 15 in Amplify/Categories/Auth/Request/AuthFetchSessionRequest.swift

View workflow job for this annotation

GitHub Actions / run-swiftlint

Lines should not have trailing whitespace (trailing_whitespace)
/// forceReconfigure flag when true will reconfigure the auth state machine.
/// Should be used when sharing credentials via access group
public let forceReconfigure: Bool

public init(options: Options) {

public init(options: Options, forceReconfigure: Bool = false) {
self.options = options
self.forceReconfigure = forceReconfigure
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ extension AWSCognitoAuthPlugin: AuthCategoryBehavior {
}

public func fetchAuthSession(options: AuthFetchSessionRequest.Options?) async throws -> AuthSession {
let options = options ?? AuthFetchSessionRequest.Options()
let request = AuthFetchSessionRequest(options: options)
let task = AWSAuthFetchSessionTask(request, authStateMachine: authStateMachine)
var options = options ?? AuthFetchSessionRequest.Options()
let request = AuthFetchSessionRequest(options: options, forceReconfigure: secureStoragePreferences?.accessGroup?.name != nil)
let task = AWSAuthFetchSessionTask(request, authStateMachine: authStateMachine, configuration: authConfiguration)
return try await taskQueue.sync {
return try await task.value
} as! AuthSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ public struct AWSCognitoSecureStoragePreferences {

/// The access group that the keychain will use for auth items
public let accessGroup: AccessGroup?

public let migrateKeychainItemsOfUserSession: Bool

public init(accessGroup: AccessGroup? = nil, migrateKeychainItemsOfUserSession: Bool = true) {
public init(accessGroup: AccessGroup? = nil) {
self.accessGroup = accessGroup
self.migrateKeychainItemsOfUserSession = migrateKeychainItemsOfUserSession
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ class AWSAuthFetchSessionTask: AuthFetchSessionTask, DefaultLogger {
private let authStateMachine: AuthStateMachine
private let fetchAuthSessionHelper: FetchAuthSessionOperationHelper
private let taskHelper: AWSAuthTaskHelper
private let configuration: AuthConfiguration

var eventName: HubPayloadEventName {
HubPayload.EventName.Auth.fetchSessionAPI
}

init(_ request: AuthFetchSessionRequest, authStateMachine: AuthStateMachine) {
init(_ request: AuthFetchSessionRequest, authStateMachine: AuthStateMachine, configuration: AuthConfiguration) {
self.request = request
self.authStateMachine = authStateMachine
self.fetchAuthSessionHelper = FetchAuthSessionOperationHelper()
self.taskHelper = AWSAuthTaskHelper(authStateMachine: authStateMachine)
self.configuration = configuration
}

func execute() async throws -> AuthSession {
log.verbose("Starting execution")
let doesNeedForceReconfigure = request.forceReconfigure
if doesNeedForceReconfigure {
log.verbose("Reconfiguring for keychain sharing")
let event = AuthEvent(eventType: .reconfigure(configuration))
await authStateMachine.send(event)
}
await taskHelper.didStateMachineConfigured()
let doesNeedForceRefresh = request.options.forceRefresh
return try await fetchAuthSessionHelper.fetch(authStateMachine,
Expand Down

0 comments on commit af6f9c0

Please sign in to comment.