1 /* 2 * The contents of this file are subject to the terms of the Common Development and 3 * Distribution License (the License). You may not use this file except in compliance with the 4 * License. 5 * 6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 7 * specific language governing permission and limitations under the License. 8 * 9 * When distributing Covered Software, include this CDDL Header Notice in each file and include 10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 11 * Header, with the fields enclosed by brackets [] replaced by your own identifying 12 * information: "Portions Copyright [year] [name of copyright owner]". 13 * 14 * Copyright 2016 ForgeRock AS. 15 */ 16 17 package org.forgerock.http.oauth2; 18 19 import static org.forgerock.util.Reject.checkNotNull; 20 21 import java.util.Collections; 22 import java.util.Map; 23 import java.util.Set; 24 25 import org.forgerock.json.JsonValue; 26 27 /** Represents an <a href="http://tools.ietf.org/html/rfc6749#section-1.4">OAuth2 Access Token</a>. */ 28 public class AccessTokenInfo { 29 30 /** Marker for never ending tokens. */ 31 public static final long NEVER_EXPIRES = Long.MAX_VALUE; 32 33 private final JsonValue rawInfo; 34 private final String token; 35 private final Set<String> scopes; 36 private final long expiresAt; 37 38 /** 39 * Build an {@link AccessTokenInfo} with the provided information. 40 * 41 * @param rawInfo 42 * raw response message 43 * @param token 44 * token identifier 45 * @param scopes 46 * scopes of the token 47 * @param expiresAt 48 * Token expiration time expressed as a timestamp, in milliseconds since epoch 49 */ 50 public AccessTokenInfo(final JsonValue rawInfo, 51 final String token, 52 final Set<String> scopes, 53 final long expiresAt) { 54 this.rawInfo = checkNotNull(rawInfo).clone(); 55 this.token = checkNotNull(token); 56 this.scopes = Collections.unmodifiableSet(checkNotNull(scopes)); 57 this.expiresAt = expiresAt; 58 } 59 60 /** 61 * Returns the raw JSON as a map. 62 * 63 * @return the raw JSON as a map. 64 */ 65 public Map<String, Object> getInfo() { 66 return rawInfo.asMap(); 67 } 68 69 /** 70 * Returns the raw JSON as a {@link JsonValue}. 71 * 72 * @return the raw JSON as a {@link JsonValue}. 73 */ 74 public JsonValue asJsonValue() { 75 return rawInfo; 76 } 77 78 /** 79 * Returns the access token identifier issued from the authorization server. 80 * 81 * @return the access token identifier issued from the authorization server. 82 */ 83 public String getToken() { 84 return token; 85 } 86 87 /** 88 * Returns the scopes associated to this token. Will return an empty Set if no scopes are associated with this 89 * token. 90 * 91 * @return the scopes associated to this token. 92 */ 93 public Set<String> getScopes() { 94 return scopes; 95 } 96 97 /** 98 * Returns the time (expressed as a timestamp in milliseconds since epoch) when this token will be expired. If the 99 * {@link #NEVER_EXPIRES} constant is returned, this token is always considered as available. 100 * 101 * @return the time (expressed as a timestamp, in milliseconds since epoch) when this token will be expired. 102 */ 103 public long getExpiresAt() { 104 return expiresAt; 105 } 106 }