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 2014-2016 ForgeRock AS. 015 */ 016 017package org.forgerock.http.oauth2; 018 019/** OAuth2 utility class. */ 020public final class OAuth2 { 021 022 private static final String BEARER_TOKEN_KEY = "BEARER"; 023 024 /** 025 * Extracts the bearer token from the request's authorization header. 026 * <p> 027 * Expected ABNF format (as per RFC 6750): 028 * <pre> 029 * {@code 030 * b64token = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"=" 031 * credentials = "Bearer" 1*SP b64token 032 * } 033 * </pre> 034 * 035 * @param authorizationHeader 036 * The authorization header from the request. 037 * @return The access token, or {@code null} if the access token was not present or was not using Bearer 038 * authorization. 039 */ 040 public static String getBearerAccessToken(final String authorizationHeader) { 041 042 if (authorizationHeader == null) { 043 return null; 044 } 045 String authorization = authorizationHeader.trim(); 046 final int index = authorization.indexOf(' '); 047 if (index <= 0) { 048 return null; 049 } 050 051 final String tokenType = authorization.substring(0, index); 052 053 if (BEARER_TOKEN_KEY.equalsIgnoreCase(tokenType)) { 054 return authorization.substring(index + 1); 055 } 056 057 return null; 058 } 059 060 private OAuth2() { 061 /* utility class */ 062 } 063}