-
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
Open
terrerox
wants to merge
4
commits into
feature/PB-5925-kotlin-api-client
Choose a base branch
from
feature/PB-5926-document-query-roots
base: feature/PB-5925-kotlin-api-client
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
08b5cf9
feat(android): show Internxt Drive in SAF picker per session state
terrerox 5fe9130
refactor(auth): replace InternxtAuthCredentialsModule with direct fun…
terrerox 5f9bdb7
feat(auth): include client name and version in authentication credent…
terrerox 0c4e0f3
feat(auth): enhance credential management with error handling and roo…
terrerox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
85 changes: 85 additions & 0 deletions
85
android/app/src/main/java/com/internxt/cloud/auth/InternxtAuthCredentialsModule.kt
This file contains hidden or 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,85 @@ | ||
| 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) { | ||
| val manager = authManager ?: run { | ||
| promise.reject("E_AUTH_UNAVAILABLE", "Encrypted credential storage unavailable") | ||
| return | ||
| } | ||
| 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"), | ||
| ) | ||
| if (!manager.saveCredentials(creds)) { | ||
| promise.reject("E_SAVE_CREDENTIALS", "Failed to persist credentials") | ||
| return | ||
| } | ||
| 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) { | ||
| val manager = authManager ?: run { | ||
| promise.reject("E_AUTH_UNAVAILABLE", "Encrypted credential storage unavailable") | ||
| return | ||
| } | ||
| try { | ||
| if (!manager.clear()) { | ||
| promise.reject("E_CLEAR_CREDENTIALS", "Failed to clear credentials") | ||
| return | ||
| } | ||
| 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" | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
android/app/src/main/java/com/internxt/cloud/auth/InternxtAuthCredentialsPackage.kt
This file contains hidden or 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,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() | ||
| } |
This file contains hidden or 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
110 changes: 110 additions & 0 deletions
110
android/app/src/main/java/com/internxt/cloud/documents/auth/InternxtAuthManager.kt
This file contains hidden or 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,110 @@ | ||
| package com.internxt.cloud.documents.auth | ||
|
|
||
| import android.content.Context | ||
| import android.content.SharedPreferences | ||
| import android.util.Log | ||
| import androidx.security.crypto.EncryptedSharedPreferences | ||
| import androidx.security.crypto.MasterKey | ||
| import com.internxt.cloud.BuildConfig | ||
| import com.internxt.cloud.documents.api.AuthConfig | ||
| import java.io.IOException | ||
| import java.security.GeneralSecurityException | ||
|
|
||
| class InternxtAuthManager(private val prefs: SharedPreferences) { | ||
|
|
||
| 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): Boolean = | ||
| 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) | ||
| .commit() | ||
|
|
||
| fun clear(): Boolean = prefs.edit().clear().commit() | ||
|
|
||
| companion object { | ||
| private const val TAG = "InternxtAuthManager" | ||
| 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? { | ||
| return try { | ||
| 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, | ||
| ) | ||
| InternxtAuthManager(prefs) | ||
| } catch (e: GeneralSecurityException) { | ||
| Log.e(TAG, "Keystore unavailable, SAF auth disabled", e) | ||
| null | ||
| } catch (e: IOException) { | ||
| Log.e(TAG, "Could not open encrypted prefs, SAF auth disabled", e) | ||
| null | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this AuthManager in docuemtns instead of the other auth directory?
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.
Because only the file picker reads these credentials. The auth/ folder is just the JS <-> Android bridge, it writes, but doesn't own the data.