From c966bf14d0f6db273221e5d6a9cfbbdd3ed5482d Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Wed, 20 Apr 2022 15:12:38 +0200 Subject: [PATCH 1/5] add moshi classes for webfinger responses --- .../GetOCInstanceViaWebfingerOperation.kt | 39 ++++++++++++++ .../webfinger/responses/WebfingerResponse.kt | 39 ++++++++++++++ .../shares/responses/ShareeResponseTest.kt | 1 + .../GetOCInstanceViaWebfingerOperationTest.kt | 11 ++++ .../responses/WebfingerResponseTest.kt | 51 +++++++++++++++++++ .../broken_response.json | 15 ++++++ ...ot_containing_releavant_info_response.json | 10 ++++ .../simple_response.json | 9 ++++ .../to_much_information_response.json | 27 ++++++++++ 9 files changed, 202 insertions(+) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt create mode 100644 owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt create mode 100644 owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt create mode 100644 owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/broken_response.json create mode 100644 owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json create mode 100644 owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json create mode 100644 owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt new file mode 100644 index 00000000..e74f50b5 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt @@ -0,0 +1,39 @@ +/* ownCloud Android Library is available under MIT license + * + * Copyright (C) 2022 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.owncloud.android.lib.resources.webfinger + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.operations.RemoteOperation +import com.owncloud.android.lib.common.operations.RemoteOperationResult + +class GetOCInstanceViaWebfingerOperation( + private val rel:String, + private val resource:String +) : RemoteOperation() { + + override fun run(client: OwnCloudClient?): RemoteOperationResult { + TODO("Not yet implemented") + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt new file mode 100644 index 00000000..fcf9bf84 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt @@ -0,0 +1,39 @@ +/* ownCloud Android Library is available under MIT license + * + * Copyright (C) 2022 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.owncloud.android.lib.resources.webfinger.responses + +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class WebfingerJrdResponse ( + val subject:String, + val links:List +) + +@JsonClass(generateAdapter = true) +data class LinkItem( + val href:String, + val rel:String +) \ No newline at end of file diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt index 3981ab5b..fad1178a 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt @@ -25,6 +25,7 @@ package com.owncloud.android.lib.resources.shares.responses import com.owncloud.android.lib.resources.CommonOcsResponse +import com.owncloud.android.lib.resources.webfinger.responses.WebfingerJrdResponse import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import com.squareup.moshi.Types diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt new file mode 100644 index 00000000..d4f84a83 --- /dev/null +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt @@ -0,0 +1,11 @@ +package com.owncloud.android.lib.resources.webfinger + +import android.os.Build +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [Build.VERSION_CODES.O], manifest = Config.NONE) +class GetOCInstanceViaWebfingerOperationTest { +} \ No newline at end of file diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt new file mode 100644 index 00000000..9704326a --- /dev/null +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt @@ -0,0 +1,51 @@ +package com.owncloud.android.lib.resources.webfinger.responses + +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.JsonDataException +import com.squareup.moshi.Moshi +import org.junit.Assert +import org.junit.Assert.assertThrows +import org.junit.Before +import org.junit.Test +import java.io.File + +class WebfingerResponseTest { + lateinit var adapter: JsonAdapter + + private fun loadResponses(fileName: String) = + adapter.fromJson(File(fileName).readText()) + + @Before + fun prepare() { + val moshi = Moshi.Builder().build() + adapter = moshi.adapter(WebfingerJrdResponse::class.java) + } + + @Test + fun `check rel in to much information - ok - correct rell is returned`() { + val response = loadResponses(TO_MUCH_INFORMATION_JSON)!! + Assert.assertEquals("https://gast.somedomain.de", response.links[0].href) + Assert.assertEquals("http://webfinger.owncloud/rel/server-instance", response.links[0].rel) + } + + @Test(expected = JsonDataException::class) + fun `check key value pairs - ko - no href key`() { + val response = loadResponses(BROKEN_JSON)!! + Assert.assertEquals("https://gast.somedomain.de", response.links[0].href) + } + + @Test(expected = JsonDataException::class) + fun `check key value pairs - ko - no rel key`() { + val response = loadResponses(BROKEN_JSON)!! + Assert.assertEquals("https://gast.somedomain.de", response.links[0].href) + } + + companion object { + val RESOURCES_PATH = + "src/test/responses/com.owncloud.android.lib.resources.webfinger.responses" + val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/simple_response.json" + val TO_MUCH_INFORMATION_JSON = "$RESOURCES_PATH/to_much_information_response.json" + val BROKEN_JSON = "$RESOURCES_PATH/broken_response.json" + val NOT_CONTAINING_RELEVANT_INFORMATION_JSON = "$RESOURCES_PATH/not_containing_relevant_info_response.json" + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/broken_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/broken_response.json new file mode 100644 index 00000000..85dd2279 --- /dev/null +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/broken_response.json @@ -0,0 +1,15 @@ +{ + "subject": "acct:bob@example.com", + "aliases": [ + "https://www.example.com/~bob/" + ], + "properties": { + "http://example.com/ns/role": "employee" + }, + "links": [ + { + "lel": "http://webfinger.example/rel/profile-page", + "foo": "https://www.example.com/~bob/" + } + ] +} \ No newline at end of file diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json new file mode 100644 index 00000000..cae2ae74 --- /dev/null +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json @@ -0,0 +1,10 @@ +{ + "subject": "acct:peter.sine@gurken.xxx", + "links" : + [ + { + "rel" : "http://webfinger.example/rel/businesscard", + "href" : "https://www.example.com/~bob/bob.vcf" + } + ] +} \ No newline at end of file diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json new file mode 100644 index 00000000..a7e18f2c --- /dev/null +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json @@ -0,0 +1,9 @@ +{ + "links": [ + { + "href": "https://gast.somedomain.de", + "rel": "http://webfinger.owncloud/rel/server-instance" + } + ], + "subject": "acct:peter.sine@gurken.xxx" +} diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json new file mode 100644 index 00000000..be02c6ad --- /dev/null +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json @@ -0,0 +1,27 @@ +{ + "subject": "acct:peter.sine@gurken.xxx", + "aliases" : + [ + "https://www.example.com/~bob/" + ], + "properties" : + { + "http://example.com/ns/role" : "employee" + }, + "gurken": { + "whatever": 42 + }, + "links" : + [ + { + "gurken": "sallat", + "href": "https://gast.somedomain.de", + "rel": "http://webfinger.owncloud/rel/server-instance" + }, + { + "gurken": "sallat", + "rel" : "http://webfinger.example/rel/businesscard", + "href" : "https://www.example.com/~bob/bob.vcf" + } + ] +} \ No newline at end of file From f19b2355b41c83443782216b328d50a4cfd1969b Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Wed, 20 Apr 2022 18:25:31 +0200 Subject: [PATCH 2/5] create scaffold for webfinger request operation --- .../GetOCInstanceViaWebfingerOperation.kt | 75 ++++++++++++++++++- .../GetOCInstanceViaWebfingerOperationTest.kt | 11 --- 2 files changed, 73 insertions(+), 13 deletions(-) delete mode 100644 owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt index e74f50b5..ee6c7e95 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt @@ -24,16 +24,87 @@ package com.owncloud.android.lib.resources.webfinger +import android.net.Uri import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod +import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.status.HttpScheme +import com.owncloud.android.lib.resources.webfinger.responses.WebfingerJrdResponse +import com.squareup.moshi.Moshi +import timber.log.Timber +import java.net.URL class GetOCInstanceViaWebfingerOperation( + private val lockupServerDomain:String, private val rel:String, private val resource:String ) : RemoteOperation() { - override fun run(client: OwnCloudClient?): RemoteOperationResult { - TODO("Not yet implemented") + private fun buildRequestUri() = + Uri.parse(lockupServerDomain).buildUpon() + .scheme(HttpScheme.HTTPS_SCHEME) + .path(WEBFINGER_PATH) + .appendQueryParameter("rel", rel) + .appendQueryParameter("resource", resource) + .build() + + private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK + + private fun parseResponse(response: String): WebfingerJrdResponse { + val moshi = Moshi.Builder().build() + val adapter = moshi.adapter(WebfingerJrdResponse::class.java) + return adapter.fromJson(response)!! + } + + private fun onResultUnsuccessful( + method: HttpMethod, + response: String?, + status: Int + ): RemoteOperationResult { + Timber.e("Failed requesting webfinger info") + if (response != null) { + Timber.e("*** status code: $status; response message: $response") + } else { + Timber.e("*** status code: $status") + } + return RemoteOperationResult(method) + } + + private fun onRequestSuccessful(rawResponse:String): RemoteOperationResult { + val response = parseResponse(rawResponse) + for(i in response.links) { + if (i.rel == rel) { + val operationResult = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + operationResult.data = i.href + return operationResult + } + } + Timber.e("Could not find ownCloud relevant information in webfinger response: $rawResponse") + throw java.lang.Exception("Could not find ownCloud relevant information in webfinger response") + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + val requestUri = buildRequestUri() + val getMethod = GetMethod(URL(requestUri.toString())) + return try { + val status = client.executeHttpMethod(getMethod) + val response = getMethod.getResponseBodyAsString()!! + + if (isSuccess(status)) { + onRequestSuccessful(response) + } else { + onResultUnsuccessful(getMethod, response, status) + } + } catch(e: Exception) { + Timber.e(e, "Requesting webfinger info failed") + RemoteOperationResult(e) + } + } + + companion object { + val WEBFINGER_PATH = "/.well-known/webfinger" } } \ No newline at end of file diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt deleted file mode 100644 index d4f84a83..00000000 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.owncloud.android.lib.resources.webfinger - -import android.os.Build -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import org.robolectric.annotation.Config - -@RunWith(RobolectricTestRunner::class) -@Config(sdk = [Build.VERSION_CODES.O], manifest = Config.NONE) -class GetOCInstanceViaWebfingerOperationTest { -} \ No newline at end of file From 1afa124b7d705a4871bfc0ae0d3f5932876b9bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garc=C3=ADa=20de=20Prada?= Date: Mon, 10 Oct 2022 13:07:11 +0200 Subject: [PATCH 3/5] Add webfinger service and update the remote operation --- .../GetOCInstanceViaWebfingerOperation.kt | 20 +++++----- .../webfinger/services/WebfingerService.kt | 29 ++++++++++++++ .../implementation/OCWebfingerService.kt | 38 +++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt index ee6c7e95..42ee4b12 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt @@ -31,22 +31,20 @@ import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult -import com.owncloud.android.lib.resources.status.HttpScheme import com.owncloud.android.lib.resources.webfinger.responses.WebfingerJrdResponse import com.squareup.moshi.Moshi import timber.log.Timber import java.net.URL class GetOCInstanceViaWebfingerOperation( - private val lockupServerDomain:String, - private val rel:String, - private val resource:String + private val lockupServerDomain: String, + private val rel: String, + private val resource: String, ) : RemoteOperation() { private fun buildRequestUri() = Uri.parse(lockupServerDomain).buildUpon() - .scheme(HttpScheme.HTTPS_SCHEME) - .path(WEBFINGER_PATH) + .path(ENDPOINT_WEBFINGER_PATH) .appendQueryParameter("rel", rel) .appendQueryParameter("resource", resource) .build() @@ -73,9 +71,9 @@ class GetOCInstanceViaWebfingerOperation( return RemoteOperationResult(method) } - private fun onRequestSuccessful(rawResponse:String): RemoteOperationResult { + private fun onRequestSuccessful(rawResponse: String): RemoteOperationResult { val response = parseResponse(rawResponse) - for(i in response.links) { + for (i in response.links) { if (i.rel == rel) { val operationResult = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) operationResult.data = i.href @@ -98,13 +96,13 @@ class GetOCInstanceViaWebfingerOperation( } else { onResultUnsuccessful(getMethod, response, status) } - } catch(e: Exception) { + } catch (e: Exception) { Timber.e(e, "Requesting webfinger info failed") RemoteOperationResult(e) } } companion object { - val WEBFINGER_PATH = "/.well-known/webfinger" + private const val ENDPOINT_WEBFINGER_PATH = "/.well-known/webfinger" } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt new file mode 100644 index 00000000..4e057beb --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt @@ -0,0 +1,29 @@ +/** + * ownCloud Android client application + * + * Copyright (C) 2022 ownCloud GmbH. + * + * 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 . + */ +package com.owncloud.android.lib.resources.webfinger.services + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.operations.RemoteOperationResult + +interface WebfingerService { + fun getInstanceFromWebfinger( + lookupServer: String, + username: String, + client: OwnCloudClient, + ): RemoteOperationResult +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt new file mode 100644 index 00000000..3eab83d4 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt @@ -0,0 +1,38 @@ +/** + * ownCloud Android client application + * + * Copyright (C) 2022 ownCloud GmbH. + * + * 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 . + */ +package com.owncloud.android.lib.resources.webfinger.services.implementation + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.webfinger.GetOCInstanceViaWebfingerOperation +import com.owncloud.android.lib.resources.webfinger.services.WebfingerService + +class OCWebfingerService : WebfingerService { + + override fun getInstanceFromWebfinger( + lookupServer: String, + username: String, + client: OwnCloudClient, + ): RemoteOperationResult = + GetOCInstanceViaWebfingerOperation(lookupServer, OWNCLOUD_REL, username).execute(client) + + companion object { + private const val OWNCLOUD_REL = "http://webfinger.owncloud/rel/server-instance" + } + +} From 70bf35f6830d6fcbb4c2160f5d6ea0e6520f9a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garc=C3=ADa=20de=20Prada?= Date: Tue, 11 Oct 2022 18:00:03 +0200 Subject: [PATCH 4/5] Remove unused imports --- .../lib/resources/shares/responses/ShareeResponseTest.kt | 3 +-- .../lib/resources/webfinger/responses/WebfingerResponseTest.kt | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt index fad1178a..374429c3 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponseTest.kt @@ -25,7 +25,6 @@ package com.owncloud.android.lib.resources.shares.responses import com.owncloud.android.lib.resources.CommonOcsResponse -import com.owncloud.android.lib.resources.webfinger.responses.WebfingerJrdResponse import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import com.squareup.moshi.Types @@ -91,4 +90,4 @@ class ShareeResponseTest { val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/example_sharee_response.json" val EMPTY_RESPONSE_JSON = "$RESOURCES_PATH/empty_sharee_response.json" } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt index 9704326a..303e06d9 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt @@ -4,7 +4,6 @@ import com.squareup.moshi.JsonAdapter import com.squareup.moshi.JsonDataException import com.squareup.moshi.Moshi import org.junit.Assert -import org.junit.Assert.assertThrows import org.junit.Before import org.junit.Test import java.io.File @@ -48,4 +47,4 @@ class WebfingerResponseTest { val BROKEN_JSON = "$RESOURCES_PATH/broken_response.json" val NOT_CONTAINING_RELEVANT_INFORMATION_JSON = "$RESOURCES_PATH/not_containing_relevant_info_response.json" } -} \ No newline at end of file +} From e7696849202a11c613604362d2f2a23036d899df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garc=C3=ADa=20de=20Prada?= Date: Thu, 13 Oct 2022 10:22:39 +0200 Subject: [PATCH 5/5] Reformat some webfinger classes --- .../webfinger/responses/WebfingerResponse.kt | 12 ++++++------ .../responses/WebfingerResponseTest.kt | 17 ++++++++--------- .../not_containing_releavant_info_response.json | 10 ---------- .../not_containing_relevant_info_response.json | 9 +++++++++ .../simple_response.json | 14 +++++++------- .../to_much_information_response.json | 17 +++++++---------- 6 files changed, 37 insertions(+), 42 deletions(-) delete mode 100644 owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json create mode 100644 owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_relevant_info_response.json diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt index fcf9bf84..45e85a75 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponse.kt @@ -27,13 +27,13 @@ package com.owncloud.android.lib.resources.webfinger.responses import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class WebfingerJrdResponse ( - val subject:String, - val links:List +data class WebfingerJrdResponse( + val subject: String, + val links: List ) @JsonClass(generateAdapter = true) data class LinkItem( - val href:String, - val rel:String -) \ No newline at end of file + val href: String, + val rel: String +) diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt index 303e06d9..2d0c6235 100644 --- a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/responses/WebfingerResponseTest.kt @@ -11,8 +11,7 @@ import java.io.File class WebfingerResponseTest { lateinit var adapter: JsonAdapter - private fun loadResponses(fileName: String) = - adapter.fromJson(File(fileName).readText()) + private fun loadResponses(fileName: String) = adapter.fromJson(File(fileName).readText()) @Before fun prepare() { @@ -21,8 +20,8 @@ class WebfingerResponseTest { } @Test - fun `check rel in to much information - ok - correct rell is returned`() { - val response = loadResponses(TO_MUCH_INFORMATION_JSON)!! + fun `check rel in too much information - ok`() { + val response = loadResponses(TOO_MUCH_INFORMATION_JSON)!! Assert.assertEquals("https://gast.somedomain.de", response.links[0].href) Assert.assertEquals("http://webfinger.owncloud/rel/server-instance", response.links[0].rel) } @@ -40,11 +39,11 @@ class WebfingerResponseTest { } companion object { - val RESOURCES_PATH = + private const val RESOURCES_PATH = "src/test/responses/com.owncloud.android.lib.resources.webfinger.responses" - val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/simple_response.json" - val TO_MUCH_INFORMATION_JSON = "$RESOURCES_PATH/to_much_information_response.json" - val BROKEN_JSON = "$RESOURCES_PATH/broken_response.json" - val NOT_CONTAINING_RELEVANT_INFORMATION_JSON = "$RESOURCES_PATH/not_containing_relevant_info_response.json" + private const val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/simple_response.json" + private const val TOO_MUCH_INFORMATION_JSON = "$RESOURCES_PATH/to_much_information_response.json" + private const val BROKEN_JSON = "$RESOURCES_PATH/broken_response.json" + private const val NOT_CONTAINING_RELEVANT_INFORMATION_JSON = "$RESOURCES_PATH/not_containing_relevant_info_response.json" } } diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json deleted file mode 100644 index cae2ae74..00000000 --- a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_releavant_info_response.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "subject": "acct:peter.sine@gurken.xxx", - "links" : - [ - { - "rel" : "http://webfinger.example/rel/businesscard", - "href" : "https://www.example.com/~bob/bob.vcf" - } - ] -} \ No newline at end of file diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_relevant_info_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_relevant_info_response.json new file mode 100644 index 00000000..712d113a --- /dev/null +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/not_containing_relevant_info_response.json @@ -0,0 +1,9 @@ +{ + "subject": "acct:peter.sine@gurken.xxx", + "links": [ + { + "rel": "http://webfinger.example/rel/businesscard", + "href": "https://www.example.com/~bob/bob.vcf" + } + ] +} diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json index a7e18f2c..bda67449 100644 --- a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/simple_response.json @@ -1,9 +1,9 @@ { - "links": [ - { - "href": "https://gast.somedomain.de", - "rel": "http://webfinger.owncloud/rel/server-instance" - } - ], - "subject": "acct:peter.sine@gurken.xxx" + "links": [ + { + "href": "https://gast.somedomain.de", + "rel": "http://webfinger.owncloud/rel/server-instance" + } + ], + "subject": "acct:peter.sine@gurken.xxx" } diff --git a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json index be02c6ad..79c68f3c 100644 --- a/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json +++ b/owncloudComLibrary/src/test/responses/com.owncloud.android.lib.resources.webfinger.responses/to_much_information_response.json @@ -1,18 +1,15 @@ { "subject": "acct:peter.sine@gurken.xxx", - "aliases" : - [ + "aliases": [ "https://www.example.com/~bob/" ], - "properties" : - { - "http://example.com/ns/role" : "employee" + "properties": { + "http://example.com/ns/role": "employee" }, "gurken": { "whatever": 42 }, - "links" : - [ + "links": [ { "gurken": "sallat", "href": "https://gast.somedomain.de", @@ -20,8 +17,8 @@ }, { "gurken": "sallat", - "rel" : "http://webfinger.example/rel/businesscard", - "href" : "https://www.example.com/~bob/bob.vcf" + "rel": "http://webfinger.example/rel/businesscard", + "href": "https://www.example.com/~bob/bob.vcf" } ] -} \ No newline at end of file +}