Repository navigation
Expand file tree
/
Copy pathAudit.java
More file actions
120 lines (95 loc) · 4.59 KB
/
Copy pathAudit.java
File metadata and controls
120 lines (95 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.pubnub.api.endpoints.access;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.PubNubUtil;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.endpoints.Endpoint;
import com.pubnub.api.enums.PNOperationType;
import com.pubnub.api.models.consumer.access_manager.PNAccessManagerAuditResult;
import com.pubnub.api.models.server.Envelope;
import com.pubnub.api.models.server.access_manager.AccessManagerAuditPayload;
import lombok.Setter;
import lombok.experimental.Accessors;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Accessors(chain = true, fluent = true)
public class Audit extends Endpoint<Envelope<AccessManagerAuditPayload>, PNAccessManagerAuditResult> {
@Setter
private List<String> authKeys;
@Setter
private String channel;
@Setter
private String channelGroup;
public Audit(PubNub pubnub, Retrofit retrofit) {
super(pubnub, retrofit);
authKeys = new ArrayList<>();
}
@Override
protected void validateParams() throws PubNubException {
if (authKeys.size() == 0) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_AUTH_KEYS_MISSING).build();
}
if (this.getPubnub().getConfiguration().getSecretKey() == null || this.getPubnub().getConfiguration().getSecretKey().isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_SECRET_KEY_MISSING).build();
}
if (this.getPubnub().getConfiguration().getSubscribeKey() == null || this.getPubnub().getConfiguration().getSubscribeKey().isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_SUBSCRIBE_KEY_MISSING).build();
}
if (this.getPubnub().getConfiguration().getPublishKey() == null || this.getPubnub().getConfiguration().getPublishKey().isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PUBLISH_KEY_MISSING).build();
}
if (channel == null && channelGroup == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_CHANNEL_AND_GROUP_MISSING).build();
}
}
@Override
protected Call<Envelope<AccessManagerAuditPayload>> doWork(Map<String, String> queryParams) throws PubNubException {
String signature;
int timestamp = this.getPubnub().getTimestamp();
String signInput = this.getPubnub().getConfiguration().getSubscribeKey() + "\n"
+ this.getPubnub().getConfiguration().getPublishKey() + "\n"
+ "audit" + "\n";
queryParams.put("timestamp", String.valueOf(timestamp));
if (channel != null) {
queryParams.put("channel", channel);
}
if (channelGroup != null) {
queryParams.put("channel-group", channelGroup);
}
if (authKeys.size() > 0) {
queryParams.put("auth", PubNubUtil.joinString(authKeys, ","));
}
signInput += PubNubUtil.preparePamArguments(queryParams);
signature = PubNubUtil.signSHA256(this.getPubnub().getConfiguration().getSecretKey(), signInput);
queryParams.put("signature", signature);
AccessManagerService service = this.getRetrofit().create(AccessManagerService.class);
return service.audit(this.getPubnub().getConfiguration().getSubscribeKey(), queryParams);
}
@Override
protected PNAccessManagerAuditResult createResponse(final Response<Envelope<AccessManagerAuditPayload>> input) throws PubNubException {
PNAccessManagerAuditResult.PNAccessManagerAuditResultBuilder pnAccessManagerAuditResult = PNAccessManagerAuditResult.builder();
if (input.body() == null || input.body().getPayload() == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();
}
AccessManagerAuditPayload auditPayload = input.body().getPayload();
pnAccessManagerAuditResult
.authKeys(auditPayload.getAuthKeys())
.channel(auditPayload.getChannel())
.channelGroup(auditPayload.getChannelGroup())
.level(auditPayload.getLevel())
.subscribeKey(auditPayload.getSubscribeKey());
return pnAccessManagerAuditResult.build();
}
@Override
protected PNOperationType getOperationType() {
return PNOperationType.PNAccessManagerAudit;
}
@Override
protected boolean isAuthRequired() {
return false;
}
}