Skip to content

Commit

Permalink
Merge pull request #15 from curityio/feature/dpop-bound-tokens
Browse files Browse the repository at this point in the history
Feature/dpop bound tokens
  • Loading branch information
daniellindau authored Mar 13, 2024
2 parents c046000 + 62a2eaf commit d3a58fb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +36,7 @@ import javax.net.ssl.SSLContext

object HaapiConfigurationUtil {

fun createConfiguration(conf: HashMap<String, Any>): HaapiConfiguration = HaapiConfiguration(
fun createConfiguration(conf: HashMap<String, Any>, reactContext: ReactApplicationContext) = HaapiConfiguration(
keyStoreAlias = asStringOrDefault(
conf, "keyStoreAlias", "haapi-react-native-android"
),
Expand Down Expand Up @@ -67,6 +70,7 @@ object HaapiConfigurationUtil {
}
} as HttpURLConnection
},
tokenBoundConfiguration = createTokenBoundConfiguration(reactContext)
)

fun addFallbackConfiguration(accessorFactory: HaapiAccessorFactory, conf: HashMap<String, Any>, context: Context) {
Expand All @@ -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<String, Any>, parameter: String, defaultValue: String): String =
asOptionalString(conf, parameter) ?: defaultValue

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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
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<String, String> {
@Suppress("UNCHECKED_CAST")
return getSharedPreferences().all.filterValues { it is String } as Map<String, String>
}

private fun getSharedPreferences(): SharedPreferences =
context.getSharedPreferences(name, ReactApplicationContext.MODE_PRIVATE)
}
2 changes: 1 addition & 1 deletion ios/HaapiModule/HaapiModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down

0 comments on commit d3a58fb

Please sign in to comment.