001 package com.nimbusds.oauth2.sdk.token; 002 003 004 import net.minidev.json.JSONObject; 005 006 007 /** 008 * Access and refresh token pair. This class is immutable. 009 * 010 * @author Vladimir Dzhuvinov 011 * @version $version$ (2013-02-13) 012 */ 013 public final class TokenPair { 014 015 016 /** 017 * Access token. 018 */ 019 private final AccessToken accessToken; 020 021 022 /** 023 * Refresh token, {@code null} if not specified. 024 */ 025 private final RefreshToken refreshToken; 026 027 028 /** 029 * Creates a new access and refresh token pair. 030 * 031 * @param accessToken The access token. Must not be {@code null}. 032 * @param refreshToken The refresh token. If none {@code null}. 033 */ 034 public TokenPair(final AccessToken accessToken, final RefreshToken refreshToken) { 035 036 if (accessToken == null) 037 throw new IllegalArgumentException("The access token must not be null"); 038 039 this.accessToken = accessToken; 040 041 this.refreshToken = refreshToken; 042 } 043 044 045 /** 046 * Gets the access token. 047 * 048 * @return The access token. 049 */ 050 public AccessToken getAccessToken() { 051 052 return accessToken; 053 } 054 055 056 /** 057 * Gets the refresh token. 058 * 059 * @return The refresh token. If none {@code null}. 060 */ 061 public RefreshToken getRefreshToken() { 062 063 return refreshToken; 064 } 065 066 067 /** 068 * Returns the JSON object representation of this token pair. 069 * 070 * <p>Example JSON object: 071 * 072 * <pre> 073 * { 074 * "access_token" : "dZdt8BlltORMTz5U", 075 * "refresh_token" : "E87zjAoeNXaSoF1U" 076 * } 077 * </pre> 078 * 079 * @return The JSON object representation. 080 */ 081 public JSONObject toJSONObject() { 082 083 JSONObject o = accessToken.toJSONObject(); 084 085 if (refreshToken != null) 086 o.putAll(refreshToken.toJSONObject()); 087 088 return o; 089 } 090 091 092 @Override 093 public String toString() { 094 095 return "TokenPair [accessToken=" + accessToken + 096 " refreshToken=" + refreshToken + "]"; 097 } 098 }