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