Source Code

class MainActivity : AppCompatActivity() {
    companion object {
        private const val REQUEST_CODE = 1234
    }

    private lateinit var smartAuth: CafSmartAuth
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)
        setupCafSmartAuth()

        binding.verifyPolicyButton.setOnClickListener { view ->
            smartAuth.verifyPolicy(
                "userDocument",
                "myPolicyId",
                setupListener()
            )
        }
    }

    override fun onResume() {
        super.onResume()
        checkPermissionStatus()
    }

    private fun setupCafSmartAuth() {
        smartAuth = CafSmartAuth.CafBuilder("myMFAToken", this)
            .setStage(CafStage.PROD)
            .setEmailUrl("myEmailUrl") // Not mandatory
            .setPhoneUrl("myPhoneUrl") // Not mandatory
            .setFaceAuthenticatorSettings(
                CafFaceAuthenticatorSettings(
                    "myFaceAuthToken",
                    false,
                    true,
                    CafFilterStyle.LINE_DRAWING
                )
            ).build()
    }

    private fun setupListener() = object : CafVerifyPolicyListener {
        override fun onSuccess(
            isAuthorized: Boolean,
            attemptId: String?,
            attestation: String?
        ) {
            println("CafVerifyPolicyListener.onSuccess isAuthorized: $isAuthorized")
            println("CafVerifyPolicyListener.onSuccess attemptId: $attemptId")
            println("CafVerifyPolicyListener.onSuccess attestation: $attestation")
        }

        override fun onPending(isAuthorized: Boolean, attestation: String) {
            println("CafVerifyPolicyListener.onPending isAuthorized: $isAuthorized")
            println("CafVerifyPolicyListener.onPending attestation: $attestation")
        }

        override fun onError(failure: CafFailure) {
            println("CafVerifyPolicyListener.onError failure: ${failure.message}")
        }

        override fun onCancel() {
            println("CafVerifyPolicyListener.onCancel")
        }

        override fun onLoading() {
            println("CafVerifyPolicyListener.onLoading")
        }

        override fun onLoaded() {
            println("CafVerifyPolicyListener.onLoaded")
        }
    }

    private fun checkPermissionStatus() {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                REQUEST_CODE
            )
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                REQUEST_CODE
            )
        }
    }

        override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                // Permission was granted.
            } else {
                // Permission was denied.
            }
        }
    }
}

Last updated

Logo

2023 © Caf. - All rights reserved