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