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;
019
020
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import net.jcip.annotations.Immutable;
025
026import com.nimbusds.oauth2.sdk.token.RefreshToken;
027
028
029/**
030 * Refresh token grant. Used in refresh token requests.
031 *
032 * <p>Note that the optional scope parameter is not supported.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OAuth 2.0 (RFC 6749), section 6.
038 * </ul>
039 */
040@Immutable
041public class RefreshTokenGrant extends AuthorizationGrant {
042
043
044        /**
045         * The grant type.
046         */
047        public static final GrantType GRANT_TYPE = GrantType.REFRESH_TOKEN;
048
049
050        /**
051         * The refresh token.
052         */
053        private final RefreshToken refreshToken;
054
055
056        /**
057         * Creates a new refresh token grant.
058         *
059         * @param refreshToken The refresh token. Must not be {@code null}.
060         */
061        public RefreshTokenGrant(final RefreshToken refreshToken) {
062
063
064                super(GRANT_TYPE);
065
066                if (refreshToken == null)
067                        throw new IllegalArgumentException("The refresh token must not be null");
068
069                this.refreshToken = refreshToken;
070        }
071
072
073        /**
074         * Gets the refresh token.
075         *
076         * @return The refresh token.
077         */
078        public RefreshToken getRefreshToken() {
079
080                return refreshToken;
081        }
082
083
084        @Override
085        public Map<String,String> toParameters() {
086
087                Map<String,String> params = new LinkedHashMap<>();
088                params.put("grant_type", GRANT_TYPE.getValue());
089                params.put("refresh_token", refreshToken.getValue());
090                return params;
091        }
092
093
094        @Override
095        public boolean equals(Object o) {
096                if (this == o) return true;
097                if (o == null || getClass() != o.getClass()) return false;
098
099                RefreshTokenGrant grant = (RefreshTokenGrant) o;
100
101                return refreshToken.equals(grant.refreshToken);
102
103        }
104
105
106        @Override
107        public int hashCode() {
108                return refreshToken.hashCode();
109        }
110
111
112        /**
113         * Parses a refresh token grant from the specified parameters.
114         *
115         * <p>Example:
116         *
117         * <pre>
118         * grant_type=refresh_token
119         * refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
120         * </pre>
121         *
122         * @param params The parameters.
123         *
124         * @return The refresh token grant.
125         *
126         * @throws ParseException If parsing failed.
127         */
128        public static RefreshTokenGrant parse(final Map<String,String> params)
129                throws ParseException {
130
131                // Parse grant type
132                String grantTypeString = params.get("grant_type");
133
134                if (grantTypeString == null)
135                        throw new ParseException("Missing \"grant_type\" parameter", OAuth2Error.INVALID_REQUEST);
136
137                if (! GrantType.parse(grantTypeString).equals(GRANT_TYPE))
138                        throw new ParseException("The \"grant_type\" must be " + GRANT_TYPE, OAuth2Error.UNSUPPORTED_GRANT_TYPE);
139
140                // Parse refresh token
141                String refreshTokenString = params.get("refresh_token");
142
143                if (refreshTokenString == null || refreshTokenString.trim().isEmpty())
144                        throw new ParseException("Missing or empty \"refresh_token\" parameter", OAuth2Error.INVALID_REQUEST);
145
146                RefreshToken refreshToken = new RefreshToken(refreshTokenString);
147
148                return new RefreshTokenGrant(refreshToken);
149        }
150}