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 org.forgerock.services.context.AbstractContext; 020import org.forgerock.services.context.Context; 021 022/** 023 * An {@link OAuth2Context} could be used to store and retrieve an {@link AccessTokenInfo}. 024 * <p> 025 * Once a {@link ResourceServerFilter} has authorized an OAuth2 request and 026 * obtained the access token's state, the access token can be stored in the 027 * {@link OAuth2Context} in order to influence subsequent processing of the request. 028 * <p> 029 * For example, the information provided in the AccessToken may be used for additional fine-grained authorization. 030 * <p> 031 * The following code illustrates how an application may store/retrieve an access token: 032 * 033 * <pre> 034 * AccessToken accessToken = ...; 035 * Context parentContext = ...; 036 * // Create the OAuth2 context and store the access token 037 * OAuth2Context context = new OAuth2Context(parentContext, accessToken); 038 * [...] 039 * AccessToken myToken = context.asContext(OAuth2Context.class).getAccessToken(); 040 * </pre> 041 */ 042public class OAuth2Context extends AbstractContext { 043 044 private final AccessTokenInfo accessToken; 045 046 /** 047 * Creates a new OAuth2 context with the provided {@link AccessTokenInfo}. 048 * 049 * @param parent 050 * The parent context. 051 * @param accessToken 052 * The access token to store. 053 */ 054 public OAuth2Context(final Context parent, final AccessTokenInfo accessToken) { 055 super(parent, "oauth2"); 056 this.accessToken = accessToken; 057 } 058 059 /** 060 * Returns the access token associated with this OAuth2 context. 061 * 062 * @return the access token associated with this OAuth2 context. 063 */ 064 public AccessTokenInfo getAccessToken() { 065 return accessToken; 066 } 067}