Coder Social home page Coder Social logo

faltobelli / okta-jwt-verifier-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from okta/okta-jwt-verifier-java

0.0 2.0 0.0 212 KB

okta-jwt-verifier-java

Home Page: https://github.com/okta/okta-jwt-verifier-java

Shell 14.38% Java 35.03% Groovy 50.59%

okta-jwt-verifier-java's Introduction

Maven Central License Support

Okta JWT Verifier for Java

As a result of a successful authentication by obtaining an authorization grant from a user or using the Okta API, you will be provided with a signed JWT (id_token and/or access_token). A common use case for these access tokens is to use it inside of the Bearer authentication header to let your application know who the user is that is making the request. In order for you to know this use is valid, you will need to know how to validate the token against Okta. This guide gives you an example of how to do this using Okta's JWT Validation library for Java.

If you are validating access tokens from a Spring application take a look at the Okta Spring Boot Starter.

Things you will need

For validating a JWT, you will need a few different items:

  1. Your issuer URL
  2. The JWT string you want to verify
  3. The Okta JWT Verifier for Java library, for example in your Apache Maven pom.xml:
  <dependency>
    <groupId>com.okta.jwt</groupId>
    <artifactId>okta-jwt-verifier</artifactId>
    <version>${okta-jwt.version}</version>
  </dependency>

Setting up the Library

The Okta JWT Verifier can created via a fluent JwtHelper class:

JwtVerifier jwtVerifier = new JwtHelper()
    .setIssuerUrl("https://{yourOktaDomain}/oauth2/default")
    .setAudience("api://default")  // defaults to 'api://default'
    .setConnectionTimeout(1000)    // defaults to 1000ms
    .setReadTimeout(1000)          // defaults to 1000ms
    .setClientId("your_client_id") // optional
    .build();

This helper class configures a JWT parser with the details found via the OpenID Connect discovery endpoint. The public keys used to validate the JWTs will also be retrieved and cached automatically.

Validating a JWT

After you have a JwtVerifier from above section and a access_token from a successful login, or from the Bearer token in the authorization header, you will need to make sure that this is still valid. All you need to do is call the decode method (where jwtString is your access token in string format).

Jwt jwt = jwtVerifier.decodeAccessToken(jwtString);

This will validate your JWT for the following:

  • Token expiration date
  • Valid token not before date
  • The token issuer matches the expected value passed into the above helper
  • The token audience matches the expected value passed into the above helper

The result from the decode method is a Jwt object which you can introspect additional claims by calling:

jwt.getClaims().get("aClaimKey");

Conclusion

The above are the basic steps for verifying an access token locally. The steps are not tied directly to a framework so you could plug in the okta-jwt-verifier into the framework of your choice (Dropwizard, Guice, Servlet API, or JAX-RS).

For more information on this project take a look at the following resources:

okta-jwt-verifier-java's People

Contributors

bdemers avatar oktauploader-okta avatar

Watchers

 avatar  avatar

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.