001package com.nimbusds.oauth2.sdk.token;
002
003
004import net.minidev.json.JSONObject;
005
006import com.nimbusds.oauth2.sdk.id.Identifier;
007
008
009/**
010 * The base abstract class for access and refresh tokens. Concrete extending
011 * classes should be immutable.
012 * 
013 * <p>Related specifications:
014 *
015 * <ul>
016 *     <li>OAuth 2.0 (RFC 6749), sections 1.4 and 1.5.
017 * </ul>
018 *
019 * @author Vladimir Dzhuvinov
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 parameters as a JSON object, as required for the
061         * composition of an access token response. See OAuth 2.0 (RFC 6749), 
062         * section 5.1.
063         *
064         * <p>Note that JSONObject implements {@code Map<String,Object>}.
065         *
066         * <p>Example:
067         *
068         * <pre>
069         * {
070         *   "access_token"      : "2YotnFZFEjr1zCsicMWpAA",
071         *   "token_type"        : "example",
072         *   "expires_in"        : 3600,
073         *   "example_parameter" : "example_value"
074         * }
075         * </pre>
076         *
077         * @return The token parameters as a JSON object.
078         */
079        public abstract JSONObject toJSONObject();
080}