001package com.nimbusds.oauth2.sdk.token; 002 003 004import java.util.Set; 005 006import net.jcip.annotations.Immutable; 007 008import net.minidev.json.JSONObject; 009 010import com.nimbusds.oauth2.sdk.ParseException; 011 012 013/** 014 * Access and optional refresh token. 015 */ 016@Immutable 017public class Tokens { 018 019 020 /** 021 * Access token. 022 */ 023 private final AccessToken accessToken; 024 025 026 /** 027 * Refresh token, {@code null} if not specified. 028 */ 029 private final RefreshToken refreshToken; 030 031 032 /** 033 * Creates a new tokens instance. 034 * 035 * @param accessToken The access token. Must not be {@code null}. 036 * @param refreshToken The refresh token. If none {@code null}. 037 */ 038 public Tokens(final AccessToken accessToken, final RefreshToken refreshToken) { 039 040 if (accessToken == null) 041 throw new IllegalArgumentException("The access token must not be null"); 042 043 this.accessToken = accessToken; 044 045 this.refreshToken = refreshToken; 046 } 047 048 049 /** 050 * Returns the access token. 051 * 052 * @return The access token. 053 */ 054 public AccessToken getAccessToken() { 055 056 return accessToken; 057 } 058 059 060 /** 061 * Returns the access token as type bearer. 062 * 063 * @return The bearer access token, {@code null} if the type is 064 * different. 065 */ 066 public BearerAccessToken getBearerAccessToken() { 067 068 if (accessToken instanceof BearerAccessToken) { 069 return (BearerAccessToken) accessToken; 070 } 071 072 return null; 073 } 074 075 076 /** 077 * Returns the optional refresh token. 078 * 079 * @return The refresh token, {@code null} if none. 080 */ 081 public RefreshToken getRefreshToken() { 082 083 return refreshToken; 084 } 085 086 087 /** 088 * Returns the token parameter names for the included tokens. 089 * 090 * @return The token parameter names. 091 */ 092 public Set<String> getParameterNames() { 093 094 // Get the std param names for the access + refresh token 095 Set<String> paramNames = accessToken.getParameterNames(); 096 097 if (refreshToken != null) 098 paramNames.addAll(refreshToken.getParameterNames()); 099 100 return paramNames; 101 } 102 103 104 /** 105 * Returns the JSON object representation of this token pair. 106 * 107 * <p>Example JSON object: 108 * 109 * <pre> 110 * { 111 * "access_token" : "dZdt8BlltORMTz5U", 112 * "refresh_token" : "E87zjAoeNXaSoF1U" 113 * } 114 * </pre> 115 * 116 * @return The JSON object representation. 117 */ 118 public JSONObject toJSONObject() { 119 120 JSONObject o = accessToken.toJSONObject(); 121 122 if (refreshToken != null) 123 o.putAll(refreshToken.toJSONObject()); 124 125 return o; 126 } 127 128 129 @Override 130 public String toString() { 131 132 return toJSONObject().toJSONString(); 133 } 134 135 136 /** 137 * Parses an access and optional refresh token from the specified JSON 138 * object. 139 * 140 * @param jsonObject The JSON object to parse. Must not be {@code null}. 141 * 142 * @return The tokens. 143 * 144 * @throws ParseException If the JSON object couldn't be parsed to a 145 * tokens instance. 146 */ 147 public static Tokens parse(final JSONObject jsonObject) 148 throws ParseException { 149 150 return new Tokens(AccessToken.parse(jsonObject), RefreshToken.parse(jsonObject)); 151 } 152}