Authen-U2F-Tester

 view release on metacpan or  search on metacpan

lib/Authen/U2F/Tester.pm  view on Meta::CPAN

#
# This file is part of Authen-U2F-Tester
#
# This software is copyright (c) 2017 by Michael Schout.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#
package Authen::U2F::Tester;
$Authen::U2F::Tester::VERSION = '0.03';
# ABSTRACT: FIDO/U2F Authentication Test Client

use Moose;

use strictures 2;
use Authen::U2F::Tester::Const qw(OK DEVICE_INELIGIBLE);
use Authen::U2F::Tester::Error;
use Authen::U2F::Tester::Keypair;
use Authen::U2F::Tester::RegisterResponse;
use Authen::U2F::Tester::SignResponse;
use Crypt::OpenSSL::X509;
use Crypt::PK::ECC;
use Digest::SHA qw(sha256);
use JSON::MaybeXS qw(encode_json);
use List::Util qw(first);
use MIME::Base64 qw(encode_base64url decode_base64url);
use namespace::autoclean;

my $COUNTER = 0;


has key => (
    is       => 'ro',
    isa      => 'Crypt::PK::ECC',
    required => 1);


has keystore => (
    is       => 'ro',
    does     => 'Authen::U2F::Tester::Role::Keystore',
    required => 1);


has certificate => (
    is       => 'ro',
    isa      => 'Crypt::OpenSSL::X509',
    required => 1);

around BUILDARGS => sub {
    my ($orig, $self) = splice @_, 0, 2;

    if (@_ > 1) {
        my %args = @_;

        if (my $keyfile = delete $args{key_file}) {
            $args{key} = Crypt::PK::ECC->new($keyfile);
        }

        if (my $certfile = delete $args{cert_file}) {
            $args{certificate} = Crypt::OpenSSL::X509->new_from_file($certfile);
        }

        # if no keystore was given, use the wrapped keystore
        unless (defined $args{keystore}) {
            require Authen::U2F::Tester::Keystore::Wrapped;
            $args{keystore} = Authen::U2F::Tester::Keystore::Wrapped->new(key => $args{key});
        }

        return $self->$orig(%args);
    }
    else {
        return $self->$orig(@_);
    }
};


sub register {
    my ($self, $app_id, $challenge, @registered_handles) = @_;

    # check if this device has already been registered
    for my $registered (@registered_handles) {
        if ($self->keystore->exists($registered)) {
            return Authen::U2F::Tester::Error->new(DEVICE_INELIGIBLE);
        }
    }

    # generate a new keypair for this application



( run in 1.969 second using v1.01-cache-2.11-cpan-98e64b0badf )