001package com.nimbusds.oauth2.sdk.token; 002 003 004import java.util.Set; 005 006import net.minidev.json.JSONObject; 007 008import com.nimbusds.oauth2.sdk.id.Identifier; 009 010 011/** 012 * The base abstract class for access and refresh tokens. Concrete extending 013 * classes should be immutable. 014 * 015 * <p>Related specifications: 016 * 017 * <ul> 018 * <li>OAuth 2.0 (RFC 6749), sections 1.4 and 1.5. 019 * </ul> 020 */ 021public abstract class Token extends Identifier { 022 023 024 /** 025 * Creates a new token with the specified value. 026 * 027 * @param value The token value. Must not be {@code null} or empty 028 * string. 029 */ 030 protected Token(final String value) { 031 032 super(value); 033 } 034 035 036 /** 037 * Creates a new token with a randomly generated value of the specified 038 * byte length, Base64URL-encoded. 039 * 040 * @param byteLength The byte length of the value to generate. Must be 041 * greater than one. 042 */ 043 protected Token(final int byteLength) { 044 045 super(byteLength); 046 } 047 048 049 /** 050 * Creates a new token with a randomly generated 256-bit (32-byte) 051 * value, Base64URL-encoded. 052 */ 053 protected Token() { 054 055 super(); 056 } 057 058 059 /** 060 * Returns the token parameter names included in the JSON object, as 061 * required for the composition of an access token response. See OAuth 062 * 2.0 (RFC 6749), section 5.1. 063 * 064 * @return The token parameter names. 065 */ 066 public abstract Set<String> getParamNames(); 067 068 069 /** 070 * Returns the token parameters as a JSON object, as required for the 071 * composition of an access token response. See OAuth 2.0 (RFC 6749), 072 * section 5.1. 073 * 074 * <p>Note that JSONObject implements {@literal Map<String,Object>}. 075 * 076 * <p>Example: 077 * 078 * <pre> 079 * { 080 * "access_token" : "2YotnFZFEjr1zCsicMWpAA", 081 * "token_type" : "example", 082 * "expires_in" : 3600, 083 * "example_parameter" : "example_value" 084 * } 085 * </pre> 086 * 087 * @return The token parameters as a JSON object. 088 */ 089 public abstract JSONObject toJSONObject(); 090}