-
Notifications
You must be signed in to change notification settings - Fork 26
feat: [MM-69203] Implement collection, storage and attachment of session attributes #171
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
30 changes: 30 additions & 0 deletions
30
...d/src/main/java/com/mattermost/networkclient/interceptors/SessionAttributesInterceptor.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,30 @@ | ||
| package com.mattermost.networkclient.interceptors | ||
|
|
||
| import com.mattermost.networkclient.sessionattributes.SessionAttributesConstants | ||
| import com.mattermost.networkclient.sessionattributes.SessionAttributesEngine | ||
| import okhttp3.Interceptor | ||
| import okhttp3.Response | ||
| import java.io.IOException | ||
|
|
||
| class SessionAttributesInterceptor(private val serverUrl: String) : Interceptor { | ||
| @Throws(IOException::class) | ||
| override fun intercept(chain: Interceptor.Chain): Response { | ||
| val request = chain.request() | ||
|
|
||
| val hasAuthorization = request.header("Authorization") != null | ||
| val hasSessionAttributes = request.header(SessionAttributesConstants.HEADER_NAME) != null | ||
|
|
||
| if (!hasAuthorization || hasSessionAttributes) { | ||
| return chain.proceed(request) | ||
| } | ||
|
|
||
| val header = SessionAttributesEngine.getOutboundHeader(serverUrl) | ||
| ?: return chain.proceed(request) | ||
|
|
||
| val newRequest = request.newBuilder() | ||
| .header(SessionAttributesConstants.HEADER_NAME, header) | ||
| .build() | ||
|
|
||
| return chain.proceed(newRequest) | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
android/src/main/java/com/mattermost/networkclient/sessionattributes/SessionAttributes.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,24 @@ | ||
| package com.mattermost.networkclient.sessionattributes | ||
|
|
||
| import android.content.Context | ||
|
|
||
| /** | ||
| * React-free entry point for native code outside this library (e.g. app | ||
| * background handlers, standalone OkHttp usage) to resolve the outbound | ||
| * X-MM-Session-Attributes header for a server. | ||
| * | ||
| * Callers that reach this outside the React module (e.g. WorkManager jobs) must | ||
| * call [init] from their Application.onCreate, which runs before any component | ||
| * in the process. | ||
| */ | ||
| object SessionAttributes { | ||
| @JvmStatic | ||
| fun init(context: Context) { | ||
| SessionAttributesEngine.init(context) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun getOutboundHeader(serverUrl: String): String? { | ||
| return SessionAttributesEngine.getOutboundHeader(serverUrl) | ||
| } | ||
| } |
144 changes: 144 additions & 0 deletions
144
...rc/main/java/com/mattermost/networkclient/sessionattributes/SessionAttributesCollector.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,144 @@ | ||
| package com.mattermost.networkclient.sessionattributes | ||
|
|
||
| import android.app.admin.DevicePolicyManager | ||
| import android.content.Context | ||
| import android.content.RestrictionsManager | ||
| import android.net.ConnectivityManager | ||
| import android.net.NetworkCapabilities | ||
| import android.net.Uri | ||
| import android.net.wifi.WifiManager | ||
| import android.os.Build | ||
| import android.util.Log | ||
| import java.net.Inet4Address | ||
| import java.net.NetworkInterface | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| data class NetworkSnapshot( | ||
| val interfaceType: String, | ||
| val ipAddress: String, | ||
| val vpnActive: Boolean, | ||
| val ssid: String, | ||
| ) | ||
|
|
||
| class SessionAttributesCollector( | ||
| private val context: Context, | ||
| private val store: SessionAttributesStore, | ||
| ) { | ||
| private val fqdnCache = ConcurrentHashMap<String, String>() | ||
|
|
||
| fun collect(name: String, serverUrl: String): String { | ||
| store.getStableValue(name)?.let { return it } | ||
|
|
||
| val snapshot = currentNetworkSnapshot() | ||
| return when (name) { | ||
| SessionAttributesConstants.AttributeKey.CLIENT_IP_ADDRESS -> snapshot.ipAddress | ||
| SessionAttributesConstants.AttributeKey.NETWORK_INTERFACE_TYPE -> snapshot.interfaceType | ||
| SessionAttributesConstants.AttributeKey.VPN_ACTIVE -> if (snapshot.vpnActive) "true" else "false" | ||
| SessionAttributesConstants.AttributeKey.SSID -> snapshot.ssid | ||
| SessionAttributesConstants.AttributeKey.MDM_ENROLLED -> if (isMdmEnrolled()) "true" else "false" | ||
| SessionAttributesConstants.AttributeKey.SERVER_FQDN -> getServerFqdn(serverUrl) | ||
| else -> "" | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private fun getServerFqdn(serverUrl: String): String { | ||
| return fqdnCache.computeIfAbsent(serverUrl) { url -> | ||
| try { | ||
| Uri.parse(url).host ?: "" | ||
| } catch (e: Exception) { | ||
| Log.w("NetworkClient", "Failed to resolve server FQDN: ${e.message}") | ||
| "" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun isMdmEnrolled(): Boolean { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| val devicePolicyManager = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as? DevicePolicyManager | ||
| if (devicePolicyManager?.isOrganizationOwnedDeviceWithManagedProfile == true) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| val restrictionsManager = context.getSystemService(Context.RESTRICTIONS_SERVICE) as? RestrictionsManager | ||
| val restrictions = restrictionsManager?.applicationRestrictions | ||
| if (restrictions != null) { | ||
| if (isTruthyManagedFlag(restrictions.get(SessionAttributesConstants.IS_DEVICE_MANAGED_KEY)) || | ||
| isTruthyManagedFlag(restrictions.get(SessionAttributesConstants.IS_SUPERVISED_KEY)) | ||
| ) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| private fun isTruthyManagedFlag(value: Any?): Boolean { | ||
| return when (value) { | ||
| is Boolean -> value | ||
| is Int -> value != 0 | ||
| is String -> { | ||
| val normalized = value.trim().lowercase() | ||
| normalized == "true" || normalized == "1" || normalized == "yes" | ||
| } | ||
| else -> false | ||
| } | ||
| } | ||
|
|
||
| fun currentNetworkSnapshot(): NetworkSnapshot { | ||
| val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
| val activeNetwork = connectivityManager.activeNetwork | ||
| val capabilities = activeNetwork?.let { connectivityManager.getNetworkCapabilities(it) } | ||
|
|
||
| val vpnActive = capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_VPN) == true | ||
| val interfaceType = when { | ||
| vpnActive -> "vpn" | ||
| capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true -> "wifi" | ||
| capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true -> "cellular" | ||
| capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) == true -> "ethernet" | ||
| activeNetwork == null -> "" | ||
| else -> "other" | ||
| } | ||
|
|
||
| val ipAddress = resolveIpAddress() | ||
| val ssid = if (interfaceType == "wifi") resolveSsid() else "" | ||
|
|
||
| return NetworkSnapshot(interfaceType, ipAddress, vpnActive, ssid) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private fun resolveIpAddress(): String { | ||
| return try { | ||
| val interfaces = NetworkInterface.getNetworkInterfaces() | ||
| while (interfaces.hasMoreElements()) { | ||
| val networkInterface = interfaces.nextElement() | ||
| val addresses = networkInterface.inetAddresses | ||
| while (addresses.hasMoreElements()) { | ||
| val address = addresses.nextElement() | ||
| if (!address.isLoopbackAddress && address is Inet4Address) { | ||
| return address.hostAddress ?: "" | ||
| } | ||
| } | ||
| } | ||
| "" | ||
| } catch (e: Exception) { | ||
| Log.w("NetworkClient", "Failed to resolve client IP address: ${e.message}") | ||
| "" | ||
|
devinbinnie marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @Suppress("DEPRECATION") | ||
| private fun resolveSsid(): String { | ||
| return try { | ||
| val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager | ||
| val info = wifiManager.connectionInfo | ||
| val ssid = info?.ssid?.replace("\"", "") ?: "" | ||
| if (ssid == "<unknown ssid>" || ssid == "0x" || ssid == "Wi-Fi" || ssid == "WLAN") { | ||
| "" | ||
| } else { | ||
| ssid | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.w("NetworkClient", "Failed to resolve SSID: ${e.message}") | ||
| "" | ||
|
devinbinnie marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
...rc/main/java/com/mattermost/networkclient/sessionattributes/SessionAttributesConstants.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,24 @@ | ||
| package com.mattermost.networkclient.sessionattributes | ||
|
|
||
| object SessionAttributesConstants { | ||
| const val HEADER_NAME = "X-MM-Session-Attributes" | ||
| const val STORE_PREFIX = "sa_" | ||
| const val STATE_ALIAS_SUFFIX = "SA_STATE" | ||
| const val STABLE_VALUES_ALIAS = "SA_STABLE_VALUES" | ||
| const val IS_DEVICE_MANAGED_KEY = "isDeviceManaged" | ||
| const val IS_SUPERVISED_KEY = "isSupervised" | ||
|
|
||
| object AttributeKey { | ||
| const val VPN_ACTIVE = "vpn_active" | ||
| const val CLIENT_DEVICE_ID = "client_device_id" | ||
| const val CLIENT_IP_ADDRESS = "client_ip_address" | ||
| const val CLIENT_VERSION = "client_version" | ||
| const val JAILBREAK_DETECTED = "jailbreak_detected" | ||
| const val MDM_ENROLLED = "mdm_enrolled" | ||
| const val NETWORK_INTERFACE_TYPE = "network_interface_type" | ||
| const val OS_PLATFORM = "os_platform" | ||
| const val OS_VERSION = "os_version" | ||
| const val SERVER_FQDN = "server_fqdn" | ||
| const val SSID = "ssid" | ||
| } | ||
| } |
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.
nice