View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.authc;
20  
21  
22  /**
23   * A {@link AuthenticationToken} that contains an a Bearer token or API key, typically received via an HTTP {@code Authorization} header. This
24   * class also implements the {@link org.apache.shiro.authc.HostAuthenticationToken HostAuthenticationToken} interface to retain the host name
25   * or IP address location from where the authentication attempt is occurring.
26   *
27   * @see <a href="https://tools.ietf.org/html/rfc2617">RFC 2617</a>
28   * @see <a href="https://tools.ietf.org/html/rfc6750#section-2.1">OAuth2 Authorization Request Header Field</a>
29   *
30   * @since 1.5
31   */
32  public class BearerToken implements HostAuthenticationToken {
33  
34      private final String token;
35  
36      /**
37       * The location from where the login attempt occurs, or <code>null</code> if not known or explicitly
38       * omitted.
39       */
40      private String host;
41  
42      public BearerToken(String token) {
43          this(token, null);
44      }
45  
46      public BearerToken(String token, String host) {
47          this.token = token;
48          this.host = host;
49      }
50  
51      @Override
52      public String getHost() {
53          return host;
54      }
55  
56      @Override
57      public Object getPrincipal() {
58          return token;
59      }
60  
61      @Override
62      public Object getCredentials() {
63          return token;
64      }
65  
66      public String getToken() {
67          return token;
68      }
69  }