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 com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
023import net.jcip.annotations.Immutable;
024import net.minidev.json.JSONObject;
025
026import java.util.HashSet;
027import java.util.Set;
028
029
030/**
031 * Refresh token.
032 *
033 * <p>Related specifications:
034 *
035 * <ul>
036 *     <li>OAuth 2.0 (RFC 6749), section 1.5.
037 * </ul>
038 */
039@Immutable
040public final class RefreshToken extends Token {
041        
042        
043        private static final long serialVersionUID = 1482806259791531877L;
044        
045        
046        /**
047         * Creates a new refresh token with a randomly generated 256-bit 
048         * (32-byte) value, Base64URL-encoded.
049         */
050        public RefreshToken() {
051        
052                this(32);
053        }       
054
055
056        /**
057         * Creates a new refresh token with a randomly generated value of the 
058         * specified length, Base64URL-encoded.
059         *
060         * @param byteLength The byte length of the value to generate. Must be
061         *                   greater than one.
062         */
063        public RefreshToken(final int byteLength) {
064        
065                super(byteLength);
066        }
067
068
069        /**
070         * Creates a new refresh token with the specified value.
071         *
072         * @param value The refresh token value. Must not be {@code null} or 
073         *              empty string.
074         */
075        public RefreshToken(final String value) {
076        
077                super(value);
078        }
079
080
081        @Override
082        public Set<String> getParameterNames() {
083
084                Set<String> paramNames = new HashSet<>(getCustomParameters().keySet());
085                paramNames.add("refresh_token");
086                return paramNames;
087        }
088
089
090        @Override
091        public JSONObject toJSONObject() {
092
093                JSONObject o = new JSONObject();
094                o.putAll(getCustomParameters());
095                o.put("refresh_token", getValue());
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}