8589dbd2181d692452be333002afe59bd29e2bcd
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / network / BearerAuthScheme.java
1 package com.owncloud.android.oc_framework.network;
2 /* ownCloud Android client application
3 * Copyright (C) 2012 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19
20
21 import java.util.Map;
22
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;
30
31 import android.util.Log;
32
33
34 /**
35 * Bearer authentication scheme as defined in RFC 6750.
36 *
37 * @author David A. Velasco
38 */
39
40 public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
41
42 private static final String TAG = BearerAuthScheme.class.getSimpleName();
43
44 public static final String AUTH_POLICY = "Bearer";
45
46 /** Whether the bearer authentication process is complete */
47 private boolean mComplete;
48
49 /** Authentication parameter map */
50 private Map mParams = null;
51
52
53 /**
54 * Default constructor for the bearer authentication scheme.
55 */
56 public BearerAuthScheme() {
57 mComplete = false;
58 }
59
60 /**
61 * Constructor for the basic authentication scheme.
62 *
63 * @param challenge Authentication challenge
64 *
65 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
66 *
67 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} method
68 */
69 public BearerAuthScheme(final String challenge) throws MalformedChallengeException {
70 processChallenge(challenge);
71 mComplete = true;
72 }
73
74 /**
75 * Returns textual designation of the bearer authentication scheme.
76 *
77 * @return "Bearer"
78 */
79 public String getSchemeName() {
80 return "bearer";
81 }
82
83 /**
84 * Processes the Bearer challenge.
85 *
86 * @param challenge The challenge string
87 *
88 * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
89 */
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);
95 }
96 mParams = AuthChallengeParser.extractParams(challenge);
97 mComplete = true;
98 }
99
100 /**
101 * Tests if the Bearer authentication process has been completed.
102 *
103 * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
104 */
105 public boolean isComplete() {
106 return this.mComplete;
107 }
108
109 /**
110 * Produces bearer authorization string for the given set of
111 * {@link Credentials}.
112 *
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
120 *
121 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
122 */
123 public String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException {
124 Log.d(TAG, "enter BearerScheme.authenticate(Credentials, String, String)");
125
126 BearerCredentials bearer = null;
127 try {
128 bearer = (BearerCredentials) credentials;
129 } catch (ClassCastException e) {
130 throw new InvalidCredentialsException(
131 "Credentials cannot be used for bearer authentication: "
132 + credentials.getClass().getName());
133 }
134 return BearerAuthScheme.authenticate(bearer);
135 }
136
137
138 /**
139 * Returns 'false'. Bearer authentication scheme is request based.
140 *
141 * @return 'false'.
142 */
143 public boolean isConnectionBased() {
144 return false;
145 }
146
147 /**
148 * Produces bearer authorization string for the given set of {@link Credentials}.
149 *
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
153 * scheme.
154 * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
155 *
156 * @return a basic authorization string
157 */
158 public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
159 Log.d(TAG, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
160
161 if (method == null) {
162 throw new IllegalArgumentException("Method may not be null");
163 }
164 BearerCredentials bearer = null;
165 try {
166 bearer = (BearerCredentials) credentials;
167 } catch (ClassCastException e) {
168 throw new InvalidCredentialsException(
169 "Credentials cannot be used for bearer authentication: "
170 + credentials.getClass().getName());
171 }
172 return BearerAuthScheme.authenticate(
173 bearer,
174 method.getParams().getCredentialCharset());
175 }
176
177 /**
178 * @deprecated Use {@link #authenticate(BearerCredentials, String)}
179 *
180 * Returns a bearer Authorization header value for the given
181 * {@link BearerCredentials}.
182 *
183 * @param credentials The credentials to encode.
184 *
185 * @return A bearer authorization string
186 */
187 public static String authenticate(BearerCredentials credentials) {
188 return authenticate(credentials, "ISO-8859-1");
189 }
190
191 /**
192 * Returns a bearer Authorization header value for the given
193 * {@link BearerCredentials} and charset.
194 *
195 * @param credentials The credentials to encode.
196 * @param charset The charset to use for encoding the credentials
197 *
198 * @return A bearer authorization string
199 *
200 * @since 3.0
201 */
202 public static String authenticate(BearerCredentials credentials, String charset) {
203 Log.d(TAG, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
204
205 if (credentials == null) {
206 throw new IllegalArgumentException("Credentials may not be null");
207 }
208 if (charset == null || charset.length() == 0) {
209 throw new IllegalArgumentException("charset may not be null or empty");
210 }
211 StringBuffer buffer = new StringBuffer();
212 buffer.append(credentials.getAccessToken());
213
214 //return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
215 return "Bearer " + buffer.toString();
216 }
217
218 /**
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.
224 *
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
229 * be returned.
230 *
231 * This method simply returns the realm for the challenge.
232 *
233 * @return String a String identifying the authentication challenge.
234 *
235 * @deprecated no longer used
236 */
237 @Override
238 public String getID() {
239 return getRealm();
240 }
241
242 /**
243 * Returns authentication parameter with the given name, if available.
244 *
245 * @param name The name of the parameter to be returned
246 *
247 * @return The parameter with the given name
248 */
249 @Override
250 public String getParameter(String name) {
251 if (name == null) {
252 throw new IllegalArgumentException("Parameter name may not be null");
253 }
254 if (mParams == null) {
255 return null;
256 }
257 return (String) mParams.get(name.toLowerCase());
258 }
259
260 /**
261 * Returns authentication realm. The realm may not be null.
262 *
263 * @return The authentication realm
264 */
265 @Override
266 public String getRealm() {
267 return getParameter("realm");
268 }
269
270 }