15
15
16
16
import fr .free .nrw .commons .mwapi .MediaWikiApi ;
17
17
18
+ import static android .accounts .AccountManager .ERROR_CODE_UNSUPPORTED_OPERATION ;
19
+ import static android .accounts .AccountManager .KEY_ACCOUNT_AUTHENTICATOR_RESPONSE ;
20
+ import static android .accounts .AccountManager .KEY_ACCOUNT_NAME ;
21
+ import static android .accounts .AccountManager .KEY_ACCOUNT_TYPE ;
22
+ import static android .accounts .AccountManager .KEY_AUTHTOKEN ;
23
+ import static android .accounts .AccountManager .KEY_BOOLEAN_RESULT ;
24
+ import static android .accounts .AccountManager .KEY_ERROR_CODE ;
25
+ import static android .accounts .AccountManager .KEY_ERROR_MESSAGE ;
26
+ import static android .accounts .AccountManager .KEY_INTENT ;
27
+ import static fr .free .nrw .commons .auth .AccountUtil .ACCOUNT_TYPE ;
28
+ import static fr .free .nrw .commons .auth .LoginActivity .PARAM_USERNAME ;
29
+
18
30
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
19
31
20
32
private final Context context ;
21
- private final AccountUtil accountUtil ;
22
33
private MediaWikiApi mediaWikiApi ;
23
34
24
- public WikiAccountAuthenticator (Context context , AccountUtil accountUtil , MediaWikiApi mwApi ) {
35
+ public WikiAccountAuthenticator (Context context , MediaWikiApi mwApi ) {
25
36
super (context );
26
37
this .context = context ;
27
- this .accountUtil = accountUtil ;
28
38
this .mediaWikiApi = mwApi ;
29
39
}
30
40
31
41
private Bundle unsupportedOperation () {
32
42
Bundle bundle = new Bundle ();
33
- bundle .putInt (AccountManager . KEY_ERROR_CODE , AccountManager . ERROR_CODE_UNSUPPORTED_OPERATION );
43
+ bundle .putInt (KEY_ERROR_CODE , ERROR_CODE_UNSUPPORTED_OPERATION );
34
44
35
45
// HACK: the docs indicate that this is a required key bit it's not displayed to the user.
36
- bundle .putString (AccountManager . KEY_ERROR_MESSAGE , "" );
46
+ bundle .putString (KEY_ERROR_MESSAGE , "" );
37
47
38
48
return bundle ;
39
49
}
40
50
41
51
private boolean supportedAccountType (@ Nullable String type ) {
42
- return accountUtil . accountType () .equals (type );
52
+ return ACCOUNT_TYPE .equals (type );
43
53
}
44
54
45
55
@ Override
@@ -57,10 +67,10 @@ public Bundle addAccount(@NonNull AccountAuthenticatorResponse response,
57
67
58
68
private Bundle addAccount (AccountAuthenticatorResponse response ) {
59
69
Intent Intent = new Intent (context , LoginActivity .class );
60
- Intent .putExtra (AccountManager . KEY_ACCOUNT_AUTHENTICATOR_RESPONSE , response );
70
+ Intent .putExtra (KEY_ACCOUNT_AUTHENTICATOR_RESPONSE , response );
61
71
62
72
Bundle bundle = new Bundle ();
63
- bundle .putParcelable (AccountManager . KEY_INTENT , Intent );
73
+ bundle .putParcelable (KEY_INTENT , Intent );
64
74
65
75
return bundle ;
66
76
}
@@ -80,12 +90,13 @@ public Bundle editProperties(AccountAuthenticatorResponse response, String accou
80
90
private String getAuthCookie (String username , String password ) throws IOException {
81
91
//TODO add 2fa support here
82
92
String result = mediaWikiApi .login (username , password );
83
- if (result .equals ("PASS" )) {
93
+ if (result .equals ("PASS" )) {
84
94
return mediaWikiApi .getAuthCookie ();
85
95
} else {
86
96
return null ;
87
97
}
88
98
}
99
+
89
100
@ Override
90
101
public Bundle getAuthToken (AccountAuthenticatorResponse response , Account account , String authTokenType , Bundle options ) throws NetworkErrorException {
91
102
// Extract the username and password from the Account Manager, and ask
@@ -103,9 +114,9 @@ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account accoun
103
114
}
104
115
if (authCookie != null ) {
105
116
final Bundle result = new Bundle ();
106
- result .putString (AccountManager . KEY_ACCOUNT_NAME , account .name );
107
- result .putString (AccountManager . KEY_ACCOUNT_TYPE , accountUtil . accountType () );
108
- result .putString (AccountManager . KEY_AUTHTOKEN , authCookie );
117
+ result .putString (KEY_ACCOUNT_NAME , account .name );
118
+ result .putString (KEY_ACCOUNT_TYPE , ACCOUNT_TYPE );
119
+ result .putString (KEY_AUTHTOKEN , authCookie );
109
120
return result ;
110
121
}
111
122
}
@@ -114,10 +125,10 @@ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account accoun
114
125
// need to re-prompt them for their credentials. We do that by creating
115
126
// an intent to display our AuthenticatorActivity panel.
116
127
final Intent intent = new Intent (context , LoginActivity .class );
117
- intent .putExtra (LoginActivity . PARAM_USERNAME , account .name );
118
- intent .putExtra (AccountManager . KEY_ACCOUNT_AUTHENTICATOR_RESPONSE , response );
128
+ intent .putExtra (PARAM_USERNAME , account .name );
129
+ intent .putExtra (KEY_ACCOUNT_AUTHENTICATOR_RESPONSE , response );
119
130
final Bundle bundle = new Bundle ();
120
- bundle .putParcelable (AccountManager . KEY_INTENT , intent );
131
+ bundle .putParcelable (KEY_INTENT , intent );
121
132
return bundle ;
122
133
}
123
134
@@ -135,7 +146,7 @@ public Bundle hasFeatures(@NonNull AccountAuthenticatorResponse response,
135
146
@ NonNull Account account , @ NonNull String [] features )
136
147
throws NetworkErrorException {
137
148
Bundle bundle = new Bundle ();
138
- bundle .putBoolean (AccountManager . KEY_BOOLEAN_RESULT , false );
149
+ bundle .putBoolean (KEY_BOOLEAN_RESULT , false );
139
150
return bundle ;
140
151
}
141
152
0 commit comments