1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package com
.owncloud
.android
.oc_framework
.network
;
29 import org
.apache
.commons
.httpclient
.Credentials
;
30 import org
.apache
.commons
.httpclient
.HttpMethod
;
31 import org
.apache
.commons
.httpclient
.auth
.AuthChallengeParser
;
32 import org
.apache
.commons
.httpclient
.auth
.AuthScheme
;
33 import org
.apache
.commons
.httpclient
.auth
.AuthenticationException
;
34 import org
.apache
.commons
.httpclient
.auth
.InvalidCredentialsException
;
35 import org
.apache
.commons
.httpclient
.auth
.MalformedChallengeException
;
37 import android
.util
.Log
;
42 * Bearer authentication scheme as defined in RFC 6750.
44 * @author David A. Velasco
47 public class BearerAuthScheme
implements AuthScheme
/*extends RFC2617Scheme*/ {
49 private static final String TAG
= BearerAuthScheme
.class.getSimpleName();
51 public static final String AUTH_POLICY
= "Bearer";
53 /** Whether the bearer authentication process is complete */
54 private boolean mComplete
;
56 /** Authentication parameter map */
57 @SuppressWarnings("rawtypes")
58 private Map mParams
= null
;
62 * Default constructor for the bearer authentication scheme.
64 public BearerAuthScheme() {
69 * Constructor for the basic authentication scheme.
71 * @param challenge Authentication challenge
73 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
75 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
77 public BearerAuthScheme(final String challenge
) throws MalformedChallengeException
{
78 processChallenge(challenge
);
83 * Returns textual designation of the bearer authentication scheme.
87 public String
getSchemeName() {
92 * Processes the Bearer challenge.
94 * @param challenge The challenge string
96 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
98 public void processChallenge(String challenge
) throws MalformedChallengeException
{
99 String s
= AuthChallengeParser
.extractScheme(challenge
);
100 if (!s
.equalsIgnoreCase(getSchemeName())) {
101 throw new MalformedChallengeException(
102 "Invalid " + getSchemeName() + " challenge: " + challenge
);
104 mParams
= AuthChallengeParser
.extractParams(challenge
);
109 * Tests if the Bearer authentication process has been completed.
111 * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
113 public boolean isComplete() {
114 return this.mComplete
;
118 * Produces bearer authorization string for the given set of
119 * {@link Credentials}.
121 * @param credentials The set of credentials to be used for authentication
122 * @param method Method name is ignored by the bearer authentication scheme
123 * @param uri URI is ignored by the bearer authentication scheme
124 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
125 * for this authentication scheme
126 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
127 * @return A bearer authorization string
129 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
131 public String
authenticate(Credentials credentials
, String method
, String uri
) throws AuthenticationException
{
132 Log
.d(TAG
, "enter BearerScheme.authenticate(Credentials, String, String)");
134 BearerCredentials bearer
= null
;
136 bearer
= (BearerCredentials
) credentials
;
137 } catch (ClassCastException e
) {
138 throw new InvalidCredentialsException(
139 "Credentials cannot be used for bearer authentication: "
140 + credentials
.getClass().getName());
142 return BearerAuthScheme
.authenticate(bearer
);
147 * Returns 'false'. Bearer authentication scheme is request based.
151 public boolean isConnectionBased() {
156 * Produces bearer authorization string for the given set of {@link Credentials}.
158 * @param credentials The set of credentials to be used for authentication
159 * @param method The method being authenticated
160 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
162 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
164 * @return a basic authorization string
166 public String
authenticate(Credentials credentials
, HttpMethod method
) throws AuthenticationException
{
167 Log
.d(TAG
, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
169 if (method
== null
) {
170 throw new IllegalArgumentException("Method may not be null");
172 BearerCredentials bearer
= null
;
174 bearer
= (BearerCredentials
) credentials
;
175 } catch (ClassCastException e
) {
176 throw new InvalidCredentialsException(
177 "Credentials cannot be used for bearer authentication: "
178 + credentials
.getClass().getName());
180 return BearerAuthScheme
.authenticate(
182 method
.getParams().getCredentialCharset());
186 * @deprecated Use {@link #authenticate(BearerCredentials, String)}
188 * Returns a bearer Authorization header value for the given
189 * {@link BearerCredentials}.
191 * @param credentials The credentials to encode.
193 * @return A bearer authorization string
195 public static String
authenticate(BearerCredentials credentials
) {
196 return authenticate(credentials
, "ISO-8859-1");
200 * Returns a bearer Authorization header value for the given
201 * {@link BearerCredentials} and charset.
203 * @param credentials The credentials to encode.
204 * @param charset The charset to use for encoding the credentials
206 * @return A bearer authorization string
210 public static String
authenticate(BearerCredentials credentials
, String charset
) {
211 Log
.d(TAG
, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
213 if (credentials
== null
) {
214 throw new IllegalArgumentException("Credentials may not be null");
216 if (charset
== null
|| charset
.length() == 0) {
217 throw new IllegalArgumentException("charset may not be null or empty");
219 StringBuffer buffer
= new StringBuffer();
220 buffer
.append(credentials
.getAccessToken());
222 //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
223 return "Bearer " + buffer
.toString();
227 * Returns a String identifying the authentication challenge. This is
228 * used, in combination with the host and port to determine if
229 * authorization has already been attempted or not. Schemes which
230 * require multiple requests to complete the authentication should
231 * return a different value for each stage in the request.
233 * Additionally, the ID should take into account any changes to the
234 * authentication challenge and return a different value when appropriate.
235 * For example when the realm changes in basic authentication it should be
236 * considered a different authentication attempt and a different value should
239 * This method simply returns the realm for the challenge.
241 * @return String a String identifying the authentication challenge.
243 * @deprecated no longer used
246 public String
getID() {
251 * Returns authentication parameter with the given name, if available.
253 * @param name The name of the parameter to be returned
255 * @return The parameter with the given name
258 public String
getParameter(String name
) {
260 throw new IllegalArgumentException("Parameter name may not be null");
262 if (mParams
== null
) {
265 return (String
) mParams
.get(name
.toLowerCase());
269 * Returns authentication realm. The realm may not be null.
271 * @return The authentication realm
274 public String
getRealm() {
275 return getParameter("realm");