app/src/main/java/com/geekorum/ttrss/accounts/TinyrssAcountManager.kt
author Da Risk <da_risk@geekorum.com>
Wed, 03 Jan 2024 15:26:40 -0400
changeset 1174 731f6ee517b6
parent 943 298742859784
child 1370 13e39ef920a8
permissions -rw-r--r--
update license headers
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
 *
1174
731f6ee517b6 update license headers
Da Risk <da_risk@geekorum.com>
parents: 943
diff changeset
     4
 * Copyright (C) 2017-2024 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
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    26
import android.os.StrictMode
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import android.util.Base64
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    28
import com.geekorum.geekdroid.security.SecretCipher
265
bc4d82c760f6 Move BackgroundJobManager into its own package
Da Risk <da_risk@geekorum.com>
parents: 163
diff changeset
    29
import com.geekorum.ttrss.background_job.BackgroundJobManager
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    30
import com.geekorum.ttrss.debugtools.withStrictMode
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    31
import com.geekorum.ttrss.providers.ArticlesContract
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    32
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
    33
import timber.log.Timber
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
import java.security.GeneralSecurityException
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    35
import javax.crypto.spec.GCMParameterSpec
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    36
import javax.inject.Inject
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
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
 * Represents a Tinyrss account
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
data class Account(val username: String, val url: String)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    42
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    43
/**
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    44
 * 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
    45
 */
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    46
abstract class ServerInformation {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    47
    abstract val apiUrl: String
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    48
    abstract val basicHttpAuthUsername: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    49
    abstract val basicHttpAuthPassword: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    50
}
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    51
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
    52
/**
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
 * API of the AccountManager for Tinyrss
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
interface TinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
    /**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
     * Add an account.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
     * @return true on success
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
    fun addAccount(account: Account, password: String): Boolean
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
712
fc75653c8faf app: Login, save ServerInformation when adding account
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    63
    fun updateServerInformation(account: Account, serverInformation: ServerInformation)
fc75653c8faf app: Login, save ServerInformation when adding account
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    64
0
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
     * Initialize synchronisation jobs for an account.
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
    fun initializeAccountSync(account: Account)
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
    fun updatePassword(account: Account, password: String)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
}
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
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
 * Implementation of [TinyrssAccountManager] on Android platform
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    76
 */
739
92eb14abed21 app: Few dependecy injection tweaks to prepare to convert test to hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    77
class AndroidTinyrssAccountManager(
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    78
    private val accountManager: AccountManager,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    79
    private val secretCipher: SecretCipher
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    80
) : TinyrssAccountManager {
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
    companion object {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    83
        const val ACCOUNT_TYPE = AccountAuthenticator.TTRSS_ACCOUNT_TYPE
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    84
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    85
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    86
    override fun addAccount(account: Account, password: String): Boolean {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    87
        val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    88
        val userdata = Bundle()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    89
        userdata.putString(AccountAuthenticator.USERDATA_URL, account.url)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    90
        val encryptedPassword = encrypt(password)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    91
        return accountManager.addAccountExplicitly(androidAccount, encryptedPassword, userdata)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    92
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    93
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    94
    override fun updatePassword(account: Account, password: String) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    95
        val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    96
        val encryptedPassword = encrypt(password)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    97
        accountManager.setPassword(androidAccount, encryptedPassword)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    98
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    99
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   100
    override fun initializeAccountSync(account: Account) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
        val extras = Bundle()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   102
        extras.putInt(SyncContract.EXTRA_NUMBER_OF_LATEST_ARTICLES_TO_REFRESH, -1)
498
1bbc2d7d66fb ArticleSynchronizer: only update feed icons on periodic syncs
Da Risk <da_risk@geekorum.com>
parents: 265
diff changeset
   103
        extras.putBoolean(SyncContract.EXTRA_UPDATE_FEED_ICONS, true)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   104
        android.accounts.Account(account.username, ACCOUNT_TYPE).also {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   105
            ContentResolver.setSyncAutomatically(it, ArticlesContract.AUTHORITY, true)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
            ContentResolver.addPeriodicSync(it, ArticlesContract.AUTHORITY, Bundle(),
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   107
                BackgroundJobManager.PERIODIC_REFRESH_JOB_INTERVAL_S)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   108
            ContentResolver.addPeriodicSync(it, ArticlesContract.AUTHORITY, extras,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   109
                BackgroundJobManager.PERIODIC_FULL_REFRESH_JOB_INTERVAL_S)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   110
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   111
    }
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
    @Throws(GeneralSecurityException::class, IllegalArgumentException::class)
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   114
    fun getPassword(account: Account): String? {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   115
        val encryptedPassword: String? = android.accounts.Account(account.username, ACCOUNT_TYPE).let {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   116
            accountManager.getPassword(it)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   117
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   118
        return encryptedPassword?.let { decrypt(it) }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   119
    }
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
    fun fromAndroidAccount(androidAccount: android.accounts.Account): Account {
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   122
        return withStrictMode(StrictMode.allowThreadDiskReads()) {
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   123
            check(ACCOUNT_TYPE == androidAccount.type) { "Invalid account type ${androidAccount.type}" }
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   124
            val url = accountManager.getUserData(androidAccount, AccountAuthenticator.USERDATA_URL)
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   125
            Account(androidAccount.name, url)
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   126
        }
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   127
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   128
712
fc75653c8faf app: Login, save ServerInformation when adding account
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
   129
    override fun updateServerInformation(account: Account, serverInformation: ServerInformation) {
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   130
        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
   131
        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
   132
        accountManager.run {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   133
            setUserData(androidAccount, AccountAuthenticator.USERDATA_URL, serverInformation.apiUrl)
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   134
            setUserData(androidAccount, AccountAuthenticator.USERDATA_BASIC_HTTP_AUTH_USERNAME,
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   135
                serverInformation.basicHttpAuthUsername)
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   136
            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
   137
        }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   138
    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   139
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   140
    fun getServerInformation(account: Account): ServerInformation {
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   141
        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
   142
        return object : ServerInformation() {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   143
            override val apiUrl: String
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   144
                get() = accountManager.getUserData(androidAccount, AccountAuthenticator.USERDATA_URL)
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   145
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   146
            override val basicHttpAuthUsername: String?
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   147
                get() =  accountManager.getUserData(androidAccount,
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   148
                    AccountAuthenticator.USERDATA_BASIC_HTTP_AUTH_USERNAME)
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   149
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   150
            override val basicHttpAuthPassword: String?
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   151
                get() {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   152
                    val encryptedPassword = accountManager.getUserData(androidAccount,
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   153
                        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
   154
                    return try {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   155
                        encryptedPassword?.let { decrypt(it) }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   156
                    } catch (e: Exception) {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   157
                        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
   158
                        null
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   159
                    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   160
                }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   161
        }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   162
    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   163
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 80
diff changeset
   164
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   165
    private fun decrypt(encryptedPassword: String): String {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   166
        val lines = encryptedPassword.lines()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   167
        val encryptedPasswordPart = lines[0]
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   168
        val iv = Base64.decode(lines[1], Base64.NO_WRAP)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   169
        val tlen = lines[2].toInt()
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   170
        val input = Base64.decode(encryptedPasswordPart, Base64.NO_WRAP)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   171
        val gcmParameterSpec = GCMParameterSpec(tlen, iv)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   172
        val output = secretCipher.decrypt(input, gcmParameterSpec)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   173
        return output.toString(Charsets.UTF_8)
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
    private fun encrypt(plaintextPassword: String): String {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   177
        val input = plaintextPassword.toByteArray(Charsets.UTF_8)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   178
        val output = secretCipher.encrypt(input)
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   179
        val base64EncryptedPassword = Base64.encodeToString(output, Base64.NO_WRAP)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   180
        val gcmParameterSpec = secretCipher.parametersSpec
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   181
        val base64IV = Base64.encodeToString(gcmParameterSpec.iv, Base64.NO_WRAP)
163
07a90afdb9db StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   182
        return "$base64EncryptedPassword\n$$base64IV\n${gcmParameterSpec.tLen}\n"
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   183
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   184
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   185