a346e2c80668122b975abb1b985a7eada48a43bf
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / AccountAuthenticator.java
1 package eu.alefzero.owncloud.authenticator;
2
3 import eu.alefzero.owncloud.ui.activity.AuthenticatorActivity;
4 import android.accounts.*;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.util.Log;
9
10 public class AccountAuthenticator extends AbstractAccountAuthenticator {
11 public static final String OPTIONS_USERNAME = "username";
12 public static final String OPTIONS_PASSWORD = "password";
13
14 public static final String ACCOUNT_TYPE = "owncloud";
15 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
16
17 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
18 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
19 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
20 public static final String KEY_ACCOUNT = "account";
21 public static final String KEY_OC_URL = "oc_url";
22 public static final String KEY_CONTACT_URL = "oc_contact_url";
23
24 private Context mContext;
25
26 public AccountAuthenticator(Context context) {
27 super(context);
28 mContext = context;
29 }
30
31 /**
32 * {@inheritDoc}
33 */
34 @Override
35 public Bundle addAccount(AccountAuthenticatorResponse response,
36 String accountType, String authTokenType, String[] requiredFeatures,
37 Bundle options) throws NetworkErrorException {
38 Log.i(getClass().getName(), "Adding account with type " + accountType +
39 " and auth token " + authTokenType);
40 try {
41 validateAccountType(accountType);
42 //validateAuthTokenType(authTokenType);
43 validateRequiredFeatures(requiredFeatures);
44 } catch (AuthenticatorException e) {
45 e.printStackTrace();
46 return e.getFailureBundle();
47 }
48 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
49 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
50 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
51 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
52 intent.putExtra(KEY_LOGIN_OPTIONS, options);
53
54 setIntentFlags(intent);
55 Log.i(getClass().getName(), intent.toString());
56 final Bundle bundle = new Bundle();
57 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
58 return bundle;
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 @Override
65 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
66 Account account, Bundle options) throws NetworkErrorException {
67 try {
68 validateAccountType(account.type);
69 } catch (AuthenticatorException e) {
70 return e.getFailureBundle();
71 }
72 Intent intent = new Intent(mContext, AuthenticatorActivity.class);
73 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
74 intent.putExtra(KEY_ACCOUNT, account);
75 intent.putExtra(KEY_LOGIN_OPTIONS, options);
76
77 setIntentFlags(intent);
78
79 Bundle resultBundle = new Bundle();
80 resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
81 return resultBundle;
82 }
83
84 @Override
85 public Bundle editProperties(AccountAuthenticatorResponse response,
86 String accountType) {
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
91 public Bundle getAuthToken(AccountAuthenticatorResponse response,
92 Account account, String authTokenType, Bundle options)
93 throws NetworkErrorException {
94 Log.i(getClass().getName(), "Getting authToken");
95 try {
96 validateAccountType(account.type);
97 validateAuthTokenType(authTokenType);
98 } catch (AuthenticatorException e) {
99 Log.w(getClass().getName(), "Validating failded in getAuthToken");
100 return e.getFailureBundle();
101 }
102 final AccountManager am = AccountManager.get(mContext);
103 final String password = am.getPassword(account);
104 if (password != null) {
105 final Bundle result = new Bundle();
106 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
107 result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
108 result.putString(AccountManager.KEY_AUTHTOKEN, password);
109 return result;
110 }
111
112 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
113 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
114 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
115 intent.putExtra(KEY_LOGIN_OPTIONS, options);
116 intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
117
118 final Bundle bundle = new Bundle();
119 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
120 return bundle;
121 }
122
123 @Override
124 public String getAuthTokenLabel(String authTokenType) {
125 return null;
126 }
127
128 @Override
129 public Bundle hasFeatures(AccountAuthenticatorResponse response,
130 Account account, String[] features) throws NetworkErrorException {
131 final Bundle result = new Bundle();
132 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
133 return result;
134 }
135
136 @Override
137 public Bundle updateCredentials(AccountAuthenticatorResponse response,
138 Account account, String authTokenType, Bundle options)
139 throws NetworkErrorException {
140 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
141 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
142 intent.putExtra(KEY_ACCOUNT, account);
143 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
144 intent.putExtra(KEY_LOGIN_OPTIONS, options);
145 setIntentFlags(intent);
146
147 final Bundle bundle = new Bundle();
148 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
149 return bundle;
150 }
151
152 @Override
153 public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response,
154 Account account) throws NetworkErrorException {
155 return super.getAccountRemovalAllowed(response, account);
156 }
157
158 private void setIntentFlags(Intent intent) {
159 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
160 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
161 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
162 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
163 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
164 }
165
166 private void validateAccountType(String type) throws UnsupportedAccountTypeException {
167 if (!type.equals(ACCOUNT_TYPE)) {
168 throw new UnsupportedAccountTypeException();
169 }
170 }
171
172 private void validateAuthTokenType(String authTokenType) throws UnsupportedAuthTokenTypeException {
173 if (!authTokenType.equals(AUTH_TOKEN_TYPE)) {
174 throw new UnsupportedAuthTokenTypeException();
175 }
176 }
177
178 private void validateRequiredFeatures(String[] requiredFeatures) throws UnsupportedFeaturesException {
179 }
180
181 private void validateCreaditials(String username, String password, String path) throws AccessDeniedException {
182
183 }
184
185 public static class AuthenticatorException extends Exception {
186 private static final long serialVersionUID = 1L;
187 private Bundle mFailureBundle;
188
189 public AuthenticatorException(int code, String errorMsg) {
190 mFailureBundle = new Bundle();
191 mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);
192 mFailureBundle.putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
193 }
194
195 public Bundle getFailureBundle() {
196 return mFailureBundle;
197 }
198 }
199
200 public static class UnsupportedAccountTypeException extends AuthenticatorException {
201 private static final long serialVersionUID = 1L;
202
203 public UnsupportedAccountTypeException() {
204 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported account type");
205 }
206 }
207
208 public static class UnsupportedAuthTokenTypeException extends AuthenticatorException {
209 private static final long serialVersionUID = 1L;
210
211 public UnsupportedAuthTokenTypeException() {
212 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported auth token type");
213 }
214 }
215
216 public static class UnsupportedFeaturesException extends AuthenticatorException {
217 public static final long serialVersionUID = 1L;
218
219 public UnsupportedFeaturesException() {
220 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported features");
221 }
222 }
223
224 public static class AccessDeniedException extends AuthenticatorException {
225 public AccessDeniedException(int code, String errorMsg) {
226 super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
227 }
228
229 private static final long serialVersionUID = 1L;
230
231 }
232 }