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