001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2016 ForgeRock AS. 015 */ 016 017package org.forgerock.http.oauth2; 018 019import static org.forgerock.util.Reject.checkNotNull; 020 021import java.util.Collections; 022import java.util.Map; 023import java.util.Set; 024 025import org.forgerock.json.JsonValue; 026 027/** Represents an <a href="http://tools.ietf.org/html/rfc6749#section-1.4">OAuth2 Access Token</a>. */ 028public class AccessTokenInfo { 029 030 /** Marker for never ending tokens. */ 031 public static final long NEVER_EXPIRES = Long.MAX_VALUE; 032 033 private final JsonValue rawInfo; 034 private final String token; 035 private final Set<String> scopes; 036 private final long expiresAt; 037 038 /** 039 * Build an {@link AccessTokenInfo} with the provided information. 040 * 041 * @param rawInfo 042 * raw response message 043 * @param token 044 * token identifier 045 * @param scopes 046 * scopes of the token 047 * @param expiresAt 048 * Token expiration time expressed as a timestamp, in milliseconds since epoch 049 */ 050 public AccessTokenInfo(final JsonValue rawInfo, 051 final String token, 052 final Set<String> scopes, 053 final long expiresAt) { 054 this.rawInfo = checkNotNull(rawInfo).clone(); 055 this.token = checkNotNull(token); 056 this.scopes = Collections.unmodifiableSet(checkNotNull(scopes)); 057 this.expiresAt = expiresAt; 058 } 059 060 /** 061 * Returns the raw JSON as a map. 062 * 063 * @return the raw JSON as a map. 064 */ 065 public Map<String, Object> getInfo() { 066 return rawInfo.asMap(); 067 } 068 069 /** 070 * Returns the raw JSON as a {@link JsonValue}. 071 * 072 * @return the raw JSON as a {@link JsonValue}. 073 */ 074 public JsonValue asJsonValue() { 075 return rawInfo; 076 } 077 078 /** 079 * Returns the access token identifier issued from the authorization server. 080 * 081 * @return the access token identifier issued from the authorization server. 082 */ 083 public String getToken() { 084 return token; 085 } 086 087 /** 088 * Returns the scopes associated to this token. Will return an empty Set if no scopes are associated with this 089 * token. 090 * 091 * @return the scopes associated to this token. 092 */ 093 public Set<String> getScopes() { 094 return scopes; 095 } 096 097 /** 098 * Returns the time (expressed as a timestamp in milliseconds since epoch) when this token will be expired. If the 099 * {@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}