app/src/main/java/com/geekorum/ttrss/accounts/TinyrssAcountManager.kt
author Da Risk <da_risk@geekorum.com>
Thu, 09 May 2019 17:53:48 -0700
changeset 137 5464f07a306c
parent 104 8b4047ebec59
child 163 07a90afdb9db
permissions -rw-r--r--
Update copyright headers for 2019
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
     1
/*
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * Geekttrss is a RSS feed reader application on the Android Platform.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 *
137
5464f07a306c Update copyright headers for 2019
Da Risk <da_risk@geekorum.com>
parents: 104
diff changeset
     4
 * Copyright (C) 2017-2019 by Frederic-Charles Barthelery.
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     5
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 * This file is part of Geekttrss.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 * Geekttrss is free software: you can redistribute it and/or modify
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * it under the terms of the GNU General Public License as published by
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * the Free Software Foundation, either version 3 of the License, or
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * (at your option) any later version.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 * Geekttrss is distributed in the hope that it will be useful,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * GNU General Public License for more details.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    21
package com.geekorum.ttrss.accounts
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    23
import android.accounts.AccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    24
import android.content.ContentResolver
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import android.os.Bundle
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import android.util.Base64
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import com.geekorum.geekdroid.security.SecretCipher
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    28
import com.geekorum.ttrss.BackgroundJobManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    29
import com.geekorum.ttrss.providers.ArticlesContract
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    30
import com.geekorum.ttrss.sync.SyncContract
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    31
import timber.log.Timber
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    32
import java.security.GeneralSecurityException
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    33
import javax.crypto.spec.GCMParameterSpec
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
import javax.inject.Inject
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    35
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    36
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
 * Represents a Tinyrss account
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    38
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    39
data class Account(val username: String, val url: String)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    41
/**
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    42
 * Holds information about the TinyTinyRss server we are connecting to.
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    43
 */
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    44
abstract class ServerInformation {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    45
    abstract val apiUrl: String
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    46
    abstract val basicHttpAuthUsername: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    47
    abstract val basicHttpAuthPassword: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    48
}
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    49
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    50
/**
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
 * API of the AccountManager for Tinyrss
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
interface TinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    54
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    55
    /**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
     * Add an account.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
     * @return true on success
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
     */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
    fun addAccount(account: Account, password: String): Boolean
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    60
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    61
    /**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
     * Initialize synchronisation jobs for an account.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    63
     */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
    fun initializeAccountSync(account: Account)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    66
    fun updatePassword(account: Account, password: String)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    67
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    68
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    69
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    70
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
 * Implementation of [TinyrssAccountManager] on Android platform
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    72
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    73
class AndroidTinyrssAccountManager @Inject constructor(
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
    private val accountManager: AccountManager,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
    private val secretCipher: SecretCipher
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    76
) : TinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    77
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    78
    companion object {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    79
        const val ACCOUNT_TYPE = AccountAuthenticator.TTRSS_ACCOUNT_TYPE
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    80
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    81
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    82
    override fun addAccount(account: Account, password: String): Boolean {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    83
        val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    84
        val userdata = Bundle()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    85
        userdata.putString(AccountAuthenticator.USERDATA_URL, account.url)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    86
        val encryptedPassword = encrypt(password)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    87
        return accountManager.addAccountExplicitly(androidAccount, encryptedPassword, userdata)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    88
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    89
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    90
    override fun updatePassword(account: Account, password: String) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    91
        val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    92
        val encryptedPassword = encrypt(password)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    93
        accountManager.setPassword(androidAccount, encryptedPassword)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    94
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    95
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    96
    override fun initializeAccountSync(account: Account) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    97
        val extras = Bundle()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    98
        extras.putInt(SyncContract.EXTRA_NUMBER_OF_LATEST_ARTICLES_TO_REFRESH, -1)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    99
        android.accounts.Account(account.username, ACCOUNT_TYPE).also {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   100
            ContentResolver.setSyncAutomatically(it, ArticlesContract.AUTHORITY, true)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
            ContentResolver.addPeriodicSync(it, ArticlesContract.AUTHORITY, Bundle(),
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   102
                BackgroundJobManager.PERIODIC_REFRESH_JOB_INTERVAL_S)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   103
            ContentResolver.addPeriodicSync(it, ArticlesContract.AUTHORITY, extras,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   104
                BackgroundJobManager.PERIODIC_FULL_REFRESH_JOB_INTERVAL_S)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   105
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   107
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   108
    @Throws(GeneralSecurityException::class, IllegalArgumentException::class)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   109
    fun getPassword(account: Account) : String? {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   110
        val encryptedPassword: String? = android.accounts.Account(account.username, ACCOUNT_TYPE).let {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   111
            accountManager.getPassword(it)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   112
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   113
        return encryptedPassword?.let { decrypt(it) }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   114
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   115
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   116
    fun fromAndroidAccount(androidAccount: android.accounts.Account): Account {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   117
        check(ACCOUNT_TYPE == androidAccount.type) {"Invalid account type ${androidAccount.type}"}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   118
        val url = accountManager.getUserData(androidAccount, AccountAuthenticator.USERDATA_URL)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   119
        return Account(androidAccount.name, url)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   120
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   121
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   122
    fun updateServerInformation(account: Account, serverInformation: ServerInformation) {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   123
        val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   124
        val encryptedPassword = serverInformation.basicHttpAuthPassword?.let { encrypt(it) }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   125
        accountManager.run {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   126
            setUserData(androidAccount, AccountAuthenticator.USERDATA_URL, serverInformation.apiUrl)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   127
            setUserData(androidAccount, AccountAuthenticator.USERDATA_BASIC_HTTP_AUTH_USERNAME, serverInformation.basicHttpAuthUsername)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   128
            setUserData(androidAccount, AccountAuthenticator.USERDATA_BASIC_HTTP_AUTH_PASSWORD, encryptedPassword)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   129
        }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   130
    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   131
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   132
    fun getServerInformation(account: Account) : ServerInformation {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   133
        val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   134
        return object : ServerInformation() {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   135
            override val apiUrl: String
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   136
                get() =  accountManager.getUserData(androidAccount, AccountAuthenticator.USERDATA_URL)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   137
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   138
            override val basicHttpAuthUsername: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   139
                get() =  accountManager.getUserData(androidAccount, AccountAuthenticator.USERDATA_BASIC_HTTP_AUTH_USERNAME)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   140
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   141
            override val basicHttpAuthPassword: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   142
                get() {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   143
                    val encryptedPassword = accountManager.getUserData(androidAccount,
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   144
                        AccountAuthenticator.USERDATA_BASIC_HTTP_AUTH_PASSWORD)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   145
                    return try {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   146
                        encryptedPassword?.let { decrypt(it) }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   147
                    } catch (e: Exception) {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   148
                        Timber.w(e, "unable to decrypt basic http auth password")
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   149
                        null
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   150
                    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   151
                }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   152
        }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   153
    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   154
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   155
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   156
    private fun decrypt(encryptedPassword: String): String {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   157
        val lines = encryptedPassword.lines()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   158
        val encryptedPasswordPart = lines[0]
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   159
        val iv = Base64.decode(lines[1], Base64.NO_WRAP)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   160
        val tlen = lines[2].toInt()
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   161
        val input = Base64.decode(encryptedPasswordPart, Base64.NO_WRAP)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   162
        val gcmParameterSpec = GCMParameterSpec(tlen, iv)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   163
        val output = secretCipher.decrypt(input, gcmParameterSpec)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   164
        return output.toString(Charsets.UTF_8)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   165
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   166
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   167
    private fun encrypt(plaintextPassword: String): String {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   168
        val input = plaintextPassword.toByteArray(Charsets.UTF_8)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   169
        val output = secretCipher.encrypt(input)
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   170
        val base64EncryptedPassword = Base64.encodeToString(output, Base64.NO_WRAP)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   171
        val gcmParameterSpec = secretCipher.parametersSpec
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   172
        val base64IV = Base64.encodeToString(gcmParameterSpec.iv, Base64.NO_WRAP)
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   173
        return "${base64EncryptedPassword}\n$${base64IV}\n${gcmParameterSpec.tLen}\n"
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   174
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   175
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   176