001package com.nimbusds.oauth2.sdk.token; 002 003 004import com.nimbusds.oauth2.sdk.ParseException; 005import net.jcip.annotations.Immutable; 006 007import java.io.Serializable; 008import java.net.URI; 009import java.net.URISyntaxException; 010import java.util.Collections; 011import java.util.HashMap; 012import java.util.Map; 013import java.util.Objects; 014 015 016/** 017 * Token type URI. A URN used to identify the type of token in a token 018 * exchange. The token type URIs can potentially be used in other contexts. 019 * 020 * <p>The standard OAuth URIs are registered at IANA, see 021 * https://www.iana.org/assignments/oauth-parameters/oauth-parameters.xhtml#uri 022 * 023 * <ul> 024 * <li>OAuth 2.0 Token Exchange (RFC 8693) 025 * </ul> 026 */ 027@Immutable 028public final class TokenTypeURI implements Serializable { 029 030 031 private static final long serialVersionUID = 1371197657238309877L; 032 033 034 /** 035 * The token type URI for an OAuth 2.0 access token. 036 */ 037 public static final TokenTypeURI ACCESS_TOKEN = new TokenTypeURI(URI.create("urn:ietf:params:oauth:token-type:access_token")); 038 039 040 /** 041 * The token type URI for an OAuth 2.0 refresh token. 042 */ 043 public static final TokenTypeURI REFRESH_TOKEN = new TokenTypeURI(URI.create("urn:ietf:params:oauth:token-type:refresh_token")); 044 045 046 /** 047 * The token type URI for an OpenID Connect ID Token. 048 */ 049 public static final TokenTypeURI ID_TOKEN = new TokenTypeURI(URI.create("urn:ietf:params:oauth:token-type:id_token")); 050 051 052 /** 053 * The token type URI for a BASE64URL-encoded SAML 1.1 assertion. 054 */ 055 public static final TokenTypeURI SAML1 = new TokenTypeURI(URI.create("urn:ietf:params:oauth:token-type:saml1")); 056 057 058 /** 059 * The token type URI for a BASE64URL-encoded SAML 2.0 assertion. 060 */ 061 public static final TokenTypeURI SAML2 = new TokenTypeURI(URI.create("urn:ietf:params:oauth:token-type:saml2")); 062 063 064 /** 065 * The token type URI for a JSON Web Token (JWT). 066 */ 067 public static final TokenTypeURI JWT = new TokenTypeURI(URI.create("urn:ietf:params:oauth:token-type:jwt")); 068 069 070 /** 071 * The token type URI for a {@code device_secret} for OpenID Connect 072 * native SSO. 073 */ 074 public static final TokenTypeURI DEVICE_SECRET = new TokenTypeURI(URI.create("urn:openid:params:token-type:device-secret")); 075 076 077 private static final Map<String, TokenTypeURI> KNOWN_TOKEN_TYPE_URIS; 078 079 static { 080 Map<String, TokenTypeURI> knownTokenTypeUris = new HashMap<>(); 081 knownTokenTypeUris.put(ACCESS_TOKEN.getURI().toString(), ACCESS_TOKEN); 082 knownTokenTypeUris.put(REFRESH_TOKEN.getURI().toString(), REFRESH_TOKEN); 083 knownTokenTypeUris.put(ID_TOKEN.getURI().toString(), ID_TOKEN); 084 knownTokenTypeUris.put(SAML1.getURI().toString(), SAML1); 085 knownTokenTypeUris.put(SAML2.getURI().toString(), SAML2); 086 knownTokenTypeUris.put(JWT.getURI().toString(), JWT); 087 knownTokenTypeUris.put(DEVICE_SECRET.getURI().toString(), DEVICE_SECRET); 088 KNOWN_TOKEN_TYPE_URIS = Collections.unmodifiableMap(knownTokenTypeUris); 089 } 090 091 private final URI uri; 092 093 094 /** 095 * Creates a new token type URI with the specified value. 096 * 097 * @param uri The URI value. Must not be {@code null}. 098 */ 099 private TokenTypeURI(final URI uri) { 100 this.uri = Objects.requireNonNull(uri); 101 } 102 103 104 /** 105 * Returns the URI for this token type. 106 * 107 * @return The URI. 108 */ 109 public URI getURI() { 110 return uri; 111 } 112 113 114 /** 115 * Parses a token type URI from the specified string. 116 * 117 * @param uriValue The URI string value. Must not be {@code null}. 118 * 119 * @return The token type URI. 120 * 121 * @throws ParseException If the token type URI value is illegal. 122 */ 123 public static TokenTypeURI parse(final String uriValue) 124 throws ParseException { 125 126 if (uriValue == null) { 127 throw new IllegalArgumentException("The URI value must not be null"); 128 } 129 130 TokenTypeURI knownURI = KNOWN_TOKEN_TYPE_URIS.get(uriValue); 131 132 if (knownURI != null) { 133 return knownURI; 134 } 135 136 try { 137 return new TokenTypeURI(new URI(uriValue)); 138 } catch (URISyntaxException e) { 139 throw new ParseException("Illegal token type URI: " + uriValue); 140 } 141 } 142 143 144 @Override 145 public String toString() { 146 return getURI().toString(); 147 } 148 149 150 @Override 151 public boolean equals(final Object o) { 152 if (this == o) return true; 153 if (o == null || getClass() != o.getClass()) return false; 154 155 TokenTypeURI that = (TokenTypeURI) o; 156 157 return uri.equals(that.getURI()); 158 } 159 160 161 @Override 162 public int hashCode() { 163 return uri.hashCode(); 164 } 165}