Skip to content

fix(datastore): added missing defaultAuthType method #4964

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

Merged
merged 6 commits into from
Jun 4, 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 @@ -901,6 +901,7 @@ class AmplifyDataStorePlugin :

override fun addApiPlugin(
authProvidersList: List<String>,
endpoints: Map<String, String>,
callback: (kotlin.Result<Unit>) -> Unit
) {
try {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 37 additions & 2 deletions packages/amplify_datastore/ios/Classes/FlutterApiPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,57 @@ import Foundation
import Flutter
import Combine

public class FlutterApiPlugin: APICategoryPlugin
public class FlutterApiPlugin: APICategoryPlugin, AWSAPIAuthInformation
{
public var key: PluginKey = "awsAPIPlugin"
private let apiAuthFactory: APIAuthProviderFactory
private let nativeApiPlugin: NativeApiPlugin
private let nativeSubscriptionEvents: PassthroughSubject<NativeGraphQLSubscriptionResponse, Never>
private var cancellables = AtomicDictionary<AnyCancellable, Void>()
private var endpoints: [String: String]

init(
apiAuthProviderFactory: APIAuthProviderFactory,
nativeApiPlugin: NativeApiPlugin,
subscriptionEventBus: PassthroughSubject<NativeGraphQLSubscriptionResponse, Never>
subscriptionEventBus: PassthroughSubject<NativeGraphQLSubscriptionResponse, Never>,
endpoints: [String: String]
) {
self.apiAuthFactory = apiAuthProviderFactory
self.nativeApiPlugin = nativeApiPlugin
self.nativeSubscriptionEvents = subscriptionEventBus
self.endpoints = endpoints
}

public func defaultAuthType() throws -> AWSAuthorizationType {
try defaultAuthType(for: nil)
}

public func defaultAuthType(for apiName: String?) throws -> AWSAuthorizationType {
if apiName == nil {
if self.endpoints.count == 1 {
return try stringToAWSAuthType(string: self.endpoints.first?.value)
}
return try stringToAWSAuthType(string: "")
}
let authType = self.endpoints[apiName!]
return try stringToAWSAuthType(string: authType)
}

private func stringToAWSAuthType(string: String?) throws -> AWSAuthorizationType {
switch(string){
case .some("apiKey"):
return AWSAuthorizationType.apiKey
case .some("none"):
return AWSAuthorizationType.none
case .some("iam"):
return AWSAuthorizationType.awsIAM
case .some("oidc"):
return AWSAuthorizationType.openIDConnect
case .some("userPools"):
return AWSAuthorizationType.amazonCognitoUserPools
default:
throw DataStoreError.configuration("No AWSAuthorizationType found from given string", "Please check API configuration")
}
}

public func query<R>(request: GraphQLRequest<R>) async throws -> GraphQLTask<R>.Success where R : Decodable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ public class SwiftAmplifyDataStorePlugin: NSObject, FlutterPlugin, NativeAmplify
}
}

func addApiPlugin(authProvidersList: [String], completion: @escaping (Result<Void, Error>) -> Void) {
func addApiPlugin(authProvidersList: [String], endpoints: [String: String], completion: @escaping (Result<Void, Error>) -> Void) {
do {
let authProviders = authProvidersList.compactMap {
AWSAuthorizationType(rawValue: $0)
}

try Amplify.add(
plugin: FlutterApiPlugin(
apiAuthProviderFactory: FlutterAuthProviders(
authProviders: authProviders,
nativeApiPlugin: nativeApiPlugin
),
nativeApiPlugin: nativeApiPlugin,
subscriptionEventBus: nativeSubscriptionEventBus
subscriptionEventBus: nativeSubscriptionEventBus,
endpoints: endpoints
)
)
return completion(.success(()))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,3 @@ struct FlutterModelSchema {
return (fields, name)
}
}

// TODO: Migrate to Async Swift v2
// This enables custom selection set behavior within Amplify-Swift v1.
// Which allows models to be decoded when created on Android and received to iOS
//extension FlutterModelSchema: SubscriptionSelectionSetBehavior {
// public var includePrimaryKeysOnly: Bool {
// return true
// }
//}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/amplify_datastore/lib/amplify_datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ class AmplifyDataStore extends DataStorePluginInterface
if (apiPlugin != null && gqlConfig != null) {
// ignore: invalid_use_of_protected_member
final authProviders = apiPlugin.authProviders;
Map<String, String> endpoints = {};
config.api?.awsPlugin?.all.entries.forEach((e) {
endpoints[e.key] = e.value.authorizationType.name;
});
final nativePlugin = _NativeAmplifyApi(authProviders);
NativeApiPlugin.setup(nativePlugin);

final nativeBridge = NativeApiBridge();
try {
final authProvidersList =
authProviders.keys.map((key) => key.rawValue).toList();
await nativeBridge.addApiPlugin(authProvidersList);
await nativeBridge.addApiPlugin(authProvidersList, endpoints);
} on PlatformException catch (e) {
if (e.code.contains('AmplifyAlreadyConfiguredException') ||
e.code.contains('AlreadyConfiguredException')) {
Expand Down
6 changes: 4 additions & 2 deletions packages/amplify_datastore/lib/src/native_plugin.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/amplify_datastore/pigeons/native_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ abstract class NativeAuthBridge {
@HostApi()
abstract class NativeApiBridge {
@async
void addApiPlugin(List<String> authProvidersList);
void addApiPlugin(
List<String> authProvidersList, Map<String, String> endpoints);

@async
void sendSubscriptionEvent(NativeGraphQLSubscriptionResponse event);
Expand Down
Loading