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