1 /* ownCloud Android client application
2 * Copyright (C) 2012 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.oc_framework
.network
;
23 import org
.apache
.commons
.httpclient
.Credentials
;
24 import org
.apache
.commons
.httpclient
.HttpMethod
;
25 import org
.apache
.commons
.httpclient
.auth
.AuthChallengeParser
;
26 import org
.apache
.commons
.httpclient
.auth
.AuthScheme
;
27 import org
.apache
.commons
.httpclient
.auth
.AuthenticationException
;
28 import org
.apache
.commons
.httpclient
.auth
.InvalidCredentialsException
;
29 import org
.apache
.commons
.httpclient
.auth
.MalformedChallengeException
;
31 import android
.util
.Log
;
36 * Bearer authentication scheme as defined in RFC 6750.
38 * @author David A. Velasco
41 public class BearerAuthScheme
implements AuthScheme
/*extends RFC2617Scheme*/ {
43 private static final String TAG
= BearerAuthScheme
.class.getSimpleName();
45 public static final String AUTH_POLICY
= "Bearer";
47 /** Whether the bearer authentication process is complete */
48 private boolean mComplete
;
50 /** Authentication parameter map */
51 @SuppressWarnings("rawtypes")
52 private Map mParams
= null
;
56 * Default constructor for the bearer authentication scheme.
58 public BearerAuthScheme() {
63 * Constructor for the basic authentication scheme.
65 * @param challenge Authentication challenge
67 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
69 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
71 public BearerAuthScheme(final String challenge
) throws MalformedChallengeException
{
72 processChallenge(challenge
);
77 * Returns textual designation of the bearer authentication scheme.
81 public String
getSchemeName() {
86 * Processes the Bearer challenge.
88 * @param challenge The challenge string
90 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
92 public void processChallenge(String challenge
) throws MalformedChallengeException
{
93 String s
= AuthChallengeParser
.extractScheme(challenge
);
94 if (!s
.equalsIgnoreCase(getSchemeName())) {
95 throw new MalformedChallengeException(
96 "Invalid " + getSchemeName() + " challenge: " + challenge
);
98 mParams
= AuthChallengeParser
.extractParams(challenge
);
103 * Tests if the Bearer authentication process has been completed.
105 * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
107 public boolean isComplete() {
108 return this.mComplete
;
112 * Produces bearer authorization string for the given set of
113 * {@link Credentials}.
115 * @param credentials The set of credentials to be used for authentication
116 * @param method Method name is ignored by the bearer authentication scheme
117 * @param uri URI is ignored by the bearer authentication scheme
118 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
119 * for this authentication scheme
120 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
121 * @return A bearer authorization string
123 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
125 public String
authenticate(Credentials credentials
, String method
, String uri
) throws AuthenticationException
{
126 Log
.d(TAG
, "enter BearerScheme.authenticate(Credentials, String, String)");
128 BearerCredentials bearer
= null
;
130 bearer
= (BearerCredentials
) credentials
;
131 } catch (ClassCastException e
) {
132 throw new InvalidCredentialsException(
133 "Credentials cannot be used for bearer authentication: "
134 + credentials
.getClass().getName());
136 return BearerAuthScheme
.authenticate(bearer
);
141 * Returns 'false'. Bearer authentication scheme is request based.
145 public boolean isConnectionBased() {
150 * Produces bearer authorization string for the given set of {@link Credentials}.
152 * @param credentials The set of credentials to be used for authentication
153 * @param method The method being authenticated
154 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
156 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
158 * @return a basic authorization string
160 public String
authenticate(Credentials credentials
, HttpMethod method
) throws AuthenticationException
{
161 Log
.d(TAG
, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
163 if (method
== null
) {
164 throw new IllegalArgumentException("Method may not be null");
166 BearerCredentials bearer
= null
;
168 bearer
= (BearerCredentials
) credentials
;
169 } catch (ClassCastException e
) {
170 throw new InvalidCredentialsException(
171 "Credentials cannot be used for bearer authentication: "
172 + credentials
.getClass().getName());
174 return BearerAuthScheme
.authenticate(
176 method
.getParams().getCredentialCharset());
180 * @deprecated Use {@link #authenticate(BearerCredentials, String)}
182 * Returns a bearer Authorization header value for the given
183 * {@link BearerCredentials}.
185 * @param credentials The credentials to encode.
187 * @return A bearer authorization string
189 public static String
authenticate(BearerCredentials credentials
) {
190 return authenticate(credentials
, "ISO-8859-1");
194 * Returns a bearer Authorization header value for the given
195 * {@link BearerCredentials} and charset.
197 * @param credentials The credentials to encode.
198 * @param charset The charset to use for encoding the credentials
200 * @return A bearer authorization string
204 public static String
authenticate(BearerCredentials credentials
, String charset
) {
205 Log
.d(TAG
, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
207 if (credentials
== null
) {
208 throw new IllegalArgumentException("Credentials may not be null");
210 if (charset
== null
|| charset
.length() == 0) {
211 throw new IllegalArgumentException("charset may not be null or empty");
213 StringBuffer buffer
= new StringBuffer();
214 buffer
.append(credentials
.getAccessToken());
216 //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
217 return "Bearer " + buffer
.toString();
221 * Returns a String identifying the authentication challenge. This is
222 * used, in combination with the host and port to determine if
223 * authorization has already been attempted or not. Schemes which
224 * require multiple requests to complete the authentication should
225 * return a different value for each stage in the request.
227 * Additionally, the ID should take into account any changes to the
228 * authentication challenge and return a different value when appropriate.
229 * For example when the realm changes in basic authentication it should be
230 * considered a different authentication attempt and a different value should
233 * This method simply returns the realm for the challenge.
235 * @return String a String identifying the authentication challenge.
237 * @deprecated no longer used
240 public String
getID() {
245 * Returns authentication parameter with the given name, if available.
247 * @param name The name of the parameter to be returned
249 * @return The parameter with the given name
252 public String
getParameter(String name
) {
254 throw new IllegalArgumentException("Parameter name may not be null");
256 if (mParams
== null
) {
259 return (String
) mParams
.get(name
.toLowerCase());
263 * Returns authentication realm. The realm may not be null.
265 * @return The authentication realm
268 public String
getRealm() {
269 return getParameter("realm");