001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.oauth2.sdk.token; 019 020 021import java.util.Collections; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Set; 025 026import net.minidev.json.JSONObject; 027 028import com.nimbusds.oauth2.sdk.ParseException; 029import com.nimbusds.openid.connect.sdk.token.OIDCTokens; 030 031 032/** 033 * Access and optional refresh token. 034 */ 035public class Tokens { 036 037 038 /** 039 * Access token. 040 */ 041 private final AccessToken accessToken; 042 043 044 /** 045 * Refresh token, {@code null} if not specified. 046 */ 047 private final RefreshToken refreshToken; 048 049 050 /** 051 * Optional token metadata, intended for server environments. 052 */ 053 054 private final Map<String,Object> metadata = new HashMap<>(); 055 056 057 /** 058 * Creates a new tokens instance. 059 * 060 * @param accessToken The access token. Must not be {@code null}. 061 * @param refreshToken The refresh token. If none {@code null}. 062 */ 063 public Tokens(final AccessToken accessToken, final RefreshToken refreshToken) { 064 065 if (accessToken == null) 066 throw new IllegalArgumentException("The access token must not be null"); 067 068 this.accessToken = accessToken; 069 070 this.refreshToken = refreshToken; 071 } 072 073 074 /** 075 * Returns the access token. 076 * 077 * @return The access token. 078 */ 079 public AccessToken getAccessToken() { 080 081 return accessToken; 082 } 083 084 085 /** 086 * Returns the access token as type bearer. 087 * 088 * @return The bearer access token, {@code null} if the type is 089 * different. 090 */ 091 public BearerAccessToken getBearerAccessToken() { 092 093 if (accessToken instanceof BearerAccessToken) { 094 return (BearerAccessToken) accessToken; 095 } 096 097 return null; 098 } 099 100 101 /** 102 * Returns the optional refresh token. 103 * 104 * @return The refresh token, {@code null} if none. 105 */ 106 public RefreshToken getRefreshToken() { 107 108 return refreshToken; 109 } 110 111 112 /** 113 * Returns the token parameter names for the included tokens. 114 * 115 * @return The token parameter names. 116 */ 117 public Set<String> getParameterNames() { 118 119 // Get the std param names for the access + refresh token 120 Set<String> paramNames = accessToken.getParameterNames(); 121 122 if (refreshToken != null) 123 paramNames.addAll(refreshToken.getParameterNames()); 124 125 return Collections.unmodifiableSet(paramNames); 126 } 127 128 129 /** 130 * Returns the optional modifiable token metadata. Intended for server 131 * environments. 132 * 133 * @return The token metadata. 134 */ 135 public Map<String, Object> getMetadata() { 136 137 return metadata; 138 } 139 140 141 /** 142 * Returns the JSON object representation of this token pair. 143 * 144 * <p>Example JSON object: 145 * 146 * <pre> 147 * { 148 * "access_token" : "dZdt8BlltORMTz5U", 149 * "refresh_token" : "E87zjAoeNXaSoF1U" 150 * } 151 * </pre> 152 * 153 * @return The JSON object representation. 154 */ 155 public JSONObject toJSONObject() { 156 157 JSONObject o = accessToken.toJSONObject(); 158 159 if (refreshToken != null) 160 o.putAll(refreshToken.toJSONObject()); 161 162 return o; 163 } 164 165 166 /** 167 * Casts to OpenID Connect tokens. 168 * 169 * @return The OpenID Connect tokens (including an ID token). 170 */ 171 public OIDCTokens toOIDCTokens() { 172 173 return (OIDCTokens)this; 174 } 175 176 177 @Override 178 public String toString() { 179 180 return toJSONObject().toJSONString(); 181 } 182 183 184 /** 185 * Parses an access and optional refresh token from the specified JSON 186 * object. 187 * 188 * @param jsonObject The JSON object to parse. Must not be {@code null}. 189 * 190 * @return The tokens. 191 * 192 * @throws ParseException If the JSON object couldn't be parsed to a 193 * tokens instance. 194 */ 195 public static Tokens parse(final JSONObject jsonObject) 196 throws ParseException { 197 198 return new Tokens(AccessToken.parse(jsonObject), RefreshToken.parse(jsonObject)); 199 } 200}