| 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-- |
|
104
8b4047ebec59
Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents:
80
diff
changeset
|
1 |
/* |
| 0 | 2 |
* Geekttrss is a RSS feed reader application on the Android Platform. |
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 | 5 |
* |
6 |
* This file is part of Geekttrss. |
|
7 |
* |
|
8 |
* Geekttrss is free software: you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation, either version 3 of the License, or |
|
11 |
* (at your option) any later version. |
|
12 |
* |
|
13 |
* Geekttrss is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with Geekttrss. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
*/ |
|
21 |
package com.geekorum.ttrss.accounts |
|
22 |
||
23 |
import android.accounts.AccountManager |
|
24 |
import android.content.ContentResolver |
|
25 |
import android.os.Bundle |
|
26 |
import android.util.Base64 |
|
27 |
import com.geekorum.geekdroid.security.SecretCipher |
|
28 |
import com.geekorum.ttrss.BackgroundJobManager |
|
29 |
import com.geekorum.ttrss.providers.ArticlesContract |
|
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 | 32 |
import java.security.GeneralSecurityException |
33 |
import javax.crypto.spec.GCMParameterSpec |
|
34 |
import javax.inject.Inject |
|
35 |
||
36 |
/** |
|
37 |
* Represents a Tinyrss account |
|
38 |
*/ |
|
39 |
data class Account(val username: String, val url: String) |
|
40 |
||
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 | 51 |
* API of the AccountManager for Tinyrss |
52 |
*/ |
|
53 |
interface TinyrssAccountManager {
|
|
54 |
||
55 |
/** |
|
56 |
* Add an account. |
|
57 |
* @return true on success |
|
58 |
*/ |
|
59 |
fun addAccount(account: Account, password: String): Boolean |
|
60 |
||
61 |
/** |
|
62 |
* Initialize synchronisation jobs for an account. |
|
63 |
*/ |
|
64 |
fun initializeAccountSync(account: Account) |
|
65 |
||
66 |
fun updatePassword(account: Account, password: String) |
|
67 |
} |
|
68 |
||
69 |
||
70 |
/** |
|
71 |
* Implementation of [TinyrssAccountManager] on Android platform |
|
72 |
*/ |
|
73 |
class AndroidTinyrssAccountManager @Inject constructor( |
|
74 |
private val accountManager: AccountManager, |
|
75 |
private val secretCipher: SecretCipher |
|
76 |
) : TinyrssAccountManager {
|
|
77 |
||
78 |
companion object {
|
|
79 |
const val ACCOUNT_TYPE = AccountAuthenticator.TTRSS_ACCOUNT_TYPE |
|
80 |
} |
|
81 |
||
82 |
override fun addAccount(account: Account, password: String): Boolean {
|
|
83 |
val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE) |
|
84 |
val userdata = Bundle() |
|
85 |
userdata.putString(AccountAuthenticator.USERDATA_URL, account.url) |
|
86 |
val encryptedPassword = encrypt(password) |
|
87 |
return accountManager.addAccountExplicitly(androidAccount, encryptedPassword, userdata) |
|
88 |
} |
|
89 |
||
90 |
override fun updatePassword(account: Account, password: String) {
|
|
91 |
val androidAccount = android.accounts.Account(account.username, ACCOUNT_TYPE) |
|
92 |
val encryptedPassword = encrypt(password) |
|
93 |
accountManager.setPassword(androidAccount, encryptedPassword) |
|
94 |
} |
|
95 |
||
96 |
override fun initializeAccountSync(account: Account) {
|
|
97 |
val extras = Bundle() |
|
98 |
extras.putInt(SyncContract.EXTRA_NUMBER_OF_LATEST_ARTICLES_TO_REFRESH, -1) |
|
99 |
android.accounts.Account(account.username, ACCOUNT_TYPE).also {
|
|
100 |
ContentResolver.setSyncAutomatically(it, ArticlesContract.AUTHORITY, true) |
|
101 |
ContentResolver.addPeriodicSync(it, ArticlesContract.AUTHORITY, Bundle(), |
|
102 |
BackgroundJobManager.PERIODIC_REFRESH_JOB_INTERVAL_S) |
|
103 |
ContentResolver.addPeriodicSync(it, ArticlesContract.AUTHORITY, extras, |
|
104 |
BackgroundJobManager.PERIODIC_FULL_REFRESH_JOB_INTERVAL_S) |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
@Throws(GeneralSecurityException::class, IllegalArgumentException::class) |
|
109 |
fun getPassword(account: Account) : String? {
|
|
110 |
val encryptedPassword: String? = android.accounts.Account(account.username, ACCOUNT_TYPE).let {
|
|
111 |
accountManager.getPassword(it) |
|
112 |
} |
|
113 |
return encryptedPassword?.let { decrypt(it) }
|
|
114 |
} |
|
115 |
||
116 |
fun fromAndroidAccount(androidAccount: android.accounts.Account): Account {
|
|
117 |
check(ACCOUNT_TYPE == androidAccount.type) {"Invalid account type ${androidAccount.type}"}
|
|
118 |
val url = accountManager.getUserData(androidAccount, AccountAuthenticator.USERDATA_URL) |
|
119 |
return Account(androidAccount.name, url) |
|
120 |
} |
|
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 | 156 |
private fun decrypt(encryptedPassword: String): String {
|
157 |
val lines = encryptedPassword.lines() |
|
158 |
val encryptedPasswordPart = lines[0] |
|
| 80 | 159 |
val iv = Base64.decode(lines[1], Base64.NO_WRAP) |
| 0 | 160 |
val tlen = lines[2].toInt() |
| 80 | 161 |
val input = Base64.decode(encryptedPasswordPart, Base64.NO_WRAP) |
| 0 | 162 |
val gcmParameterSpec = GCMParameterSpec(tlen, iv) |
163 |
val output = secretCipher.decrypt(input, gcmParameterSpec) |
|
164 |
return output.toString(Charsets.UTF_8) |
|
165 |
} |
|
166 |
||
167 |
private fun encrypt(plaintextPassword: String): String {
|
|
168 |
val input = plaintextPassword.toByteArray(Charsets.UTF_8) |
|
169 |
val output = secretCipher.encrypt(input) |
|
| 80 | 170 |
val base64EncryptedPassword = Base64.encodeToString(output, Base64.NO_WRAP) |
| 0 | 171 |
val gcmParameterSpec = secretCipher.parametersSpec |
| 80 | 172 |
val base64IV = Base64.encodeToString(gcmParameterSpec.iv, Base64.NO_WRAP) |
173 |
return "${base64EncryptedPassword}\n$${base64IV}\n${gcmParameterSpec.tLen}\n"
|
|
| 0 | 174 |
} |
175 |
} |
|
176 |