-
Notifications
You must be signed in to change notification settings - Fork 260
feat: move App Sync subscription headers to protocol #5301
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ import 'package:amplify_api_dart/src/graphql/web_socket/types/subscriptions_even | |
import 'package:amplify_api_dart/src/graphql/web_socket/types/web_socket_message_stream_transformer.dart'; | ||
import 'package:amplify_api_dart/src/graphql/web_socket/types/web_socket_types.dart'; | ||
import 'package:amplify_core/amplify_core.dart'; | ||
// ignore: implementation_imports | ||
import 'package:amplify_core/src/config/amplify_outputs/api_outputs.dart'; | ||
import 'package:async/async.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:stream_transform/stream_transform.dart'; | ||
|
@@ -72,15 +74,14 @@ class AmplifyWebSocketService | |
); | ||
|
||
try { | ||
const webSocketProtocols = ['graphql-ws']; | ||
final connectionUri = await generateConnectionUri( | ||
final protocols = await generateProtocols( | ||
state.config, | ||
state.authProviderRepo, | ||
); | ||
|
||
final connectionUri = await generateConnectionUri(state.config); | ||
final channel = WebSocketChannel.connect( | ||
connectionUri, | ||
protocols: webSocketProtocols, | ||
protocols: protocols, | ||
); | ||
sink = channel.sink; | ||
|
||
|
@@ -95,6 +96,28 @@ class AmplifyWebSocketService | |
} | ||
} | ||
|
||
/// Generates a list of protocols from a [WebSocketState]. | ||
@visibleForTesting | ||
Future<List<String>> generateProtocols( | ||
ApiOutputs outputs, | ||
AmplifyAuthProviderRepository authRepo, | ||
) async { | ||
final authorizationHeaders = await generateAuthorizationHeaders( | ||
outputs, | ||
isConnectionInit: true, | ||
authRepo: authRepo, | ||
body: appSyncDefaultPayload, | ||
); | ||
final encodedAuthHeaders = base64Url | ||
.encode(json.encode(authorizationHeaders).codeUnits) | ||
// remove padding char ("=") as it is optional in base64Url encoding and | ||
// is not permitted in protocol names. | ||
// Base64Url Spec: https://datatracker.ietf.org/doc/html/rfc4648#section-5 | ||
// Protocol name separators: https://www.rfc-editor.org/rfc/rfc2616 (see "separators") | ||
.replaceAll('=', ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Is it safe to remove all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is safe to remove all. base64url uses |
||
return ['graphql-ws', 'header-$encodedAuthHeaders']; | ||
} | ||
|
||
@override | ||
Future<void> register( | ||
ConnectedState state, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:amplify_api_dart/src/graphql/providers/app_sync_api_key_auth_provider.dart'; | ||
import 'package:amplify_api_dart/src/graphql/web_socket/services/web_socket_service.dart'; | ||
import 'package:amplify_core/amplify_core.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../util.dart'; | ||
|
||
void main() { | ||
group('AmplifyWebSocketService', () { | ||
group('generateProtocols', () {}); | ||
const apiKey = 'fake-key'; | ||
test('should generate a protocol that includes the appropriate headers', | ||
() async { | ||
final (outputs, repo) = createOutputsAndRepo( | ||
AppSyncApiKeyAuthProvider(), | ||
APIAuthorizationType.apiKey, | ||
apiKey, | ||
); | ||
final service = AmplifyWebSocketService(); | ||
final protocols = await service.generateProtocols(outputs, repo); | ||
final encodedHeaders = protocols[1].replaceFirst('header-', ''); | ||
final headers = json.decode( | ||
String.fromCharCodes(base64Url.decode(encodedHeaders)), | ||
) as Map<String, dynamic>; | ||
expect(headers[xApiKey], apiKey); | ||
expect(headers.containsKey(AWSHeaders.accept), true); | ||
expect(headers.containsKey(AWSHeaders.contentEncoding), true); | ||
expect(headers.containsKey(AWSHeaders.contentType), true); | ||
expect(headers.containsKey(AWSHeaders.host), true); | ||
}); | ||
}); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.