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

io.soffa.foundation.models.Token Maven / Gradle / Ivy

The newest version!
package io.soffa.foundation.models;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;
import java.util.Optional;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Token {

    private String type = "JWT";
    private String value;
    private String subject;
    private Map claims;
    private int expiresIn;

    public Token(String value) {
        this.value = value;
    }

    public Token(String value, String subject, Map claims) {
        this(value);
        this.subject = subject;
        this.claims = claims;
    }

    public Token(String value, String subject, Map claims, int expiresIn) {
        this(value, subject, claims);
        this.expiresIn = expiresIn;
    }

    public Optional lookupClaim(String... candidates) {
        if (claims == null || claims.isEmpty()) {
            return Optional.empty();
        }

        for (String claim : claims.keySet()) {
            for (String candidate : candidates) {
                if (claim.equalsIgnoreCase(candidate)) {
                    Object value = claims.get(claim);
                    if (value!=null) {
                        return Optional.of(value.toString());
                    }
                }
            }
        }
        return Optional.empty();
    }

    public int getExpiresInSeconds() {
        return expiresIn * 60;
    }

}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy