Skip to content

Commit c112fd8

Browse files
Merge pull request #1913 from nextcloud/apppassword-onetime
Apppassword onetime
2 parents 306f87a + a3f57f0 commit c112fd8

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
import com.nextcloud.common.NextcloudClient
11+
import com.nextcloud.operations.GetMethod
12+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
13+
import com.owncloud.android.lib.common.utils.Log_OC
14+
import com.owncloud.android.lib.resources.OCSRemoteOperation
15+
import okio.IOException
16+
import org.apache.commons.httpclient.HttpStatus
17+
import org.json.JSONObject
18+
19+
/**
20+
* Generate an app password via username / login and **onetime** password. Available since Nextcloud 33
21+
*/
22+
class GenerateOneTimeAppPasswordRemoteOperation : OCSRemoteOperation<String?>() {
23+
override fun run(client: NextcloudClient): RemoteOperationResult<String?> {
24+
var result: RemoteOperationResult<String?>
25+
var getMethod: GetMethod? = null
26+
27+
try {
28+
getMethod = GetMethod(client.baseUri.toString() + ENDPOINT + JSON_FORMAT, true)
29+
30+
// remote request
31+
val status: Int = client.execute(getMethod)
32+
33+
if (status == HttpStatus.SC_OK) {
34+
val response = getMethod.getResponseBodyAsString()
35+
36+
val respJSON = JSONObject(response)
37+
val password =
38+
respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString(
39+
NODE_APPPASSWORD
40+
)
41+
42+
result = RemoteOperationResult(true, getMethod)
43+
result.resultData = password
44+
} else {
45+
result = RemoteOperationResult(false, getMethod)
46+
}
47+
} catch (e: IOException) {
48+
result = RemoteOperationResult(e)
49+
Log_OC.e(
50+
TAG,
51+
"Generate app password failed: " + result.getLogMessage(),
52+
result.exception
53+
)
54+
} catch (e: org.json.JSONException) {
55+
result = RemoteOperationResult(e)
56+
Log_OC.e(
57+
TAG,
58+
"Generate app password failed: " + result.getLogMessage(),
59+
result.exception
60+
)
61+
} finally {
62+
getMethod?.releaseConnection()
63+
}
64+
return result
65+
}
66+
67+
companion object {
68+
private val TAG: String = GenerateOneTimeAppPasswordRemoteOperation::class.java.getSimpleName()
69+
private const val ENDPOINT = "/ocs/v2.php/core/getapppassword-onetime"
70+
71+
// JSON node names
72+
private const val NODE_OCS = "ocs"
73+
private const val NODE_DATA = "data"
74+
private const val NODE_APPPASSWORD = "apppassword"
75+
}
76+
}

0 commit comments

Comments
 (0)