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