Repository navigation
Expand file tree
/
Copy pathGrant.java
More file actions
238 lines (204 loc) · 9 KB
/
Copy pathGrant.java
File metadata and controls
238 lines (204 loc) · 9 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.pubnub.api.endpoints.access;
import com.google.gson.JsonElement;
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.managers.MapperManager;
import com.pubnub.api.managers.RetrofitManager;
import com.pubnub.api.managers.TelemetryManager;
import com.pubnub.api.managers.token_manager.TokenManager;
import com.pubnub.api.models.consumer.access_manager.PNAccessManagerGrantResult;
import com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData;
import com.pubnub.api.models.server.Envelope;
import com.pubnub.api.models.server.access_manager.AccessManagerGrantPayload;
import lombok.Setter;
import lombok.experimental.Accessors;
import retrofit2.Call;
import retrofit2.Response;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Accessors(chain = true, fluent = true)
public class Grant extends Endpoint<Envelope<AccessManagerGrantPayload>, PNAccessManagerGrantResult> {
@Setter
private boolean read;
@Setter
private boolean write;
@Setter
private boolean manage;
@Setter
private boolean delete;
@Setter
private boolean get;
@Setter
private boolean update;
@Setter
private boolean join;
@Setter
private Integer ttl;
@Setter
private List<String> authKeys;
@Setter
private List<String> channels;
@Setter
private List<String> channelGroups;
@Setter
private List<String> uuids = Collections.emptyList();
public Grant(PubNub pubnub,
TelemetryManager telemetryManager,
RetrofitManager retrofit,
TokenManager tokenManager) {
super(pubnub, telemetryManager, retrofit, tokenManager);
authKeys = new ArrayList<>();
channels = new ArrayList<>();
channelGroups = new ArrayList<>();
}
@Override
protected List<String> getAffectedChannels() {
return channels;
}
@Override
protected List<String> getAffectedChannelGroups() {
return channelGroups;
}
@Override
protected void validateParams() throws PubNubException {
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 ((!channels.isEmpty() || !channelGroups.isEmpty()) && !uuids.isEmpty()) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS)
.errormsg("Grants for channels or channelGroups can't be changed together with grants for UUIDs")
.build();
}
if (!uuids.isEmpty() && authKeys.isEmpty()) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS)
.errormsg("UUIDs grant management require providing non empty authKeys")
.build();
}
}
@Override
protected Call<Envelope<AccessManagerGrantPayload>> doWork(Map<String, String> queryParams) throws PubNubException {
if (channels.size() > 0) {
queryParams.put("channel", PubNubUtil.joinString(channels, ","));
}
if (channelGroups.size() > 0) {
queryParams.put("channel-group", PubNubUtil.joinString(channelGroups, ","));
}
if (uuids.size() > 0) {
queryParams.put("target-uuid", PubNubUtil.joinString(uuids, ","));
}
if (authKeys.size() > 0) {
queryParams.put("auth", PubNubUtil.joinString(authKeys, ","));
}
if (ttl != null && ttl >= -1) {
queryParams.put("ttl", String.valueOf(ttl));
}
queryParams.put("r", (read) ? "1" : "0");
queryParams.put("w", (write) ? "1" : "0");
queryParams.put("m", (manage) ? "1" : "0");
queryParams.put("d", (delete) ? "1" : "0");
queryParams.put("g", get ? "1" : "0");
queryParams.put("u", update ? "1" : "0");
queryParams.put("j", join ? "1" : "0");
return this.getRetrofit()
.getAccessManagerService()
.grant(this.getPubnub().getConfiguration().getSubscribeKey(), queryParams);
}
@Override
protected PNAccessManagerGrantResult createResponse(Response<Envelope<AccessManagerGrantPayload>> input) throws
PubNubException {
MapperManager mapperManager = getPubnub().getMapper();
PNAccessManagerGrantResult.PNAccessManagerGrantResultBuilder pnAccessManagerGrantResult =
PNAccessManagerGrantResult.builder();
if (input.body() == null || input.body().getPayload() == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();
}
AccessManagerGrantPayload data = input.body().getPayload();
Map<String, Map<String, PNAccessManagerKeyData>> constructedChannels = new HashMap<>();
Map<String, Map<String, PNAccessManagerKeyData>> constructedGroups = new HashMap<>();
// we have a case of a singular channel.
if (data.getChannel() != null) {
constructedChannels.put(data.getChannel(), data.getAuthKeys());
}
if (channelGroups != null) {
if (channelGroups.size() == 1) {
constructedGroups.put(mapperManager.elementToString(data.getChannelGroups()), data.getAuthKeys());
} else if (channelGroups.size() > 1) {
Iterator<Map.Entry<String, JsonElement>> it = mapperManager.getObjectIterator(data.getChannelGroups());
while (it.hasNext()) {
Map.Entry<String, JsonElement> channelGroup = it.next();
constructedGroups.put(channelGroup.getKey(), createKeyMap(channelGroup.getValue()));
}
}
}
if (data.getChannels() != null) {
for (String fetchedChannel : data.getChannels().keySet()) {
constructedChannels.put(fetchedChannel, data.getChannels().get(fetchedChannel).getAuthKeys());
}
}
Map<String, Map<String, PNAccessManagerKeyData>> constructedUuids = new HashMap<>();
if (data.getUuids() != null) {
for (String fetchedUuid : data.getUuids().keySet()) {
constructedUuids.put(fetchedUuid, data.getUuids().get(fetchedUuid).getAuthKeys());
}
}
return pnAccessManagerGrantResult
.subscribeKey(data.getSubscribeKey())
.level(data.getLevel())
.ttl(data.getTtl())
.channels(constructedChannels)
.channelGroups(constructedGroups)
.uuids(constructedUuids)
.build();
}
@Override
protected PNOperationType getOperationType() {
return PNOperationType.PNAccessManagerGrant;
}
@Override
protected boolean isAuthRequired() {
return false;
}
private Map<String, PNAccessManagerKeyData> createKeyMap(JsonElement input) {
Map<String, PNAccessManagerKeyData> result = new HashMap<>();
MapperManager mapper = getPubnub().getMapper();
Iterator<Map.Entry<String, JsonElement>> it = mapper.getObjectIterator(input, "auths");
while (it.hasNext()) {
Map.Entry<String, JsonElement> keyMap = it.next();
PNAccessManagerKeyData pnAccessManagerKeyData = PNAccessManagerKeyData.builder()
.manageEnabled(mapper.getAsBoolean(keyMap.getValue(), "m"))
.writeEnabled(mapper.getAsBoolean(keyMap.getValue(), "w"))
.readEnabled(mapper.getAsBoolean(keyMap.getValue(), "r"))
.deleteEnabled(mapper.getAsBoolean(keyMap.getValue(), "d"))
.getEnabled(mapper.getAsBoolean(keyMap.getValue(), "g"))
.updateEnabled(mapper.getAsBoolean(keyMap.getValue(), "u"))
.joinEnabled(mapper.getAsBoolean(keyMap.getValue(), "j")).build();
result.put(keyMap.getKey(), pnAccessManagerKeyData);
}
return result;
}
}