-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Expire token relative to current date time instead of just time
- Loading branch information
Showing
5 changed files
with
69 additions
and
58 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
12 changes: 10 additions & 2 deletions
12
src/main/kotlin/app/revanced/api/configuration/Security.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,9 +1,17 @@ | ||
package app.revanced.api.configuration | ||
|
||
import app.revanced.api.configuration.services.AuthService | ||
import app.revanced.api.configuration.services.AuthenticationService | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import org.koin.ktor.ext.get | ||
|
||
fun Application.configureSecurity() { | ||
get<AuthService>().configureSecurity(this) | ||
val authenticationService = get<AuthenticationService>() | ||
|
||
install(Authentication) { | ||
with(authenticationService) { | ||
jwt() | ||
digest() | ||
} | ||
} | ||
} |
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
49 changes: 0 additions & 49 deletions
49
src/main/kotlin/app/revanced/api/configuration/services/AuthService.kt
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/app/revanced/api/configuration/services/AuthenticationService.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,53 @@ | ||
package app.revanced.api.configuration.services | ||
|
||
import com.auth0.jwt.JWT | ||
import com.auth0.jwt.algorithms.Algorithm | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.auth.jwt.* | ||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import kotlin.text.HexFormat | ||
|
||
internal class AuthenticationService private constructor( | ||
private val issuer: String, | ||
private val validityInMin: Long, | ||
private val jwtSecret: String, | ||
private val authSHA256Digest: ByteArray, | ||
) { | ||
@OptIn(ExperimentalStdlibApi::class) | ||
constructor(issuer: String, validityInMin: Long, jwtSecret: String, authSHA256DigestString: String) : this( | ||
issuer, | ||
validityInMin, | ||
jwtSecret, | ||
authSHA256DigestString.hexToByteArray(HexFormat.Default), | ||
) | ||
|
||
fun AuthenticationConfig.jwt() { | ||
jwt("jwt") { | ||
realm = "ReVanced" | ||
|
||
verifier(JWT.require(Algorithm.HMAC256(jwtSecret)).withIssuer(issuer).build()) | ||
} | ||
} | ||
|
||
fun AuthenticationConfig.digest() { | ||
digest("auth-digest") { | ||
realm = "ReVanced" | ||
algorithmName = "SHA-256" | ||
|
||
digestProvider { _, _ -> | ||
authSHA256Digest | ||
} | ||
} | ||
} | ||
|
||
fun newToken(): String { | ||
val issuedAt = Instant.now() | ||
|
||
return JWT.create() | ||
.withIssuer(issuer) | ||
.withIssuedAt(issuedAt) | ||
.withExpiresAt(issuedAt.plus(validityInMin, ChronoUnit.MINUTES)) | ||
.sign(Algorithm.HMAC256(jwtSecret)) | ||
} | ||
} |