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 @@
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/E7KGloJKj0wMdN1B5lcXeQLeI2vIzuCBZ4nCdbX5Qpcg1xDaEWsOqrRxDNg6p6mOavm865m3o0iJVqwLd3QPM6GclJ4d7vwr3zfZC7L0MOeO5mr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WkHj3w02aa0kYShbte5aXNbZGbqjqWbfoUKljBOGZCWrgnxjIojZgPep2Dv72KSH7JiP8cJdW69lyKr7ouvrViHZBtuP8ogPTNRU9lv6nGXiGyT8/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 @@
1
-ownCloud-Account-Importer
1
+Nextcloud Single Sign On
2 2
 =========================
3 3
 
4
-Account Importer (Android Library Project)
4
+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.
5 5
 
6 6
 
7 7
 
8 8
 How to use it?
9 9
 --------------
10 10
 
11
-1) you'll need to extend **IAccountImport**
11
+1) Add this library as a submodule to your project (TODO release this lib on jitpack)
12
+2) Add the following permission to your `AndroidManifest.xml` 
13
+
14
+```xml
15
+<uses-permission android:name="com.owncloud.android.sso"/>
16
+```
17
+
18
+2) To choose an account, include the following code in your login dialog:
19
+
20
+```java
21
+final int CHOOSE_ACCOUNT = 12;
22
+
23
+private void showAccountChooserLogin() {
24
+    Intent intent = AccountManager.newChooseAccountIntent(null, null, new String[] {"nextcloud"}, true, null, null, null, null);
25
+    startActivityForResult(intent, CHOOSE_ACCOUNT);
26
+}
27
+
28
+@Override
29
+public void onActivityResult(int requestCode, int resultCode, Intent data) {
30
+    super.onActivityResult(requestCode, resultCode, data);
31
+
32
+    if (resultCode == RESULT_OK) {
33
+        if (requestCode == CHOOSE_ACCOUNT) {
34
+            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
35
+            AccountManager accountManager = AccountManager.get(getActivity());
36
+            for (Account account : accountManager.getAccountsByType("nextcloud")) {
37
+                if(account.name.equals(accountName)) {
38
+                    AccountImporter.SetCurrentAccount(getActivity(), account);
39
+                    break;
40
+                }
41
+            }
42
+        }
43
+    }
44
+}
45
+```
46
+
47
+3) How to get account information?
48
+
49
+```java
50
+Account account = AccountImporter.GetCurrentAccount(getActivity());
51
+SingleSignOnAccount ssoAccount = AccountImporter.GetAuthTokenInSeparateThread(getActivity(), account);
52
+
53
+// ssoAccount.name // Name of the account used in the android account manager
54
+// ssoAccount.username
55
+// ssoAccount.password
56
+// ssoAccount.url 
57
+// ssoAccount.disableHostnameVerification (TODO remove)
58
+```
12 59
 
13
-That means that you have to implement the following method:
60
+4) How to make a network request?
61
+
62
+Well.. if you're already using Retrofit, it's plain simple. If you have an interface such as the following: 
63
+
64
+```java
65
+public interface API {
14 66
     
15
-    public void accountAccessGranted(OwnCloudAccount account);
67
+    @GET("user")
68
+    Observable<UserInfo> user();
69
+
70
+    @GET("status")
71
+    Observable<NextcloudStatus> status();
72
+
73
+    @GET("version")
74
+    Observable<NextcloudNewsVersion> version();
75
+
76
+    ...
77
+}
78
+
79
+
80
+// Typical use of API using Retrofit
81
+public class ApiProvider {
82
+
83
+    private API mApi;
84
+
85
+    public ApiProvider() {
86
+        mApi = retrofit.create(API.class);
87
+    } 
88
+}
89
+```
90
+
91
+you can implement that interface and use the nextcloud network stack instead of the retrofit one!
92
+
93
+```java
94
+public class API_SSO implements API {
95
+
96
+    private static final String mApiEndpoint = "/index.php/apps/news/api/v1-2/";
97
+    private NextcloudAPI nextcloudAPI;
98
+
99
+    public API_SSO(NextcloudAPI nextcloudAPI) {
100
+        this.nextcloudAPI = nextcloudAPI;
101
+    }
16 102
 
103
+    @Override
104
+    public Observable<UserInfo> user() {
105
+        final Type type = UserInfo.class;
106
+        NextcloudRequest request = new NextcloudRequest.Builder()
107
+                .setMethod("GET")
108
+                .setUrl(mApiEndpoint + "user")
109
+                .build();
110
+        return nextcloudAPI.performRequestObservable(type, request);
111
+    }
17 112
 
18
-As you can see in the following example, it's really easy to get the account data
113
+    @Override
114
+    public Observable<NextcloudStatus> status() {
115
+        Type type = NextcloudStatus.class;
116
+        NextcloudRequest request = new NextcloudRequest.Builder()
117
+                .setMethod("GET")
118
+                .setUrl(mApiEndpoint + "status")
119
+                .build();
120
+        return nextcloudAPI.performRequestObservable(type, request);
121
+    }
19 122
 
20 123
     @Override
21
-    public void accountAccessGranted(OwnCloudAccount account) {
22
-        mUsernameView.setText(account.getUsername());
23
-        mPasswordView.setText(account.getPassword());
24
-        mOc_root_path_View.setText(account.getUrl());
124
+    public Observable<NextcloudNewsVersion> version() {
125
+        Type type = NextcloudNewsVersion.class;
126
+        NextcloudRequest request = new NextcloudRequest.Builder()
127
+                .setMethod("GET")
128
+                .setUrl(mApiEndpoint + "version")
129
+                .build();
130
+        return nextcloudAPI.performRequestObservable(type, request);
25 131
     }
26 132
 
133
+    ...
134
+}
135
+
136
+
137
+
138
+// Typical use of new API using the nextcloud app network stack (Example)
139
+public class ApiProvider {
140
+
141
+    private API mApi;
27 142
 
28
-And then you can call the dialog with the following code:
143
+    public ApiProvider(NextcloudAPI.ApiConnectedListener callback) {
144
+        SingleSignOnAccount ssoAccount = 
145
+            AccountImporter.GetAuthTokenInSeparateThread(context, account);
146
+        NextcloudAPI nextcloudAPI = 
147
+            new NextcloudAPI(ssoAccount, GsonConfig.GetGson());
148
+        nextcloudAPI.start(context, callback);
149
+        mApi = new API_SSO(nextcloudAPI)
150
+    } 
151
+}
152
+```
29 153
 
30
-    public static void show(FragmentActivity activity, IAccountImport accountImport)
31 154
 
32 155
 
33
-Here a small example:
156
+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!
34 157
 
35
-    view.findViewById(R.id.btn_importAccount).setOnClickListener(new View.OnClickListener() {
36
-        @Override
37
-        public void onClick(View view) {
38
-            ImportAccountsDialogFragment.show(getActivity(), LoginDialogFragment.this);
158
+# But... I don't use retrofit..
159
+
160
+Well.. no worries, the NextcloudAPI provides a method called `performNetworkRequest(NextcloudRequest request)` that allows you to handle the server response yourself.
161
+
162
+
163
+## Example: 
164
+```java
165
+private void downloadFile() {
166
+    NextcloudRequest nr = new NextcloudRequest.Builder()
167
+            .setMethod("GET")
168
+            .setUrl("/remote.php/webdav/sample.mp4")
169
+            .build();
170
+
171
+    try {
172
+        InputStream os = nextcloudAPI.performNetworkRequest(nr);
173
+        while(os.available() > 0) {
174
+            os.read();
175
+            // TODO do something useful with the data here..
176
+            // like writing it to a file..?
39 177
         }
40
-    });
178
+        os.close();
179
+    } catch (Exception e) {
180
+        e.printStackTrace();
181
+    }
182
+}
183
+```
184
+
185
+
186
+
187
+
188
+# Flow Diagram
41 189
 
42
-    //If no other accounts (from other apps) are available.. hide the button
43
-    if(AccountImporter.findAccounts(getActivity()).size() <= 0) {
44
-        view.findViewById(R.id.btn_importAccount).setVisibility(View.GONE);
45
-    }
190
+![](NextcloudSingleSignOn.png)

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

@@ -133,7 +133,7 @@ public class AccountImporter {
133 133
     }
134 134
 
135 135
 
136
-    public static SingleSignOnAccount GetAuthTokenInSeperateThread(final Context context, final Account account) {
136
+    public static SingleSignOnAccount GetAuthTokenInSeparateThread(final Context context, final Account account) {
137 137
         SingleSignOnAccount ssoAccount = null;
138 138
         ExecutorService executor = Executors.newSingleThreadExecutor();
139 139
         Callable<SingleSignOnAccount> callable = new Callable<SingleSignOnAccount>() {

Loading…
Cancel
Save