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/>.
18 package com
.owncloud
.android
.network
;
22 import org
.apache
.commons
.httpclient
.Credentials
;
23 import org
.apache
.commons
.httpclient
.HttpMethod
;
24 import org
.apache
.commons
.httpclient
.auth
.AuthChallengeParser
;
25 import org
.apache
.commons
.httpclient
.auth
.AuthScheme
;
26 import org
.apache
.commons
.httpclient
.auth
.AuthenticationException
;
27 import org
.apache
.commons
.httpclient
.auth
.InvalidCredentialsException
;
28 import org
.apache
.commons
.httpclient
.auth
.MalformedChallengeException
;
30 import com
.owncloud
.android
.Log_OC
;
33 * Bearer authentication scheme as defined in RFC 6750.
35 * @author David A. Velasco
38 public class BearerAuthScheme
implements AuthScheme
/*extends RFC2617Scheme*/ {
40 private static final String TAG
= BearerAuthScheme
.class.getSimpleName();
42 public static final String AUTH_POLICY
= "Bearer";
44 /** Whether the bearer authentication process is complete */
45 private boolean mComplete
;
47 /** Authentication parameter map */
48 private Map mParams
= null
;
52 * Default constructor for the bearer authentication scheme.
54 public BearerAuthScheme() {
59 * Constructor for the basic authentication scheme.
61 * @param challenge Authentication challenge
63 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
65 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
67 public BearerAuthScheme(final String challenge
) throws MalformedChallengeException
{
68 processChallenge(challenge
);
73 * Returns textual designation of the bearer authentication scheme.
77 public String
getSchemeName() {
82 * Processes the Bearer challenge.
84 * @param challenge The challenge string
86 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
88 public void processChallenge(String challenge
) throws MalformedChallengeException
{
89 String s
= AuthChallengeParser
.extractScheme(challenge
);
90 if (!s
.equalsIgnoreCase(getSchemeName())) {
91 throw new MalformedChallengeException(
92 "Invalid " + getSchemeName() + " challenge: " + challenge
);
94 mParams
= AuthChallengeParser
.extractParams(challenge
);
99 * Tests if the Bearer authentication process has been completed.
101 * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
103 public boolean isComplete() {
104 return this.mComplete
;
108 * Produces bearer authorization string for the given set of
109 * {@link Credentials}.
111 * @param credentials The set of credentials to be used for authentication
112 * @param method Method name is ignored by the bearer authentication scheme
113 * @param uri URI is ignored by the bearer authentication scheme
114 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
115 * for this authentication scheme
116 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
117 * @return A bearer authorization string
119 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
121 public String
authenticate(Credentials credentials
, String method
, String uri
) throws AuthenticationException
{
122 Log_OC
.d(TAG
, "enter BearerScheme.authenticate(Credentials, String, String)");
124 BearerCredentials bearer
= null
;
126 bearer
= (BearerCredentials
) credentials
;
127 } catch (ClassCastException e
) {
128 throw new InvalidCredentialsException(
129 "Credentials cannot be used for bearer authentication: "
130 + credentials
.getClass().getName());
132 return BearerAuthScheme
.authenticate(bearer
);
137 * Returns 'false'. Bearer authentication scheme is request based.
141 public boolean isConnectionBased() {
146 * Produces bearer authorization string for the given set of {@link Credentials}.
148 * @param credentials The set of credentials to be used for authentication
149 * @param method The method being authenticated
150 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
152 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
154 * @return a basic authorization string
156 public String
authenticate(Credentials credentials
, HttpMethod method
) throws AuthenticationException
{
157 Log_OC
.d(TAG
, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
159 if (method
== null
) {
160 throw new IllegalArgumentException("Method may not be null");
162 BearerCredentials bearer
= null
;
164 bearer
= (BearerCredentials
) credentials
;
165 } catch (ClassCastException e
) {
166 throw new InvalidCredentialsException(
167 "Credentials cannot be used for bearer authentication: "
168 + credentials
.getClass().getName());
170 return BearerAuthScheme
.authenticate(
172 method
.getParams().getCredentialCharset());
176 * @deprecated Use {@link #authenticate(BearerCredentials, String)}
178 * Returns a bearer Authorization header value for the given
179 * {@link BearerCredentials}.
181 * @param credentials The credentials to encode.
183 * @return A bearer authorization string
185 public static String
authenticate(BearerCredentials credentials
) {
186 return authenticate(credentials
, "ISO-8859-1");
190 * Returns a bearer Authorization header value for the given
191 * {@link BearerCredentials} and charset.
193 * @param credentials The credentials to encode.
194 * @param charset The charset to use for encoding the credentials
196 * @return A bearer authorization string
200 public static String
authenticate(BearerCredentials credentials
, String charset
) {
201 Log_OC
.d(TAG
, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
203 if (credentials
== null
) {
204 throw new IllegalArgumentException("Credentials may not be null");
206 if (charset
== null
|| charset
.length() == 0) {
207 throw new IllegalArgumentException("charset may not be null or empty");
209 StringBuffer buffer
= new StringBuffer();
210 buffer
.append(credentials
.getAccessToken());
212 //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
213 return "Bearer " + buffer
.toString();
217 * Returns a String identifying the authentication challenge. This is
218 * used, in combination with the host and port to determine if
219 * authorization has already been attempted or not. Schemes which
220 * require multiple requests to complete the authentication should
221 * return a different value for each stage in the request.
223 * Additionally, the ID should take into account any changes to the
224 * authentication challenge and return a different value when appropriate.
225 * For example when the realm changes in basic authentication it should be
226 * considered a different authentication attempt and a different value should
229 * This method simply returns the realm for the challenge.
231 * @return String a String identifying the authentication challenge.
233 * @deprecated no longer used
236 public String
getID() {
241 * Returns authentication parameter with the given name, if available.
243 * @param name The name of the parameter to be returned
245 * @return The parameter with the given name
248 public String
getParameter(String name
) {
250 throw new IllegalArgumentException("Parameter name may not be null");
252 if (mParams
== null
) {
255 return (String
) mParams
.get(name
.toLowerCase());
259 * Returns authentication realm. The realm may not be null.
261 * @return The authentication realm
264 public String
getRealm() {
265 return getParameter("realm");