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        private static final long serialVersionUID = 1797025947209047077L;
042        
043        
044        /**
045         * Creates a new token with the specified value.
046         *
047         * @param value The token value. Must not be {@code null} or empty 
048         *              string.
049         */
050        protected Token(final String value) {
051
052                super(value);
053        }
054
055
056        /**
057         * Creates a new token with a randomly generated value of the specified 
058         * byte length, Base64URL-encoded.
059         *
060         * @param byteLength The byte length of the value to generate. Must be
061         *                   greater than one.
062         */
063        protected Token(final int byteLength) {
064        
065                super(byteLength);
066        }
067        
068        
069        /**
070         * Creates a new token with a randomly generated 256-bit (32-byte) 
071         * value, Base64URL-encoded.
072         */
073        protected Token() {
074        
075                super();
076        }
077
078
079        /**
080         * Returns the token parameter names included in the JSON object, as
081         * required for the composition of an access token response. See OAuth
082         * 2.0 (RFC 6749), section 5.1.
083         *
084         * @return The token parameter names.
085         */
086        public abstract Set<String> getParameterNames();
087
088
089        /**
090         * Returns the token parameters as a JSON object, as required for the
091         * composition of an access token response. See OAuth 2.0 (RFC 6749), 
092         * section 5.1.
093         *
094         * <p>Note that JSONObject implements {@literal Map&lt;String,Object&gt;}.
095         *
096         * <p>Example:
097         *
098         * <pre>
099         * {
100         *   "access_token"      : "2YotnFZFEjr1zCsicMWpAA",
101         *   "token_type"        : "example",
102         *   "expires_in"        : 3600,
103         *   "example_parameter" : "example_value"
104         * }
105         * </pre>
106         *
107         * @return The token parameters as a JSON object.
108         */
109        public abstract JSONObject toJSONObject();
110}