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
;
34 * Bearer authentication scheme as defined in RFC 6750.
36 * @author David A. Velasco
39 public class BearerAuthScheme
implements AuthScheme
/*extends RFC2617Scheme*/ {
41 private static final String TAG
= BearerAuthScheme
.class.getSimpleName();
43 public static final String AUTH_POLICY
= "Bearer";
45 /** Whether the bearer authentication process is complete */
46 private boolean mComplete
;
48 /** Authentication parameter map */
49 private Map mParams
= null
;
53 * Default constructor for the bearer authentication scheme.
55 public BearerAuthScheme() {
60 * Constructor for the basic authentication scheme.
62 * @param challenge Authentication challenge
64 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
66 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
68 public BearerAuthScheme(final String challenge
) throws MalformedChallengeException
{
69 processChallenge(challenge
);
74 * Returns textual designation of the bearer authentication scheme.
78 public String
getSchemeName() {
83 * Processes the Bearer challenge.
85 * @param challenge The challenge string
87 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
89 public void processChallenge(String challenge
) throws MalformedChallengeException
{
90 String s
= AuthChallengeParser
.extractScheme(challenge
);
91 if (!s
.equalsIgnoreCase(getSchemeName())) {
92 throw new MalformedChallengeException(
93 "Invalid " + getSchemeName() + " challenge: " + challenge
);
95 mParams
= AuthChallengeParser
.extractParams(challenge
);
100 * Tests if the Bearer authentication process has been completed.
102 * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
104 public boolean isComplete() {
105 return this.mComplete
;
109 * Produces bearer authorization string for the given set of
110 * {@link Credentials}.
112 * @param credentials The set of credentials to be used for authentication
113 * @param method Method name is ignored by the bearer authentication scheme
114 * @param uri URI is ignored by the bearer authentication scheme
115 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
116 * for this authentication scheme
117 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
118 * @return A bearer authorization string
120 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
122 public String
authenticate(Credentials credentials
, String method
, String uri
) throws AuthenticationException
{
123 Log_OC
.d(TAG
, "enter BearerScheme.authenticate(Credentials, String, String)");
125 BearerCredentials bearer
= null
;
127 bearer
= (BearerCredentials
) credentials
;
128 } catch (ClassCastException e
) {
129 throw new InvalidCredentialsException(
130 "Credentials cannot be used for bearer authentication: "
131 + credentials
.getClass().getName());
133 return BearerAuthScheme
.authenticate(bearer
);
138 * Returns 'false'. Bearer authentication scheme is request based.
142 public boolean isConnectionBased() {
147 * Produces bearer authorization string for the given set of {@link Credentials}.
149 * @param credentials The set of credentials to be used for authentication
150 * @param method The method being authenticated
151 * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
153 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
155 * @return a basic authorization string
157 public String
authenticate(Credentials credentials
, HttpMethod method
) throws AuthenticationException
{
158 Log_OC
.d(TAG
, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
160 if (method
== null
) {
161 throw new IllegalArgumentException("Method may not be null");
163 BearerCredentials bearer
= null
;
165 bearer
= (BearerCredentials
) credentials
;
166 } catch (ClassCastException e
) {
167 throw new InvalidCredentialsException(
168 "Credentials cannot be used for bearer authentication: "
169 + credentials
.getClass().getName());
171 return BearerAuthScheme
.authenticate(
173 method
.getParams().getCredentialCharset());
177 * @deprecated Use {@link #authenticate(BearerCredentials, String)}
179 * Returns a bearer Authorization header value for the given
180 * {@link BearerCredentials}.
182 * @param credentials The credentials to encode.
184 * @return A bearer authorization string
186 public static String
authenticate(BearerCredentials credentials
) {
187 return authenticate(credentials
, "ISO-8859-1");
191 * Returns a bearer Authorization header value for the given
192 * {@link BearerCredentials} and charset.
194 * @param credentials The credentials to encode.
195 * @param charset The charset to use for encoding the credentials
197 * @return A bearer authorization string
201 public static String
authenticate(BearerCredentials credentials
, String charset
) {
202 Log_OC
.d(TAG
, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
204 if (credentials
== null
) {
205 throw new IllegalArgumentException("Credentials may not be null");
207 if (charset
== null
|| charset
.length() == 0) {
208 throw new IllegalArgumentException("charset may not be null or empty");
210 StringBuffer buffer
= new StringBuffer();
211 buffer
.append(credentials
.getAccessToken());
213 //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
214 return "Bearer " + buffer
.toString();
218 * Returns a String identifying the authentication challenge. This is
219 * used, in combination with the host and port to determine if
220 * authorization has already been attempted or not. Schemes which
221 * require multiple requests to complete the authentication should
222 * return a different value for each stage in the request.
224 * Additionally, the ID should take into account any changes to the
225 * authentication challenge and return a different value when appropriate.
226 * For example when the realm changes in basic authentication it should be
227 * considered a different authentication attempt and a different value should
230 * This method simply returns the realm for the challenge.
232 * @return String a String identifying the authentication challenge.
234 * @deprecated no longer used
237 public String
getID() {
242 * Returns authentication parameter with the given name, if available.
244 * @param name The name of the parameter to be returned
246 * @return The parameter with the given name
249 public String
getParameter(String name
) {
251 throw new IllegalArgumentException("Parameter name may not be null");
253 if (mParams
== null
) {
256 return (String
) mParams
.get(name
.toLowerCase());
260 * Returns authentication realm. The realm may not be null.
262 * @return The authentication realm
265 public String
getRealm() {
266 return getParameter("realm");