Skip to content

Commit

Permalink
feat: liquid deep-links
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed May 14, 2024
1 parent 61b175f commit 354ee32
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="liquid"/>
</intent-filter>
</activity>
</application>

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/foundation/algorand/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package foundation.algorand.demo
import android.R.attr.duration
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.StrictMode
Expand Down Expand Up @@ -102,6 +103,23 @@ class MainActivity : AppCompatActivity() {
fido2Client = Fido2ApiClient(this@MainActivity)
scanner = GmsBarcodeScanning.getClient(this@MainActivity)
executor = ContextCompat.getMainExecutor(this)

// Get Intent Data
val intentUri: Uri? = intent?.data
if (intentUri !== null) {
Log.d(TAG, "Intent Detected: $intentUri")
val msg = AuthMessage.fromUri(intentUri)
viewModel.setMessage(msg)
signalClient = SignalClient(msg.origin, this@MainActivity, httpClient)
lifecycleScope.launch {
if (viewModel.credential.value === null) {
register(msg)
} else {
authenticate(msg, viewModel.credential.value!!)
}
}
}

// View Bindings
binding = ActivityMainBinding.inflate(layoutInflater)
binding.lifecycleOwner = this
Expand Down
42 changes: 27 additions & 15 deletions auth/src/main/java/foundation/algorand/auth/connect/AuthMessage.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package foundation.algorand.auth.connect

import android.net.Uri
import android.util.Log
import com.google.mlkit.vision.barcode.common.Barcode
import org.json.JSONObject
import java.net.URI
import javax.inject.Inject

private fun URI.findParameterValue(parameterName: String): String? {
return query.split('&').map {
private fun Uri.findParameterValue(parameterName: String): String? {
return query?.split('&')?.map {
val parts = it.split('=')
val name = parts.firstOrNull() ?: ""
val value = parts.drop(1).firstOrNull() ?: ""
Pair(name, value)
}.firstOrNull{it.first == parameterName}?.second
}?.firstOrNull{it.first == parameterName}?.second
}
class AuthMessage @Inject constructor(
var origin: String,
Expand All @@ -21,29 +21,41 @@ class AuthMessage @Inject constructor(

companion object {
const val TAG = "connect.Message"
fun fromUri(uri: Uri): AuthMessage {
Log.d(TAG, "fromUri($uri)")
val origin = "https://${uri.host}"
val requestId = uri.findParameterValue("requestId")!!.toDouble()
return AuthMessage(origin, requestId)
}
/**
* Parse the `Barcode`
*
* uses the following url scheme:
* Parse the Uri string
*
* `liquid://<ORIGIN>/?requestId=<REQUEST_ID>`
*/
fun fromBarcode(barcode: Barcode): AuthMessage {
Log.d(TAG, "fromBarcode(${barcode.displayValue})")
val stringContents = barcode.displayValue ?: throw Exception("Barcode does not contain a display value")
fun fromString(stringContents: String): AuthMessage {
Log.d(TAG, "fromString($stringContents)")
if(stringContents.startsWith("liquid://")) {
val uri = URI(stringContents)
val origin = "https://${uri.host}"
val requestId = uri.findParameterValue("requestId")!!.toDouble()
return AuthMessage(origin, requestId)
return fromUri(Uri.parse(stringContents))
} else {
// Fallback to JSON renderer
val json = JSONObject(barcode.displayValue.toString())
val json = JSONObject(stringContents)
val origin = json.get("origin").toString()
val requestId = json.get("requestId").toString().toDouble()
return AuthMessage(origin, requestId)
}
}
/**
* Parse the `Barcode`
*
* uses the following url scheme:
*
* `liquid://<ORIGIN>/?requestId=<REQUEST_ID>`
*/
fun fromBarcode(barcode: Barcode): AuthMessage {
Log.d(TAG, "fromBarcode(${barcode.displayValue})")
val stringContents = barcode.displayValue ?: throw Exception("Barcode does not contain a display value")
return fromString(stringContents)
}
}
fun toJSON() : JSONObject {
val result = JSONObject()
Expand Down

0 comments on commit 354ee32

Please sign in to comment.