Skip to content

Commit 5cbc21b

Browse files
authored
feat(api): Pass authorization in header instead of query parameter for API category (#2918)
1 parent 2485058 commit 5cbc21b

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

aws-api/src/main/java/com/amplifyframework/api/aws/SubscriptionEndpoint.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
package com.amplifyframework.api.aws;
1717

1818
import android.net.Uri;
19-
import android.util.Base64;
2019
import androidx.annotation.NonNull;
2120
import androidx.annotation.Nullable;
2221
import androidx.core.util.ObjectsCompat;
2322

2423
import com.amplifyframework.AmplifyException;
2524
import com.amplifyframework.api.ApiException;
2625
import com.amplifyframework.api.ApiException.ApiAuthException;
26+
import com.amplifyframework.api.aws.utils.JSONObjectExtensionsKt;
2727
import com.amplifyframework.api.graphql.GraphQLRequest;
2828
import com.amplifyframework.api.graphql.GraphQLResponse;
2929
import com.amplifyframework.core.Action;
@@ -126,11 +126,13 @@ <T> void requestSubscription(
126126
if (webSocketListener == null || webSocketListener.isDisconnectedState()) {
127127
webSocketListener = new AmplifyWebSocketListener();
128128
try {
129-
webSocket = okHttpClient.newWebSocket(new Request.Builder()
130-
.url(buildConnectionRequestUrl(authType))
131-
.addHeader("Sec-WebSocket-Protocol", "graphql-ws")
132-
.header("User-Agent", UserAgent.string())
133-
.build(), webSocketListener);
129+
Request.Builder builder = new Request.Builder()
130+
.url(buildConnectionRequestUrl())
131+
.addHeader("Sec-WebSocket-Protocol", "graphql-ws")
132+
.header("User-Agent", UserAgent.string());
133+
// Add all authorization headers
134+
getConnectionAuthorizationHeaders(authType).forEach(builder::header);
135+
webSocket = okHttpClient.newWebSocket(builder.build(), webSocketListener);
134136
} catch (ApiException apiException) {
135137
onSubscriptionError.accept(apiException);
136138
return;
@@ -303,17 +305,17 @@ void releaseSubscription(String subscriptionId) throws ApiException {
303305
}
304306
}
305307

308+
private Map<String, String> getConnectionAuthorizationHeaders(AuthorizationType authType) throws ApiException {
309+
JSONObject headers = authorizer.createHeadersForConnection(authType);
310+
return JSONObjectExtensionsKt.toStringMap(headers);
311+
}
312+
306313
/*
307314
* Discover WebSocket endpoint from the AppSync endpoint.
308315
* AppSync endpoint : https://xxxxxxxxxxxx.appsync-api.ap-southeast-2.amazonaws.com/graphql
309316
* Discovered WebSocket endpoint : wss:// xxxxxxxxxxxx.appsync-realtime-api.ap-southeast-2.amazonaws.com/graphql
310317
*/
311-
private String buildConnectionRequestUrl(AuthorizationType authType) throws ApiException {
312-
// Construct the authorization header for connection request
313-
final byte[] rawHeader = authorizer.createHeadersForConnection(authType)
314-
.toString()
315-
.getBytes();
316-
318+
private String buildConnectionRequestUrl() throws ApiException {
317319
URL appSyncEndpoint = null;
318320
try {
319321
appSyncEndpoint = new URL(apiConfiguration.getEndpoint());
@@ -343,8 +345,6 @@ private String buildConnectionRequestUrl(AuthorizationType authType) throws ApiE
343345
.scheme("wss")
344346
.authority(authority)
345347
.path(path)
346-
.appendQueryParameter("header", Base64.encodeToString(rawHeader, Base64.DEFAULT))
347-
.appendQueryParameter("payload", "e30=")
348348
.build()
349349
.toString();
350350
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.api.aws.utils
17+
18+
import org.json.JSONObject
19+
20+
internal fun JSONObject.toStringMap(): Map<String, String> {
21+
val map = mutableMapOf<String, String>()
22+
this.keys().forEach { key -> map[key] = this.getString(key) }
23+
return map
24+
}

0 commit comments

Comments
 (0)