Skip to content

Commit 9477534

Browse files
author
Max Presman
committed
signature interceptor
1 parent b0fda76 commit 9477534

File tree

5 files changed

+80
-28
lines changed

5 files changed

+80
-28
lines changed

src/main/java/com/pubnub/api/endpoints/access/Audit.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ protected void validateParams() throws PubNubException {
5555

5656
@Override
5757
protected Call<Envelope<AccessManagerAuditPayload>> doWork(Map<String, String> queryParams) throws PubNubException {
58-
String signature;
59-
60-
int timestamp = this.getPubnub().getTimestamp();
61-
62-
String signInput = this.getPubnub().getConfiguration().getSubscribeKey() + "\n"
63-
+ this.getPubnub().getConfiguration().getPublishKey() + "\n"
64-
+ "audit" + "\n";
65-
66-
queryParams.put("timestamp", String.valueOf(timestamp));
6758

6859
if (channel != null) {
6960
queryParams.put("channel", channel);
@@ -77,12 +68,6 @@ protected Call<Envelope<AccessManagerAuditPayload>> doWork(Map<String, String> q
7768
queryParams.put("auth", PubNubUtil.joinString(authKeys, ","));
7869
}
7970

80-
signInput += PubNubUtil.preparePamArguments(queryParams);
81-
82-
signature = PubNubUtil.signSHA256(this.getPubnub().getConfiguration().getSecretKey(), signInput);
83-
84-
queryParams.put("signature", signature);
85-
8671
AccessManagerService service = this.getRetrofit().create(AccessManagerService.class);
8772
return service.audit(this.getPubnub().getConfiguration().getSubscribeKey(), queryParams);
8873
}

src/main/java/com/pubnub/api/endpoints/access/Grant.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ protected void validateParams() throws PubNubException {
7474

7575
@Override
7676
protected Call<Envelope<AccessManagerGrantPayload>> doWork(Map<String, String> queryParams) throws PubNubException {
77-
String signature;
78-
79-
String signInput = this.getPubnub().getConfiguration().getSubscribeKey() + "\n"
80-
+ this.getPubnub().getConfiguration().getPublishKey() + "\n"
81-
+ "grant" + "\n";
82-
83-
queryParams.put("timestamp", String.valueOf(this.getPubnub().getTimestamp()));
8477

8578
if (channels.size() > 0) {
8679
queryParams.put("channel", PubNubUtil.joinString(channels, ","));
@@ -102,12 +95,6 @@ protected Call<Envelope<AccessManagerGrantPayload>> doWork(Map<String, String> q
10295
queryParams.put("w", (write) ? "1" : "0");
10396
queryParams.put("m", (manage) ? "1" : "0");
10497

105-
signInput += PubNubUtil.preparePamArguments(queryParams);
106-
107-
signature = PubNubUtil.signSHA256(this.getPubnub().getConfiguration().getSecretKey(), signInput);
108-
109-
queryParams.put("signature", signature);
110-
11198
AccessManagerService service = this.getRetrofit().create(AccessManagerService.class);
11299
return service.grant(this.getPubnub().getConfiguration().getSubscribeKey(), queryParams);
113100
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.pubnub.api.interceptors;
2+
3+
import com.pubnub.api.PubNub;
4+
import com.pubnub.api.PubNubException;
5+
import com.pubnub.api.PubNubUtil;
6+
import okhttp3.HttpUrl;
7+
import okhttp3.Interceptor;
8+
import okhttp3.Request;
9+
import okhttp3.Response;
10+
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
16+
public class SignatureInterceptor implements Interceptor {
17+
18+
PubNub pubNub;
19+
20+
public SignatureInterceptor(PubNub pubNubInstance) {
21+
this.pubNub = pubNubInstance;
22+
}
23+
24+
@Override
25+
public Response intercept(Chain chain) throws IOException {
26+
Request originalRequest = chain.request();
27+
28+
// only sign if we have a secret key in place.
29+
if (this.pubNub.getConfiguration().getSecretKey() == null) {
30+
return chain.proceed(originalRequest);
31+
}
32+
33+
HttpUrl url = chain.request().url();
34+
String requestURL = url.encodedPath();
35+
int timestamp = this.pubNub.getTimestamp();
36+
Map<String, String> queryParams = new HashMap<>();
37+
String signature = "";
38+
39+
for (String queryKey: url.queryParameterNames()) {
40+
queryParams.put(queryKey, url.queryParameter(queryKey));
41+
}
42+
43+
queryParams.put("timestamp", String.valueOf(timestamp));
44+
45+
String signInput = pubNub.getConfiguration().getSubscribeKey() + "\n"
46+
+ pubNub.getConfiguration().getPublishKey() + "\n";
47+
48+
if (requestURL.startsWith("/v1/auth/audit")) {
49+
signInput += "audit" + "\n";
50+
} else if (requestURL.startsWith("/v1/auth/grant")) {
51+
signInput += "grant" + "\n";
52+
} else {
53+
int moose = 10;
54+
}
55+
56+
signInput += PubNubUtil.preparePamArguments(queryParams);
57+
58+
try {
59+
signature = PubNubUtil.signSHA256(pubNub.getConfiguration().getSecretKey(), signInput);
60+
} catch (PubNubException e) {
61+
int moose = 11;
62+
}
63+
64+
HttpUrl rebuiltUrl = url.newBuilder()
65+
.addQueryParameter("timestamp", String.valueOf(timestamp))
66+
.addQueryParameter("signature", signature)
67+
.build();
68+
Request request = chain.request().newBuilder().url(rebuiltUrl).build();
69+
return chain.proceed(request);
70+
}
71+
}

src/main/java/com/pubnub/api/managers/RetrofitManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.pubnub.api.PubNub;
55
import com.pubnub.api.enums.PNLogVerbosity;
6+
import com.pubnub.api.interceptors.SignatureInterceptor;
67
import lombok.Getter;
78
import okhttp3.OkHttpClient;
89
import okhttp3.logging.HttpLoggingInterceptor;
@@ -16,6 +17,8 @@ public class RetrofitManager {
1617

1718
private PubNub pubnub;
1819

20+
private SignatureInterceptor signatureInterceptor;
21+
1922
private OkHttpClient transactionClientInstance;
2023
private OkHttpClient subscriptionClientInstance;
2124

@@ -25,6 +28,8 @@ public class RetrofitManager {
2528
public RetrofitManager(PubNub pubNubInstance) {
2629
this.pubnub = pubNubInstance;
2730

31+
this.signatureInterceptor = new SignatureInterceptor(pubNubInstance);
32+
2833
this.transactionClientInstance = createOkHttpClient(
2934
this.pubnub.getConfiguration().getNonSubscribeRequestTimeout(),
3035
this.pubnub.getConfiguration().getConnectTimeout()
@@ -54,6 +59,8 @@ private OkHttpClient createOkHttpClient(int requestTimeout, int connectTimeOut)
5459
httpClient.proxy(pubnub.getConfiguration().getProxy());
5560
}
5661

62+
httpClient.addInterceptor(this.signatureInterceptor);
63+
5764
return httpClient.build();
5865
}
5966

src/test/java/com/pubnub/api/endpoints/access/AuditEndpointTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public void beforeEach() throws IOException {
4646
@Test
4747
public void testSuccessChannelGroupSync() throws PubNubException {
4848

49+
// http://localhost:8080/v1/auth/audit/sub-key/mySubscribeKey?pnsdk=PubNub-Java-Unified/suchJava&channel-group=cg1&instanceid=PubNubInstanceId&auth=key1&requestid=PubNubRequestId&uuid=myUUID&timestamp=1337&signature=rnb_-C8C4twE5IlyMeSlTyF4538WNv4uKCQu6jQwggU%3D%0A
50+
4951
stubFor(get(urlPathEqualTo("/v1/auth/audit/sub-key/mySubscribeKey"))
5052
.withQueryParam("pnsdk", matching("PubNub-Java-Unified/suchJava"))
5153
.withQueryParam("instanceid", matching("PubNubInstanceId"))

0 commit comments

Comments
 (0)