Coder Social home page Coder Social logo

webauthn-android's Introduction

WebAuthnKit (Android)

Esta biblioteca oferece uma maneira fácil de lidar com a API de Autenticação na Web do W3C (também conhecida como WebAuthN / FIDO 2.0).

Atenção

ESTA VERSÃO AINDA NÃO É ESTÁVEL

Esta biblioteca não funciona como esperado no Android5 atualmente.

Instalação

No build.gradle da sua aplicação:

dependencies {
  implementation 'webauthnkit:webauthnkit-core:0.9.3'
}

pom

<dependency>
  <groupId>webauthnkit</groupId>
  <artifactId>webauthnkit-core</artifactId>
  <version>0.9.3</version>
  <type>pom</type>
</dependency>

Começando

Configuração do AutoBackup

Certifique-se de excluir 'webauthnkit.db'

  • AndroidManifest.xml
<application
        android:fullBackupContent="@xml/backup_rules">
  • values/backup_rules.xml
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <exclude domain="database" path="webauthnkit.db" />
</full-backup-content>

Ou você pode simplesmente definir allowBackup="false".

<application
        android:allowBackup="false">

Atividade

O WebAuthnKit usa recursos experimentais do Kotlin. Portanto, adicione algumas anotações à sua Activity.

FragmentActivity é necessário para ser vinculado aos recursos de UI do WebAuthnKit. Claro, androidx.appcompat.app.AppCompatActivity também está bem.

@ExperimentalCoroutinesApi
@ExperimentalUnsignedTypes
class AuthenticationActivity : AppCompatActivity() {
  //...
}

Configurar seu WebAuthnClient

Primeiro, prepare a UserConsentUI na sua Activity.

import webauthnkit.core.authenticator.internal.ui.UserConsentUI
import webauthnkit.core.authenticator.internal.ui.UserConsentUIFactory

var consentUI: UserConsentUI? = null

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  consentUI = UserConsentUIFactory.create(this)

  // Você pode configurar consent-ui aqui
  // consentUI.config.registrationDialogTitle = "Nova Chave de Login"
  // consentUI.config.selectionDialogTitle = "Selecione a Chave"
  // ...
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  consentUI?.onActivityResult(requestCode, resultCode, data)
}

Então, crie o WebAuthnClient

import webauthnkit.core.client.WebAuthnClient

val client = WebAuthnClient.create(
  activity = this,
  origin   = "https://example.org"
  ui       = consentUI!!
)
// Você pode configurar o cliente aqui
// client.maxTimeout = 120
// client.defaultTimeout = 60

Fluxo de Registro

Com um fluxo que é descrito nos seguintes documentos, o WebAuthnClient cria uma credencial se for bem-sucedido.

private suspend fun executeRegistration() {

    val options = PublicKeyCredentialCreationOptions()

    options.challenge        = ByteArrayUtil.fromHex(challenge)
    options.user.id          = userId
    options.user.name        = username
    options.user.displayName = userDisplayName
    options.user.icon        = userIconURL
    options.rp.id            = "https://example.org"
    options.rp.name          = "nome_do_seu_serviço"
    options.rp.icon          = suaURLdeIconeDoServiço
    options.attestation      = attestationConveyance

    options.addPubKeyCredParam(
        alg = COSEAlgorithmIdentifier.es256
    )

    options.authenticatorSelection = AuthenticatorSelectionCriteria(
        requireResidentKey = true,
        userVerification   = userVerification
    )

    try {

        val credential = client.create(options)

        // enviar parâmetros para seu servidor
        // credential.id
        // credential.rawId
        // credential.response.attestationObject
        // credential.response.clientDataJSON

    } catch (e: Exception) {
        // tratamento de erro
    }

}

Se você quiser parar enquanto o cliente está em andamento, pode chamar o método cancel.

client.cancel()

webauthnkit.core.CancelledException será lançado na sua função suspendida.

Fluxo de Autenticação

Com um fluxo que é descrito nos seguintes documentos, o WebAuthnClient encontra credenciais, permite que o usuário selecione uma (se múltiplas) e assina a resposta com ela.

private suspend fun executeAuthentication() {

    val options = PublicKeyCredentialRequestOptions()

    options.challenge        = ByteArrayUtil.fromHex(challenge)
    options.rpId             = relyingParty
    options.userVerification = userVerification

    if (credId.isNotEmpty()) {
        options.addAllowCredential(
            credentialId = ByteArrayUtil.fromHex(credId),
            transports   = mutableListOf(AuthenticatorTransport.Internal))
    }

    try {

        val assertion = client.get(options)

        // enviar parâmetros para seu servidor
        //assertion.id
        //assertion.rawId
        //assertion.response.authenticatorData
        //assertion.response.signature
        //assertion.response.userHandle
        //assertion.response.clientDataJSON

    } catch (e: Exception) {
        // tratamento de erro
    }

}

Recursos

Ainda não implementado

  • Token Binding
  • Extensões
  • Autenticador BLE
  • Serviço de Roaming BLE
  • Attestation do SafetyNet

Suporte de Algoritmo de Chave

  • ES256

Chave Residente

O InternalAuthenticator força o uso de chave residente.

Attestation

Atualmente, esta biblioteca suporta apenas auto-attestation.

Veja também

Licença

MIT-LICENSE

Autor

Lyo K

webauthn-android's People

Contributors

leocluque avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.