forked from SteamWar/SteamWar
Authentication v2
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2025 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.util
|
||||
|
||||
import de.steamwar.sql.Token
|
||||
import java.time.LocalDateTime
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlin.time.Duration.Companion.hours
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlin.time.toJavaDuration
|
||||
|
||||
val Token.type: TokenType
|
||||
get() = when (name.substring((0..1))) {
|
||||
"RT" -> TokenType.REFRESH_TOKEN
|
||||
"AT" -> TokenType.ACCESS_TOKEN
|
||||
"PT" -> TokenType.RESET_PASSWORD
|
||||
else -> TokenType.OLD_TOKEN
|
||||
}
|
||||
|
||||
val TokenType.lifetime: Duration
|
||||
get() = when (this) {
|
||||
TokenType.REFRESH_TOKEN -> 7.days
|
||||
TokenType.ACCESS_TOKEN -> 1.hours
|
||||
TokenType.RESET_PASSWORD -> 10.minutes
|
||||
TokenType.OLD_TOKEN -> 1.days
|
||||
}
|
||||
|
||||
val Token.lifetime: Duration
|
||||
get() = type.lifetime
|
||||
|
||||
val Token.isValid: Boolean
|
||||
get() = created.toLocalDateTime().plus(lifetime.toJavaDuration()).isAfter(LocalDateTime.now())
|
||||
|
||||
enum class TokenType {
|
||||
RESET_PASSWORD,
|
||||
ACCESS_TOKEN,
|
||||
REFRESH_TOKEN,
|
||||
OLD_TOKEN
|
||||
}
|
||||
Reference in New Issue
Block a user