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

chore(core): resolve swiftformat errors and warnings #3847

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

/// Plugin specific options type
///
Expand All @@ -20,8 +20,10 @@ public struct AWSAPIPluginDataStoreOptions {
/// name of the model
public let modelName: String

public init(authType: AWSAuthorizationType?,
modelName: String) {
public init(
authType: AWSAuthorizationType?,
modelName: String
) {
self.authType = authType
self.modelName = modelName
}
Expand Down
8 changes: 5 additions & 3 deletions AmplifyPlugins/Core/AWSPluginsCore/AWSPluginOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

/// Plugin specific options type
///
Expand Down Expand Up @@ -37,8 +37,10 @@ public struct AWSPluginOptions {
/// name of the model
public let modelName: String?

public init(authType: AWSAuthorizationType?,
modelName: String) {
public init(
authType: AWSAuthorizationType?,
modelName: String
) {
self.authType = authType
self.modelName = modelName
}
Expand Down
44 changes: 26 additions & 18 deletions AmplifyPlugins/Core/AWSPluginsCore/Auth/AWSAuthModeStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Combine
import Amplify
import Combine
import Foundation

/// Represents different auth strategies supported by a client
/// interfacing with an AppSync backend
Expand Down Expand Up @@ -56,7 +56,7 @@ public protocol AuthorizationTypeIterator {

/// Total number of values
var count: Int { get }

/// Whether iterator has next available `AuthorizationType` to return or not
var hasNext: Bool { get }

Expand Down Expand Up @@ -85,7 +85,7 @@ public struct AWSAuthorizationTypeIterator: AuthorizationTypeIterator, Sequence,
public var count: Int {
_count
}

public var hasNext: Bool {
_position < _count
}
Expand All @@ -95,7 +95,7 @@ public struct AWSAuthorizationTypeIterator: AuthorizationTypeIterator, Sequence,
_position += 1
return value
}

return nil
}
}
Expand All @@ -108,15 +108,19 @@ public struct AWSAuthorizationTypeIterator: AuthorizationTypeIterator, Sequence,
/// registered as interceptor for the API
public class AWSDefaultAuthModeStrategy: AuthModeStrategy {
public weak var authDelegate: AuthModeStrategyDelegate?
required public init() {}
public required init() {}

public func authTypesFor(schema: ModelSchema,
operation: ModelOperation) -> AWSAuthorizationTypeIterator {
public func authTypesFor(
schema: ModelSchema,
operation: ModelOperation
) -> AWSAuthorizationTypeIterator {
return AWSAuthorizationTypeIterator(withValues: [.inferred])
}

public func authTypesFor(schema: ModelSchema,
operations: [ModelOperation]) -> AWSAuthorizationTypeIterator {
public func authTypesFor(
schema: ModelSchema,
operations: [ModelOperation]
) -> AWSAuthorizationTypeIterator {
return AWSAuthorizationTypeIterator(withValues: [.inferred])
}
}
Expand All @@ -129,7 +133,7 @@ public class AWSMultiAuthModeStrategy: AuthModeStrategy {

private typealias AuthPriority = Int

required public init() {}
public required init() {}

private static func defaultAuthTypeFor(authStrategy: AuthStrategy) -> AWSAuthorizationType {
switch authStrategy {
Expand Down Expand Up @@ -210,8 +214,10 @@ public class AWSMultiAuthModeStrategy: AuthModeStrategy {
/// - schema: model schema
/// - operation: model operation
/// - Returns: an iterator for the applicable auth rules
public func authTypesFor(schema: ModelSchema,
operation: ModelOperation) async -> AWSAuthorizationTypeIterator {
public func authTypesFor(
schema: ModelSchema,
operation: ModelOperation
) async -> AWSAuthorizationTypeIterator {
return await authTypesFor(schema: schema, operations: [operation])
}

Expand All @@ -220,19 +226,21 @@ public class AWSMultiAuthModeStrategy: AuthModeStrategy {
/// - schema: model schema
/// - operations: model operations
/// - Returns: an iterator for the applicable auth rules
public func authTypesFor(schema: ModelSchema,
operations: [ModelOperation]) async -> AWSAuthorizationTypeIterator {
public func authTypesFor(
schema: ModelSchema,
operations: [ModelOperation]
) async -> AWSAuthorizationTypeIterator {
var sortedRules = operations
.flatMap { schema.authRules.filter(modelOperation: $0) }
.reduce(into: [AuthRule](), { array, rule in
.reduce(into: [AuthRule]()) { array, rule in
if !array.contains(rule) {
array.append(rule)
}
})
}
.sorted(by: AWSMultiAuthModeStrategy.comparator)

// if there isn't a user signed in, returns only public or custom rules
if let authDelegate = authDelegate, await !authDelegate.isUserLoggedIn() {
if let authDelegate, await !authDelegate.isUserLoggedIn() {
sortedRules = sortedRules.filter { rule in
return rule.allow == .public || rule.allow == .custom
}
Expand Down
26 changes: 19 additions & 7 deletions AmplifyPlugins/Core/AWSPluginsCore/Auth/AWSAuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

public class AWSAuthService: AWSAuthServiceBehavior {

Expand Down Expand Up @@ -44,23 +44,35 @@ public class AWSAuthService: AWSAuthServiceBehavior {

guard let claimsData = encodedData else {
return .failure(
.validation("", "Cannot get claims in `Data` form. Token is not valid base64 encoded string.",
"", nil))
.validation(
"",
"Cannot get claims in `Data` form. Token is not valid base64 encoded string.",
"",
nil
))
}

let jsonObject: Any?
do {
jsonObject = try JSONSerialization.jsonObject(with: claimsData, options: [])
} catch {
return .failure(
.validation("", "Cannot get claims in `Data` form. Token is not valid JSON string.",
"", error))
.validation(
"",
"Cannot get claims in `Data` form. Token is not valid JSON string.",
"",
error
))
}

guard let convertedDictionary = jsonObject as? [String: AnyObject] else {
return .failure(
.validation("", "Cannot get claims in `Data` form. Unable to convert to [String: AnyObject].",
"", nil))
.validation(
"",
"Cannot get claims in `Data` form. Unable to convert to [String: AnyObject].",
"",
nil
))
}
return .success(convertedDictionary)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

public protocol AWSAuthServiceBehavior: AnyObject {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

/// Defines the contract for an AuthSession that can vend AWS credentials and store a user ID
/// (`sub`) for the underlying OIDC-compliant authentication provider such as Cognito user pools.
Expand All @@ -19,13 +19,13 @@ import Amplify
/// successfully completed a `signIn` flow, and has not subsequently signed out.
public protocol AWSAuthSessionBehavior<Tokens>: AuthSession {

/// The concrete type holding the OIDC tokens from the authentication provider.
/// The concrete type holding the OIDC tokens from the authentication provider.
/// Generally, this type will have at least methods for retrieving an identity token and an access token.
associatedtype Tokens

/// The result of the most recent attempt to get AWS Credentials. There is no guarantee that the credentials
/// are not expired, but conforming types may have logic in place to automatically refresh the credentials.
/// The credentials may be fore either the unauthenticated or authenticated role, depending on the
/// The credentials may be fore either the unauthenticated or authenticated role, depending on the
/// configuration of the identity pool and the tokens used to retrieve the identity ID from Cognito.
///
/// If the most recent attempt caused an error, the result will contain the details of the error.
Expand All @@ -34,7 +34,7 @@ public protocol AWSAuthSessionBehavior<Tokens>: AuthSession {
// swiftlint:disable line_length
/// The result of the most recent attempt to get a
/// [Cognito identity pool identity ID](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetId.html#CognitoIdentity-GetId-response-IdentityId).
/// The identityID may represent either an unauthenticated or authenticated identity,
/// The identityID may represent either an unauthenticated or authenticated identity,
/// depending on the configuration of the identity pool and the tokens used to
/// retrieve the identity ID from Cognito.
///
Expand All @@ -49,7 +49,7 @@ public protocol AWSAuthSessionBehavior<Tokens>: AuthSession {
/// If the most recent attempt caused an error, the result will contain the details of the error.
var userSubResult: Result<String, AuthError> { get }

/// The result of the most recent attempt to get the current user's `sub` (unique User ID).
/// The result of the most recent attempt to get the current user's `sub` (unique User ID).
/// Depending on the underlying implementation,
/// the details of the tokens may vary, but it is expected that the type will have at least methods for
/// retrieving an identity token and an access token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

// swiftlint:disable line_length

Expand Down Expand Up @@ -49,8 +49,8 @@ extension AWSAuthorizationType: CaseIterable { }
extension AWSAuthorizationType: Codable { }

/// Indicates whether the authotization type requires the auth plugin to operate.
extension AWSAuthorizationType {
public var requiresAuthPlugin: Bool {
public extension AWSAuthorizationType {
var requiresAuthPlugin: Bool {
switch self {
case .none, .apiKey, .openIDConnect, .function:
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// SPDX-License-Identifier: Apache-2.0
//


import Foundation

/// - Warning: Although this has `public` access, it is intended for internal use and should not be used directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public extension AuthAWSCredentialsProvider where Self: AWSAuthSessionBehavior {
/// Return the most recent Result of fetching the AWS Credentials. If the temporary credentials are expired, returns
/// a `AuthError.sessionExpired` failure.
func getAWSCredentials() -> Result<AWSCredentials, AuthError> {
let result: Result<AWSCredentials, AuthError>
switch awsCredentialsResult {
case .failure(let error): result = .failure(error)
let result: Result<AWSCredentials, AuthError> = switch awsCredentialsResult {
case .failure(let error): .failure(error)
case .success(let tempCreds):
if tempCreds.expiration > Date() {
result = .success(tempCreds)
.success(tempCreds)
} else {
result = .failure(AuthError.sessionExpired("AWS Credentials are expired", ""))
.failure(AuthError.sessionExpired("AWS Credentials are expired", ""))
}
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

public enum AWSAuthorizationConfiguration {
case none
Expand All @@ -21,17 +21,19 @@ public enum AWSAuthorizationConfiguration {
extension AWSAuthorizationConfiguration {
private static func awsIAMAuthorizationConfiguration(region: String?)
throws -> AWSAuthorizationConfiguration {
guard let region = region else {
throw PluginError.pluginConfigurationError("Region is not set for IAM",
"Set the region")
guard let region else {
throw PluginError.pluginConfigurationError(
"Region is not set for IAM",
"Set the region"
)
}
return .awsIAM(AWSIAMConfiguration(region: region))
}

private static func apiKeyAuthorizationConfiguration(apiKey: String?)
throws -> AWSAuthorizationConfiguration {

guard let apiKey = apiKey else {
guard let apiKey else {
throw PluginError.pluginConfigurationError(
"Could not get `ApiKey` from plugin configuration",
"""
Expand All @@ -53,9 +55,11 @@ extension AWSAuthorizationConfiguration {
/// - Throws: if the region is not valid and `authType` is `iam`
/// or if `apiKey` is not valid and `authType` is `apiKey`
/// - Returns: an `AWSAuthorizationConfiguration` according to the provided `authType`
public static func makeConfiguration(authType: AWSAuthorizationType,
region: String?,
apiKey: String?) throws -> AWSAuthorizationConfiguration {
public static func makeConfiguration(
authType: AWSAuthorizationType,
region: String?,
apiKey: String?
) throws -> AWSAuthorizationConfiguration {
switch authType {
case .none:
return .none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ extension KeychainStatus: CustomStringConvertible {
self = .success
case -128:
self = .userCanceled
case -25299:
case -25_299:
self = .duplicateItem
case -25300:
case -25_300:
self = .itemNotFound
case -34018:
case -34_018:
self = .missingEntitlement
default:
self = .unexpectedError(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// SPDX-License-Identifier: Apache-2.0
//

import Amplify
import Foundation
import Security
import Amplify

// swiftlint:disable identifier_name
public protocol KeychainStoreBehavior {
Expand Down Expand Up @@ -233,7 +233,7 @@ public struct KeychainStore: KeychainStoreBehavior {
}

extension KeychainStore {
struct Constants {
enum Constants {
/** Class Key Constant */
static let Class = String(kSecClass)
static let ClassGenericPassword = String(kSecClassGenericPassword)
Expand Down
Loading
Loading