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