1 package com
.owncloud
.android
.oc_framework
.network
;
2 /* ownCloud Android client application
3 * Copyright (C) 2012 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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
;
35 * Bearer authentication scheme as defined in RFC 6750.
37 * @author David A. Velasco
40 public class BearerAuthScheme
implements AuthScheme
/*extends RFC2617Scheme*/ {
42 private static final String TAG
= BearerAuthScheme
.class.getSimpleName();
44 public static final String AUTH_POLICY
= "Bearer";
46 /** Whether the bearer authentication process is complete */
47 private boolean mComplete
;
49 /** Authentication parameter map */
50 private Map mParams
= null
;
54 * Default constructor for the bearer authentication scheme.
56 public BearerAuthScheme() {
61 * Constructor for the basic authentication scheme.
63 * @param challenge Authentication challenge
65 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
67 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
69 public BearerAuthScheme(final String challenge
) throws MalformedChallengeException
{
70 processChallenge(challenge
);
75 * Returns textual designation of the bearer authentication scheme.
79 public String
getSchemeName() {
84 * Processes the Bearer challenge.
86 * @param challenge The challenge string
88 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
90 public void processChallenge(String challenge
) throws MalformedChallengeException
{
91 String s
= AuthChallengeParser
.extractScheme(challenge
);
92 if (!s
.equalsIgnoreCase(getSchemeName())) {
93 throw new MalformedChallengeException(
94 "Invalid " + getSchemeName() + " challenge: " + challenge
);
96 mParams
= AuthChallengeParser
.extractParams(challenge
);
101 * Tests if the Bearer authentication process has been completed.
103 * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
105 public boolean isComplete() {
106 return this.mComplete
;
110 * Produces bearer authorization string for the given set of
111 * {@link Credentials}.
113 * @param credentials The set of credentials to be used for authentication
114 * @param method Method name is ignored by the bearer authentication scheme
115 * @param uri URI is ignored by the bearer authentication scheme
116 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
117 * for this authentication scheme
118 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
119 * @return A bearer authorization string
121 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
123 public String
authenticate(Credentials credentials
, String method
, String uri
) throws AuthenticationException
{
124 Log
.d(TAG
, "enter BearerScheme.authenticate(Credentials, String, String)");
126 BearerCredentials bearer
= null
;
128 bearer
= (BearerCredentials
) credentials
;
129 } catch (ClassCastException e
) {
130 throw new InvalidCredentialsException(
131 "Credentials cannot be used for bearer authentication: "
132 + credentials
.getClass().getName());
134 return BearerAuthScheme
.authenticate(bearer
);
139 * Returns 'false'. Bearer authentication scheme is request based.
143 public boolean isConnectionBased() {
148 * Produces bearer authorization string for the given set of {@link Credentials}.
150 * @param credentials The set of credentials to be used for authentication
151 * @param method The method being authenticated
152 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
154 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
156 * @return a basic authorization string
158 public String
authenticate(Credentials credentials
, HttpMethod method
) throws AuthenticationException
{
159 Log
.d(TAG
, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
161 if (method
== null
) {
162 throw new IllegalArgumentException("Method may not be null");
164 BearerCredentials bearer
= null
;
166 bearer
= (BearerCredentials
) credentials
;
167 } catch (ClassCastException e
) {
168 throw new InvalidCredentialsException(
169 "Credentials cannot be used for bearer authentication: "
170 + credentials
.getClass().getName());
172 return BearerAuthScheme
.authenticate(
174 method
.getParams().getCredentialCharset());
178 * @deprecated Use {@link #authenticate(BearerCredentials, String)}
180 * Returns a bearer Authorization header value for the given
181 * {@link BearerCredentials}.
183 * @param credentials The credentials to encode.
185 * @return A bearer authorization string
187 public static String
authenticate(BearerCredentials credentials
) {
188 return authenticate(credentials
, "ISO-8859-1");
192 * Returns a bearer Authorization header value for the given
193 * {@link BearerCredentials} and charset.
195 * @param credentials The credentials to encode.
196 * @param charset The charset to use for encoding the credentials
198 * @return A bearer authorization string
202 public static String
authenticate(BearerCredentials credentials
, String charset
) {
203 Log
.d(TAG
, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
205 if (credentials
== null
) {
206 throw new IllegalArgumentException("Credentials may not be null");
208 if (charset
== null
|| charset
.length() == 0) {
209 throw new IllegalArgumentException("charset may not be null or empty");
211 StringBuffer buffer
= new StringBuffer();
212 buffer
.append(credentials
.getAccessToken());
214 //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
215 return "Bearer " + buffer
.toString();
219 * Returns a String identifying the authentication challenge. This is
220 * used, in combination with the host and port to determine if
221 * authorization has already been attempted or not. Schemes which
222 * require multiple requests to complete the authentication should
223 * return a different value for each stage in the request.
225 * Additionally, the ID should take into account any changes to the
226 * authentication challenge and return a different value when appropriate.
227 * For example when the realm changes in basic authentication it should be
228 * considered a different authentication attempt and a different value should
231 * This method simply returns the realm for the challenge.
233 * @return String a String identifying the authentication challenge.
235 * @deprecated no longer used
238 public String
getID() {
243 * Returns authentication parameter with the given name, if available.
245 * @param name The name of the parameter to be returned
247 * @return The parameter with the given name
250 public String
getParameter(String name
) {
252 throw new IllegalArgumentException("Parameter name may not be null");
254 if (mParams
== null
) {
257 return (String
) mParams
.get(name
.toLowerCase());
261 * Returns authentication realm. The realm may not be null.
263 * @return The authentication realm
266 public String
getRealm() {
267 return getParameter("realm");