-
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.
feature(SDK): Add Support for Authentication
closes: #76
- Loading branch information
Showing
5 changed files
with
127 additions
and
3 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
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
43 changes: 43 additions & 0 deletions
43
kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/authentication/JsonWebToken.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,43 @@ | ||
/* | ||
* 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.authentication | ||
|
||
/** | ||
* A JsonWebToken can be used to authenticate against the DataBroker. For authentication to work the DataBroker must be | ||
* started with authentication enabled first. | ||
* The JsonWebToken is defined by an [authScheme] and [token]. The [authScheme] is set to "Bearer". The [token] should | ||
* contain a valid JsonWebToken. | ||
* | ||
* It will be send to the DataBroker as part of the Header Metadata in the following format: | ||
* | ||
* Headers | ||
* Authorization: [authScheme] [token] | ||
* | ||
*/ | ||
data class JsonWebToken( | ||
val token: String, | ||
) { | ||
val authScheme: String | ||
get() = DEFAULT_AUTH_SCHEME | ||
|
||
private companion object { | ||
private const val DEFAULT_AUTH_SCHEME = "Bearer" | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
kuksa-sdk/src/main/kotlin/org/eclipse/kuksa/authentication/VALStubExtension.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,50 @@ | ||
/* | ||
* 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.authentication | ||
|
||
import com.google.common.net.HttpHeaders | ||
import io.grpc.ClientInterceptor | ||
import io.grpc.Metadata | ||
import io.grpc.stub.MetadataUtils | ||
import org.eclipse.kuksa.proto.v1.VALGrpc.VALBlockingStub | ||
import org.eclipse.kuksa.proto.v1.VALGrpc.VALStub | ||
|
||
internal fun VALBlockingStub.withAuthenticationInterceptor(jsonWebToken: JsonWebToken?): VALBlockingStub { | ||
if (jsonWebToken == null) return this | ||
|
||
val authenticationInterceptor = clientInterceptor(jsonWebToken) | ||
return withInterceptors(authenticationInterceptor) | ||
} | ||
|
||
internal fun VALStub.withAuthenticationInterceptor(jsonWebToken: JsonWebToken?): VALStub { | ||
if (jsonWebToken == null) return this | ||
|
||
val authenticationInterceptor = clientInterceptor(jsonWebToken) | ||
return withInterceptors(authenticationInterceptor) | ||
} | ||
|
||
private fun clientInterceptor(jsonWebToken: JsonWebToken): ClientInterceptor? { | ||
val authorizationHeader = Metadata.Key.of(HttpHeaders.AUTHORIZATION, Metadata.ASCII_STRING_MARSHALLER) | ||
|
||
val metadata = Metadata() | ||
metadata.put(authorizationHeader, "${jsonWebToken.authScheme} ${jsonWebToken.token}") | ||
|
||
return MetadataUtils.newAttachHeadersInterceptor(metadata) | ||
} |