File Coverage

blib/lib/Net/Duo/Auth/Async.pm
Criterion Covered Total %
statement 25 27 92.5
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 38 41 92.6


line stmt bran cond sub pod time code
1             # Class representing an asynchronous Duo authentication.
2             #
3             # This class wraps the transaction ID returned by Duo from an asynchronous
4             # authentication and provides a method to long-poll the status of that
5             # authentication attempt.
6             #
7             # SPDX-License-Identifier: MIT
8              
9             package Net::Duo::Auth::Async 1.02;
10              
11 3     3   65 use 5.014;
  3         12  
12 3     3   15 use strict;
  3         7  
  3         60  
13 3     3   14 use warnings;
  3         5  
  3         98  
14              
15 3     3   17 use Net::Duo;
  3         5  
  3         683  
16              
17             # All dies are of constructed objects, which perlcritic misdiagnoses.
18             ## no critic (ErrorHandling::RequireCarping)
19              
20             # Create a new Net::Duo::Auth::Async object from the transaction ID and a
21             # Net::Duo object.
22             #
23             # $class - Class of object to create
24             # $duo - Net::Duo object to use for calls
25             # $id - The transaction ID of an asynchronous transaction
26             #
27             # Returns: Newly-created object
28             # Throws: Net::Duo::Exception on any problem creating the object
29             sub new {
30 2     2 1 10 my ($class, $duo, $id) = @_;
31 2         9 my $self = { _duo => $duo, id => $id };
32 2         6 bless($self, $class);
33 2         17 return $self;
34             }
35              
36             # Return the transaction ID.
37             #
38             # $self - The Net::Duo::Auth::Async object
39             #
40             # Returns: The underlying transaction ID
41             sub id {
42 3     3 1 1815 my ($self) = @_;
43 3         16 return $self->{id};
44             }
45              
46             # Check on the current status of an asynchronous authentication. This uses
47             # long polling, meaning that the call returns for every status change,
48             # but does not otherwise have a timeout.
49             #
50             # $self - The Net::Duo::Auth::Async object
51             #
52             # Returns: Scalar context: the current status
53             # List context: list of current status and reference to hash of data
54             # Throws: Net::Duo::Exception on failure
55             sub status {
56 2     2 1 5 my ($self) = @_;
57              
58             # Make the Duo call.
59 2         19 my $data = { txid => $self->{id} };
60 2         5 my $uri = '/auth/v2/auth_status';
61 2         8 my $result = $self->{_duo}->call_json('GET', $uri, $data);
62              
63             # Ensure the response included a result field.
64 2 50       8 if (!defined($result->{result})) {
65 0         0 my $error = 'no authentication result from Duo';
66 0         0 die Net::Duo::Exception->protocol($error, $result);
67             }
68 2         15 my $status = $result->{result};
69 2         5 delete $result->{result};
70              
71             # Return the result as appropriate for context.
72 2 100       16 return wantarray ? ($status, $result) : $status;
73             }
74              
75             1;
76             __END__