All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.auth0.jwt.impl.HeaderDeserializer Maven / Gradle / Ivy

Go to download

This is a drop in replacement for the auth0 java-jwt library (see https://github.com/auth0/java-jwt). This jar makes sure there are no external dependencies (e.g. fasterXml, Apacha Commons) needed. This is useful when deploying to an application server (e.g. tomcat with Alfreso or Pega).

The newest version!
package com.auth0.jwt.impl;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.util.Map;

class HeaderDeserializer extends StdDeserializer {

    HeaderDeserializer() {
        this(null);
    }

    private HeaderDeserializer(Class vc) {
        super(vc);
    }

    @Override
    public BasicHeader deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        Map tree = p.getCodec().readValue(p, new TypeReference>() {
        });
        if (tree == null) {
            throw new JWTDecodeException("Parsing the Header's JSON resulted on a Null map");
        }

        String algorithm = getString(tree, PublicClaims.ALGORITHM);
        String type = getString(tree, PublicClaims.TYPE);
        String contentType = getString(tree, PublicClaims.CONTENT_TYPE);
        String keyId = getString(tree, PublicClaims.KEY_ID);
        return new BasicHeader(algorithm, type, contentType, keyId, tree);
    }

    String getString(Map tree, String claimName) {
        JsonNode node = tree.get(claimName);
        if (node == null || node.isNull()) {
            return null;
        }
        return node.asText(null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy