OXYGENHUB

Coverage / app/src/main/java/io/oxygenhub/android/auth/Session.kt 93.3%

14 of 15 lines run, 14 of 16 functions, 4 of 8 branches · run #1646 on main · run missed

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
    /** Whether the token may make repositories (#27); a sign-in before 0.1.1 was not asked for it. */
19
    val canCreate: Boolean get() = scopes.isEmpty() || io.oxygenhub.android.api.AdminScope in scopes
20
21
    // The token never reaches a log through toString.
22
    override fun toString(): String = "Session(hub=$hub, username=$username, scopes=$scopes, token=<redacted>)"
23
}
24
25
/** Where the session is kept between launches. */
26
interface SessionStore {
27
    fun load(): Session?
28
    fun save(session: Session)
29
    fun clear()
30
31
    /** The hub last signed in to (or typed), so the sign-in screen offers it again. */
32
    var lastHub: String?
33
}
34
35
/** A store in memory, for tests and previews. */
36
class MemorySessionStore(private var session: Session? = null, override var lastHub: String? = null) : SessionStore {
37
    override fun load(): Session? = session
38
    override fun save(session: Session) {
39
        this.session = session
40
    }
41
42
    override fun clear() {
43
        session = null
44
    }
45
}