Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 18b2716

Browse files
committed
Get OIDC discovery config
1 parent c3ffe3d commit 18b2716

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* @author Abel García de Prada
4+
*
5+
* Copyright (C) 2020 ownCloud GmbH.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
package com.owncloud.android.lib.resources.oauth
28+
29+
import com.owncloud.android.lib.common.OwnCloudClient
30+
import com.owncloud.android.lib.common.http.HttpConstants
31+
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
32+
import com.owncloud.android.lib.common.operations.RemoteOperation
33+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
34+
import com.owncloud.android.lib.resources.oauth.responses.OIDCDiscoveryResponse
35+
import com.squareup.moshi.JsonAdapter
36+
import com.squareup.moshi.Moshi
37+
import timber.log.Timber
38+
import java.net.URL
39+
40+
/**
41+
* Get OIDC Discovery
42+
*
43+
* @author Abel García de Prada
44+
*/
45+
class GetOIDCDiscoveryRemoteOperation : RemoteOperation<OIDCDiscoveryResponse>() {
46+
47+
override fun run(client: OwnCloudClient): RemoteOperationResult<OIDCDiscoveryResponse> {
48+
var result: RemoteOperationResult<OIDCDiscoveryResponse>
49+
50+
try {
51+
val uriBuilder = client.baseUri.buildUpon().apply {
52+
appendPath(WELL_KNOWN_PATH) // avoid starting "/" in this method
53+
appendPath(OPENID_CONFIGURATION_RESOURCE)
54+
}.build()
55+
56+
val getMethod = GetMethod(URL(uriBuilder.toString())).apply {
57+
addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
58+
}
59+
60+
val status = client.executeHttpMethod(getMethod)
61+
62+
val responseBody = getMethod.getResponseBodyAsString()
63+
64+
if (status == HttpConstants.HTTP_OK && responseBody != null) {
65+
Timber.d("Successful response $responseBody")
66+
67+
// Parse the response
68+
val moshi: Moshi = Moshi.Builder().build()
69+
val jsonAdapter: JsonAdapter<OIDCDiscoveryResponse> = moshi.adapter(OIDCDiscoveryResponse::class.java)
70+
val oidcDiscoveryResponse: OIDCDiscoveryResponse? = jsonAdapter.fromJson(responseBody)
71+
72+
result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
73+
result.data = oidcDiscoveryResponse
74+
75+
Timber.d("Get OIDC Discovery completed and parsed to $oidcDiscoveryResponse")
76+
} else {
77+
result = RemoteOperationResult(getMethod)
78+
Timber.e("Failed response while getting OIDC server discovery from the server status code: $status; response message: $responseBody")
79+
}
80+
81+
} catch (e: Exception) {
82+
result = RemoteOperationResult(e)
83+
Timber.e(e, "Exception while getting OIDC server discovery")
84+
}
85+
86+
return result
87+
}
88+
89+
companion object {
90+
private const val WELL_KNOWN_PATH = ".well-known"
91+
private const val OPENID_CONFIGURATION_RESOURCE = "openid-configuration"
92+
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* @author Abel García de Prada
4+
*
5+
* Copyright (C) 2020 ownCloud GmbH.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
package com.owncloud.android.lib.resources.oauth.responses
28+
29+
import com.squareup.moshi.JsonClass
30+
31+
@JsonClass(generateAdapter = true)
32+
data class OIDCDiscoveryResponse(
33+
val authorization_endpoint: String,
34+
val check_session_iframe: String,
35+
val end_session_endpoint: String,
36+
val issuer: String,
37+
val registration_endpoint: String,
38+
val response_types_supported: List<String>,
39+
val scopes_supported: List<String>,
40+
val token_endpoint: String,
41+
val token_endpoint_auth_methods_supported: List<String>,
42+
val userinfo_endpoint: String,
43+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* Copyright (C) 2020 ownCloud GmbH.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.owncloud.android.lib.resources.oauth.services
25+
26+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
27+
import com.owncloud.android.lib.resources.oauth.responses.OIDCDiscoveryResponse
28+
29+
interface OIDCService {
30+
31+
fun getOIDCServerDiscovery(baseUrl: String): RemoteOperationResult<OIDCDiscoveryResponse>
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* Copyright (C) 2020 ownCloud GmbH.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.owncloud.android.lib.resources.oauth.services.implementation
25+
26+
import android.net.Uri
27+
import com.owncloud.android.lib.common.OwnCloudClient
28+
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory
29+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
30+
import com.owncloud.android.lib.resources.oauth.GetOIDCDiscoveryRemoteOperation
31+
import com.owncloud.android.lib.resources.oauth.responses.OIDCDiscoveryResponse
32+
import com.owncloud.android.lib.resources.oauth.services.OIDCService
33+
34+
class OCOIDCService() : OIDCService {
35+
36+
override fun getOIDCServerDiscovery(baseUrl: String): RemoteOperationResult<OIDCDiscoveryResponse> =
37+
GetOIDCDiscoveryRemoteOperation().execute(createClientFromPath(baseUrl))
38+
39+
private fun createClientFromPath(path: String): OwnCloudClient =
40+
OwnCloudClient(Uri.parse(path)).apply {
41+
credentials = OwnCloudCredentialsFactory.getAnonymousCredentials()
42+
}
43+
44+
}

0 commit comments

Comments
 (0)