Giter Site home page Giter Site logo

nexmo-jwt-jdk's Introduction

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();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.