001package com.nimbusds.oauth2.sdk.token; 002 003 004import java.io.Serializable; 005import java.net.URI; 006import java.net.URISyntaxException; 007import java.util.Collections; 008import java.util.HashMap; 009import java.util.Map; 010 011import net.jcip.annotations.Immutable; 012 013import com.nimbusds.oauth2.sdk.ParseException; 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), section 3. 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 private static final Map<String, TokenTypeURI> KNOWN_TOKEN_TYPE_URIS; 071 072 static { 073 Map<String, TokenTypeURI> knownTokenTypeUris = new HashMap<>(); 074 knownTokenTypeUris.put(ACCESS_TOKEN.getURI().toString(), ACCESS_TOKEN); 075 knownTokenTypeUris.put(REFRESH_TOKEN.getURI().toString(), REFRESH_TOKEN); 076 knownTokenTypeUris.put(ID_TOKEN.getURI().toString(), ID_TOKEN); 077 knownTokenTypeUris.put(SAML1.getURI().toString(), SAML1); 078 knownTokenTypeUris.put(SAML2.getURI().toString(), SAML2); 079 knownTokenTypeUris.put(JWT.getURI().toString(), JWT); 080 KNOWN_TOKEN_TYPE_URIS = Collections.unmodifiableMap(knownTokenTypeUris); 081 } 082 083 private final URI uri; 084 085 086 /** 087 * Creates a new token type URI with the specified value. 088 * 089 * @param uri The URI value. Must not be {@code null}. 090 */ 091 private TokenTypeURI(final URI uri) { 092 if (uri == null) { 093 throw new IllegalArgumentException("The URI must not be null"); 094 } 095 this.uri = uri; 096 } 097 098 099 /** 100 * Returns the URI for this token type. 101 * 102 * @return The URI. 103 */ 104 public URI getURI() { 105 return uri; 106 } 107 108 109 /** 110 * Parses a token type URI from the specified string. 111 * 112 * @param uriValue The URI string value. Must not be {@code null}. 113 * 114 * @return The token type URI. 115 * 116 * @throws ParseException If the token type URI value is illegal. 117 */ 118 public static TokenTypeURI parse(final String uriValue) 119 throws ParseException { 120 121 if (uriValue == null) { 122 throw new IllegalArgumentException("The URI value must not be null"); 123 } 124 125 TokenTypeURI knownURI = KNOWN_TOKEN_TYPE_URIS.get(uriValue); 126 127 if (knownURI != null) { 128 return knownURI; 129 } 130 131 try { 132 return new TokenTypeURI(new URI(uriValue)); 133 } catch (URISyntaxException e) { 134 throw new ParseException("Illegal token type URI: " + uriValue); 135 } 136 } 137 138 139 @Override 140 public String toString() { 141 return getURI().toString(); 142 } 143 144 145 @Override 146 public boolean equals(final Object o) { 147 if (this == o) return true; 148 if (o == null || getClass() != o.getClass()) return false; 149 150 TokenTypeURI that = (TokenTypeURI) o; 151 152 return uri.equals(that.getURI()); 153 } 154 155 156 @Override 157 public int hashCode() { 158 return uri.hashCode(); 159 } 160}