-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Don't use JWT when Authentication is disabled
- Loading branch information
Showing
6 changed files
with
126 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/src/main/kotlin/org/eclipse/kuksa/testapp/extension/ConnectionInfoExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.eclipse.kuksa.testapp.extension | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import io.grpc.ChannelCredentials | ||
import io.grpc.Grpc | ||
import io.grpc.ManagedChannel | ||
import io.grpc.ManagedChannelBuilder | ||
import io.grpc.TlsChannelCredentials | ||
import org.eclipse.kuksa.authentication.JsonWebToken | ||
import org.eclipse.kuksa.testapp.databroker.model.ConnectionInfo | ||
import java.io.IOException | ||
|
||
fun ConnectionInfo.createInsecureManagedChannel(): ManagedChannel { | ||
val host = host.trim() | ||
|
||
return ManagedChannelBuilder | ||
.forAddress(host, port) | ||
.usePlaintext() | ||
.build() | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun ConnectionInfo.createSecureManagedChannel(context: Context): ManagedChannel { | ||
val rootCertFile = context.contentResolver.openInputStream(certificate.uri) | ||
|
||
val tlsCredentials: ChannelCredentials = TlsChannelCredentials.newBuilder() | ||
.trustManager(rootCertFile) | ||
.build() | ||
|
||
val host = host.trim() | ||
val port = port | ||
val channelBuilder = Grpc | ||
.newChannelBuilderForAddress(host, port, tlsCredentials) | ||
|
||
val overrideAuthority = certificate.overrideAuthority.trim() | ||
val hasOverrideAuthority = overrideAuthority.isNotEmpty() | ||
if (hasOverrideAuthority) { | ||
channelBuilder.overrideAuthority(overrideAuthority) | ||
} | ||
|
||
return channelBuilder.build() | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun ConnectionInfo.loadJsonWebToken(context: Context): JsonWebToken? { | ||
if (!isAuthenticationEnabled || jwtUriPath == null) { | ||
return null | ||
} | ||
|
||
val uri: Uri = Uri.parse(jwtUriPath) | ||
val token = uri.readAsText(context) | ||
|
||
return JsonWebToken(token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.