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.HashSet;
022import java.util.Set;
023
024import net.jcip.annotations.Immutable;
025
026import net.minidev.json.JSONObject;
027
028import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
029import com.nimbusds.oauth2.sdk.ParseException;
030
031
032/**
033 * Refresh token.
034 *
035 * <p>Related specifications:
036 *
037 * <ul>
038 *     <li>OAuth 2.0 (RFC 6749), section 1.5.
039 * </ul>
040 */
041@Immutable
042public final class RefreshToken extends Token {
043
044
045        /**
046         * Creates a new refresh token with a randomly generated 256-bit 
047         * (32-byte) value, Base64URL-encoded.
048         */
049        public RefreshToken() {
050        
051                this(32);
052        }       
053
054
055        /**
056         * Creates a new refresh token with a randomly generated value of the 
057         * specified length, Base64URL-encoded.
058         *
059         * @param byteLength The byte length of the value to generate. Must be
060         *                   greater than one.
061         */
062        public RefreshToken(final int byteLength) {
063        
064                super(byteLength);
065        }
066
067
068        /**
069         * Creates a new refresh token with the specified value.
070         *
071         * @param value The refresh token value. Must not be {@code null} or 
072         *              empty string.
073         */
074        public RefreshToken(final String value) {
075        
076                super(value);
077        }
078
079
080        @Override
081        public Set<String> getParameterNames() {
082
083                Set<String> paramNames = new HashSet<>();
084                paramNames.add("refresh_token");
085                return paramNames;
086        }
087
088
089        @Override
090        public JSONObject toJSONObject() {
091
092                JSONObject o = new JSONObject();
093
094                o.put("refresh_token", getValue());
095                
096                return o;
097        }
098
099
100        /**
101         * Parses a refresh token from a JSON object access token response.
102         *
103         * @param jsonObject The JSON object to parse. Must not be 
104         *                   {@code null}.
105         *
106         * @return The refresh token, {@code null} if not found.
107         *
108         * @throws ParseException If the JSON object couldn't be parsed to a
109         *                        refresh token.
110         */
111        public static RefreshToken parse(final JSONObject jsonObject)
112                throws ParseException {
113
114                String value = JSONObjectUtils.getString(jsonObject, "refresh_token", null);
115                
116                if (value == null) return null;
117
118                return new RefreshToken(value);
119        }
120
121
122        @Override
123        public boolean equals(final Object object) {
124        
125                return object instanceof RefreshToken &&
126                       this.toString().equals(object.toString());
127        }
128}