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