64
64
import com .owncloud .android .oc_framework .operations .RemoteOperationResult ;
65
65
import com .owncloud .android .oc_framework .operations .RemoteOperationResult .ResultCode ;
66
66
import com .owncloud .android .oc_framework .operations .remote .ExistenceCheckRemoteOperation ;
67
+ import com .owncloud .android .oc_framework .operations .remote .GetUserNameRemoteOperation ;
67
68
import com .owncloud .android .ui .dialog .SamlWebViewDialog ;
68
69
import com .owncloud .android .ui .dialog .SslValidatorDialog ;
69
70
import com .owncloud .android .ui .dialog .SslValidatorDialog .OnSslValidatorListener ;
@@ -102,8 +103,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
102
103
private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT" ;
103
104
private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON" ;
104
105
private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED" ;
105
-
106
- private static final String KEY_OC_USERNAME_EQUALS = "oc_username=" ;
107
106
108
107
private static final String AUTH_ON = "on" ;
109
108
private static final String AUTH_OFF = "off" ;
@@ -792,10 +791,41 @@ public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationRe
792
791
} else {
793
792
onAuthorizationCheckFinish ((ExistenceCheckRemoteOperation )operation , result );
794
793
}
794
+ } else if (operation instanceof GetUserNameRemoteOperation ) {
795
+ onGetUserNameFinish ((GetUserNameRemoteOperation ) operation , result );
796
+
795
797
}
798
+
796
799
}
797
-
798
-
800
+
801
+ private void onGetUserNameFinish (GetUserNameRemoteOperation operation , RemoteOperationResult result ) {
802
+ if (result .isSuccess ()) {
803
+ boolean success = false ;
804
+ String username = operation .getUserName ();
805
+
806
+ if ( mAction == ACTION_CREATE ) {
807
+ mUsernameInput .setText (username );
808
+ success = createAccount ();
809
+ } else {
810
+
811
+ if (!mUsernameInput .getText ().toString ().equals (username )) {
812
+ // fail - not a new account, but an existing one; disallow
813
+ result = new RemoteOperationResult (ResultCode .ACCOUNT_NOT_THE_SAME );
814
+ updateAuthStatusIconAndText (result );
815
+ showAuthStatus ();
816
+ Log_OC .d (TAG , result .getLogMessage ());
817
+ } else {
818
+ updateToken ();
819
+ success = true ;
820
+ }
821
+ }
822
+
823
+ if (success )
824
+ finish ();
825
+ }
826
+
827
+ }
828
+
799
829
private void onSamlBasedFederatedSingleSignOnAuthorizationStart (RemoteOperation operation , RemoteOperationResult result ) {
800
830
try {
801
831
dismissDialog (DIALOG_LOGIN_PROGRESS );
@@ -1120,7 +1150,8 @@ private void onAuthorizationCheckFinish(ExistenceCheckRemoteOperation operation,
1120
1150
success = createAccount ();
1121
1151
1122
1152
} else {
1123
- success = updateToken ();
1153
+ updateToken ();
1154
+ success = true ;
1124
1155
}
1125
1156
1126
1157
if (success ) {
@@ -1166,7 +1197,7 @@ private void onAuthorizationCheckFinish(ExistenceCheckRemoteOperation operation,
1166
1197
* Sets the proper response to get that the Account Authenticator that started this activity saves
1167
1198
* a new authorization token for mAccount.
1168
1199
*/
1169
- private boolean updateToken () {
1200
+ private void updateToken () {
1170
1201
Bundle response = new Bundle ();
1171
1202
response .putString (AccountManager .KEY_ACCOUNT_NAME , mAccount .name );
1172
1203
response .putString (AccountManager .KEY_ACCOUNT_TYPE , mAccount .type );
@@ -1177,16 +1208,6 @@ private boolean updateToken() {
1177
1208
mAccountMgr .setAuthToken (mAccount , mAuthTokenType , mAuthToken );
1178
1209
1179
1210
} else if (AccountTypeUtils .getAuthTokenTypeSamlSessionCookie (MainApp .getAccountType ()).equals (mAuthTokenType )) {
1180
- String username = getUserNameForSamlSso ();
1181
- if (!mUsernameInput .getText ().toString ().equals (username )) {
1182
- // fail - not a new account, but an existing one; disallow
1183
- RemoteOperationResult result = new RemoteOperationResult (ResultCode .ACCOUNT_NOT_THE_SAME );
1184
- updateAuthStatusIconAndText (result );
1185
- showAuthStatus ();
1186
- Log_OC .d (TAG , result .getLogMessage ());
1187
-
1188
- return false ;
1189
- }
1190
1211
1191
1212
response .putString (AccountManager .KEY_AUTHTOKEN , mAuthToken );
1192
1213
// the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention
@@ -1198,7 +1219,6 @@ private boolean updateToken() {
1198
1219
}
1199
1220
setAccountAuthenticatorResult (response );
1200
1221
1201
- return true ;
1202
1222
}
1203
1223
1204
1224
@@ -1216,10 +1236,7 @@ private boolean createAccount() {
1216
1236
1217
1237
Uri uri = Uri .parse (mHostBaseUrl );
1218
1238
String username = mUsernameInput .getText ().toString ().trim ();
1219
- if (isSaml ) {
1220
- username = getUserNameForSamlSso ();
1221
-
1222
- } else if (isOAuth ) {
1239
+ if (isOAuth ) {
1223
1240
username = "OAuth_user" + (new java .util .Random (System .currentTimeMillis ())).nextLong ();
1224
1241
}
1225
1242
String accountName = username + "@" + uri .getHost ();
@@ -1279,20 +1296,6 @@ private boolean createAccount() {
1279
1296
}
1280
1297
}
1281
1298
1282
-
1283
- private String getUserNameForSamlSso () {
1284
- if (mAuthToken != null ) {
1285
- String [] cookies = mAuthToken .split (";" );
1286
- for (int i =0 ; i <cookies .length ; i ++) {
1287
- if (cookies [i ].startsWith (KEY_OC_USERNAME_EQUALS )) {
1288
- String value = Uri .decode (cookies [i ].substring (KEY_OC_USERNAME_EQUALS .length ()));
1289
- return value ;
1290
- }
1291
- }
1292
- }
1293
- return "" ;
1294
- }
1295
-
1296
1299
1297
1300
/**
1298
1301
* {@inheritDoc}
@@ -1593,16 +1596,11 @@ public void onSamlDialogSuccess(String sessionCookie){
1593
1596
1594
1597
if (sessionCookie != null && sessionCookie .length () > 0 ) {
1595
1598
mAuthToken = sessionCookie ;
1596
- boolean success = false ;
1597
- if (mAction == ACTION_CREATE ) {
1598
- success = createAccount ();
1599
-
1600
- } else {
1601
- success = updateToken ();
1602
- }
1603
- if (success ) {
1604
- finish ();
1605
- }
1599
+
1600
+ GetUserNameRemoteOperation getUserOperation = new GetUserNameRemoteOperation ();
1601
+ WebdavClient client = OwnCloudClientFactory .createOwnCloudClient (Uri .parse (mHostBaseUrl ), getApplicationContext (), true );
1602
+ client .setSsoSessionCookie (mAuthToken );
1603
+ getUserOperation .execute (client , this , mHandler );
1606
1604
}
1607
1605
1608
1606
@@ -1652,4 +1650,5 @@ public boolean onTouchEvent(MotionEvent event) {
1652
1650
}
1653
1651
return super .onTouchEvent (event );
1654
1652
}
1653
+
1655
1654
}
0 commit comments