Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

[New arch] Share modularization #255

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
}
Expand Down
1 change: 0 additions & 1 deletion owncloudComLibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-allopen'

dependencies {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/**
* Dynamic implementation of {@link OwnCloudClientManager}.
* <p>
*
* Wraps instances of {@link SingleSessionManager} and {@link SimpleFactoryManager} and delegates on one
* or the other depending on the known version of the server corresponding to the {@link OwnCloudAccount}
*
Expand Down Expand Up @@ -60,4 +60,4 @@ public void saveAllClients(Context context, String accountType) {
mSimpleFactoryManager.saveAllClients(context, accountType);
mSingleSessionManager.saveAllClients(context, accountType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public int executeHttpMethod(HttpBaseMethod method) throws Exception {
status = method.execute();
checkFirstRedirection(method);

if (mFollowRedirects && !isIdPRedirection()) {
if (mFollowRedirects) {
status = followRedirection(method).getLastStatus();
}

Expand Down Expand Up @@ -422,7 +422,7 @@ private boolean checkUnauthorizedAccess(int status, int repeatCounter) {
*/
private boolean shouldInvalidateAccountCredentials(int httpStatusCode) {

boolean should = (httpStatusCode == HttpConstants.HTTP_UNAUTHORIZED || isIdPRedirection()); // invalid credentials
boolean should = (httpStatusCode == HttpConstants.HTTP_UNAUTHORIZED); // invalid credentials

should &= (mCredentials != null && // real credentials
!(mCredentials instanceof OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials));
Expand Down Expand Up @@ -459,17 +459,6 @@ void setOwnCloudClientManager(OwnCloudClientManager clientManager) {
mOwnCloudClientManager = clientManager;
}

/**
* Check if the redirection is to an identity provider such as SAML or wayf
*
* @return true if the redirection location includes SAML or wayf, false otherwise
*/
private boolean isIdPRedirection() {
return (mRedirectedLocation != null &&
(mRedirectedLocation.toUpperCase().contains("SAML") ||
mRedirectedLocation.toLowerCase().contains("wayf")));
}

public boolean followRedirects() {
return mFollowRedirects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,123 +24,13 @@

package com.owncloud.android.lib.common;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;

import java.io.IOException;

public class OwnCloudClientFactory {

final private static String TAG = OwnCloudClientFactory.class.getSimpleName();

/**
* Creates a OwnCloudClient setup for an ownCloud account
* <p>
* Do not call this method from the main thread.
*
* @param account The ownCloud account
* @param appContext Android application context
* @param currentActivity Caller {@link Activity}
* @return A OwnCloudClient object ready to be used
* @throws AuthenticatorException If the authenticator failed to get the authorization
* token for the account.
* @throws OperationCanceledException If the authenticator operation was cancelled while
* getting the authorization token for the account.
* @throws IOException If there was some I/O error while getting the
* authorization token for the account.
* @throws AccountNotFoundException If 'account' is unknown for the AccountManager
*/
public static OwnCloudClient createOwnCloudClient(Account account, Context appContext,
Activity currentActivity)
throws OperationCanceledException, AuthenticatorException, IOException,
AccountNotFoundException {
Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
AccountManager am = AccountManager.get(appContext);
// TODO avoid calling to getUserData here
boolean isOauth2 =
am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null;
boolean isSamlSso =
am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso);

String username = AccountUtils.getUsernameForAccount(account);
if (isOauth2) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken(
account,
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
null,
currentActivity,
null,
null);

Bundle result = future.getResult();
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) {
throw new AuthenticatorException("WTF!");
}
client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken)
);

} else if (isSamlSso) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken(
account,
AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type),
null,
currentActivity,
null,
null);

Bundle result = future.getResult();
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) {
throw new AuthenticatorException("WTF!");
}
client.setCredentials(
OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken)
);

} else {
AccountManagerFuture<Bundle> future = am.getAuthToken(
account,
AccountTypeUtils.getAuthTokenTypePass(account.type),
null,
currentActivity,
null,
null
);

Bundle result = future.getResult();
String password = result.getString(AccountManager.KEY_AUTHTOKEN);
OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext);
client.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials(
username,
password,
(version != null && version.isPreemptiveAuthenticationPreferred())
)
);
}

// Restore cookies
AccountUtils.restoreCookies(account, client, appContext);

return client;
}

/**
* Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud
* client connections.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2016 ownCloud GmbH.
* Copyright (C) 2019 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -38,9 +38,6 @@ public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
case ALWAYS_NEW_CLIENT:
return new SimpleFactoryManager();

case SINGLE_SESSION_PER_ACCOUNT:
return new SingleSessionManager();

case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING:
return new DynamicSessionManager();

Expand Down Expand Up @@ -86,16 +83,11 @@ private static boolean defaultSingletonMustBeUpdated(Policy policy) {
!(sDefaultSingleton instanceof SimpleFactoryManager)) {
return true;
}
if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT &&
!(sDefaultSingleton instanceof SingleSessionManager)) {
return true;
}
return false;
}

public static enum Policy {
public enum Policy {
ALWAYS_NEW_CLIENT,
SINGLE_SESSION_PER_ACCOUNT,
SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import android.util.Log;

import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.authentication.OwnCloudSamlSsoCredentials;
import com.owncloud.android.lib.common.utils.Log_OC;

import java.io.IOException;
Expand Down Expand Up @@ -119,10 +118,6 @@ public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) thr
account.loadCredentials(context);
client.setCredentials(account.getCredentials());

if (client.getCredentials() instanceof OwnCloudSamlSsoCredentials) {
client.disableAutomaticCookiesHandling();
}

if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Expand Down Expand Up @@ -232,4 +227,4 @@ private void keepUriUpdated(OwnCloudAccount account, OwnCloudClient reusedClient
reusedClient.setBaseUri(recentUri);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2016 ownCloud GmbH.
* Copyright (C) 2019 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,9 +41,4 @@ public static String getAuthTokenTypeAccessToken(String accountType) {
public static String getAuthTokenTypeRefreshToken(String accountType) {
return accountType + ".oauth2.refresh_token";
}

public static String getAuthTokenTypeSamlSessionCookie(String accountType) {
return accountType + ".saml.web_sso.session_cookie";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public static String getWebDavUrlForAccount(Context context, Account account)
OwnCloudCredentials ownCloudCredentials = getCredentialsForAccount(context, account);
webDavUrlForAccount = getBaseUrlForAccount(context, account) + OwnCloudClient.WEBDAV_FILES_PATH_4_0
+ ownCloudCredentials.getUsername();
} catch (OperationCanceledException e) {
e.printStackTrace();
} catch (AuthenticatorException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (OperationCanceledException | AuthenticatorException | IOException e) {
e.printStackTrace();
}

Expand Down Expand Up @@ -148,11 +144,6 @@ public static OwnCloudCredentials getCredentialsForAccount(Context context, Acco
String supportsOAuth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2);
boolean isOauth2 = supportsOAuth2 != null && supportsOAuth2.equals("TRUE");

String supportsSamlSSo = am.getUserData(account,
AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO);

boolean isSamlSso = supportsSamlSSo != null && supportsSamlSSo.equals("TRUE");

String username = AccountUtils.getUsernameForAccount(account);
OwnCloudVersion version = new OwnCloudVersion(am.getUserData(account, Constants.KEY_OC_VERSION));

Expand All @@ -164,14 +155,6 @@ public static OwnCloudCredentials getCredentialsForAccount(Context context, Acco

credentials = OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken);

} else if (isSamlSso) {
String accessToken = am.blockingGetAuthToken(
account,
AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type),
false);

credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken);

} else {
String password = am.blockingGetAuthToken(
account,
Expand Down Expand Up @@ -317,10 +300,7 @@ public static class Constants {
* Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
*/
public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";
/**
* Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
*/
public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso";

/**
* OC account cookies
*/
Expand All @@ -345,6 +325,5 @@ public static class Constants {
* OAuth2 refresh token
**/
public static final String KEY_OAUTH2_REFRESH_TOKEN = "oc_oauth2_refresh_token";

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ public static OwnCloudCredentials newBearerCredentials(String username, String a
return new OwnCloudBearerCredentials(username, authToken);
}

public static OwnCloudCredentials newSamlSsoCredentials(String username, String sessionCookie) {
return new OwnCloudSamlSsoCredentials(username, sessionCookie);
}

public static final OwnCloudCredentials getAnonymousCredentials() {
if (sAnonymousCredentials == null) {
sAnonymousCredentials = new OwnCloudAnonymousCredentials();
Expand Down
Loading