Skip to content

Commit 546ee32

Browse files
wip
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
1 parent b711bec commit 546ee32

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2020 Tobias Kaminsky <tobias@kaminsky.me>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.android.lib.resources.users;
9+
10+
import static junit.framework.TestCase.assertFalse;
11+
import static junit.framework.TestCase.assertTrue;
12+
13+
import android.net.Uri;
14+
import android.text.TextUtils;
15+
16+
import com.owncloud.android.AbstractIT;
17+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
18+
19+
import org.junit.Test;
20+
21+
import okhttp3.Credentials;
22+
23+
public class GenerateOneTimeAppPasswordRemoteOperationIT extends AbstractIT {
24+
25+
@Test
26+
public void generateAppPassword() {
27+
// nc://onetime-login/user:user1&password:Z8i8J-QLDbr-mSn9A-ijXzN-NSBSt&server:https://qr.ltd3.nextcloud.com
28+
29+
nextcloudClient.setBaseUri(Uri.parse("https://qr.ltd3.nextcloud.com"));
30+
nextcloudClient.setCredentials(Credentials.basic("user1", "9swi2-c9WGn-29cPg-eRNJm-XknFJ"));
31+
32+
GenerateOneTimeAppPasswordRemoteOperation sut = new GenerateOneTimeAppPasswordRemoteOperation();
33+
RemoteOperationResult<String> result = sut.execute(nextcloudClient);
34+
35+
assertTrue(result.isSuccess());
36+
37+
String appPassword = result.getResultData();
38+
assertFalse(TextUtils.isEmpty(appPassword));
39+
40+
// re-using onetime password should fail
41+
assertFalse(new GenerateOneTimeAppPasswordRemoteOperation().execute(nextcloudClient).isSuccess());
42+
}
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2025 Tobias Kaminsky
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.android.lib.resources.users;
9+
10+
11+
import com.nextcloud.common.NextcloudClient;
12+
import com.nextcloud.operations.GetMethod;
13+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
14+
import com.owncloud.android.lib.common.utils.Log_OC;
15+
import com.owncloud.android.lib.resources.OCSRemoteOperation;
16+
17+
import org.apache.commons.httpclient.HttpStatus;
18+
import org.json.JSONObject;
19+
20+
/**
21+
* Generate an app password via username / login and **onetime** password. Available since Nextcloud 33
22+
*/
23+
24+
25+
public class GenerateOneTimeAppPasswordRemoteOperation extends OCSRemoteOperation<String> {
26+
private static final String TAG = GenerateOneTimeAppPasswordRemoteOperation.class.getSimpleName();
27+
private static final String DIRECT_ENDPOINT = "/ocs/v2.php/core/getapppassword-onetime";
28+
29+
// JSON node names
30+
private static final String NODE_OCS = "ocs";
31+
private static final String NODE_DATA = "data";
32+
private static final String NODE_APPPASSWORD = "apppassword";
33+
34+
public RemoteOperationResult<String> run(NextcloudClient client) {
35+
RemoteOperationResult<String> result;
36+
GetMethod getMethod = null;
37+
38+
try {
39+
getMethod = new GetMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT, true);
40+
41+
// remote request
42+
int status = client.execute(getMethod);
43+
44+
if (status == HttpStatus.SC_OK) {
45+
String response = getMethod.getResponseBodyAsString();
46+
47+
JSONObject respJSON = new JSONObject(response);
48+
String password = respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString(NODE_APPPASSWORD);
49+
50+
result = new RemoteOperationResult<>(true, getMethod);
51+
result.setResultData(password);
52+
} else {
53+
result = new RemoteOperationResult<>(false, getMethod);
54+
}
55+
} catch (Exception e) {
56+
result = new RemoteOperationResult<>(e);
57+
Log_OC.e(TAG, "Generate app password failed: " + result.getLogMessage(),
58+
result.getException());
59+
} finally {
60+
if (getMethod != null) {
61+
getMethod.releaseConnection();
62+
}
63+
}
64+
return result;
65+
}
66+
}

0 commit comments

Comments
 (0)