This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store token per server Url and handle migration of the previous scala…
…r token
- Loading branch information
Showing
2 changed files
with
110 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
vector/src/main/java/im/vector/widgets/tokens/TokensStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2019 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.vector.widgets.tokens | ||
|
||
import android.content.Context | ||
import android.support.v7.preference.PreferenceManager | ||
import androidx.core.content.edit | ||
import org.matrix.androidsdk.core.JsonUtils | ||
|
||
class TokensStore(context: Context) { | ||
|
||
private val prefs = PreferenceManager.getDefaultSharedPreferences(context) | ||
private val gson = JsonUtils.getBasicGson() | ||
|
||
private data class TokensStore( | ||
// Keys are user Id | ||
@JvmField | ||
val userToServerTokens: MutableMap<String, ServerTokens> = mutableMapOf() | ||
) | ||
|
||
private data class ServerTokens( | ||
// Keys are server Url, values are token | ||
@JvmField | ||
val serverTokens: MutableMap<String, String> = mutableMapOf() | ||
) | ||
|
||
fun getToken(userId: String, serverUrl: String): String? { | ||
handleMigration(userId) | ||
|
||
return readStore() | ||
.userToServerTokens[userId] | ||
?.serverTokens | ||
?.get(serverUrl) | ||
} | ||
|
||
private fun handleMigration(userId: String) { | ||
val prefKey = SCALAR_TOKEN_LEGACY_PREFERENCE_KEY + userId | ||
|
||
val previousStoredToken = prefs.getString(prefKey, null) | ||
|
||
if (!previousStoredToken.isNullOrBlank()) { | ||
// It was maybe a token for scalar.vector.im. If it is not the case, it will be invalid and will be replaced. | ||
setToken(userId, "https://scalar.vector.im/api", previousStoredToken) | ||
|
||
prefs.edit { | ||
remove(prefKey) | ||
} | ||
} | ||
} | ||
|
||
fun setToken(userId: String, serverUrl: String, token: String) { | ||
readStore() | ||
.apply { | ||
userToServerTokens.getOrPut(userId) { ServerTokens() } | ||
.serverTokens[serverUrl] = token | ||
} | ||
.commit() | ||
} | ||
|
||
private fun readStore(): TokensStore { | ||
return prefs.getString(SCALAR_TOKENS_PREFERENCE_KEY, null) | ||
?.toModel() | ||
?: TokensStore() | ||
} | ||
|
||
private fun TokensStore.commit() { | ||
prefs.edit { | ||
putString(SCALAR_TOKENS_PREFERENCE_KEY, this@commit.fromModel()) | ||
} | ||
} | ||
|
||
fun clear() { | ||
prefs.edit { | ||
remove(SCALAR_TOKENS_PREFERENCE_KEY) | ||
} | ||
} | ||
|
||
private fun String.toModel(): TokensStore? { | ||
return gson.fromJson<TokensStore>(this, TokensStore::class.java) | ||
} | ||
|
||
private fun TokensStore.fromModel(): String? { | ||
return gson.toJson(this) | ||
} | ||
|
||
companion object { | ||
private const val SCALAR_TOKEN_LEGACY_PREFERENCE_KEY = "SCALAR_TOKEN_PREFERENCE_KEY" | ||
|
||
private const val SCALAR_TOKENS_PREFERENCE_KEY = "SCALAR_TOKENS_PREFERENCE_KEY" | ||
} | ||
} |