Git Product home page Git Product logo

auth-jwt's Introduction

JWT signature and encryption library (jwtk/jjwt wrapper)

Tip

see more about jwtk/jjwt

build coverage

guide

implement KeyFactory

@RequiredArgsConstructor
class PrivateKeyFactory implements KeyFactory<PrivateKey> {

    /**
     * Some repository for storing public keys
     */
    private final PublicKeyRepositury publicKeyRepository;

    /**
     * @return {@link java.security.PrivateKey} for sign JWT
     */
    public PrivateKey create(String kid) {
        var keys = Jwts.SIG.RS256.keyPair().build();
        publicKeyRepository.save(kid, keys.getPublic());
        return keys.getPrivate();
    }
}

implement KeyProvider

@RequiredArgsConstructor
class PublicKeyProvider implements KeyProvider<PrivateKey> {

    /**
     * Some repository for storing public keys
     */
    private final PublicKeyRepositury publicKeyRepository;

    /**
     * @return {@link java.security.PublicKey} for parse JWT
     */
    public PublicKey get(String kid) {
        return publicKeyRepository.get(kid);
    }
}

Important

use the private key to sign the token and the public key to parse it

Important

in the case of encryption, you must use a public key for encryption and a private key for decryption

start

public static void main(String[] args) {
    var publicKeyRepository = new PublicKeyRepositury();
    var keyFactory = new PrivateKeyFactory(publicKeyRepository);
    var keyProvider = new PublicKeyProvider(publicKeyRepository);
    var keyLocator = new JwsKeyLocator(keyProvider);
    var serializer = new JwsSerializer(keyFactory);             //use to sign token
    var deserializer = new SkinnyJwsDeserializer(keyLocator);   //use to parse token
    var issuedAt = Instant.now();
    var token = SkinnyJwt.builder()
            .tokenId(UUID.randomUUID().toString())
            .subject("some_username")
            .issuedAt(Date.from(issuedAt))
            .expiresAt(Date.from(issuedAt.plusSeconds(60 * 30))) //30 minutes
            .build();
    var serializedToken = serializer.serialize(token);
    System.out.println(serializedToken);
}

auth-jwt's People

Contributors

ocooleast avatar

Watchers

 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.