-
Notifications
You must be signed in to change notification settings - Fork 18
[PB-5926]: show Internxt Drive in SAF picker per session state #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/PB-5925-kotlin-api-client
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.internxt.cloud.auth | ||
|
|
||
| import android.provider.DocumentsContract | ||
| import com.facebook.react.bridge.Promise | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
| import com.facebook.react.bridge.ReactMethod | ||
| import com.facebook.react.bridge.ReadableMap | ||
| import com.internxt.cloud.documents.InternxtDocumentsProvider | ||
| import com.internxt.cloud.documents.auth.InternxtAuthManager | ||
|
|
||
| class InternxtAuthCredentialsModule(private val ctx: ReactApplicationContext) : | ||
| ReactContextBaseJavaModule(ctx) { | ||
|
|
||
| private val authManager by lazy { InternxtAuthManager.create(ctx.applicationContext) } | ||
|
|
||
| override fun getName() = MODULE_NAME | ||
|
|
||
| @ReactMethod | ||
| fun setCredentials(map: ReadableMap, promise: Promise) { | ||
| try { | ||
| val creds = InternxtAuthManager.Credentials( | ||
| bearerToken = map.requireString("bearerToken"), | ||
| userId = map.requireString("userId"), | ||
| bridgeUser = map.requireString("bridgeUser"), | ||
| rootFolderUuid = map.requireString("rootFolderUuid"), | ||
| email = map.optString("email"), | ||
| driveBaseUrl = map.requireString("driveBaseUrl"), | ||
| bridgeBaseUrl = map.requireString("bridgeBaseUrl"), | ||
| desktopToken = map.optString("desktopToken"), | ||
| ) | ||
| authManager.saveCredentials(creds) | ||
| notifyRootsChanged() | ||
| promise.resolve(null) | ||
| } catch (e: IllegalArgumentException) { | ||
| promise.reject("E_MISSING_FIELD", e.message, e) | ||
| } catch (e: Exception) { | ||
| promise.reject("E_SAVE_CREDENTIALS", e.message, e) | ||
| } | ||
| } | ||
|
|
||
| @ReactMethod | ||
| fun clearCredentials(promise: Promise) { | ||
| try { | ||
| authManager.clear() | ||
| notifyRootsChanged() | ||
| promise.resolve(null) | ||
| } catch (e: Exception) { | ||
| promise.reject("E_CLEAR_CREDENTIALS", e.message, e) | ||
| } | ||
| } | ||
|
|
||
| private fun notifyRootsChanged() { | ||
| ctx.contentResolver.notifyChange( | ||
| DocumentsContract.buildRootsUri(InternxtDocumentsProvider.AUTHORITY), | ||
| null, | ||
| ) | ||
| } | ||
|
|
||
| private fun ReadableMap.nonBlankString(key: String): String? = | ||
| if (hasKey(key) && !isNull(key)) getString(key)?.takeIf { it.isNotBlank() } else null | ||
|
|
||
| private fun ReadableMap.requireString(key: String): String = | ||
| nonBlankString(key) ?: throw IllegalArgumentException("Missing or blank credential field: $key") | ||
|
|
||
| private fun ReadableMap.optString(key: String): String? = nonBlankString(key) | ||
|
|
||
| companion object { | ||
| const val MODULE_NAME = "InternxtAuthCredentialsModule" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.internxt.cloud.auth | ||
|
|
||
| import com.facebook.react.ReactPackage | ||
| import com.facebook.react.bridge.NativeModule | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.uimanager.ViewManager | ||
|
|
||
| class InternxtAuthCredentialsPackage : ReactPackage { | ||
| override fun createNativeModules(context: ReactApplicationContext): List<NativeModule> = | ||
| listOf(InternxtAuthCredentialsModule(context)) | ||
|
|
||
| override fun createViewManagers(context: ReactApplicationContext): List<ViewManager<*, *>> = | ||
| emptyList() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package com.internxt.cloud.documents.auth | ||
|
|
||
| import android.content.Context | ||
| import android.content.SharedPreferences | ||
| import androidx.security.crypto.EncryptedSharedPreferences | ||
| import androidx.security.crypto.MasterKey | ||
| import com.internxt.cloud.BuildConfig | ||
| import com.internxt.cloud.documents.api.AuthConfig | ||
|
|
||
| class InternxtAuthManager(private val prefs: SharedPreferences) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this AuthManager in docuemtns instead of the other auth directory? |
||
|
|
||
| data class Credentials( | ||
| val bearerToken: String, | ||
| val userId: String, | ||
| val bridgeUser: String, | ||
| val rootFolderUuid: String, | ||
| val email: String?, | ||
| val driveBaseUrl: String, | ||
| val bridgeBaseUrl: String, | ||
| val desktopToken: String?, | ||
| ) | ||
|
|
||
| fun isLoggedIn(): Boolean = | ||
| REQUIRED_KEYS.all { !prefs.getString(it, null).isNullOrBlank() } | ||
|
|
||
| fun rootFolderUuid(): String? = prefs.getString(KEY_ROOT_FOLDER_UUID, null)?.takeIf { it.isNotBlank() } | ||
|
|
||
| fun authenticatedRootUuid(): String? = if (isLoggedIn()) rootFolderUuid() else null | ||
|
|
||
| fun userEmail(): String? = prefs.getString(KEY_EMAIL, null)?.takeIf { it.isNotBlank() } | ||
|
|
||
| fun loadAuthConfig(): AuthConfig? { | ||
| if (!isLoggedIn()) return null | ||
| return AuthConfig( | ||
| driveBaseUrl = required(KEY_DRIVE_BASE_URL), | ||
| bridgeBaseUrl = required(KEY_BRIDGE_BASE_URL), | ||
| bearerToken = required(KEY_BEARER_TOKEN), | ||
| bridgeUser = required(KEY_BRIDGE_USER), | ||
| userId = required(KEY_USER_ID), | ||
| clientName = BuildConfig.INTERNXT_CLIENT_NAME, | ||
| clientVersion = BuildConfig.INTERNXT_CLIENT_VERSION, | ||
| desktopToken = prefs.getString(KEY_DESKTOP_TOKEN, null)?.takeIf { it.isNotBlank() }, | ||
| ) | ||
| } | ||
|
|
||
| private fun required(key: String): String = | ||
| prefs.getString(key, null) ?: error("$key missing after isLoggedIn() returned true") | ||
|
|
||
| fun saveCredentials(creds: Credentials) { | ||
| prefs.edit() | ||
| .putString(KEY_BEARER_TOKEN, creds.bearerToken) | ||
| .putString(KEY_USER_ID, creds.userId) | ||
| .putString(KEY_BRIDGE_USER, creds.bridgeUser) | ||
| .putString(KEY_ROOT_FOLDER_UUID, creds.rootFolderUuid) | ||
| .putString(KEY_EMAIL, creds.email) | ||
| .putString(KEY_DRIVE_BASE_URL, creds.driveBaseUrl) | ||
| .putString(KEY_BRIDGE_BASE_URL, creds.bridgeBaseUrl) | ||
| .putString(KEY_DESKTOP_TOKEN, creds.desktopToken) | ||
| .apply() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apply is async, maybe for this it is preferable use commit |
||
| } | ||
|
|
||
| fun clear() { | ||
| prefs.edit().clear().apply() | ||
| } | ||
|
|
||
| companion object { | ||
| private const val PREFS_FILE = "internxt_documents_auth" | ||
|
|
||
| private const val KEY_BEARER_TOKEN = "bearerToken" | ||
| private const val KEY_USER_ID = "userId" | ||
| private const val KEY_BRIDGE_USER = "bridgeUser" | ||
| private const val KEY_ROOT_FOLDER_UUID = "rootFolderUuid" | ||
| private const val KEY_EMAIL = "email" | ||
| private const val KEY_DRIVE_BASE_URL = "driveBaseUrl" | ||
| private const val KEY_BRIDGE_BASE_URL = "bridgeBaseUrl" | ||
| private const val KEY_DESKTOP_TOKEN = "desktopToken" | ||
|
|
||
| private val REQUIRED_KEYS = listOf( | ||
| KEY_BEARER_TOKEN, | ||
| KEY_USER_ID, | ||
| KEY_BRIDGE_USER, | ||
| KEY_ROOT_FOLDER_UUID, | ||
| KEY_DRIVE_BASE_URL, | ||
| KEY_BRIDGE_BASE_URL, | ||
| ) | ||
|
|
||
| fun create(context: Context): InternxtAuthManager { | ||
| val masterKey = MasterKey.Builder(context) | ||
| .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) | ||
| .build() | ||
| val prefs = EncryptedSharedPreferences.create( | ||
| context, | ||
| PREFS_FILE, | ||
| masterKey, | ||
| EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
| EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, | ||
| ) | ||
| return InternxtAuthManager(prefs) | ||
| } | ||
|
Comment on lines
+87
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that is recommendable to wrap this logic in a try/catch and return null on failure so the provider degrades gracefully instead of crashing If implement something like the example, then we need to handle the nullable return in |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be carefull with not null assertion operator, I think this logic could lead in a unexpected bugs, better return false if there are no context