-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactored OIDF-Client according to mdoc-cbor-crypto-multip…
…latform
- Loading branch information
Zoe Maas
committed
Oct 24, 2024
1 parent
f4d4a9f
commit bebef87
Showing
16 changed files
with
423 additions
and
162 deletions.
There are no files selected for viewing
25 changes: 17 additions & 8 deletions
25
modules/logger/src/commonMain/kotlin/com/sphereon/oid/fed/logger/Logger.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 |
---|---|---|
@@ -1,26 +1,35 @@ | ||
package com.sphereon.oid.fed.common.logging | ||
package com.sphereon.oid.fed.logger | ||
|
||
import co.touchlab.kermit.Logger | ||
import co.touchlab.kermit.Severity | ||
|
||
object Logger { | ||
|
||
fun verbose(tag: String, message: String) { | ||
class Logger(val tag: String = "") { | ||
fun verbose(message: String, tag: String = this.tag) { | ||
Logger.v(tag = tag, messageString = message) | ||
} | ||
|
||
fun debug(tag: String, message: String) { | ||
fun debug(message: String, tag: String = this.tag) { | ||
Logger.d(tag = tag, messageString = message) | ||
} | ||
|
||
fun info(tag: String, message: String) { | ||
fun info(message: String, tag: String = this.tag) { | ||
Logger.i(tag = tag, messageString = message) | ||
} | ||
|
||
fun warn(tag: String, message: String) { | ||
fun warn(message: String, tag: String = this.tag) { | ||
Logger.w(tag = tag, messageString = message) | ||
} | ||
|
||
fun error(tag: String, message: String, throwable: Throwable? = null) { | ||
fun error(message: String, throwable: Throwable? = null, tag: String = this.tag) { | ||
Logger.e(tag = tag, messageString = message, throwable = throwable) | ||
} | ||
|
||
fun setMinSeverity(severity: Severity) = Logger.setMinSeverity(severity) | ||
|
||
object Static { | ||
fun tag(tag: String = "", severity: Severity = Severity.Info) = Logger(tag).also { it.setMinSeverity(severity) } | ||
} | ||
|
||
} | ||
|
||
val DefaultLogger = Logger("") |
12 changes: 4 additions & 8 deletions
12
modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/Client.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
90 changes: 72 additions & 18 deletions
90
...enid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.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
9 changes: 9 additions & 0 deletions
9
...federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/crypto/CryptoConst.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,9 @@ | ||
package com.sphereon.oid.fed.client.crypto | ||
|
||
//import com.sphereon.oid.fed.logger | ||
|
||
object CryptoConst { | ||
// val LOG_NAMESPACE = "sphereon:oidf:client:crypto" | ||
// val LOG = Logger.Static.tag(LOG_NAMESPACE) | ||
// val CRYPTO_LITERAL = "CRYPTO" | ||
} |
72 changes: 45 additions & 27 deletions
72
...openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.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 |
---|---|---|
@@ -1,50 +1,68 @@ | ||
package com.sphereon.oid.fed.client.fetch | ||
|
||
import com.sphereon.oid.fed.client.types.ICallbackService | ||
import com.sphereon.oid.fed.client.service.DefaultCallbacks | ||
import com.sphereon.oid.fed.client.service.ICallbackService | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import kotlin.js.JsExport | ||
|
||
expect interface IFetchCallbackMarkerType | ||
interface IFetchMarkerType | ||
|
||
interface IFetchService { | ||
fun getHttpClient(): HttpClient | ||
@JsExport.Ignore | ||
interface IFetchCallbackService: IFetchCallbackMarkerType { | ||
suspend fun fetchStatement( | ||
endpoint: String | ||
): String | ||
suspend fun getHttpClient(): HttpClient | ||
} | ||
|
||
interface IFetchServiceInternal { | ||
@JsExport.Ignore | ||
interface IFetchService: IFetchMarkerType { | ||
suspend fun fetchStatement( | ||
endpoint: String | ||
): String | ||
suspend fun getHttpClient(): HttpClient | ||
} | ||
|
||
interface IFetchCallbackService : ICallbackService<IFetchService>, IFetchService, IFetchServiceInternal | ||
expect fun fetchService(platformCallback: IFetchCallbackMarkerType = DefaultCallbacks.fetchService()): IFetchService | ||
|
||
object FetchServiceObject : IFetchCallbackService { | ||
private lateinit var platformCallback: IFetchService | ||
private lateinit var httpClient: HttpClient | ||
abstract class AbstractFetchService<CallbackServiceType>(open val platformCallback: CallbackServiceType): ICallbackService<CallbackServiceType> { | ||
private var disabled = false | ||
|
||
override suspend fun fetchStatement(endpoint: String): String { | ||
return httpClient | ||
.get(endpoint) { | ||
headers { | ||
append(HttpHeaders.Accept, "application/entity-statement+jwt") | ||
} | ||
}.body() | ||
override fun isEnabled(): Boolean { | ||
return !this.disabled | ||
} | ||
|
||
override fun getHttpClient(): HttpClient { | ||
return this.platformCallback.getHttpClient() | ||
override fun disable() = apply { | ||
this.disabled = true | ||
} | ||
|
||
override fun enable() = apply { | ||
this.disabled = false | ||
} | ||
|
||
class DefaultPlatformCallback : IFetchService { | ||
override fun getHttpClient(): HttpClient { | ||
return HttpClient() | ||
protected fun assertEnabled() { | ||
if (!isEnabled()) { | ||
//FetchConst.LOG.info("CRYPTO verify has been disabled") | ||
throw IllegalStateException("CRYPTO service is disable; cannot verify") | ||
} else if (this.platformCallback == null) { | ||
//FetchConst.LOG.error("CRYPTO callback is not registered") | ||
throw IllegalStateException("CRYPTO has not been initialized. Please register your CryptoCallback implementation, or register a default implementation") | ||
} | ||
} | ||
} | ||
|
||
override fun register(platformCallback: IFetchService?): IFetchCallbackService { | ||
this.platformCallback = platformCallback ?: DefaultPlatformCallback() | ||
this.httpClient = this.platformCallback.getHttpClient() | ||
return this | ||
class FetchService(override val platformCallback: IFetchCallbackService = DefaultCallbacks.jwtService()): AbstractFetchService<IFetchCallbackService>(platformCallback), IFetchService { | ||
|
||
override fun platform(): IFetchCallbackService { | ||
return this.platformCallback | ||
} | ||
|
||
override suspend fun fetchStatement(endpoint: String): String { | ||
return this.platformCallback.fetchStatement(endpoint) | ||
} | ||
|
||
override suspend fun getHttpClient(): HttpClient { | ||
return this.platformCallback.getHttpClient() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...d-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/fetch/FetchConst.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,9 @@ | ||
package com.sphereon.oid.fed.client.fetch | ||
|
||
//import com.sphereon.oid.fed.logger | ||
|
||
object FetchConst { | ||
// val LOG_NAMESPACE = "sphereon:oidf:client:crypto" | ||
// val LOG = Logger.Static.tag(LOG_NAMESPACE) | ||
// val FETCH_LITERAL = "FETCH" | ||
} |
57 changes: 57 additions & 0 deletions
57
...on-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/service/OIDFClientServices.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,57 @@ | ||
package com.sphereon.oid.fed.client.service | ||
|
||
import com.sphereon.oid.fed.client.crypto.ICryptoCallbackMarkerType | ||
import com.sphereon.oid.fed.client.fetch.IFetchCallbackMarkerType | ||
import kotlin.js.JsExport | ||
|
||
@JsExport | ||
object DefaultCallbacks { | ||
private var cryptoCallbackService: ICryptoCallbackMarkerType? = null | ||
private var fetchCallbackService: IFetchCallbackMarkerType? = null | ||
|
||
fun <CallbackType: ICryptoCallbackMarkerType> jwtService(): CallbackType { | ||
if (cryptoCallbackService == null) { | ||
throw IllegalStateException("No default Crypto Platform Callback implementation was registered") | ||
} | ||
return cryptoCallbackService as CallbackType | ||
} | ||
|
||
fun setCryptoServiceDefault(cryptoCallbackService: ICryptoCallbackMarkerType?) { | ||
this.cryptoCallbackService = cryptoCallbackService | ||
} | ||
|
||
fun <CallbackType: IFetchCallbackMarkerType> fetchService(): CallbackType { | ||
if (fetchCallbackService == null) { | ||
throw IllegalStateException("No default Crypto Platform Callback implementation was registered") | ||
} | ||
return fetchCallbackService as CallbackType | ||
} | ||
|
||
fun setFetchServiceDefault(fetchCallbackService: IFetchCallbackMarkerType?) { | ||
this.fetchCallbackService = fetchCallbackService | ||
} | ||
} | ||
|
||
/** | ||
* The main entry point for platform validation, delegating to a platform specific callback implemented by external developers | ||
*/ | ||
|
||
interface ICallbackService<PlatformCallbackType> { | ||
|
||
/** | ||
* Disable callback verification (be careful!) | ||
*/ | ||
fun disable(): ICallbackService<PlatformCallbackType> | ||
|
||
/** | ||
* Enable the callback verification (default) | ||
*/ | ||
fun enable(): ICallbackService<PlatformCallbackType> | ||
|
||
/** | ||
* Is the service enabled or not | ||
*/ | ||
fun isEnabled(): Boolean | ||
|
||
fun platform(): PlatformCallbackType | ||
} |
Oops, something went wrong.