mirror of
https://github.com/tursom/TursomServer.git
synced 2025-02-13 05:10:58 +08:00
Merge remote-tracking branch 'origin/master'
# Conflicts: # utils/src/main/kotlin/cn/tursom/utils/TokenUtil.kt
This commit is contained in:
commit
75dedbc837
@ -16,16 +16,16 @@ open class TokenUtil {
|
|||||||
val digestBase64: String = digest.base64()
|
val digestBase64: String = digest.base64()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> generate(secretKey: String, data: T, timeout: Long = 30 * 60 * 1000, type: DigestType = DigestType.MD5): String {
|
fun <T : Token> generate(secretKey: String, data: T, type: DigestType = DigestType.MD5): String {
|
||||||
val head = type.digestBase64
|
val head = type.digestBase64
|
||||||
val body = toJson(TokenBody(System.currentTimeMillis(), timeout, data)).base64()
|
val body = toJson(data).base64()
|
||||||
val encryptSource = "$head.$body".toByteArray()
|
val encryptSource = "$head.$body".toByteArray()
|
||||||
val encrypt = encrypt(secretKey, encryptSource, type.digest)
|
val encrypt = encrypt(secretKey, encryptSource, type.digest)
|
||||||
return "$head.$body.$encrypt"
|
return "$head.$body.$encrypt"
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(TokenException::class)
|
@Throws(TokenException::class)
|
||||||
fun <T : Any> decode(secretKey: String, token: String, dataClazz: Class<T>): T {
|
fun <T : Token> decode(secretKey: String, token: String, dataClazz: Class<T>): T {
|
||||||
val splitToken = token.split(".")
|
val splitToken = token.split(".")
|
||||||
if (splitToken.size != 3) {
|
if (splitToken.size != 3) {
|
||||||
throw WrongTokenSyntaxException()
|
throw WrongTokenSyntaxException()
|
||||||
@ -34,14 +34,14 @@ open class TokenUtil {
|
|||||||
if (signature != splitToken[2]) {
|
if (signature != splitToken[2]) {
|
||||||
throw WrongSignatureException()
|
throw WrongSignatureException()
|
||||||
}
|
}
|
||||||
val decode = fromJson<TokenBody<T>>(splitToken[1].base64decode())
|
val decode = fromJson(splitToken[1].base64decode(), dataClazz)
|
||||||
if (decode.tim + decode.exp < System.currentTimeMillis()) {
|
if (decode.tim + decode.exp < System.currentTimeMillis()) {
|
||||||
throw TokenTimeoutException()
|
throw TokenTimeoutException()
|
||||||
}
|
}
|
||||||
return fromJson(toJson(decode.dat!!), dataClazz)
|
return decode
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun encrypt(secretKey: String, encryptSource: ByteArray, type: String): String {
|
protected open fun encrypt(secretKey: String, encryptSource: ByteArray, type: String): String {
|
||||||
val inner = secretKey.toByteArray().digest(type)!!
|
val inner = secretKey.toByteArray().digest(type)!!
|
||||||
encryptSource.forEachIndexed { index, _ ->
|
encryptSource.forEachIndexed { index, _ ->
|
||||||
encryptSource[index] = encryptSource[index] xor inner[index % inner.size]
|
encryptSource[index] = encryptSource[index] xor inner[index % inner.size]
|
||||||
@ -56,9 +56,10 @@ open class TokenUtil {
|
|||||||
open fun toJson(bean: Any): String = gson.toJson(bean)
|
open fun toJson(bean: Any): String = gson.toJson(bean)
|
||||||
open fun <T> fromJson(json: String, clazz: Class<T>): T = gson.fromJson(json, clazz)
|
open fun <T> fromJson(json: String, clazz: Class<T>): T = gson.fromJson(json, clazz)
|
||||||
|
|
||||||
private inline fun <reified T : Any> fromJson(json: String): T = fromJson(json, T::class.java)
|
interface Token {
|
||||||
|
val tim: Long
|
||||||
data class TokenBody<T>(val tim: Long = System.currentTimeMillis(), val exp: Long = 0L, val dat: T? = null)
|
val exp: Long
|
||||||
|
}
|
||||||
|
|
||||||
open class TokenException : Exception()
|
open class TokenException : Exception()
|
||||||
class WrongTokenSyntaxException : TokenException()
|
class WrongTokenSyntaxException : TokenException()
|
||||||
@ -67,6 +68,6 @@ open class TokenUtil {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val instance = TokenUtil()
|
val gsonTokenUtil = TokenUtil()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user