001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.authc; 020 021 022/** 023 * A {@link AuthenticationToken} that contains an a Bearer token or API key, typically received via an HTTP {@code Authorization} header. This 024 * class also implements the {@link org.apache.shiro.authc.HostAuthenticationToken HostAuthenticationToken} interface to retain the host name 025 * or IP address location from where the authentication attempt is occurring. 026 * 027 * @see <a href="https://tools.ietf.org/html/rfc2617">RFC 2617</a> 028 * @see <a href="https://tools.ietf.org/html/rfc6750#section-2.1">OAuth2 Authorization Request Header Field</a> 029 * 030 * @since 1.5 031 */ 032public class BearerToken implements HostAuthenticationToken { 033 034 private final String token; 035 036 /** 037 * The location from where the login attempt occurs, or <code>null</code> if not known or explicitly 038 * omitted. 039 */ 040 private String host; 041 042 public BearerToken(String token) { 043 this(token, null); 044 } 045 046 public BearerToken(String token, String host) { 047 this.token = token; 048 this.host = host; 049 } 050 051 @Override 052 public String getHost() { 053 return host; 054 } 055 056 @Override 057 public Object getPrincipal() { 058 return token; 059 } 060 061 @Override 062 public Object getCredentials() { 063 return token; 064 } 065 066 public String getToken() { 067 return token; 068 } 069}