Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): fix credential decoding #3938

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -133,8 +133,8 @@ extension AWSCognitoAuthCredentialStore: AmplifyAuthCredentialStoreBehavior {
func retrieveCredential() throws -> AmplifyCredentials {
let authCredentialStoreKey = generateSessionKey(for: authConfiguration)
let authCredentialData = try keychain._getData(authCredentialStoreKey)
let awsCredential: AmplifyCredentials = try decode(data: authCredentialData)
return awsCredential
let amplifyCredential: AmplifyCredentials = try decode(data: authCredentialData)
return amplifyCredential
}

func deleteCredential() throws {
@@ -191,15 +191,15 @@ private extension AWSCognitoAuthCredentialStore {
do {
return try JSONEncoder().encode(object)
} catch {
throw KeychainStoreError.codingError("Error occurred while encoding AWSCredentials", error)
throw KeychainStoreError.codingError("Error occurred while encoding credentials", error)
}
}

func decode<T: Decodable>(data: Data) throws -> T {
do {
return try JSONDecoder().decode(T.self, from: data)
} catch {
throw KeychainStoreError.codingError("Error occurred while decoding AWSCredentials", error)
throw KeychainStoreError.codingError("Error occurred while decoding credentials", error)
}
}

Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@
switch rawValue {
case "CUSTOM_AUTH":
self = .customWithSRP
case "CUSTOM_AUTH_WITHOUT_SRP":
self = .customWithoutSRP
case "USER_SRP_AUTH":
self = .userSRP
case "USER_PASSWORD_AUTH":
@@ -51,8 +53,10 @@

var rawValue: String {
switch self {
case .custom, .customWithSRP, .customWithoutSRP:
case .custom, .customWithSRP:
return "CUSTOM_AUTH"
case .customWithoutSRP:
return "CUSTOM_AUTH_WITHOUT_SRP"
case .userSRP:
return "USER_SRP_AUTH"
case .userPassword:
@@ -62,6 +66,23 @@
}
}

internal static func legacyInit(rawValue: String) -> Self? {
switch rawValue {
case "userSRP":
return .userSRP
case "userPassword":
return .userPassword
case "custom":
return .custom
case "customWithSRP":
return .customWithSRP
case "customWithoutSRP":
return .customWithoutSRP
default:
return nil
}
}

public static var userAuth: AuthFlowType {
return .userAuth(preferredFirstFactor: nil)
}
@@ -110,9 +131,21 @@

// Decoding the enum
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let container: KeyedDecodingContainer<CodingKeys>
do {
container = try decoder.container(keyedBy: CodingKeys.self)
} catch DecodingError.typeMismatch {
let legacyContainer = try decoder.singleValueContainer()
let type = try legacyContainer.decode(String.self)
guard let authFlowType = AuthFlowType.legacyInit(rawValue: type) else {
throw DecodingError.dataCorruptedError(in: legacyContainer, debugDescription: "Invalid AuthFlowType value")
}
self = authFlowType
return
} catch {
throw error
}

// Decode the type (raw value)
let type = try container.decode(String.self, forKey: .type)

// Initialize based on the type
@@ -130,7 +163,7 @@
if let preferredFirstFactor = AuthFactorType(rawValue: preferredFirstFactorString) {
self = .userAuth(preferredFirstFactor: preferredFirstFactor)
} else {
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unable to decode preferredFirstFactor value")
throw DecodingError.dataCorruptedError(forKey: .preferredFirstFactor, in: container, debugDescription: "Unable to decode preferredFirstFactor value")

Check failure on line 166 in AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/AuthFlowType.swift

GitHub Actions / run-swiftlint

Line should be 160 characters or less; currently it has 165 characters (line_length)
}
default:
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Invalid AuthFlowType value")
@@ -152,5 +185,4 @@
return .userAuth
}
}

}

Unchanged files with check annotations Beta

///
/// - Parameter plugin: The plugin to add
/// - Tag: Amplify.add_plugin
public static func add<P: Plugin>(plugin: P) throws {

Check warning on line 82 in Amplify/Amplify.swift

GitHub Actions / run-swiftlint

Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
log.debug("Adding plugin: \(plugin))")
switch plugin {
case let plugin as AnalyticsCategoryPlugin:
/// - Parameter configuration: The AmplifyConfiguration for specified Categories
///
/// - Tag: Amplify.configure
public static func configure(_ configuration: AmplifyConfiguration? = nil) throws {

Check warning on line 112 in Amplify/Core/Configuration/AmplifyConfiguration.swift

GitHub Actions / run-swiftlint

Function should have complexity 10 or less; currently complexity is 13 (cyclomatic_complexity)
log.info("Configuring")
log.debug("Configuration: \(String(describing: configuration))")
guard !isConfigured else {
import Foundation
// swiftlint:disable cyclomatic_complexity

Check warning on line 10 in Amplify/Core/Configuration/Internal/Amplify+Reset.swift

GitHub Actions / run-swiftlint

The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
extension Amplify {
/// Resets the state of the Amplify framework.
ModelListDecoderRegistry.reset()
ModelProviderRegistry.reset()
log.verbose("Resetting ModelRegistry, ModelListDecoderRegistry, ModelProviderRegistry finished")

Check warning on line 54 in Amplify/Core/Configuration/Internal/Amplify+Reset.swift

GitHub Actions / run-swiftlint

Lines should not have trailing whitespace (trailing_whitespace)
#if os(iOS) && !os(visionOS)
await MainActor.run {
devMenu = nil
}
}
public class AmplifyInProcessReportingOperationTaskAdapter<Request: AmplifyOperationRequest,

Check warning on line 67 in Amplify/Core/Support/AmplifyTask+OperationTaskAdapters.swift

GitHub Actions / run-swiftlint

Type name 'AmplifyInProcessReportingOperationTaskAdapter' should be between 3 and 40 characters long (type_name)
InProcess,
Success,
Failure: AmplifyError>: AmplifyTask, AmplifyInProcessReportingTask {
/// - Tag: StorageListResult.items
public var items: [Item]

Check warning on line 35 in Amplify/Categories/Storage/Result/StorageListResult.swift

GitHub Actions / run-swiftlint

Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
/// Array of excluded subpaths in the Result.
/// This field is only populated when [`StorageListRequest.Options.subpathStrategy`](x-source-tag://StorageListRequestOptions.subpathStragegy) is set to [`.exclude()`](x-source-tag://SubpathStrategy.exclude).
///
///
/// - Tag: StorageDownloadFileRequest.path
public let path: (any StoragePath)?

Check warning on line 20 in Amplify/Categories/Storage/Operation/Request/StorageDownloadDataRequest.swift

GitHub Actions / run-swiftlint

Lines should not have trailing whitespace (trailing_whitespace)
/// The unique identifier for the object in storage
///
/// - Tag: StorageDownloadDataRequest.key
options: AuthConfirmSignInRequest.Options?
) async throws -> AuthSignInResult

Check warning on line 105 in Amplify/Categories/Auth/AuthCategoryBehavior.swift

GitHub Actions / run-swiftlint

Lines should not have trailing whitespace (trailing_whitespace)

Check warning on line 105 in Amplify/Categories/Auth/AuthCategoryBehavior.swift

GitHub Actions / run-swiftlint

Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
/// Auto signs in the user for passwordless sign up
func autoSignIn() async throws -> AuthSignInResult

Check warning on line 108 in Amplify/Categories/Auth/AuthCategoryBehavior.swift

GitHub Actions / run-swiftlint

Lines should not have trailing whitespace (trailing_whitespace)
/// Sign out the currently logged-in user.
///
/// - Parameters: