mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-15 06:47:27 +08:00
[+] JWT.auth
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package icu.samnyan.aqua.net.components
|
package icu.samnyan.aqua.net.components
|
||||||
|
|
||||||
import ext.Str
|
import ext.Str
|
||||||
|
import ext.minus
|
||||||
import icu.samnyan.aqua.net.db.AquaNetUser
|
import icu.samnyan.aqua.net.db.AquaNetUser
|
||||||
import icu.samnyan.aqua.net.db.AquaNetUserRepo
|
import icu.samnyan.aqua.net.db.AquaNetUserRepo
|
||||||
import io.jsonwebtoken.JwtParser
|
import io.jsonwebtoken.JwtParser
|
||||||
@@ -63,6 +64,12 @@ class JWT(
|
|||||||
.signWith(key)
|
.signWith(key)
|
||||||
.compact()
|
.compact()
|
||||||
|
|
||||||
fun parse(token: Str): AquaNetUser? =
|
fun parse(token: Str): AquaNetUser? = try {
|
||||||
userRepo.findByAuId(parser.parseSignedClaims(token).payload.subject.toLong())
|
userRepo.findByAuId(parser.parseSignedClaims(token).payload.subject.toLong())
|
||||||
}
|
} catch (e: Exception) {
|
||||||
|
log.debug("Failed to parse JWT", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun auth(token: Str) = parse(token) ?: (400 - "Invalid token")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user