Created
July 11, 2022 14:24
-
-
Save dnys1/31015083b6038ac5dae420cb6188a331 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:http/http.dart' as http; | |
enum APIAuthorizationType<T extends AuthProvider> { | |
// apiKey, | |
// userPools, | |
iam<IAMAuthProvider>(), | |
// oidc, | |
// lambda | |
} | |
void main() { | |
final repo = AuthProviderRepo(); | |
repo.set(APIAuthorizationType.iam, IAMAuthProvider()); | |
final iamProvider = repo.get(APIAuthorizationType.iam)!; | |
final options = IAMAuthProviderOptions('us-west-2', 'appsync'); | |
iamProvider.authorizeRequest(http.Request('GET', Uri()), options: options); | |
} | |
class AuthProviderRepo { | |
final _repo = <String, AuthProvider>{}; | |
void set<T extends AuthProvider>( | |
APIAuthorizationType<T> type, | |
AuthProvider provider, | |
) => | |
_repo[type.name] = provider; | |
T? get<T extends AuthProvider>(APIAuthorizationType<T> type) => | |
_repo[type.name] as T?; | |
} | |
abstract class AuthProvider { | |
Future<http.BaseRequest> authorizeRequest( | |
http.BaseRequest request, { | |
covariant AuthProviderOptions? options, | |
}); | |
} | |
class IAMAuthProvider extends AuthProvider { | |
@override | |
Future<http.BaseRequest> authorizeRequest( | |
http.BaseRequest request, { | |
IAMAuthProviderOptions? options, | |
}) { | |
throw UnimplementedError(); | |
} | |
} | |
class IAMAuthProviderOptions extends AuthProviderOptions { | |
const IAMAuthProviderOptions(this.region, this.service); | |
final String region; | |
final String service; | |
} | |
abstract class AuthProviderOptions { | |
const AuthProviderOptions(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment