1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Added OwnCloudAccount class and method OwnCloudSessionManager#gertClientFor(OwnCloudAccount, ...) to ease the reuse of OwnCloudClient instances after creating accounts with authentication based in external authentication providers

This commit is contained in:
David A. Velasco 2014-06-12 16:36:24 +02:00
parent a42f6b5d6d
commit e069a8cb9f
10 changed files with 196 additions and 2 deletions

View File

@ -0,0 +1,95 @@
/* ownCloud Android client application
* Copyright (C) 2014 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.lib.common;
import java.io.IOException;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import android.accounts.Account;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.net.Uri;
/**
* OwnCloud Account
*
* @author David A. Velasco
*/
public class OwnCloudAccount {
private Uri mBaseUri;
private OwnCloudCredentials mCredentials;
private String mSavedAccountName;
public OwnCloudAccount(Account savedAccount, Context context)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException {
if (savedAccount == null) {
throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
}
if (context == null) {
throw new IllegalArgumentException("Parameter 'context' cannot be null");
}
mSavedAccountName = savedAccount.name;
mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount));
mCredentials = AccountUtils.getCredentialsForAccount(context, savedAccount);
}
public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) {
if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be null");
}
mSavedAccountName = null;
mBaseUri = baseUri;
mCredentials = credentials != null ?
credentials : OwnCloudCredentialsFactory.getAnonymousCredentials();
String username = credentials.getUsername();
if (username != null) {
mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username);
}
}
public boolean isAnonymous() {
return (mCredentials == null);
}
public Uri getBaseUri() {
return mBaseUri;
}
public OwnCloudCredentials getCredentials() {
return mCredentials;
}
public String getName() {
return mSavedAccountName;
}
}

View File

@ -30,6 +30,11 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
);
}
@Override
public String getUsername() {
return mUsername;
}
@Override
public String getAuthToken() {
return mPassword;

View File

@ -32,6 +32,12 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials {
);
}
@Override
public String getUsername() {
// its unknown
return null;
}
@Override
public String getAuthToken() {
return mAccessToken;

View File

@ -63,4 +63,6 @@ public interface OwnCloudClientManager {
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException;
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context);
}

View File

@ -21,6 +21,8 @@ public interface OwnCloudCredentials {
public void applyTo(OwnCloudClient ownCloudClient);
public String getUsername();
public String getAuthToken();
public boolean authTokenExpires();

View File

@ -42,6 +42,12 @@ public class OwnCloudCredentialsFactory {
public boolean authTokenExpires() {
return false;
}
@Override
public String getUsername() {
// no user name
return null;
}
}
}

View File

@ -39,6 +39,12 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
}
}
@Override
public String getUsername() {
// its unknown
return null;
}
@Override
public String getAuthToken() {
return mSessionCookie;

View File

@ -13,6 +13,18 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce
public class SimpleFactoryManager implements OwnCloudClientManager {
@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) {
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
true);
client.setCredentials(account.getCredentials());
return client;
}
@Override
public OwnCloudClient getClientFor(Account savedAccount, Context context)
throws OperationCanceledException, AuthenticatorException, AccountNotFoundException,

View File

@ -64,12 +64,62 @@ public class SingleSessionManager implements OwnCloudClientManager {
private Map<String, Map<OwnCloudCredentials, OwnCloudClient>> mClientsPerServer =
new HashMap<String, Map<OwnCloudCredentials, OwnCloudClient>>();
private Map<String, OwnCloudClient> mClientsWithKnownUsername =
new HashMap<String, OwnCloudClient>();
private Map<String, OwnCloudClient> mClientsWithUnknownUsername =
new HashMap<String, OwnCloudClient>();
public static OwnCloudClientManager getInstance() {
if (mInstance == null) {
mInstance = new SingleSessionManager();
}
return mInstance ;
return mInstance;
}
@Override
public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) {
if (account == null) {
throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account");
}
OwnCloudClient client = null;
String accountName = account.getName();
String sessionName = AccountUtils.buildAccountName(
account.getBaseUri(),
account.getCredentials().getAuthToken());
if (accountName != null) {
client = mClientsWithKnownUsername.get(account.getName());
}
if (client == null) {
if (accountName != null) {
client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) {
mClientsWithKnownUsername.put(accountName, client);
}
} else {
client = mClientsWithUnknownUsername.get(sessionName);
}
}
if (client == null) {
// no client to reuse - create a new one
client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
true);
client.setCredentials(account.getCredentials());
if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
} else {
mClientsWithUnknownUsername.put(sessionName, client);
}
}
return client;
}

View File

@ -37,6 +37,7 @@ import android.accounts.AccountsException;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.net.Uri;
public class AccountUtils {
public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
@ -187,6 +188,15 @@ public class AccountUtils {
}
public static String buildAccountName(Uri serverBaseUrl, String username) {
String accountName = username + "@" + serverBaseUrl.getHost();
if (serverBaseUrl.getPort() >= 0) {
accountName += ":" + serverBaseUrl.getPort();
}
return accountName;
}
public static class AccountNotFoundException extends AccountsException {
/** Generated - should be refreshed every time the class changes!! */