# KeyStoreValues.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.api.auth.keystore

    import android.content.Context
    import android.util.Base64
    import dk.nsp.shared.util.sharedprefs.SharedPrefsHelper

    class KeyStoreValues(context: Context, storageName: String) {

        private val keyIdKey: String = "keyKey"
        private val refreshTokenKey: String = "refreshTokenKey"
        private val usernameKey: String = "usernameKey"
        private val longSecretKey: String = "longSecretKey"
        private val longSecretIvKey: String = "longSecretIvKey"
        private val migrationLevelKey: String = "migrationLevelKey"

        private val sharedPrefsHelper = SharedPrefsHelper(context, storageName)

        /** Removes all values (except the [migrationLevel]) */
        fun clear() {
            keyId = null
            encryptedRefreshToken = null
            longSecretIv = null
            encryptedLongSecret = null
            userName = null
        }

        var keyId: String?
            get() = sharedPrefsHelper.get(keyIdKey, null as? String?)
            set(value) = sharedPrefsHelper.store(keyIdKey, value)

        var encryptedRefreshToken: ByteArray?
            get() = sharedPrefsHelper.get(refreshTokenKey, null as? String?)?.base64ByteArray()
            set(value) = sharedPrefsHelper.store(refreshTokenKey, value?.base64String())

        var userName: String?
            get() = sharedPrefsHelper.get(usernameKey, null as? String?)
            set(value) = sharedPrefsHelper.store(usernameKey, value)

        var encryptedLongSecret: ByteArray?
            get() = sharedPrefsHelper.get(longSecretKey, null as? String?)?.base64ByteArray()
            set(value) = sharedPrefsHelper.store(longSecretKey, value?.base64String())

        var longSecretIv: ByteArray?
            get() = sharedPrefsHelper.get(longSecretIvKey, null as? String?)?.base64ByteArray()
            set(value) = sharedPrefsHelper.store(longSecretIvKey, value?.base64String())

        /**
         * A level of migration, incremented each time a new update changes the underlying data
         * -1 means its not there.
         */
        var migrationLevel: Int
            get() = sharedPrefsHelper.get(migrationLevelKey, -1)
            set(value) = sharedPrefsHelper.store(migrationLevelKey, value)
    }

    private fun ByteArray.base64String(): String {
        return Base64.encodeToString(this, 0)
    }

    private fun String.base64ByteArray(): ByteArray {
        return Base64.decode(this, 0)
    }