OXYGENHUB
# ff65ea456ff1 oxygenhub-android / app / src / main / java / io / oxygenhub / android / auth / Session.kt
1.4 KiB · last changed Land proposals from the phone (closes #17) 10 days ago source blame history raw
1
package io.oxygenhub.android.auth
2
3
/** A signed-in hub: its address, the account, and the device token. */
4
data class Session(
5
    /** `https://oxygenhub.io`, no trailing slash. */
6
    val hub: String,
7
    val username: String,
8
    val token: String,
9
    /** The scopes the user allowed when approving the device. */
10
    val scopes: List<String> = emptyList(),
11
) {
12
    /**
13
     * Whether the token may change things (land a proposal): the scope was
14
     * allowed, or the hub did not say which were (then the hub decides).
15
     */
16
    val canWrite: Boolean get() = scopes.isEmpty() || io.oxygenhub.android.api.WriteScope in scopes
17
18
    // The token never reaches a log through toString.
19
    override fun toString(): String = "Session(hub=$hub, username=$username, scopes=$scopes, token=<redacted>)"
20
}
21
22
/** Where the session is kept between launches. */
23
interface SessionStore {
24
    fun load(): Session?
25
    fun save(session: Session)
26
    fun clear()
27
28
    /** The hub last signed in to (or typed), so the sign-in screen offers it again. */
29
    var lastHub: String?
30
}
31
32
/** A store in memory, for tests and previews. */
33
class MemorySessionStore(private var session: Session? = null, override var lastHub: String? = null) : SessionStore {
34
    override fun load(): Session? = session
35
    override fun save(session: Session) {
36
        this.session = session
37
    }
38
39
    override fun clear() {
40
        session = null
41
    }
42
}