|
16 | 16 | package com.amplifyframework.api.aws; |
17 | 17 |
|
18 | 18 | import android.net.Uri; |
19 | | -import android.util.Base64; |
20 | 19 | import androidx.annotation.NonNull; |
21 | 20 | import androidx.annotation.Nullable; |
22 | 21 | import androidx.core.util.ObjectsCompat; |
23 | 22 |
|
24 | 23 | import com.amplifyframework.AmplifyException; |
25 | 24 | import com.amplifyframework.api.ApiException; |
26 | 25 | import com.amplifyframework.api.ApiException.ApiAuthException; |
| 26 | +import com.amplifyframework.api.aws.utils.JSONObjectExtensionsKt; |
27 | 27 | import com.amplifyframework.api.graphql.GraphQLRequest; |
28 | 28 | import com.amplifyframework.api.graphql.GraphQLResponse; |
29 | 29 | import com.amplifyframework.core.Action; |
@@ -126,11 +126,13 @@ <T> void requestSubscription( |
126 | 126 | if (webSocketListener == null || webSocketListener.isDisconnectedState()) { |
127 | 127 | webSocketListener = new AmplifyWebSocketListener(); |
128 | 128 | 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); |
134 | 136 | } catch (ApiException apiException) { |
135 | 137 | onSubscriptionError.accept(apiException); |
136 | 138 | return; |
@@ -303,17 +305,17 @@ void releaseSubscription(String subscriptionId) throws ApiException { |
303 | 305 | } |
304 | 306 | } |
305 | 307 |
|
| 308 | + private Map<String, String> getConnectionAuthorizationHeaders(AuthorizationType authType) throws ApiException { |
| 309 | + JSONObject headers = authorizer.createHeadersForConnection(authType); |
| 310 | + return JSONObjectExtensionsKt.toStringMap(headers); |
| 311 | + } |
| 312 | + |
306 | 313 | /* |
307 | 314 | * Discover WebSocket endpoint from the AppSync endpoint. |
308 | 315 | * AppSync endpoint : https://xxxxxxxxxxxx.appsync-api.ap-southeast-2.amazonaws.com/graphql |
309 | 316 | * Discovered WebSocket endpoint : wss:// xxxxxxxxxxxx.appsync-realtime-api.ap-southeast-2.amazonaws.com/graphql |
310 | 317 | */ |
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 { |
317 | 319 | URL appSyncEndpoint = null; |
318 | 320 | try { |
319 | 321 | appSyncEndpoint = new URL(apiConfiguration.getEndpoint()); |
@@ -343,8 +345,6 @@ private String buildConnectionRequestUrl(AuthorizationType authType) throws ApiE |
343 | 345 | .scheme("wss") |
344 | 346 | .authority(authority) |
345 | 347 | .path(path) |
346 | | - .appendQueryParameter("header", Base64.encodeToString(rawHeader, Base64.DEFAULT)) |
347 | | - .appendQueryParameter("payload", "e30=") |
348 | 348 | .build() |
349 | 349 | .toString(); |
350 | 350 | } |
|
0 commit comments