Skip to content

Commit

Permalink
Unfinished changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellet committed Dec 18, 2023
1 parent 3d58784 commit 4f22efa
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 134 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ plugins {
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlinx.serialization) apply false
}
}
7 changes: 2 additions & 5 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
alias(libs.plugins.kotlin.multiplatform)
// application
id("maven-publish")
// id("java-library")
}

val kotlinVersion = libs.versions.kotlin.get()
Expand All @@ -19,6 +18,8 @@ description =

kotlin {
jvm()
linuxX64()
macosArm64()

sourceSets {
val commonMain by getting {
Expand Down Expand Up @@ -54,10 +55,6 @@ kotlin {
}
}

repositories {
mavenCentral()
}

//publishing {
//
// val jitpackGroupId = "com.github.freshplatform"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.freshplatform.ktor_server.firebase_app_check.core

import com.auth0.jwt.interfaces.DecodedJWT
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
Expand All @@ -11,6 +10,7 @@ import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCh
import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtErrorType.*
import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtException
import net.freshplatform.ktor_server.firebase_app_check.service.FirebaseAppCheckTokenVerifierService
import net.freshplatform.ktor_server.firebase_app_check.services.jwt.DecodedJwt
import net.freshplatform.ktor_server.firebase_app_check.utils.FirebaseAppCheckMessages
import net.freshplatform.ktor_server.firebase_app_check.utils.extensions.protectRouteWithAppCheck

Expand Down Expand Up @@ -47,10 +47,10 @@ class FirebaseAppCheckPluginConfiguration(
var firebaseAppCheckApiBaseUrl: String = "https://firebaseappcheck.googleapis.com",
var firebaseAppCheckPublicJwtSetUrl: String = "${firebaseAppCheckApiBaseUrl}/v1/jwks",
var secureStrategy: FirebaseAppCheckSecureStrategy = FirebaseAppCheckSecureStrategy.ProtectSpecificRoutes,
var additionalSecurityCheck: suspend (decodedJwt: DecodedJWT) -> Boolean = {
var additionalSecurityCheck: suspend (decodedJwt: DecodedJwt) -> Boolean = {
true
},
var afterSecurityCheck: suspend (decodedJwt: DecodedJWT) -> Unit = {},
var afterSecurityCheck: suspend (decodedJwt: DecodedJwt) -> Unit = {},
// var consumeTheTokenAfterUsingIt: Boolean = false
var errorBuilder: suspend (e: Exception, call: ApplicationCall, pluginConfig: FirebaseAppCheckPluginConfiguration) -> Unit
= { e, call, pluginConfig ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.freshplatform.ktor_server.firebase_app_check.service

import com.auth0.jwt.interfaces.DecodedJWT
import java.security.PublicKey
import java.util.concurrent.TimeUnit
import net.freshplatform.ktor_server.firebase_app_check.services.jwt.DecodedJwt
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes


/**
* Configuration data class for fetching Firebase App Check public keys. This class
Expand All @@ -22,26 +24,19 @@ data class FetchFirebaseAppCheckPublicKeyConfig(
*
* @param cacheSize The size of the cache for public keys.
* @param expiresIn The duration for public keys to expire in the cache.
* @param timeUnit The time unit for cache expiration.
*/
data class FetchFirebaseAppCheckPublicKeyCacheConfig(
val cacheSize: Long = 10,
val expiresIn: Long = 24,
val timeUnit: TimeUnit = TimeUnit.HOURS,
val expiresIn: Duration = 24.hours,
)

/**
* Configuration data class for rate limiting of key fetch requests. This class specifies
* the bucket size, refill rate, and time unit for rate limiting.
* Configuration data class for rate limiting of key fetch requests.
*
* @param bucketSize The bucket size for rate limiting.
* @param refillRate The time to refill the rate limit.
* @param timeUnit The time unit for rate limit refilling.
* @param enabled
*/
data class FetchFirebaseAppCheckPublicKeyRateLimitedConfig(
val bucketSize: Long = 10,
val refillRate: Long = 1,
val timeUnit: TimeUnit = TimeUnit.MINUTES
val enabled: Boolean
)

/**
Expand Down Expand Up @@ -77,25 +72,5 @@ interface FirebaseAppCheckTokenVerifierService {
firebaseProjectId: String,
firebaseProjectNumber: String,
issuerBaseUrl: String
): DecodedJWT
}

class FirebaseAppCheckTokenVerifierServiceUnimplemented: FirebaseAppCheckTokenVerifierService {
override suspend fun fetchFirebaseAppCheckPublicKey(
jwtString: String,
url: String,
config: FetchFirebaseAppCheckPublicKeyConfig
): PublicKey {
TODO("Not yet implemented")
}

override suspend fun verifyFirebaseAppCheckToken(
jwtString: String,
publicKey: PublicKey,
firebaseProjectId: String,
firebaseProjectNumber: String,
issuerBaseUrl: String
): DecodedJWT {
TODO("Not yet implemented")
}
): DecodedJwt
}
Original file line number Diff line number Diff line change
@@ -1,88 +1,6 @@
package net.freshplatform.ktor_server.firebase_app_check.services.jwt

import com.auth0.jwt.interfaces.Claim
import java.time.DateTimeException
import java.time.Instant
import java.util.*

fun main() {

}

data class DecodedJwt(
val token: String,
val header: String,
val payload: String,
val signature: String
)

data class JwtPayload(
val issuer: String,
val subject: String,
val audience: String,
val expiresAt: Long,
val notBefore: Long,
val issuedAt: Long,
val id: String,
val getClaim: () -> Unit,
val claims: Map<String?, Claim?>?,
)

interface PayloadBase {

/**
* Get a Claim given its name. If the Claim wasn't specified in the Payload, a 'null claim'
* will be returned. All the methods of that claim will return `null`.
*
* @param name the name of the Claim to retrieve.
* @return a non-null Claim.
*/
fun getClaim(name: String?): Claim?

/**
* Get the Claims defined in the Token.
*
* @return a non-null Map containing the Claims defined in the Token.
*/
val claims: Map<String?, Claim?>?
}

interface HeaderBase {

/**
* Getter for the Algorithm "alg" claim defined in the JWT's Header. If the claim is missing, it will return null.
*
* @return the Algorithm defined or null.
*/
fun getAlgorithm(): String?

/**
* Getter for the Type "typ" claim defined in the JWT's Header. If the claim is missing, it will return null.
*
* @return the Type defined or null.
*/
fun getType(): String?

/**
* Getter for the Content Type "cty" claim defined in the JWT's Header. If the claim is missing, it will return null.
*
* @return the Content Type defined or null.
*/
fun getContentType(): String?

/**
* Get the value of the "kid" claim, or null if it's not available.
*
* @return the Key ID value or null.
*/
fun getKeyId(): String?

/**
* Get a Private Claim given its name. If the Claim wasn't specified in the Header, a 'null claim' will be
* returned. All the methods of that claim will return `null`.
*
* @param name the name of the Claim to retrieve.
* @return a non-null Claim.
*/
fun getHeaderClaim(name: String?): Claim?
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCh
import net.freshplatform.ktor_server.firebase_app_check.exceptions.FirebaseAppCheckVerifyJwtException
import net.freshplatform.ktor_server.firebase_app_check.service.FetchFirebaseAppCheckPublicKeyConfig
import net.freshplatform.ktor_server.firebase_app_check.service.FirebaseAppCheckTokenVerifierService
import net.freshplatform.ktor_server.firebase_app_check.services.jwt.DecodedJwt
import java.net.URL
import java.security.PublicKey
import java.security.interfaces.RSAPublicKey
import kotlin.time.toJavaDuration

class FirebaseAppCheckTokenVerifierServiceImpl : FirebaseAppCheckTokenVerifierService {
override suspend fun fetchFirebaseAppCheckPublicKey(
Expand All @@ -31,13 +33,10 @@ class FirebaseAppCheckTokenVerifierServiceImpl : FirebaseAppCheckTokenVerifierSe
JwkProviderBuilder(URL(url))
.cached(
cacheConfig.cacheSize,
cacheConfig.expiresIn,
cacheConfig.timeUnit,
cacheConfig.expiresIn.toJavaDuration(),
)
.rateLimited(
rateLimitedConfig.bucketSize,
rateLimitedConfig.refillRate,
rateLimitedConfig.timeUnit,
rateLimitedConfig.enabled
)
.build()

Expand Down Expand Up @@ -109,7 +108,7 @@ class FirebaseAppCheckTokenVerifierServiceImpl : FirebaseAppCheckTokenVerifierSe
firebaseProjectId: String,
firebaseProjectNumber: String,
issuerBaseUrl: String
): DecodedJWT {
): DecodedJwt {
return withContext(Dispatchers.IO) {
try {
val verifier = JWT
Expand All @@ -125,7 +124,7 @@ class FirebaseAppCheckTokenVerifierServiceImpl : FirebaseAppCheckTokenVerifierSe
errorType = FirebaseAppCheckVerifyJwtErrorType.HeaderTypeIsNotJwt
)
}
decodedJwt
DecodedJwt(decodedJwt.token)
} catch (e: TokenExpiredException) {
throw FirebaseAppCheckVerifyJwtException(
message = e.message.toString(),
Expand Down
8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ pluginManagement {
mavenCentral()
gradlePluginPortal()
}
}

dependencyResolutionManagement {
repositories {
mavenCentral()
google()
gradlePluginPortal()
}
}

0 comments on commit 4f22efa

Please sign in to comment.