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