jwt

Library for creating JWTs for use with Nexmo

License

License

MIT
GroupId

GroupId

com.nexmo
ArtifactId

ArtifactId

jwt
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

jwt
Library for creating JWTs for use with Nexmo
Project URL

Project URL

https://github.com/nexmo/nexmo-jwt-jdk
Project Organization

Project Organization

Nexmo
Source Code Management

Source Code Management

https://github.com/Nexmo/nexmo-jwt-jdk

Download jwt

How to add to project

<!-- https://jarcasting.com/artifacts/com.nexmo/jwt/ -->
<dependency>
    <groupId>com.nexmo</groupId>
    <artifactId>jwt</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.nexmo/jwt/
implementation 'com.nexmo:jwt:1.0.1'
// https://jarcasting.com/artifacts/com.nexmo/jwt/
implementation ("com.nexmo:jwt:1.0.1")
'com.nexmo:jwt:jar:1.0.1'
<dependency org="com.nexmo" name="jwt" rev="1.0.1">
  <artifact name="jwt" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.nexmo', module='jwt', version='1.0.1')
)
libraryDependencies += "com.nexmo" % "jwt" % "1.0.1"
[com.nexmo/jwt "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.3.31
io.jsonwebtoken : jjwt-api jar 0.10.5

runtime (2)

Group / Artifact Type Version
io.jsonwebtoken : jjwt-impl jar 0.10.5
io.jsonwebtoken : jjwt-jackson jar 0.10.5

test (4)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-test-junit jar 1.3.31
junit : junit jar 4.12
org.mockito : mockito-core jar 2.28.2
org.mockito : mockito-inline jar 2.28.2

Project Modules

There are no modules declared in this project.

Nexmo JWT JDK Library

Maven Central Build Status codecov

This library provides a wrapper for generating JWTs using Nexmo-specific claims.

Learn more about Authenticating with JSON Web Tokens.

Installation

For Gradle:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.nexmo:jwt:1.0.1'
}

For Maven:

<dependency>
  <groupId>com.nexmo</groupId>
  <artifactId>jwt</artifactId>
  <version>1.0.1</version>
</dependency>

Snapshot Repository

Snapshot releases happen periodically to test new functionality. If you'd like to use these versions, you can add the snapshot repository.

For Gradle:

repositories {
  maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots/'
  }
}

For Maven:

<repository>
  <id>sonatype-snapshot</id>
  <url>http://oss.sonatype.org/content/repositories/snapshots</url>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
</repository>

Usage

The JWT library provides a Jwt.Builder which can be used to construct a Jwt representation. The Jwt class contains a generate() method for generating JSON Web Signatures that can then be used to authenticate with the API.

Generating a JWT

The API requires an application_id claim, and the token needs to be signed with a private key. The corresponding public key is uploaded to Nexmo for signature verification. The library expects you to provide a PKCS#8 key contents or file path.

Generating a JWT with Private Key Contents

To generate a JWT with these properties you can use:

Kotlin
val jws = Jwt.builder()
    .applicationId("your-application-id")
    .privateKeyContents("private key contents")
    .build()
    .generate()
Java
String jws = Jwt.builder()
        .applicationId("your-application-id")
        .privateKeyContents("private key contents")
        .build()
        .generate();

Generating a JWT with Private Key Path

You can also provide a Path to the location of your private key:

Kotlin
val jws = Jwt.builder()
    .applicationId("your-application-id")
    .privateKeyPath(Paths.get("/path/to/private.key"))
    .build()
    .generate()
Java
String jws = Jwt.builder()
        .applicationId("your-application-id")
        .privateKeyPath(Paths.get("/path/to/private.key"))
        .build()
        .generate();

Generating a JWT with Custom Claims

In some instances, you might want to define custom claims.

Kotlin
// Add them individually using addClaim
val jws = Jwt.builder()
    .applicationId("your-application-id")
    .privateKeyPath(Paths.get("/path/to/private.key"))
    .addClaim("foo", "bar")
    .addClaim("bat", "baz")
    .build()
    .generate()

// Or add multiples using a map
val jws = Jwt.builder()
    .applicationId("your-application-id")
    .privateKeyPath(Paths.get("/path/to/private.key"))
    .claims(mapOf("foo" to "bar", "bat" to "baz"))
    .build()
    .generate()
Java
// Add them individually using addClaim
String jws = Jwt.builder()
        .applicationId("your-application-id")
        .privateKeyPath(Paths.get("/path/to/private.key"))
        .addClaim("foo", "bar")
        .addClaim("bat", "baz")
        .build()
        .generate();

// Or add multiples using a map
String jws = Jwt.builder()
        .applicationId("your-application-id")
        .privateKeyPath(Paths.get("/path/to/private.key"))
        .claims(Map.of("foo", "bar", "bat", "baz"))
        .build()
        .generate();
com.nexmo

Nexmo

Versions

Version
1.0.1
1.0.0