จัดการการออกข้อมูลเข้าสู่ระบบด้วยแอปของผู้ถือ

หากต้องการรับและจัดเก็บข้อมูลเข้าสู่ระบบจากผู้ออก แอปของผู้ถือบัตรต้องจัดการโฟลว์การออก ในขั้นตอนการออกเว็บไซต์หรือแอปของผู้ออกจะส่งข้อเสนอข้อมูลประจำตัวไปยังแอปของผู้ถือ โดยระบุรายละเอียดข้อมูลที่จำเป็นในการจัดสรรข้อมูลประจำตัว แอปผู้ถือใช้ RegistryManager เพื่อลงทะเบียนประเภทข้อมูลเข้าสู่ระบบที่ต้องการจัดการกับ Credential Manager ซึ่งจะช่วยให้แอปแสดงต่อผู้ใช้และผู้ใช้เลือกแอปได้ในระหว่างคำขอออกบัตรเพื่อรับข้อมูลเข้าสู่ระบบ

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีการทำงานของข้อมูลเข้าสู่ระบบกับ Holder API ได้ที่ แนวคิดหลักของ Holder API

ความเข้ากันได้กับเวอร์ชัน Android

API ผู้ถือใช้งานได้ใน Android 6 (ระดับ API 23) ขึ้นไป

การใช้งาน

หากต้องการใช้ Credential Manager Holder API ให้เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในสคริปต์บิลด์ของโมดูลแอป

Groovy

dependencies {
    // Use to implement credentials registrys

    implementation "androidx.credentials.registry:registry-digitalcredentials-mdoc:1.0.0-alpha04"
    implementation "androidx.credentials.registry:registry-digitalcredentials-openid:1.0.0-alpha04"
    implementation "androidx.credentials.registry:registry-digitalcredentials-sdjwtvc:1.0.0-alpha04"
    implementation "androidx.credentials.registry:registry-provider:1.0.0-alpha04"
    implementation "androidx.credentials.registry:registry-provider-play-services:1.0.0-alpha04"

}

Kotlin

dependencies {
    // Use to implement credentials registrys

    implementation("androidx.credentials.registry:registry-digitalcredentials-mdoc:1.0.0-alpha04")
    implementation("androidx.credentials.registry:registry-digitalcredentials-openid:1.0.0-alpha04")
    implementation("androidx.credentials.registry:registry-digitalcredentials-sdjwtvc:1.0.0-alpha04")
    implementation("androidx.credentials.registry:registry-provider:1.0.0-alpha04")
    implementation("androidx.credentials.registry:registry-provider-play-services:1.0.0-alpha04")

}

สร้าง RegistryManager

สร้างอินสแตนซ์ RegistryManager และลงทะเบียนคำขอ RegisterCreationOptionsRequest ด้วยอินสแตนซ์นั้น

val registryManager = RegistryManager.create(context)

try {
    registryManager.registerCreationOptions(object :
        RegisterCreationOptionsRequest(
            creationOptions = buildIssuanceData(),
            matcher = loadIssuanceMatcher(),
            type = DigitalCredential.TYPE_DIGITAL_CREDENTIAL,
            id = "openid4vci",
        ) {}
    )
} catch (e: Exception) {
    Log.e(TAG, "Issuance registration failed.", e)
}

Matcher เป็นไฟล์ไบนารี WebAssembly (Wasm) ที่จะได้รับ creationOptions ที่ตั้งค่าไว้ระหว่างการลงทะเบียนและข้อเสนอข้อมูลเข้าสู่ระบบที่ส่งโดย ผู้ออกเพื่อกำหนดรายการที่แสดงใน UI ของ Credential Manager ดูตัวอย่าง Matcher ได้ในแอปกระเป๋าเงินแบบโอเพนซอร์ส

จัดการคำขอออกบัตร

จากนั้นกระเป๋าเงินต้องจัดการเมื่อผู้ใช้เลือกตัวเลือกการสร้างข้อมูลเข้าสู่ระบบ กำหนดกิจกรรมที่รับฟัง androidx.credentials.registry.provider.action.CREATE_CREDENTIAL ตัวกรอง Intent ตามที่แสดงในกระเป๋าเงินตัวอย่าง

Intent ที่เปิดใช้งานกิจกรรมจะมีคำขอสร้างและต้นทางการเรียก ซึ่งคุณสามารถดึงข้อมูลได้ด้วยฟังก์ชัน PendingIntentHandler.retrieveProviderCreateCredentialRequest API จะแสดง ProviderCreateCredentialRequest ที่มีข้อมูลทั้งหมด ที่เชื่อมโยงกับคำขอสร้าง โดยมีองค์ประกอบสำคัญ 2 อย่าง ได้แก่

  • แอปที่ส่งคำขอ คุณดูข้อมูลนี้ได้โดยใช้ getCallingAppInfo
  • คำขอจากแอปการโทร คุณสามารถดึงข้อมูลนี้ได้ด้วย getCallingRequest ซึ่งจะแสดงผล CreateCredentialRequest หากคำขอ เป็นสำหรับข้อมูลประจำตัวดิจิทัล คำขอจะเป็นอินสแตนซ์ของ CreateDigitalCredentialRequest ซึ่งมี JSON ของคำขอออกในพร็อพเพอร์ตี้ requestJson คุณประมวลผลได้ด้วยโค้ดตัวอย่างต่อไปนี้
val pendingIntentRequest =
    PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent)
val request = pendingIntentRequest!!.callingRequest
if (request is CreateDigitalCredentialRequest) {
    Log.i(TAG, "Got DC creation request: ${request.requestJson}")
    processCreationRequest(request.requestJson)
}

ส่งคืนการตอบกลับการสร้าง

เมื่อกระเป๋าเงินทำตามขั้นตอนที่จำเป็นในการบันทึกข้อมูลเข้าสู่ระบบเสร็จแล้ว ให้ทำกิจกรรมให้เสร็จด้วยการตอบกลับข้อมูลเข้าสู่ระบบ

val resultData = Intent()
PendingIntentHandler.setCreateCredentialResponse(
    resultData,
    CreateDigitalCredentialResponse(response.responseJson)
)
setResult(RESULT_OK, resultData)
finish()

หากมีข้อยกเว้น คุณสามารถส่งข้อยกเว้นของข้อมูลเข้าสู่ระบบได้ในลักษณะเดียวกัน

val resultData = Intent()
PendingIntentHandler.setCreateCredentialException(
    resultData,
    CreateCredentialUnknownException() // Configure the proper exception
)
setResult(RESULT_OK, resultData)
finish()

หากต้องการดูตัวอย่างทั้งหมดของการส่งคืนการตอบกลับของข้อมูลเข้าสู่ระบบในบริบท โปรดดูแอปตัวอย่าง