001/*
002 * Copyright 2012-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017package com.unboundid.scim.sdk;
018
019
020/**
021 * This class represents an OAuth token, described by
022 * <i>RFC 6750</i>. This class can be extended by clients
023 * that need to add extra functionality, such as the ability to extract an
024 * expiration date, scope, target audience, etc, from the token. These details
025 * are implementation-specific.
026 */
027public class OAuthToken {
028  /**
029   * This enum defines the supported set of OAuth token types.
030   */
031  public enum Type {
032    /**
033     * OAuth 1.0 token type.
034     */
035    OAuth,
036    /**
037     * OAuth bearer token type.
038     */
039    Bearer
040  }
041
042  /**
043   * The OAuth token type.
044   */
045  private final Type type;
046
047  /**
048   * The OAuth token value.
049   */
050  private final String tokenValue;
051
052  /**
053   * Constructs an OAuth 2.0 bearer token with the given b64token value. Note
054   * that b64token is just an ABNF syntax definition and does not imply any
055   * base64-encoding of the token value.
056   *
057   * @param tokenValue The bearer token value.
058   */
059  public OAuthToken(final String tokenValue)
060  {
061    this(Type.Bearer, tokenValue);
062  }
063
064  /**
065   * Constructs an OAuthToken with the specified {@link Type} and token value.
066   *
067   * @param type The token Type.
068   * @param tokenValue The token value.
069   */
070  public OAuthToken(final Type type, final String tokenValue)
071  {
072    this.type = type;
073    this.tokenValue = tokenValue;
074  }
075
076  /**
077   * Returns the token type.
078   *
079   * @return the token type.
080   */
081  public Type getType()
082  {
083    return type;
084  }
085
086  /**
087   * Returns the token value.
088   *
089   * @return the token value.
090   */
091  public String getTokenValue()
092  {
093    return tokenValue;
094  }
095
096  /**
097   * Returns a formatted representation of the token type and value for use as
098   * an Authorization header value. For example, if this is a bearer token, this
099   * method would return a String like "Bearer vF9dft4qmT".
100   *
101   * @return the token type and value in HTTP header value form.
102   */
103  public String getFormattedValue()
104  {
105    return getType().name() + " " + getTokenValue();
106  }
107}