|
|
@@ -5,20 +5,16 @@ import android.content.SharedPreferences
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
class Token(
|
|
|
- val accessToken: String,
|
|
|
- val refreshToken: String,
|
|
|
- val expiresIn: Int = 0,
|
|
|
- val expiresAt: Long
|
|
|
-
|
|
|
+ val token: String,
|
|
|
+ private val expiresAt: Long
|
|
|
) {
|
|
|
|
|
|
+
|
|
|
fun saveToSp(context:Context): Boolean {
|
|
|
val now = System.currentTimeMillis() / 1000
|
|
|
return tokenSp(context).edit()
|
|
|
- .putString("accessToken", accessToken)
|
|
|
- .putString("refreshToken", refreshToken)
|
|
|
- .putLong("expiresAt", expiresIn + now - 60)
|
|
|
- .putLong("refreshExpiresAt", TimeUnit.DAYS.toSeconds(7) + now - 60)
|
|
|
+ .putString("token", token)
|
|
|
+ .putLong("expiresAt", now + 60 * 60 * 2)
|
|
|
.commit()
|
|
|
}
|
|
|
|
|
|
@@ -26,31 +22,30 @@ class Token(
|
|
|
return expiresAt < 0 || expiresAt > System.currentTimeMillis() / 1000
|
|
|
}
|
|
|
|
|
|
+ override fun toString(): String {
|
|
|
+ return "Token(token='$token', expiresAt=$expiresAt)"
|
|
|
+ }
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
fun fromSp(context:Context): Token? {
|
|
|
val sp = tokenSp(context)
|
|
|
- val accessToken = sp.getString("accessToken", null)
|
|
|
- val refreshToken = sp.getString("refreshToken", null)
|
|
|
- if (accessToken == null || refreshToken == null) {
|
|
|
+ val token = sp.getString("token", null)
|
|
|
+ if (token == null) {
|
|
|
return null
|
|
|
}
|
|
|
- val refreshExpiresAt = sp.getLong("refreshExpiresAt", 0)
|
|
|
+ val expiresAt = sp.getLong("expiresAt", 0)
|
|
|
val now = System.currentTimeMillis() / 1000
|
|
|
- if (refreshExpiresAt < now) {
|
|
|
+ if (expiresAt < now) {
|
|
|
return null
|
|
|
}
|
|
|
- val expiresAt = sp.getLong("expiresAt", 0)
|
|
|
- return Token(accessToken, refreshToken, expiresAt = expiresAt)
|
|
|
+ return Token(token, expiresAt)
|
|
|
}
|
|
|
|
|
|
fun clear(context: Context) : Boolean {
|
|
|
return tokenSp(context).edit()
|
|
|
- .putString("accessToken", null)
|
|
|
- .putString("refreshToken", null)
|
|
|
+ .putString("token", null)
|
|
|
.putLong("expiresAt", 0)
|
|
|
- .putLong("refreshExpiresAt", 0)
|
|
|
.commit()
|
|
|
}
|
|
|
|
|
|
@@ -58,7 +53,10 @@ class Token(
|
|
|
return context.getSharedPreferences("token", Context.MODE_PRIVATE)
|
|
|
}
|
|
|
|
|
|
+ fun refresh(context: Context) {
|
|
|
+ val now = System.currentTimeMillis() / 1000
|
|
|
+ val sp = tokenSp(context)
|
|
|
+ sp.edit().putLong("expiresAt", now + 60 * 60 * 2).apply()
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|