Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / build-logic / shared-test / java / com / vayunmathur / testsupport / FakeTokenSealing.kt
FakeTokenSealing.kt28 lines · 1.1 KiBRaw
| 1 | package com.vayunmathur.testsupport |
| 2 | |
| 3 | /** |
| 4 | * Shared host-test sealing logic for the per-app `TokenSealer` interfaces |
| 5 | * (code, fooddelivery, taxi each declare their own interface in main, so no |
| 6 | * single shared class can implement all three; each app keeps a thin |
| 7 | * `FakeTokenSealer : TokenSealer` adapter delegating here). |
| 8 | * |
| 9 | * Mirrors the real sealers' contract: sealed values carry the `v1:` prefix |
| 10 | * (every app's `KeystoreTokenSealer.PREFIX`), this fake's own output carries |
| 11 | * `v1:fake:`, legacy values pass through, and foreign sealed values fail |
| 12 | * (null) rather than passing through. Robolectric has no AndroidKeyStore, |
| 13 | * so tests seal with this reversible marker transform instead of AES-GCM. |
| 14 | */ |
| 15 | object FakeTokenSealing { |
| 16 | private const val SEALED_PREFIX = "v1:fake:" |
| 17 | private const val REAL_PREFIX = "v1:" |
| 18 | |
| 19 | fun seal(plain: String): String = SEALED_PREFIX + plain |
| 20 | |
| 21 | fun unseal(stored: String): String? = when { |
| 22 | stored.startsWith(SEALED_PREFIX) -> stored.removePrefix(SEALED_PREFIX) |
| 23 | isSealed(stored) -> null |
| 24 | else -> stored |
| 25 | } |
| 26 | |
| 27 | fun isSealed(stored: String): Boolean = stored.startsWith(REAL_PREFIX) |
| 28 | } |