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