001 package com.nimbusds.oauth2.sdk.token; 002 003 004 import net.jcip.annotations.Immutable; 005 006 import net.minidev.json.JSONObject; 007 008 import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 009 import 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 024 public final class RefreshToken 025 extends Token 026 implements Comparable<RefreshToken> { 027 028 029 /** 030 * Creates a new refresh token with a randomly generated value. The 031 * value will be made up of 32 mixed-case alphanumeric ASCII 032 * characters. 033 */ 034 public RefreshToken() { 035 036 this(32); 037 } 038 039 040 /** 041 * Creates a new refresh token with a randomly generated value of the 042 * specified length. The value will be made up of mixed-case 043 * alphanumeric ASCII characters. 044 * 045 * @param length The number of characters. Must be a positive integer. 046 */ 047 public RefreshToken(final int length) { 048 049 super(length); 050 } 051 052 053 /** 054 * Creates a new refresh token with the specified value. 055 * 056 * @param value The refresh token value. Must not be {@code null} or 057 * empty string. 058 */ 059 public RefreshToken(final String value) { 060 061 super(value); 062 } 063 064 065 @Override 066 public JSONObject toJSONObject() { 067 068 JSONObject o = new JSONObject(); 069 070 o.put("refresh_token", getValue()); 071 072 return o; 073 } 074 075 076 /** 077 * Parses a refresh token from a JSON object access token response. 078 * 079 * @param jsonObject The JSON object to parse. Must not be 080 * {@code null}. 081 * 082 * @return The refresh token, {@code null} if not found. 083 * 084 * @throws ParseException If the JSON object couldn't be parsed to a 085 * refresh token. 086 */ 087 public static RefreshToken parse(final JSONObject jsonObject) 088 throws ParseException { 089 090 // Parse value 091 if (! jsonObject.containsKey("refresh_token")) 092 return null; 093 094 String value = JSONObjectUtils.getString(jsonObject, "refresh_token"); 095 096 return new RefreshToken(value); 097 } 098 099 100 @Override 101 public boolean equals(final Object object) { 102 103 return object != null && 104 object instanceof RefreshToken && 105 this.toString().equals(object.toString()); 106 } 107 108 109 @Override 110 public int compareTo(RefreshToken other) { 111 112 return getValue().compareTo(other.getValue()); 113 } 114 }