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

pl.edu.icm.unity.oauth.as.token.BearerJWTAccessToken Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.oauth.as.token;

import java.text.ParseException;
import java.util.Optional;

import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import com.nimbusds.oauth2.sdk.Scope;
import com.nimbusds.oauth2.sdk.token.BearerAccessToken;

public class BearerJWTAccessToken extends BearerAccessToken
{
	private final JWTClaimsSet claimsSet;
	
	public BearerJWTAccessToken(String value, int lifetime, Scope scope, JWTClaimsSet claimsSet)
	{
		super(value, lifetime, scope);
		this.claimsSet = claimsSet;
	}

	public JWTClaimsSet getClaimsSet()
	{
		return claimsSet;
	}
	

	public static Optional tryParseJWT(String tokenValue)
	{
		if (!tokenValue.contains(".")) //fail fast
			return Optional.empty();
		try
		{
			return Optional.of(SignedJWT.parse(tokenValue));
		} catch (ParseException e)
		{
			//we are assuming not an JWT...
			return Optional.empty();
		}
	}

	public static Optional tryParseJWTClaimSet(String tokenValue)
	{
		return tryParseJWTClaimSet(tryParseJWT(tokenValue));
	}
	
	public static Optional tryParseJWTClaimSet(Optional jwt)
	{
		if (!jwt.isPresent())
			return Optional.empty();
		try
		{
			return Optional.of(jwt.get().getJWTClaimsSet());
		} catch (ParseException e)
		{
			//we are assuming not an JWT...
			return Optional.empty();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy