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

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

package com.auth0.jwt.impl;

import com.auth0.jwt.RegisteredClaims;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Jackson serializer implementation for converting into JWT Payload parts.
 * 

* This class is thread-safe. * * @see com.auth0.jwt.JWTCreator */ public class PayloadSerializer extends ClaimsSerializer { public PayloadSerializer() { super(PayloadClaimsHolder.class); } @Override protected void writeClaim(Map.Entry entry, JsonGenerator gen) throws IOException { if (RegisteredClaims.AUDIENCE.equals(entry.getKey())) { writeAudience(gen, entry); } else { super.writeClaim(entry, gen); } } /** * Audience may be a list of strings or a single string. This is needed to properly handle the aud claim when * added with the {@linkplain com.auth0.jwt.JWTCreator.Builder#withPayload(Map)} method. */ private void writeAudience(JsonGenerator gen, Map.Entry e) throws IOException { if (e.getValue() instanceof String) { gen.writeFieldName(e.getKey()); gen.writeString((String) e.getValue()); } else { List audArray = new ArrayList<>(); if (e.getValue() instanceof String[]) { audArray = Arrays.asList((String[]) e.getValue()); } else if (e.getValue() instanceof List) { List audList = (List) e.getValue(); for (Object aud : audList) { if (aud instanceof String) { audArray.add((String) aud); } } } if (audArray.size() == 1) { gen.writeFieldName(e.getKey()); gen.writeString(audArray.get(0)); } else if (audArray.size() > 1) { gen.writeFieldName(e.getKey()); gen.writeStartArray(); for (String aud : audArray) { gen.writeString(aud); } gen.writeEndArray(); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy