Browse Source

Update documentation / add flow diagram

tags/0.1.0
David Luhmer 1 year ago
parent
commit
8618dab5af

BIN
NextcloudSingleSignOn.png View File


+ 1
- 0
NextcloudSingleSignOn.xml View File

@@ -0,0 +1 @@
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" version="8.6.5" editor="www.draw.io" type="device"><diagram name="Page-1" id="9361dd3d-8414-5efd-6122-117bd74ce7a7">7VxZc9s4Ev41qso8iEUcvB7lazNVySQ1Tu3uPFIUJHFMERoSiu399QvwxkGJlqGMJ+VUSiabJCD2+XU3oBm63j39q4j32890RbIZdFdPM3QzgxAAGPE/gvLcUNwQ1JRNka4aWk+4T/9H2hsb6iFdkVK6kVGasXQvExOa5yRhEi0uCvoo37ammTzrPt40M7o94T6Js/Z7OF5P/0+6YtuaHkK/p38k6Wbbzg385p2XcfKwKeghb2acQbSu/tWXd3E7VjNzuY1X9HFAQrczdF1Qyuqj3dM1yQR7W8bVz92NXO1esCA5m/QADupHvsfZoXn9j3G+yshKvM0z/7hP801G5vfpJp9/yTnhU7os4kJc4jfyz9/IE0syehDHi/1+Bv2Mz321LPjRRhy9U94p75R3yjvFOiXe7flBviz31XkVUthzG8iqQESEq3f55cdtysj9Pk7E1Uceuzlty3YZPwP8cJ1m2TXNaFE9i1YxCdcJp5esoA9kcMVPQrJcd/N9JwUjT6PxB3RRjQMGQneEVaGjeQAGTSBssMK8DYyPfdj1w4a2lUIuaqhxE+033eB9vOMHTcgbCX+ur4W/ls/lPs47TqPFdZbyd5oPAhwfXLpH5T6P7XtxeNhln9I1ydKcn13tSZHy70kEM7OG/LWnnZISxzws5o8U3XmWxfsyXVazCjEXJDkUZfqd/E7KGloJKj0wMdN1B5lcXeQLeI2vIzuCBZ4nCdbX5Qpcg1xDaEWsOqrRxDNg6p6mOavm865m3o0iJVqwLd3QPM6GclJ4d7vwr3zfZC7L0MOeO5mrtUa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j3w02aa0kYShbte5aXNbZGbqjqWbfoUKljBOGZCWrgnxjIojZgPep2Dv72KSH7JiP8cJdW69lyKr7ouvrViHZBtuP8ogPTNRU9lv6nGXiGyT8/fvv2lf/5Nd8f2H2jdSc2Bl9S/STlw9iu7h2pKNtf9qKqjLo2b7IvCk8MZFP3DAnrW/FEURj9gz2R7zt9xyrw5aXPgR84g4v43PXVvtw3C18yiU0t0tvAjuNounNW8erAaJPNgkEtq9mDqqpgvXWk5El0mm++Vd3YObaT13qRjBCCQN+ogQyZmZUVLe3qNlOIYM/7ysX/aHOFSirg+9CuuRrR5YXafOpOq/Bc562WyLWBbJqdXk8+5BfJKl4lwR+TCij7+9Q1wFPl57e7CrpfI1MbvjYFqDfoNYkNpNHsmxvIcKpc/q5+EXDVauDE9nk/0tiDNsWgp2P33LuKdUmLke2Meigz7AqRS4UNaXo3xlQQln+ip9qx0gRHb6buYHl9zFOE150Pi5H4UjGvVcGBVD7Xq1d+I+yRFg/86Pe6Jmlpq7oVqLFKC5I0O1kfRb308mJSqljtSrBTe4fOkBI/7X9ssDa1/kcd0e3/AQ==</diagram></mxfile>

+ 167
- 22
README.md View File

@@ -1,45 +1,190 @@
ownCloud-Account-Importer
Nextcloud Single Sign On
=========================

Account Importer (Android Library Project)
This library allows you to use the network stack provided by the nextcloud app. Therefore you don't need to ask for the users credentials anymore as well as you don't need to worry about self-signed ssl certificates, two factor authentication, etc.



How to use it?
--------------

1) you'll need to extend **IAccountImport**
1) Add this library as a submodule to your project (TODO release this lib on jitpack)
2) Add the following permission to your `AndroidManifest.xml`

```xml
<uses-permission android:name="com.owncloud.android.sso"/>
```

2) To choose an account, include the following code in your login dialog:

```java
final int CHOOSE_ACCOUNT = 12;

private void showAccountChooserLogin() {
Intent intent = AccountManager.newChooseAccountIntent(null, null, new String[] {"nextcloud"}, true, null, null, null, null);
startActivityForResult(intent, CHOOSE_ACCOUNT);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
if (requestCode == CHOOSE_ACCOUNT) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
AccountManager accountManager = AccountManager.get(getActivity());
for (Account account : accountManager.getAccountsByType("nextcloud")) {
if(account.name.equals(accountName)) {
AccountImporter.SetCurrentAccount(getActivity(), account);
break;
}
}
}
}
}
```

3) How to get account information?

```java
Account account = AccountImporter.GetCurrentAccount(getActivity());
SingleSignOnAccount ssoAccount = AccountImporter.GetAuthTokenInSeparateThread(getActivity(), account);

// ssoAccount.name // Name of the account used in the android account manager
// ssoAccount.username
// ssoAccount.password
// ssoAccount.url
// ssoAccount.disableHostnameVerification (TODO remove)
```

That means that you have to implement the following method:
4) How to make a network request?

Well.. if you're already using Retrofit, it's plain simple. If you have an interface such as the following:

```java
public interface API {
public void accountAccessGranted(OwnCloudAccount account);
@GET("user")
Observable<UserInfo> user();

@GET("status")
Observable<NextcloudStatus> status();

@GET("version")
Observable<NextcloudNewsVersion> version();

...
}


// Typical use of API using Retrofit
public class ApiProvider {

private API mApi;

public ApiProvider() {
mApi = retrofit.create(API.class);
}
}
```

you can implement that interface and use the nextcloud network stack instead of the retrofit one!

```java
public class API_SSO implements API {

private static final String mApiEndpoint = "/index.php/apps/news/api/v1-2/";
private NextcloudAPI nextcloudAPI;

public API_SSO(NextcloudAPI nextcloudAPI) {
this.nextcloudAPI = nextcloudAPI;
}

@Override
public Observable<UserInfo> user() {
final Type type = UserInfo.class;
NextcloudRequest request = new NextcloudRequest.Builder()
.setMethod("GET")
.setUrl(mApiEndpoint + "user")
.build();
return nextcloudAPI.performRequestObservable(type, request);
}

As you can see in the following example, it's really easy to get the account data
@Override
public Observable<NextcloudStatus> status() {
Type type = NextcloudStatus.class;
NextcloudRequest request = new NextcloudRequest.Builder()
.setMethod("GET")
.setUrl(mApiEndpoint + "status")
.build();
return nextcloudAPI.performRequestObservable(type, request);
}

@Override
public void accountAccessGranted(OwnCloudAccount account) {
mUsernameView.setText(account.getUsername());
mPasswordView.setText(account.getPassword());
mOc_root_path_View.setText(account.getUrl());
public Observable<NextcloudNewsVersion> version() {
Type type = NextcloudNewsVersion.class;
NextcloudRequest request = new NextcloudRequest.Builder()
.setMethod("GET")
.setUrl(mApiEndpoint + "version")
.build();
return nextcloudAPI.performRequestObservable(type, request);
}

...
}



// Typical use of new API using the nextcloud app network stack (Example)
public class ApiProvider {

private API mApi;

And then you can call the dialog with the following code:
public ApiProvider(NextcloudAPI.ApiConnectedListener callback) {
SingleSignOnAccount ssoAccount =
AccountImporter.GetAuthTokenInSeparateThread(context, account);
NextcloudAPI nextcloudAPI =
new NextcloudAPI(ssoAccount, GsonConfig.GetGson());
nextcloudAPI.start(context, callback);
mApi = new API_SSO(nextcloudAPI)
}
}
```

public static void show(FragmentActivity activity, IAccountImport accountImport)


Here a small example:
5) Enjoy! If you're already using retrofit, you don't need to modify your application logic. Just exchange the API and you're good to go!

view.findViewById(R.id.btn_importAccount).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImportAccountsDialogFragment.show(getActivity(), LoginDialogFragment.this);
# But... I don't use retrofit..

Well.. no worries, the NextcloudAPI provides a method called `performNetworkRequest(NextcloudRequest request)` that allows you to handle the server response yourself.


## Example:
```java
private void downloadFile() {
NextcloudRequest nr = new NextcloudRequest.Builder()
.setMethod("GET")
.setUrl("/remote.php/webdav/sample.mp4")
.build();

try {
InputStream os = nextcloudAPI.performNetworkRequest(nr);
while(os.available() > 0) {
os.read();
// TODO do something useful with the data here..
// like writing it to a file..?
}
});
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
```




# Flow Diagram

//If no other accounts (from other apps) are available.. hide the button
if(AccountImporter.findAccounts(getActivity()).size() <= 0) {
view.findViewById(R.id.btn_importAccount).setVisibility(View.GONE);
}
![](NextcloudSingleSignOn.png)

+ 1
- 1
src/main/java/de/luhmer/owncloud/accountimporter/helper/AccountImporter.java View File

@@ -133,7 +133,7 @@ public class AccountImporter {
}


public static SingleSignOnAccount GetAuthTokenInSeperateThread(final Context context, final Account account) {
public static SingleSignOnAccount GetAuthTokenInSeparateThread(final Context context, final Account account) {
SingleSignOnAccount ssoAccount = null;
ExecutorService executor = Executors.newSingleThreadExecutor();
Callable<SingleSignOnAccount> callable = new Callable<SingleSignOnAccount>() {

Loading…
Cancel
Save