diff --git a/CHANGELOG.md b/CHANGELOG.md index f81915b3..24d21223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## [5.12.0](https://github.com/plivo/plivo-java/tree/v5.12.0) (2022-08-08) +**Feature - Token Creation** +- `JWT Token Creation API` added API to create a new JWT token. + + ## [5.11.0](https://github.com/plivo/plivo-java/tree/v5.11.0) (2022-05-05) **Feature - List all recordings** - `fromNumber` and `toNumber` added to filtering param [List all recordings](https://www.plivo.com/docs/voice/api/recording#list-all-recordings) diff --git a/README.md b/README.md index 9ba1a697..074f7060 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ If you are using Maven, use the following XML to include the Plivo SDK as a depe com.plivo plivo-java - 5.11.0 + 5.12.0 ``` diff --git a/build.gradle b/build.gradle index eccce3af..95cb5503 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ buildscript { dependencies { classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.0' } + } buildscript { @@ -23,7 +24,6 @@ buildscript { classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.12.0" } } - apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'java' apply plugin: 'checkstyle' @@ -53,6 +53,7 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'com.squareup.okhttp', name: 'mockwebserver', version: '2.7.5' + compile 'com.googlecode.json-simple:json-simple:1.1.1' compile 'com.squareup.retrofit2:converter-jackson:2.2.0' compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.okhttp3:logging-interceptor:3.7.0' diff --git a/pom.properties b/pom.properties index 74c96996..6f6c38c7 100644 --- a/pom.properties +++ b/pom.properties @@ -1,5 +1,5 @@ # Written manually. -version=5.11.0 +version=5.12.0 groupId=com.plivo artifactId=plivo-java diff --git a/pom.xml b/pom.xml index c7798573..70771ce5 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.plivo plivo-java - 5.11.0 + 5.12.0 plivo-java A Java SDK to make voice calls & send SMS using Plivo and to generate Plivo XML @@ -40,6 +40,11 @@ 2.2.0 compile + + com.googlecode.json-simple + json-simple + 1.1.1 + com.squareup.okhttp3 logging-interceptor @@ -76,5 +81,7 @@ 2.7.5 test + - \ No newline at end of file + + diff --git a/src/main/java/com/plivo/api/PlivoAPIService.java b/src/main/java/com/plivo/api/PlivoAPIService.java index 4662498e..143c9c61 100644 --- a/src/main/java/com/plivo/api/PlivoAPIService.java +++ b/src/main/java/com/plivo/api/PlivoAPIService.java @@ -34,6 +34,8 @@ import com.plivo.api.models.recording.Recording; import com.plivo.api.models.brand.*; import com.plivo.api.models.campaign.*; +import com.plivo.api.models.token.TokenCreateResponse; +import com.plivo.api.models.token.TokenCreator; import okhttp3.RequestBody; import okhttp3.ResponseBody; import retrofit2.Call; @@ -280,6 +282,9 @@ Call recordingDelete(@Path("authId") String authId, Call endpointCreate(@Path("authId") String authId, @Body EndpointCreator endpointCreator); + @POST("Account/{authId}/JWT/Token/") + Call tokenCreate(@Path("authId") String authId, + @Body TokenCreator tokenCreator); @GET("Account/{authId}/Endpoint/") Call> endpointList(@Path("authId") String authId, @QueryMap Map params); diff --git a/src/main/java/com/plivo/api/models/token/Token.java b/src/main/java/com/plivo/api/models/token/Token.java new file mode 100644 index 00000000..e81e32f7 --- /dev/null +++ b/src/main/java/com/plivo/api/models/token/Token.java @@ -0,0 +1,62 @@ +package com.plivo.api.models.token; + +import com.plivo.api.models.base.BaseResource; +import com.plivo.api.util.Utils; +import org.json.simple.JSONObject; +import retrofit2.Call; + +public class Token extends BaseResource { + + private String iss; + + private String sub; + + private Integer nbf; + + private Integer exp; + + private Boolean incoming_allow; + + private Boolean outgoing_allow; + + private String app; + private JSONObject per; + + public static TokenCreator creator(String iss) { + if(!Utils.allNotNull(iss)){ + throw new IllegalArgumentException("iss cannot be null"); + } + + return new TokenCreator(iss); + } + + public String getIss() { + return iss; + } + + public String getSub() { + return sub; + } + public Integer getNbf() { + return nbf; + } + public Integer getExp() { + return exp; + } + public Boolean getIncoming_allow() { + return incoming_allow; + } + public Boolean getOutgoing_allow() { + return outgoing_allow; + } + public String getApp() { + return app; + } + public JSONObject getPer() { + return per; + } + + public String getId() { + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/plivo/api/models/token/TokenCreateResponse.java b/src/main/java/com/plivo/api/models/token/TokenCreateResponse.java new file mode 100644 index 00000000..36092d6f --- /dev/null +++ b/src/main/java/com/plivo/api/models/token/TokenCreateResponse.java @@ -0,0 +1,16 @@ +package com.plivo.api.models.token; + +import com.plivo.api.models.base.BaseResponse; + +public class TokenCreateResponse extends BaseResponse { + private String api_id; + private String token; + public String getApi_id() { + return api_id; + } + + public String getToken() { + return token; + } + +} diff --git a/src/main/java/com/plivo/api/models/token/TokenCreator.java b/src/main/java/com/plivo/api/models/token/TokenCreator.java new file mode 100644 index 00000000..96c9d2c0 --- /dev/null +++ b/src/main/java/com/plivo/api/models/token/TokenCreator.java @@ -0,0 +1,93 @@ +package com.plivo.api.models.token; + +import com.plivo.api.models.base.VoiceCreator; +import retrofit2.Call; +import org.json.simple.* ; + + +public class TokenCreator extends VoiceCreator { + + private final String iss; + private String sub; + private Integer nbf; + private Integer exp; + private String app; + private Boolean incoming_allow; + private Boolean outgoing_allow; + private JSONObject per; + + + + + public TokenCreator(String iss) { + this.iss = iss; + } + public String sub() { + return this.sub; + } + public TokenCreator sub(final String sub) { + this.sub = sub; + return this; + } + public Integer nbf() { + return this.nbf; + } + public TokenCreator nbf(final Integer nbf) { + this.nbf = nbf; + return this; + } + public Integer exp() { + return this.exp; + } + public TokenCreator exp(final Integer exp) { + this.exp = exp; + return this; + } + public String app() { + return this.app; + } + public TokenCreator app(final String app) { + this.app = app; + return this; + } + public boolean incoming_allow() { + return this.incoming_allow; + } + public TokenCreator incoming_allow(final boolean incoming_allow) { + this.incoming_allow = incoming_allow; + return this; + } + public boolean outgoing_allow() { + return this.outgoing_allow; + } + public TokenCreator outgoing_allow(final boolean outgoing_allow) { + this.outgoing_allow = outgoing_allow; + return this; + } + public JSONObject per() { + return this.per; + } + public TokenCreator per(final JSONObject per) { + JSONObject permission = new JSONObject(); + JSONObject voice = new JSONObject(); + voice.put("outgoing_allow", outgoing_allow); + permission.put("voice", voice); + this.per = permission; + return this; + } + + @Override + protected Call obtainCall() { + return client().getVoiceApiService().tokenCreate(client().getAuthId(),this); + } + @Override + protected Call obtainFallback1Call() { + return client().getVoiceFallback1Service().tokenCreate(client().getAuthId(),this); + } + @Override + protected Call obtainFallback2Call() { + return client().getVoiceFallback2Service().tokenCreate(client().getAuthId(),this); + } + + +} \ No newline at end of file diff --git a/src/main/resources/com/plivo/api/version.txt b/src/main/resources/com/plivo/api/version.txt index c68d476c..dd0ad7ae 100644 --- a/src/main/resources/com/plivo/api/version.txt +++ b/src/main/resources/com/plivo/api/version.txt @@ -1 +1 @@ -5.11.0 +5.12.0 diff --git a/src/test/java/com/plivo/api/TokenTest.java b/src/test/java/com/plivo/api/TokenTest.java new file mode 100644 index 00000000..c917b976 --- /dev/null +++ b/src/test/java/com/plivo/api/TokenTest.java @@ -0,0 +1,34 @@ +package com.plivo.api; + +import com.plivo.api.models.token.Token; +import org.junit.Before; +import org.junit.Test; + +public class TokenTest extends BaseTest { + + private PlivoClient client; + + @Before + public void setUp() throws Exception { + super.setUp(); + client = new PlivoClient("MA123456789012345678", + "Zmxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + } + + @Test + public void tokenCreateWithClientShouldWork() throws Exception { + expectResponse("tokenCreateResponse.json", 201); + + Token.creator("MA123456789012345678") + .client(client) + .create(); + + assertRequest("POST", "JWT/Token/"); + } + + @Test(expected = IllegalArgumentException.class) + public void tokenCreateShouldFailWithoutAllParams() throws Exception { + Token.creator(null) + .create(); + } +} diff --git a/src/test/resources/com/plivo/api/tokenCreateResponse.json b/src/test/resources/com/plivo/api/tokenCreateResponse.json new file mode 100644 index 00000000..7d2415cf --- /dev/null +++ b/src/test/resources/com/plivo/api/tokenCreateResponse.json @@ -0,0 +1,4 @@ +{ + "api_id": "a9acb782-11e9-11ed-96c8-0242ac110002", + "token":"eyJhbGciOiJIUzI1NiIsImN0eSI6InBsaXZvO3Y9MSIsInR5cCI6IkpXVCJ9.eyJhcHAiOiIiLCJleHAiOjE2NTk0Nzk0NzksImlzcyI6Ik1BTURWTFpKWTJaR1k1TVdVMVpKIiwibmJmIjoxNjU5MzkzMDc5LCJwZXIiOnsidm9pY2UiOnsiaW5jb21pbmdfYWxsb3ciOmZhbHNlLCJvdXRnb2luZ19hbGxvdyI6ZmFsc2V9fSwic3ViIjoia293c2hpayJ9.dzmLy1JZb6Z9i7MzomCCh3cxaqyA_78O87E8ASkjk6M" +} \ No newline at end of file