Skip to content

Commit 9290a7a

Browse files
authored
Merge pull request #21 from User-green/fix/csrf-restored-session
fix(auth): establish CSRF for restored sessions, prompt relogin if impossible
2 parents 95f7e53 + 7476741 commit 9290a7a

16 files changed

Lines changed: 43 additions & 0 deletions

File tree

app/src/main/kotlin/org/astermail/android/auth/AuthRepository.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,15 @@ class AuthRepository @Inject constructor(
488488
return true
489489
}
490490

491+
suspend fun ensure_csrf_ready(): Boolean {
492+
if (!_is_signed_in.value) return true
493+
if (api_client.get_csrf() != null) return true
494+
return when (try_refresh_session()) {
495+
RefreshOutcome.Success -> api_client.get_csrf() != null
496+
RefreshOutcome.AuthFailed, RefreshOutcome.Transient -> false
497+
}
498+
}
499+
491500
fun has_stored_session(account_id: String): Boolean = session_snapshot_store.has(account_id)
492501

493502
suspend fun change_password(current_password: String, new_password: String): Result<Unit> = runCatching {

app/src/main/kotlin/org/astermail/android/auth/auth_gate_view_model.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@ package org.astermail.android.auth
2424
import androidx.lifecycle.ViewModel
2525
import androidx.lifecycle.viewModelScope
2626
import android.content.Context
27+
import android.widget.Toast
2728
import dagger.hilt.android.lifecycle.HiltViewModel
2829
import dagger.hilt.android.qualifiers.ApplicationContext
2930
import javax.inject.Inject
3031
import kotlinx.coroutines.CancellationException
32+
import kotlinx.coroutines.Dispatchers
3133
import kotlinx.coroutines.flow.MutableStateFlow
3234
import kotlinx.coroutines.flow.StateFlow
3335
import kotlinx.coroutines.flow.asStateFlow
3436
import kotlinx.coroutines.launch
37+
import kotlinx.coroutines.withContext
3538
import kotlinx.coroutines.withTimeoutOrNull
39+
import org.astermail.android.BuildConfig
40+
import org.astermail.android.R
3641
import org.astermail.android.api.auth.AuthApi
3742
import org.astermail.android.security.LockdownStore
3843

@@ -54,6 +59,21 @@ class AuthGateViewModel @Inject constructor(
5459
}
5560
_is_ready.value = true
5661
if (auth_repository.is_signed_in.value) {
62+
viewModelScope.launch {
63+
val csrf_ok = auth_repository.ensure_csrf_ready()
64+
if (!csrf_ok) {
65+
if (BuildConfig.DEBUG) {
66+
android.util.Log.w("AuthGate", "CSRF token fetch failed on launch")
67+
}
68+
withContext(Dispatchers.Main) {
69+
Toast.makeText(
70+
context,
71+
context.getString(R.string.csrf_unavailable),
72+
Toast.LENGTH_LONG,
73+
).show()
74+
}
75+
}
76+
}
5777
viewModelScope.launch {
5878
try {
5979
val info = withTimeoutOrNull(8_000L) { auth_api.me() }

app/src/main/res/values-ar/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,7 @@
12991299
<string name="pending_deletion_error">تعذّرت استعادة حسابك. يرجى المحاولة مرة أخرى.</string>
13001300
<string name="pending_deletion_restored">مرحبًا بعودتك. تمت استعادة حسابك.</string>
13011301
<string name="image_blocked_placeholder">[تم حظر الصورة]</string>
1302+
<string name="csrf_unavailable">هذه الجلسة معطوبة! يرجى تسجيل الدخول مرة أخرى.</string>
13021303
<string name="received_on_prefix">تم الاستلام على %s</string>
13031304
<string name="received_on_label">تم الاستلام على</string>
13041305
<string name="auto_advance_label">بعد الأرشفة أو الحذف</string>

app/src/main/res/values-de/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,7 @@
12921292
<string name="pending_deletion_error">Ihr Konto konnte nicht wiederhergestellt werden. Bitte versuchen Sie es erneut.</string>
12931293
<string name="pending_deletion_restored">Willkommen zurück. Ihr Konto wurde wiederhergestellt.</string>
12941294
<string name="image_blocked_placeholder">[Bild blockiert]</string>
1295+
<string name="csrf_unavailable">Diese Sitzung ist fehlerhaft! Bitte melden Sie sich erneut an.</string>
12951296
<string name="received_on_prefix">empfangen an %s</string>
12961297
<string name="received_on_label">Empfangen an</string>
12971298
<string name="auto_advance_label">Nach Archivieren oder Löschen</string>

app/src/main/res/values-es/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,7 @@
12921292
<string name="pending_deletion_error">No se pudo restaurar tu cuenta. Inténtalo de nuevo.</string>
12931293
<string name="pending_deletion_restored">Bienvenido de nuevo. Tu cuenta ha sido restaurada.</string>
12941294
<string name="image_blocked_placeholder">[Imagen bloqueada]</string>
1295+
<string name="csrf_unavailable">¡Esta sesión está dañada! Vuelve a iniciar sesión.</string>
12951296
<string name="received_on_prefix">recibido en %s</string>
12961297
<string name="received_on_label">Recibido en</string>
12971298
<string name="auto_advance_label">Después de archivar o eliminar</string>

app/src/main/res/values-fr/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,7 @@
19071907
<string name="pending_deletion_error">Impossible de restaurer votre compte. Veuillez réessayer.</string>
19081908
<string name="pending_deletion_restored">Bon retour. Votre compte a été restauré.</string>
19091909
<string name="image_blocked_placeholder">[Image bloquée]</string>
1910+
<string name="csrf_unavailable">Cette session est corrompue ! Veuillez vous reconnecter.</string>
19101911
<string name="received_on_prefix">reçu sur %s</string>
19111912
<string name="received_on_label">Reçu sur</string>
19121913
<string name="auto_advance_label">Après archivage ou suppression</string>

app/src/main/res/values-it/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@
12911291
<string name="pending_deletion_error">Impossibile ripristinare il tuo account. Riprova.</string>
12921292
<string name="pending_deletion_restored">Bentornato. Il tuo account è stato ripristinato.</string>
12931293
<string name="image_blocked_placeholder">[Immagine bloccata]</string>
1294+
<string name="csrf_unavailable">Questa sessione è danneggiata! Effettua nuovamente l\'accesso.</string>
12941295
<string name="received_on_prefix">ricevuto su %s</string>
12951296
<string name="received_on_label">Ricevuto su</string>
12961297
<string name="auto_advance_label">Dopo l\'archiviazione o l\'eliminazione</string>

app/src/main/res/values-ja/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,7 @@
12891289
<string name="pending_deletion_error">アカウントを復元できませんでした。もう一度お試しください。</string>
12901290
<string name="pending_deletion_restored">おかえりなさい。アカウントが復元されました。</string>
12911291
<string name="image_blocked_placeholder">[画像はブロックされました]</string>
1292+
<string name="csrf_unavailable">このセッションは壊れています!もう一度ログインしてください。</string>
12921293
<string name="received_on_prefix">%s で受信</string>
12931294
<string name="received_on_label">受信アドレス</string>
12941295
<string name="auto_advance_label">アーカイブまたは削除後</string>

app/src/main/res/values-ko/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,7 @@
12891289
<string name="pending_deletion_error">계정을 복원할 수 없습니다. 다시 시도해 주세요.</string>
12901290
<string name="pending_deletion_restored">다시 오신 것을 환영합니다. 계정이 복원되었습니다.</string>
12911291
<string name="image_blocked_placeholder">[이미지 차단됨]</string>
1292+
<string name="csrf_unavailable">이 세션이 손상되었습니다! 다시 로그인해 주세요.</string>
12921293
<string name="received_on_prefix">%s(으)로 수신</string>
12931294
<string name="received_on_label">수신 주소</string>
12941295
<string name="auto_advance_label">보관 또는 삭제 후</string>

app/src/main/res/values-nl/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@
12911291
<string name="pending_deletion_error">Kan je account niet herstellen. Probeer het opnieuw.</string>
12921292
<string name="pending_deletion_restored">Welkom terug. Je account is hersteld.</string>
12931293
<string name="image_blocked_placeholder">[Afbeelding geblokkeerd]</string>
1294+
<string name="csrf_unavailable">Deze sessie is beschadigd! Log opnieuw in.</string>
12941295
<string name="received_on_prefix">ontvangen op %s</string>
12951296
<string name="received_on_label">Ontvangen op</string>
12961297
<string name="auto_advance_label">Na archiveren of verwijderen</string>

0 commit comments

Comments
 (0)