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