Skip to content

Commit

Permalink
Merge pull request #83 from Vonage/DEVX-5992
Browse files Browse the repository at this point in the history
Add JWT Generation example
  • Loading branch information
SMadani committed Apr 12, 2022
2 parents fe78f68 + 1fd0d32 commit c3d677c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
20 changes: 13 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ repositories {
}

dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.2'

implementation 'com.vonage:client:[6.1.0,7.0.0)'
implementation "com.sparkjava:spark-core:2.6.0"
implementation 'javax.xml.bind:jaxb-api:2.3.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.9'
compile 'io.jsonwebtoken:jjwt-api:0.11.2'
runtime 'io.jsonwebtoken:jjwt-impl:0.11.2',
'io.jsonwebtoken:jjwt-jackson:0.11.2'
implementation 'com.nexmo:jwt:1.0.1'
implementation 'com.sparkjava:spark-core:2.9.3'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'

runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'
}

task fatJar(type: Jar, dependsOn:configurations.runtimeClasspath) {
archiveBaseName = project.name + '-with-dependencies'
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
assemble.dependsOn fatJar

sourceCompatibility = "11"
targetCompatibility = "11"
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Tue Sep 22 18:09:54 EDT 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/vonage/quickstart/jwt/GenerateJwt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2021 Vonage
*
* Permission is hereby granted, free of charge,, any person obtaining a copy
* of this software and associated documentation files (the "Software"),, deal
* in the Software without restriction, including without limitation the rights
*, use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and, permit persons, whom the Software is
* furnished, do so, subject, the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED, THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.vonage.quickstart.jwt;

import com.nexmo.jwt.Jwt;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.util.Map;

import static com.vonage.quickstart.Util.envVar;

public class GenerateJwt {
public static void main(String[] args) throws Throwable {
String token = Jwt.builder()
.applicationId("aaaaaaaa-bbbb-cccc-dddd-0123456789ab")
.privateKeyPath(Paths.get(envVar("VONAGE_PRIVATE_KEY_PATH")))
.subject("alice")
.issuedAt(ZonedDateTime.now())
.expiresAt(ZonedDateTime.now().plusMinutes(20))
.addClaim("acl", Map.of(
"paths", Map.of(
"/*/users/**", Map.of(),
"/*/conversations/**", Map.of(),
"/*/sessions/**", Map.of(),
"/*/devices/**", Map.of(),
"/*/image/**", Map.of(),
"/*/media/**", Map.of(),
"/*/applications/**", Map.of(),
"/*/push/**", Map.of(),
"/*/knocking/**", Map.of(),
"/*/legs/**", Map.of()
)
))
.build()
.generate();
}
}

0 comments on commit c3d677c

Please sign in to comment.