📖 PrzeglądOverview

Moduł SecureStorage zapewnia bezpieczne przechowywanie wrażliwych danych używając EncryptedSharedPreferences z szyfrowaniem AES-256. Idealny do przechowywaniausing EncryptedSharedPreferences with AES-256 encryption. Ideal for storing tokenów API, danych użytkownika i innych poufnych informacji.The SecureStorage module provides secure storage for sensitive data using EncryptedSharedPreferences with AES-256 encryption. Ideal for storing API tokens, user data, and other confidential information.

✅ Bezpieczeństwo: Dane są szyfrowane za pomocą AES256-GCM. Klucze przechowywane są w Android Keystore.Data is encrypted using AES256-GCM. Keys are stored in the Android Keystore.
Szybki przykładQuick Example
// Zapisywanie
ADict.SecureStorage.put("api_token", "secret123")
ADict.SecureStorage.put("user_id", 12345)

// Odczytywanie
val token = ADict.SecureStorage.getString("api_token")
val userId = ADict.SecureStorage.getInt("user_id", 0)

💾 Zapisywanie danychSaving Data

put(key: String, value: Any?)

Zapisz wartość z automatycznym wykrywaniem typu.Save value with automatic type detection.

putString(key: String, value: String)

Zapisz String.Save String.

putInt(key: String, value: Int)

Zapisz Int.Save Int.

putLong(key: String, value: Long)

Zapisz Long.Save Long.

putFloat(key: String, value: Float)

Zapisz Float.Save Float.

putBoolean(key: String, value: Boolean)

Zapisz Boolean.Save Boolean.

Zapisywanie danychSaving Data
// Automatyczne wykrywanie typu
ADict.SecureStorage.put("token", "abc123")
ADict.SecureStorage.put("user_id", 12345)
ADict.SecureStorage.put("is_premium", true)
ADict.SecureStorage.put("balance", 99.99f)

// Typowane metody
ADict.SecureStorage.putString("api_key", "sk_live_xxx")
ADict.SecureStorage.putInt("login_count", 5)
ADict.SecureStorage.putLong("last_sync", System.currentTimeMillis())
ADict.SecureStorage.putBoolean("notifications_enabled", true)

// Usunięcie przez null
ADict.SecureStorage.put("temp_data", null)

📖 Odczytywanie danychReading Data

getString(key: String, default: String? = null): String?

Pobierz String.Get String.

getInt(key: String, default: Int = 0): Int

Pobierz Int.Get Int.

getLong(key: String, default: Long = 0L): Long

Pobierz Long.Get Long.

getFloat(key: String, default: Float = 0f): Float

Pobierz Float.Get Float.

getBoolean(key: String, default: Boolean = false): Boolean

Pobierz Boolean.Get Boolean.

Odczytywanie danychReading Data
// Z wartościami domyślnymi
val token = ADict.SecureStorage.getString("api_token") // null jeśli brak
val userId = ADict.SecureStorage.getInt("user_id", -1) // -1 jeśli brak
val isPremium = ADict.SecureStorage.getBoolean("is_premium", false)
val lastSync = ADict.SecureStorage.getLong("last_sync", 0L)

// Sprawdzenie czy klucz istnieje
if (ADict.SecureStorage.contains("api_token")) {
    val token = ADict.SecureStorage.getString("api_token")!!
    makeApiCall(token)
}

🔧 NarzędziaTools

contains(key: String): Boolean

Sprawdź czy klucz istnieje.Check if key exists.

remove(key: String)

Usuń wartość.Remove value.

clear()

Wyczyść wszystkie dane.Clear all data.

getAllKeys(): Set<String>

Pobierz wszystkie klucze.Get all keys.

getAll(): Map<String, Any?>

Pobierz wszystkie wartości.Get all values.

Specjalne właściwościSpecial Properties

SecureStorage oferuje wbudowane właściwości dla typowych przypadków użycia:SecureStorage offers built-in properties for common use cases:

apiToken: String?

Get/set dla tokenu API.Get/set for API token.

ADict.SecureStorage.apiToken = "Bearer abc123"
val token = ADict.SecureStorage.apiToken

refreshToken: String?

Get/set dla refresh tokenu.Get/set for refresh token.

userId: String?

Get/set dla ID użytkownika.Get/set for user ID.

clearTokens()

Wyczyść wszystkie tokeny (apiToken i refreshToken).Clear all tokens (apiToken and refreshToken).

clearUserData()

Wyczyść dane użytkownika (tokeny + userId).Clear user data (tokens + userId).

ZarządzanieManagement sesją
// Logowanie
fun onLoginSuccess(response: LoginResponse) {
    ADict.SecureStorage.apiToken = response.accessToken
    ADict.SecureStorage.refreshToken = response.refreshToken
    ADict.SecureStorage.userId = response.userId
}

// Sprawdzenie sesji
fun isLoggedIn(): Boolean {
    return ADict.SecureStorage.apiToken != null
}

// Refresh tokenu
fun refreshAccessToken() {
    val refreshToken = ADict.SecureStorage.refreshToken ?: return
    api.refresh(refreshToken) { newToken ->
        ADict.SecureStorage.apiToken = newToken
    }
}

// Wylogowanie
fun logout() {
    ADict.SecureStorage.clearUserData()
    // lub bardziej szczegółowo:
    // ADict.SecureStorage.clearTokens()
    // ADict.SecureStorage.remove("userId")
}

💡 PrzykładyExamples praktyczne

Pełna obsługa autentykacjiFull Authentication Handling

AuthManager
object AuthManager {
    private const val KEY_USER_EMAIL = "user_email"
    private const val KEY_USER_NAME = "user_name"
    private const val KEY_TOKEN_EXPIRES = "token_expires"

    val isLoggedIn: Boolean
        get() = ADict.SecureStorage.apiToken != null

    val currentUserId: String?
        get() = ADict.SecureStorage.userId

    val currentUserEmail: String?
        get() = ADict.SecureStorage.getString(KEY_USER_EMAIL)

    fun saveSession(session: UserSession) {
        ADict.SecureStorage.apiToken = session.accessToken
        ADict.SecureStorage.refreshToken = session.refreshToken
        ADict.SecureStorage.userId = session.userId
        ADict.SecureStorage.putString(KEY_USER_EMAIL, session.email)
        ADict.SecureStorage.putString(KEY_USER_NAME, session.name)
        ADict.SecureStorage.putLong(KEY_TOKEN_EXPIRES, session.expiresAt)
    }

    fun isTokenExpired(): Boolean {
        val expiresAt = ADict.SecureStorage.getLong(KEY_TOKEN_EXPIRES, 0L)
        return System.currentTimeMillis() > expiresAt
    }

    fun getAuthHeader(): String? {
        return ADict.SecureStorage.apiToken?.let { "Bearer $it" }
    }

    fun logout() {
        ADict.SecureStorage.clearUserData()
        ADict.SecureStorage.remove(KEY_USER_EMAIL)
        ADict.SecureStorage.remove(KEY_USER_NAME)
        ADict.SecureStorage.remove(KEY_TOKEN_EXPIRES)
    }
}

// Użycie w OkHttp Interceptor
class AuthInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request().newBuilder()

        AuthManager.getAuthHeader()?.let { authHeader ->
            request.addHeader("Authorization", authHeader)
        }

        return chain.proceed(request.build())
    }
}

Przechowywanie ustawień wrażliwychStoring Sensitive Settings

Ustawienia aplikacjiApp Settings
object SecureSettings {
    // PIN aplikacji
    var appPin: String?
        get() = ADict.SecureStorage.getString("app_pin")
        set(value) {
            if (value != null) ADict.SecureStorage.putString("app_pin", value)
            else ADict.SecureStorage.remove("app_pin")
        }

    // Biometria
    var biometricEnabled: Boolean
        get() = ADict.SecureStorage.getBoolean("biometric_enabled", false)
        set(value) = ADict.SecureStorage.putBoolean("biometric_enabled", value)

    // Klucze szyfrowania
    fun saveEncryptionKey(key: ByteArray) {
        ADict.SecureStorage.putString("encryption_key", Base64.encodeToString(key, Base64.DEFAULT))
    }

    fun getEncryptionKey(): ByteArray? {
        return ADict.SecureStorage.getString("encryption_key")?.let {
            Base64.decode(it, Base64.DEFAULT)
        }
    }
}

📚 API Reference

MetodaMethod OpisDescription
put(key, value)Zapisz (auto-type)Save (auto-type)
putString(key, value)Zapisz StringSave String
putInt(key, value)Zapisz IntSave Int
putLong(key, value)Zapisz LongSave Long
putFloat(key, value)Zapisz FloatSave Float
putBoolean(key, value)Zapisz BooleanSave Boolean
getString(key, default)Pobierz StringGet String
getInt(key, default)Pobierz IntGet Int
getLong(key, default)Pobierz LongGet Long
getFloat(key, default)Pobierz FloatGet Float
getBoolean(key, default)Pobierz BooleanGet Boolean
contains(key)Sprawdź istnienieCheck existence
remove(key)Usuń wartość
clear()Wyczyść wszystkoClear all
getAllKeys()Pobierz kluczeGet keys
getAll()Pobierz wszystkie wartościGet all values
apiTokenProperty: token API
refreshTokenProperty: refresh token
userIdProperty: ID użytkownikaProperty: user ID
clearTokens()Wyczyść tokenyClear tokens
clearUserData()Wyczyść dane użytkownikaClear user data