001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.token;
019
020
021import java.util.Set;
022
023import net.minidev.json.JSONObject;
024
025import com.nimbusds.oauth2.sdk.id.Identifier;
026
027
028/**
029 * The base abstract class for access and refresh tokens. Concrete extending
030 * classes should be immutable.
031 * 
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OAuth 2.0 (RFC 6749), sections 1.4 and 1.5.
036 * </ul>
037 */
038public abstract class Token extends Identifier {
039
040
041        /**
042         * Creates a new token with the specified value.
043         *
044         * @param value The token value. Must not be {@code null} or empty 
045         *              string.
046         */
047        protected Token(final String value) {
048
049                super(value);
050        }
051
052
053        /**
054         * Creates a new token with a randomly generated value of the specified 
055         * byte length, Base64URL-encoded.
056         *
057         * @param byteLength The byte length of the value to generate. Must be
058         *                   greater than one.
059         */
060        protected Token(final int byteLength) {
061        
062                super(byteLength);
063        }
064        
065        
066        /**
067         * Creates a new token with a randomly generated 256-bit (32-byte) 
068         * value, Base64URL-encoded.
069         */
070        protected Token() {
071        
072                super();
073        }
074
075
076        /**
077         * Returns the token parameter names included in the JSON object, as
078         * required for the composition of an access token response. See OAuth
079         * 2.0 (RFC 6749), section 5.1.
080         *
081         * @return The token parameter names.
082         */
083        public abstract Set<String> getParameterNames();
084
085
086        /**
087         * Returns the token parameters as a JSON object, as required for the
088         * composition of an access token response. See OAuth 2.0 (RFC 6749), 
089         * section 5.1.
090         *
091         * <p>Note that JSONObject implements {@literal Map&lt;String,Object&gt;}.
092         *
093         * <p>Example:
094         *
095         * <pre>
096         * {
097         *   "access_token"      : "2YotnFZFEjr1zCsicMWpAA",
098         *   "token_type"        : "example",
099         *   "expires_in"        : 3600,
100         *   "example_parameter" : "example_value"
101         * }
102         * </pre>
103         *
104         * @return The token parameters as a JSON object.
105         */
106        public abstract JSONObject toJSONObject();
107}