11package com .pubnub .api .endpoints .vendor ;
22
3+ import com .pubnub .api .PubNub ;
4+ import com .pubnub .api .PubNubException ;
5+ import com .pubnub .api .PubNubUtil ;
6+
7+ import java .io .IOException ;
8+ import java .net .HttpURLConnection ;
9+ import java .net .URL ;
10+ import java .util .HashMap ;
11+ import java .util .Map ;
12+
13+ import lombok .extern .java .Log ;
314import okhttp3 .Call ;
415import okhttp3 .Callback ;
516import okhttp3 .Headers ;
17+ import okhttp3 .HttpUrl ;
618import okhttp3 .MediaType ;
719import okhttp3 .Protocol ;
820import okhttp3 .Request ;
1224import okio .BufferedSource ;
1325import okio .Okio ;
1426
15- import java .io .IOException ;
16- import java .net .HttpURLConnection ;
17- import java .net .URL ;
18-
19-
27+ @ Log
2028public class AppEngineFactory implements Call {
2129 private Request request ;
30+ private PubNub pubNub ;
2231
23- AppEngineFactory (Request request ) {
32+ AppEngineFactory (Request request , PubNub pubNub ) {
2433 this .request = request ;
34+ this .pubNub = pubNub ;
2535 }
2636
2737 @ Override
@@ -31,6 +41,8 @@ public Request request() {
3141
3242 @ Override
3343 public Response execute () throws IOException {
44+ request = signIfNecessary (request ); // We need this since we bypass the signature interceptor
45+
3446 URL url = request .url ().url ();
3547 final HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
3648 connection .setUseCaches (false );
@@ -83,6 +95,53 @@ public BufferedSource source() {
8395 return response ;
8496 }
8597
98+ // TODO: This should probably go in a util file so we don't have copied code from SignatureInterceptor
99+ public Request signIfNecessary (Request request ) {
100+ // only sign if we have a secret key in place.
101+ if (this .pubNub .getConfiguration ().getSecretKey () == null ) {
102+ return request ;
103+ }
104+
105+ HttpUrl url = request .url ();
106+ String requestURL = url .encodedPath ();
107+ int timestamp = this .pubNub .getTimestamp ();
108+ Map <String , String > queryParams = new HashMap <>();
109+ String signature = "" ;
110+
111+ for (String queryKey : url .queryParameterNames ()) {
112+ queryParams .put (queryKey , url .queryParameter (queryKey ));
113+ }
114+
115+ queryParams .put ("timestamp" , String .valueOf (timestamp ));
116+
117+ String signInput = pubNub .getConfiguration ().getSubscribeKey () + "\n "
118+ + pubNub .getConfiguration ().getPublishKey () + "\n " ;
119+
120+ if (requestURL .startsWith ("/v1/auth/audit" )) {
121+ signInput += "audit" + "\n " ;
122+ } else if (requestURL .startsWith ("/v1/auth/grant" )) {
123+ signInput += "grant" + "\n " ;
124+ } else {
125+ signInput += requestURL + "\n " ;
126+ }
127+
128+ signInput += PubNubUtil .preparePamArguments (queryParams );
129+
130+ try {
131+ signature = PubNubUtil .signSHA256 (pubNub .getConfiguration ().getSecretKey (), signInput );
132+ } catch (PubNubException e ) {
133+ log .warning ("signature failed on SignatureInterceptor: " + e .toString ());
134+ }
135+
136+ HttpUrl rebuiltUrl = url .newBuilder ()
137+ .addQueryParameter ("timestamp" , String .valueOf (timestamp ))
138+ .addQueryParameter ("signature" , signature )
139+ .build ();
140+
141+ request = request .newBuilder ().url (rebuiltUrl ).build ();
142+ return request ;
143+ }
144+
86145 @ Override
87146 public void enqueue (Callback responseCallback ) {
88147
@@ -113,9 +172,15 @@ public Call clone() {
113172 }
114173
115174 public static class Factory implements Call .Factory {
175+ private PubNub pubNub ;
176+
177+ public Factory (PubNub pubNub ) {
178+ this .pubNub = pubNub ;
179+ }
180+
116181 @ Override
117182 public Call newCall (Request request ) {
118- return new AppEngineFactory (request );
183+ return new AppEngineFactory (request , pubNub );
119184 }
120185 }
121186}
0 commit comments