|
@@ -0,0 +1,151 @@
|
|
|
|
+package com.owncloud.android.ui.activity;
|
|
|
|
+
|
|
|
|
+import android.accounts.Account;
|
|
|
|
+import android.accounts.AccountManager;
|
|
|
|
+import android.content.ComponentName;
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.content.Intent;
|
|
|
|
+import android.content.SharedPreferences;
|
|
|
|
+import android.content.pm.ApplicationInfo;
|
|
|
|
+import android.content.pm.PackageManager;
|
|
|
|
+import android.graphics.drawable.Drawable;
|
|
|
|
+import android.os.Bundle;
|
|
|
|
+import android.util.Log;
|
|
|
|
+import android.widget.ImageView;
|
|
|
|
+import android.widget.TextView;
|
|
|
|
+
|
|
|
|
+import com.nextcloud.android.sso.Constants;
|
|
|
|
+import com.owncloud.android.MainApp;
|
|
|
|
+import com.owncloud.android.R;
|
|
|
|
+import com.owncloud.android.lib.common.OwnCloudAccount;
|
|
|
|
+import com.owncloud.android.lib.common.accounts.AccountUtils;
|
|
|
|
+import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
|
+import com.owncloud.android.utils.EncryptionUtils;
|
|
|
|
+
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
|
|
+import butterknife.BindView;
|
|
|
|
+import butterknife.ButterKnife;
|
|
|
|
+import butterknife.OnClick;
|
|
|
|
+
|
|
|
|
+import static com.nextcloud.android.sso.Constants.EXCEPTION_ACCOUNT_ACCESS_DECLINED;
|
|
|
|
+import static com.nextcloud.android.sso.Constants.EXCEPTION_ACCOUNT_NOT_FOUND;
|
|
|
|
+import static com.nextcloud.android.sso.Constants.NEXTCLOUD_FILES_ACCOUNT;
|
|
|
|
+import static com.nextcloud.android.sso.Constants.NEXTCLOUD_SSO;
|
|
|
|
+import static com.nextcloud.android.sso.Constants.NEXTCLOUD_SSO_EXCEPTION;
|
|
|
|
+import static com.nextcloud.android.sso.Constants.SSO_SHARED_PREFERENCE;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+public class SsoGrantPermissionActivity extends BaseActivity {
|
|
|
|
+
|
|
|
|
+ private static final String TAG = SsoGrantPermissionActivity.class.getCanonicalName();
|
|
|
|
+
|
|
|
|
+ private String packageName;
|
|
|
|
+ private Account account;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @BindView(R.id.imageView)
|
|
|
|
+ ImageView imageView;
|
|
|
|
+
|
|
|
|
+ @BindView(R.id.tvInfoText)
|
|
|
|
+ TextView tvInfo;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
+ setContentView(R.layout.activity_sso_grant_permission);
|
|
|
|
+
|
|
|
|
+ ButterKnife.bind(this);
|
|
|
|
+ try {
|
|
|
|
+ ComponentName callingActivity = getCallingActivity();
|
|
|
|
+ if(callingActivity != null) {
|
|
|
|
+ packageName = callingActivity.getPackageName();
|
|
|
|
+ String appName = getAppNameForPackage(packageName);
|
|
|
|
+ account = getIntent().getParcelableExtra(NEXTCLOUD_FILES_ACCOUNT);
|
|
|
|
+ Drawable appIcon = getPackageManager().getApplicationIcon(packageName);
|
|
|
|
+ imageView.setImageDrawable(appIcon);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ tvInfo.setText(getString(R.string.single_sign_on_request_token, appName, account.name));
|
|
|
|
+
|
|
|
|
+ Log.v(TAG, "TOKEN-REQUEST: Calling Package: " + packageName);
|
|
|
|
+ Log.v(TAG, "TOKEN-REQUEST: App Name: " + appName);
|
|
|
|
+ } else {
|
|
|
|
+ // Activity was not started using startActivityForResult!
|
|
|
|
+ Log.e(TAG, "Calling Package is null");
|
|
|
|
+ setResultAndExit("Request was not executed properly. Use startActivityForResult()");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (PackageManager.NameNotFoundException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void setResultAndExit(String exception) {
|
|
|
|
+ Intent data = new Intent();
|
|
|
|
+ data.putExtra(NEXTCLOUD_SSO_EXCEPTION, exception);
|
|
|
|
+ setResult(RESULT_CANCELED, data);
|
|
|
|
+ finish();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getAppNameForPackage(String pkg) {
|
|
|
|
+ final PackageManager pm = getApplicationContext().getPackageManager();
|
|
|
|
+ ApplicationInfo ai;
|
|
|
|
+ try {
|
|
|
|
+ ai = pm.getApplicationInfo(pkg, 0);
|
|
|
|
+ } catch (final PackageManager.NameNotFoundException e) {
|
|
|
|
+ ai = null;
|
|
|
|
+ }
|
|
|
|
+ return (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @OnClick(R.id.btnDecline)
|
|
|
|
+ void exitFailed() {
|
|
|
|
+ setResultAndExit(EXCEPTION_ACCOUNT_ACCESS_DECLINED);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @OnClick(R.id.btnGrant)
|
|
|
|
+ void grantPermission() {
|
|
|
|
+ final Bundle result = new Bundle();
|
|
|
|
+
|
|
|
|
+ // create token
|
|
|
|
+ SharedPreferences sharedPreferences = getSharedPreferences(SSO_SHARED_PREFERENCE, Context.MODE_PRIVATE);
|
|
|
|
+ String token = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+
|
|
|
|
+ String hashedTokenWithSalt = EncryptionUtils.generateSHA512(token);
|
|
|
|
+
|
|
|
|
+ SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
|
|
+ editor.putString(packageName, hashedTokenWithSalt);
|
|
|
|
+ editor.apply();
|
|
|
|
+
|
|
|
|
+ String serverUrl;
|
|
|
|
+ String userId;
|
|
|
|
+ try {
|
|
|
|
+ OwnCloudAccount ocAccount = new OwnCloudAccount(account, this);
|
|
|
|
+ serverUrl = ocAccount.getBaseUri().toString();
|
|
|
|
+ AccountManager accountManager = AccountManager.get(this);
|
|
|
|
+ userId = accountManager.getUserData(account,
|
|
|
|
+ com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
|
|
|
|
+ } catch (AccountUtils.AccountNotFoundException e) {
|
|
|
|
+ Log_OC.e(TAG, "Account not found");
|
|
|
|
+ setResultAndExit(EXCEPTION_ACCOUNT_NOT_FOUND);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
|
|
|
|
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType(this));
|
|
|
|
+ result.putString(AccountManager.KEY_AUTHTOKEN, NEXTCLOUD_SSO);
|
|
|
|
+ result.putString(Constants.SSO_USERNAME, userId);
|
|
|
|
+ result.putString(Constants.SSO_TOKEN, token);
|
|
|
|
+ result.putString(Constants.SSO_SERVER_URL, serverUrl);
|
|
|
|
+
|
|
|
|
+ //return result;
|
|
|
|
+
|
|
|
|
+ Intent data = new Intent();
|
|
|
|
+ data.putExtra(NEXTCLOUD_SSO, result);
|
|
|
|
+ setResult(RESULT_OK, data);
|
|
|
|
+ finish();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|