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