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 access token as type DPoP. 103 * 104 * @return The DPoP access token, {@code null} if the type is 105 * different. 106 */ 107 public DPoPAccessToken getDPoPAccessToken() { 108 109 if (accessToken instanceof DPoPAccessToken) { 110 return (DPoPAccessToken) accessToken; 111 } 112 113 return null; 114 } 115 116 117 /** 118 * Returns the optional refresh token. 119 * 120 * @return The refresh token, {@code null} if none. 121 */ 122 public RefreshToken getRefreshToken() { 123 124 return refreshToken; 125 } 126 127 128 /** 129 * Returns the token parameter names for the included tokens. 130 * 131 * @return The token parameter names. 132 */ 133 public Set<String> getParameterNames() { 134 135 // Get the std param names for the access + refresh token 136 Set<String> paramNames = accessToken.getParameterNames(); 137 138 if (refreshToken != null) 139 paramNames.addAll(refreshToken.getParameterNames()); 140 141 return Collections.unmodifiableSet(paramNames); 142 } 143 144 145 /** 146 * Returns the optional modifiable token metadata. Intended for server 147 * environments. 148 * 149 * @return The token metadata. 150 */ 151 public Map<String, Object> getMetadata() { 152 153 return metadata; 154 } 155 156 157 /** 158 * Returns the JSON object representation of this token pair. 159 * 160 * <p>Example JSON object: 161 * 162 * <pre> 163 * { 164 * "access_token" : "dZdt8BlltORMTz5U", 165 * "refresh_token" : "E87zjAoeNXaSoF1U" 166 * } 167 * </pre> 168 * 169 * @return The JSON object representation. 170 */ 171 public JSONObject toJSONObject() { 172 173 JSONObject o = accessToken.toJSONObject(); 174 175 if (refreshToken != null) 176 o.putAll(refreshToken.toJSONObject()); 177 178 return o; 179 } 180 181 182 /** 183 * Casts to OpenID Connect tokens. 184 * 185 * @return The OpenID Connect tokens (including an ID token). 186 */ 187 public OIDCTokens toOIDCTokens() { 188 189 return (OIDCTokens)this; 190 } 191 192 193 @Override 194 public String toString() { 195 196 return toJSONObject().toJSONString(); 197 } 198 199 200 /** 201 * Parses an access and optional refresh token from the specified JSON 202 * object. 203 * 204 * @param jsonObject The JSON object to parse. Must not be {@code null}. 205 * 206 * @return The tokens. 207 * 208 * @throws ParseException If the JSON object couldn't be parsed to a 209 * tokens instance. 210 */ 211 public static Tokens parse(final JSONObject jsonObject) 212 throws ParseException { 213 214 return new Tokens(AccessToken.parse(jsonObject), RefreshToken.parse(jsonObject)); 215 } 216}