Skip to content

Commit

Permalink
Add Revoke Token (#221)
Browse files Browse the repository at this point in the history
Add Revoke Token
  • Loading branch information
kleewho authored Dec 16, 2021
1 parent cc10618 commit d82596d
Show file tree
Hide file tree
Showing 26 changed files with 338 additions and 49 deletions.
18 changes: 12 additions & 6 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: java
version: v5.2.4
version: v5.3.0
schema: 1
scm: github.com/pubnub/java
files:
- build/libs/pubnub-gson-5.2.4-all.jar
- build/libs/pubnub-gson-5.3.0-all.jar
sdks:
-
type: library
Expand All @@ -23,8 +23,8 @@ sdks:
-
distribution-type: library
distribution-repository: git release
package-name: pubnub-gson-5.2.4
location: https://github.com/pubnub/java/releases/download/v5.2.4/pubnub-gson-5.2.4-all.jar
package-name: pubnub-gson-5.3.0
location: https://github.com/pubnub/java/releases/download/v5.3.0/pubnub-gson-5.3.0-all.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -135,8 +135,8 @@ sdks:
-
distribution-type: library
distribution-repository: maven
package-name: pubnub-gson-5.2.4
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/5.2.4/pubnub-gson-5.2.4.jar
package-name: pubnub-gson-5.3.0
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/5.3.0/pubnub-gson-5.3.0.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -234,6 +234,11 @@ sdks:
is-required: Required

changelog:
- date: 2021-12-16
version: v5.3.0
changes:
- type: feature
text: "Add revoke token feature."
- date: 2021-12-09
version: v5.2.4
changes:
Expand Down Expand Up @@ -773,6 +778,7 @@ features:
- ACCESS-GRANT-TOKEN
- ACCESS-PARSE-TOKEN
- ACCESS-SET-TOKEN
- ACCESS-REVOKE-TOKEN
channel-groups:
- CHANNEL-GROUPS-ADD-CHANNELS
- CHANNEL-GROUPS-REMOVE-CHANNELS
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v5.3.0
December 16 2021

#### Added
- Add revoke token feature.

## v5.2.4
December 09 2021

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ You will need the publish and subscribe keys to authenticate your app. Get your
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-gson</artifactId>
<version>5.2.4</version>
<version>5.3.0</version>
</dependency>
```

* for Gradle, add the following dependency in your `gradle.build`:
```groovy
compile group: 'com.pubnub', name: 'pubnub-gson', version: '5.2.1'
compile group: 'com.pubnub', name: 'pubnub-gson', version: '5.2.3'
```

2. Configure your keys:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}
group = 'com.pubnub'

version = '5.2.4'
version = '5.3.0'

description = """"""

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/pubnub/api/PubNub.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.pubnub.api.endpoints.Time;
import com.pubnub.api.endpoints.access.Grant;
import com.pubnub.api.endpoints.access.GrantToken;
import com.pubnub.api.endpoints.access.RevokeToken;
import com.pubnub.api.endpoints.channel_groups.AddChannelChannelGroup;
import com.pubnub.api.endpoints.channel_groups.AllChannelsChannelGroup;
import com.pubnub.api.endpoints.channel_groups.DeleteChannelGroup;
Expand Down Expand Up @@ -103,7 +104,7 @@ public class PubNub {
private static final int TIMESTAMP_DIVIDER = 1000;
private static final int MAX_SEQUENCE = 65535;

private static final String SDK_VERSION = "5.2.4";
private static final String SDK_VERSION = "5.3.0";
private final ListenerManager listenerManager;
private final StateManager stateManager;

Expand Down Expand Up @@ -233,6 +234,11 @@ public GrantToken grantToken() {
return new GrantToken(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}

@NotNull
public RevokeToken revokeToken() {
return new RevokeToken(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}

@NotNull
public GetState getPresenceState() {
return new GetState(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/pubnub/api/PubNubUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ public static String joinString(List<String> val, String delim) {
builder.append(delim);
}

return builder.toString().substring(0, builder.toString().length() - 1);
return builder.substring(0, builder.toString().length() - delim.length());

}

public static String joinLong(List<Long> val, String delim) {
StringBuilder builder = new StringBuilder();
for (Long l : val) {
builder.append(Long.toString(l).toLowerCase());
builder.append(",");
builder.append(delim);
}

return builder.toString().substring(0, builder.toString().length() - 1);
return builder.substring(0, builder.toString().length() - delim.length());

}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/pubnub/api/builder/PubNubErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ public final class PubNubErrorBuilder {
*/
public static final int PNERR_PAYLOAD_TOO_LARGE = 167;

/**
* Token missing
*/
public static final int PNERR_TOKEN_MISSING = 168;

// Error Objects
public static final PubNubError PNERROBJ_TIMEOUT = PubNubError.builder()
.errorCode(PNERR_TIMEOUT)
Expand Down Expand Up @@ -690,6 +695,11 @@ public final class PubNubErrorBuilder {
.message("Payload too large.")
.build();

public static final PubNubError PNERROBJ_TOKEN_MISSING = PubNubError.builder()
.errorCode(PNERR_TOKEN_MISSING)
.message("Token missing.")
.build();

private PubNubErrorBuilder() {

}
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/com/pubnub/api/endpoints/access/RevokeToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.pubnub.api.endpoints.access;

import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.endpoints.Endpoint;
import com.pubnub.api.enums.PNOperationType;
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.v3.PNRevokeTokenResult;
import com.pubnub.api.models.server.access_manager.v3.RevokeTokenResponse;
import lombok.Setter;
import lombok.experimental.Accessors;
import retrofit2.Call;
import retrofit2.Response;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Accessors(chain = true, fluent = true)
public class RevokeToken extends Endpoint<RevokeTokenResponse, PNRevokeTokenResult> {

@Setter
private String token;

public RevokeToken(PubNub pubnub,
TelemetryManager telemetryManager,
RetrofitManager retrofit,
TokenManager tokenManager) {
super(pubnub, telemetryManager, retrofit, tokenManager);
}

@Override
protected List<String> getAffectedChannels() {
return null;
}

@Override
protected List<String> getAffectedChannelGroups() {
return null;
}

@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.token == null) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_TOKEN_MISSING)
.build();
}
}

@Override
protected Call<RevokeTokenResponse> doWork(Map<String, String> baseParams) throws PubNubException {
return getRetrofit()
.getAccessManagerService()
.revokeToken(getPubnub().getConfiguration().getSubscribeKey(), repairEncoding(token), baseParams);
}

@Override
protected PNRevokeTokenResult createResponse(Response<RevokeTokenResponse> input) throws PubNubException {
if (input.body() == null) {
return null;
}

return new PNRevokeTokenResult();
}

@Override
protected PNOperationType getOperationType() {
return PNOperationType.PNAccessManagerRevokeToken;
}

@Override
protected boolean isAuthRequired() {
return false;
}

private String repairEncoding(String token) throws PubNubException {
try {
return URLEncoder.encode(token, "utf-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_PUBNUB_ERROR)
.cause(e)
.build();
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/pubnub/api/enums/PNOperationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public enum PNOperationType {

// PAMv3
PNAccessManagerGrantToken,
PNAccessManagerRevokeToken,

// Message Actions
PNAddMessageAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private static String endpointNameForOperation(PNOperationType type) {
endpoint = "obj";
break;
case PNAccessManagerGrantToken:
case PNAccessManagerRevokeToken:
endpoint = "pamv3";
break;
case PNAddMessageAction:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pubnub.api.models.consumer.access_manager.v3;

import lombok.Data;

@Data
public class PNRevokeTokenResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.pubnub.api.models.server.access_manager.v3;

import lombok.Data;

@Data
public class RevokeTokenResponse {

private final int status;
private final RevokeTokenData data;
private final String service;

@Data
public static class RevokeTokenData {
private final String message;
private final String token;
}
}

12 changes: 7 additions & 5 deletions src/main/java/com/pubnub/api/services/AccessManagerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import com.google.gson.JsonObject;
import com.pubnub.api.models.server.Envelope;
import com.pubnub.api.models.server.access_manager.AccessManagerGrantPayload;
import com.pubnub.api.models.server.access_manager.v3.RevokeTokenResponse;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
import retrofit2.http.*;

import java.util.Map;

Expand All @@ -22,4 +19,9 @@ Call<Envelope<AccessManagerGrantPayload>> grant(@Path("subKey") String subKey,
Call<JsonObject> grantToken(@Path("subKey") String subKey,
@Body Object body,
@QueryMap Map<String, String> options);

@DELETE("/v3/pam/{subKey}/grant/{token}")
Call<RevokeTokenResponse> revokeToken(@Path("subKey") String subKey,
@Path(value = "token", encoded = true) String token,
@QueryMap Map<String, String> queryParams);
}
2 changes: 1 addition & 1 deletion src/test/java/com/pubnub/api/PubNubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void getVersionAndTimeStamp() {
pubnub = new PubNub(pnConfiguration);
String version = pubnub.getVersion();
int timeStamp = pubnub.getTimestamp();
Assert.assertEquals("5.2.4", version);
Assert.assertEquals("5.3.0", version);
Assert.assertTrue(timeStamp > 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.endpoints.TestHarness;
import com.pubnub.api.enums.PNLogVerbosity;
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGrant;
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGroupGrant;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.Objects;

import static com.pubnub.api.builder.PubNubErrorBuilder.PNERR_RESOURCES_MISSING;
import static com.pubnub.api.builder.PubNubErrorBuilder.PNERR_SECRET_KEY_MISSING;
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/pubnub/contract/ContractTestConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ interface ContractTestConfig : Config {
@Config.Key("pamSecKey")
fun pamSecKey(): String?

@Config.Key("subKey")
fun subKey(): String?

@Config.Key("pubKey")
fun pubKey(): String?

@Config.Key("serverHostPort")
fun serverHostPort(): String

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/pubnub/contract/Hooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ class Hooks {
}
.firstOrNull()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ fun PNToken.patternPermissionsMap(resourceType: ResourceType): Map<String, PNTok
ResourceType.CHANNEL_GROUP -> patterns.channelGroups
ResourceType.UUID -> patterns.uuids
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class GrantTokenState {
field = value
}
var currentResourcePermissions: PNToken.PNResourcePermissions? = null
}
}
Loading

0 comments on commit d82596d

Please sign in to comment.