From 3328650bf0071cc0eed81d646a9443d48fb1437d Mon Sep 17 00:00:00 2001 From: Daniel Lindau Date: Thu, 7 Mar 2024 23:36:30 +0100 Subject: [PATCH 1/3] Support bound code and tokens on android --- .../haapi/react/HaapiAccessorRepository.kt | 2 +- .../haapi/react/HaapiConfigurationUtil.kt | 13 +++++++++++- .../haapi/react/SharedPreferencesStorage.kt | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt diff --git a/android/src/main/java/io/curity/haapi/react/HaapiAccessorRepository.kt b/android/src/main/java/io/curity/haapi/react/HaapiAccessorRepository.kt index cd0ed1f..7766570 100644 --- a/android/src/main/java/io/curity/haapi/react/HaapiAccessorRepository.kt +++ b/android/src/main/java/io/curity/haapi/react/HaapiAccessorRepository.kt @@ -32,7 +32,7 @@ class HaapiAccessorRepository( init { val confMap = conf.toHashMap() - val haapiConfiguration = HaapiConfigurationUtil.createConfiguration(conf.toHashMap()) + val haapiConfiguration = HaapiConfigurationUtil.createConfiguration(conf.toHashMap(), reactContext) val factory = HaapiAccessorFactory(haapiConfiguration) HaapiConfigurationUtil.addFallbackConfiguration(factory, confMap, reactContext) _factory = factory diff --git a/android/src/main/java/io/curity/haapi/react/HaapiConfigurationUtil.kt b/android/src/main/java/io/curity/haapi/react/HaapiConfigurationUtil.kt index df98d4e..50ade02 100644 --- a/android/src/main/java/io/curity/haapi/react/HaapiConfigurationUtil.kt +++ b/android/src/main/java/io/curity/haapi/react/HaapiConfigurationUtil.kt @@ -17,7 +17,10 @@ package io.curity.haapi.react import android.annotation.SuppressLint import android.content.Context +import com.facebook.react.bridge.ReactApplicationContext import se.curity.identityserver.haapi.android.driver.ClientAuthenticationMethodConfiguration +import se.curity.identityserver.haapi.android.driver.KeyPairAlgorithmConfig +import se.curity.identityserver.haapi.android.driver.TokenBoundConfiguration import se.curity.identityserver.haapi.android.sdk.DcrConfiguration import se.curity.identityserver.haapi.android.sdk.HaapiAccessorFactory import se.curity.identityserver.haapi.android.sdk.HaapiConfiguration @@ -33,7 +36,7 @@ import javax.net.ssl.SSLContext object HaapiConfigurationUtil { - fun createConfiguration(conf: HashMap): HaapiConfiguration = HaapiConfiguration( + fun createConfiguration(conf: HashMap, reactContext: ReactApplicationContext) = HaapiConfiguration( keyStoreAlias = asStringOrDefault( conf, "keyStoreAlias", "haapi-react-native-android" ), @@ -67,6 +70,7 @@ object HaapiConfigurationUtil { } } as HttpURLConnection }, + tokenBoundConfiguration = createTokenBoundConfiguration(reactContext) ) fun addFallbackConfiguration(accessorFactory: HaapiAccessorFactory, conf: HashMap, context: Context) { @@ -83,6 +87,13 @@ object HaapiConfigurationUtil { ) } + private fun createTokenBoundConfiguration(reactContext: ReactApplicationContext) = TokenBoundConfiguration( + keyAlias = "haapi-module-dpop-key", + keyPairAlgorithmConfig = KeyPairAlgorithmConfig.ES256, + storage = SharedPreferencesStorage("token-bound-storage", reactContext), + currentTimeMillisProvider = { System.currentTimeMillis() } + ) + private fun asStringOrDefault(conf: HashMap, parameter: String, defaultValue: String): String = asOptionalString(conf, parameter) ?: defaultValue diff --git a/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt b/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt new file mode 100644 index 0000000..af40ae2 --- /dev/null +++ b/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt @@ -0,0 +1,21 @@ +package io.curity.haapi.react + +import android.content.SharedPreferences +import com.facebook.react.bridge.ReactApplicationContext +import se.curity.identityserver.haapi.android.driver.Storage + +class SharedPreferencesStorage(private val name: String, private val context: ReactApplicationContext) : Storage { + override fun get(key: String): String? = getSharedPreferences().getString(key, null) + + override fun set(value: String, key: String) = getSharedPreferences().edit().putString(key, value).apply() + + override fun delete(key: String) = getSharedPreferences().edit().remove(key).apply() + + override fun getAll(): Map { + @Suppress("UNCHECKED_CAST") + return getSharedPreferences().all.filterValues { it is String } as Map + } + + private fun getSharedPreferences(): SharedPreferences = + context.getSharedPreferences(name, ReactApplicationContext.MODE_PRIVATE) +} \ No newline at end of file From 08519f9d15015bd95e15f29cc8f587a860a9f0a4 Mon Sep 17 00:00:00 2001 From: Daniel Lindau Date: Thu, 7 Mar 2024 23:53:40 +0100 Subject: [PATCH 2/3] Support bound code and tokens on ios --- ios/HaapiModule/HaapiModule.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/HaapiModule/HaapiModule.swift b/ios/HaapiModule/HaapiModule.swift index 9c56b53..e1d9be8 100644 --- a/ios/HaapiModule/HaapiModule.swift +++ b/ios/HaapiModule/HaapiModule.swift @@ -242,7 +242,7 @@ class HaapiModule: RCTEventEmitter { } private func handle(codeStep: OAuthAuthorizationResponseStep, promise: Promise) { - oauthTokenManager?.fetchAccessToken(with: codeStep.oauthAuthorizationResponseProperties.code!, completionHandler: { tokenResponse in + oauthTokenManager?.fetchAccessToken(with: codeStep.oauthAuthorizationResponseProperties.code!, dpop: haapiManager?.dpop, completionHandler: { tokenResponse in self.handle(tokenResponse: tokenResponse, promise: promise) }) } From 62a2eafc508586d3ba9eddfe1571e41cc3a5b8da Mon Sep 17 00:00:00 2001 From: Daniel Lindau Date: Thu, 7 Mar 2024 23:59:45 +0100 Subject: [PATCH 3/3] Copyright --- .../haapi/react/SharedPreferencesStorage.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt b/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt index af40ae2..1f4f9ef 100644 --- a/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt +++ b/android/src/main/java/io/curity/haapi/react/SharedPreferencesStorage.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2024 Curity AB + * + * 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. + */ package io.curity.haapi.react import android.content.SharedPreferences