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