| 1 | package VisualDreams::Yubikey::online; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | our $VERSION = '0.06'; | |||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Digest::HMAC_SHA1 qw(hmac_sha1); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use LWP::Simple; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use MIME::Base64; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use URI::Escape; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | =head1 NAME | ||||||||||||
| 13 | |||||||||||||
| 14 | VisualDreams::Yubikey::online - Yubikey online authentication | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||
| 17 | |||||||||||||
| 18 | This module will authenticate with the online Yubico API authentication server and return its results | ||||||||||||
| 19 | in a HASH. This module is to be implemented in the VisualDreams engine. | ||||||||||||
| 20 | |||||||||||||
| 21 | =head1 VERSION | ||||||||||||
| 22 | |||||||||||||
| 23 | Version 0.06 | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||
| 26 | |||||||||||||
| 27 | use VisualDreams::Yubikey::online; | ||||||||||||
| 28 | |||||||||||||
| 29 | my $yubify = VisualDreams::Yubikey::online->new("ID","Base64 encoded API key"); | ||||||||||||
| 30 | |||||||||||||
| 31 | my $yubirecord = $yubify->verifyOnline($otp); | ||||||||||||
| 32 | |||||||||||||
| 33 | my $url = $yubify->createUrl($otp); | ||||||||||||
| 34 | my $signedurl = $yubify->createSignedUrl($otp); | ||||||||||||
| 35 | my $signUrl = $yubify->signUrl($url); | ||||||||||||
| 36 | |||||||||||||
| 37 | =head1 FUNCTIONS | ||||||||||||
| 38 | |||||||||||||
| 39 | =head2 new | ||||||||||||
| 40 | |||||||||||||
| 41 | Input : API-ID and API-KEY | ||||||||||||
| 42 | |||||||||||||
| 43 | Initializes the module to standard API-ID, API-KEY and API-URL. | ||||||||||||
| 44 | WARNING: Signed messages will automagically work with a valid Base-64 API key! | ||||||||||||
| 45 | |||||||||||||
| 46 | =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 49 | 0 | my $class = shift; | |||||||||||
| 50 | 0 | ▶ | my $apiId = shift || die("Need atleast an Yubikey API ID"); | ||||||||||
|
Condition0%0/2
shift() || die('Need atleast an Yubikey API ID')
MC/DC0%0/1
shift() || die('Need atleast an Yubikey API ID')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 51 | 0 | ▶ | my $apiKey = shift || ""; | ||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 52 | 0 | ▶ | my $apiUrl = shift || 'http://api.yubico.com/wsapi/verify?'; | ||||||||||
|
Condition0%0/2
shift() || 'http://api.yubico.com/wsapi/verify?'
MC/DC0%0/1
shift() || 'http://api.yubico.com/wsapi/verify?'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 53 | 0 | my $self = { apiId => $apiId, | |||||||||||
| 54 | apiKey => $apiKey, | ||||||||||||
| 55 | apiUrl => $apiUrl, | ||||||||||||
| 56 | }; | ||||||||||||
| 57 | |||||||||||||
| 58 | 0 | bless $self, $class; | |||||||||||
| 59 | 0 | return $self; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 signUrl | ||||||||||||
| 63 | |||||||||||||
| 64 | Signs the URL with API-ID, API-KEY and incoming url | ||||||||||||
| 65 | |||||||||||||
| 66 | Input : url | ||||||||||||
| 67 | Output : signed base-64 encoded hmac | ||||||||||||
| 68 | |||||||||||||
| 69 | =cut | ||||||||||||
| 70 | |||||||||||||
| 71 | 0 | ▶ | sub signUrl($) { | ||||||||||
|
Subroutine
signUrl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||
| 73 | 0 | my $message = shift; | |||||||||||
| 74 | 0 | my $apiKeyDecoded = MIME::Base64::decode($self->{apiKey}); | |||||||||||
| 75 | 0 | my $hmac = hmac_sha1($message, $apiKeyDecoded); | |||||||||||
| 76 | 0 | my $signature = uri_escape(MIME::Base64::encode($hmac),"\x2b"); | |||||||||||
| 77 | 0 | return($signature); | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | =head2 createUrl | ||||||||||||
| 81 | |||||||||||||
| 82 | Create standard URL with API-ID and OTP | ||||||||||||
| 83 | |||||||||||||
| 84 | Input : otp | ||||||||||||
| 85 | Output : piece of url containing id and otp | ||||||||||||
| 86 | |||||||||||||
| 87 | =cut | ||||||||||||
| 88 | |||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | sub createUrl { | ||||||||||
|
Subroutine
createUrl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 91 | 0 | my $self = shift; | |||||||||||
| 92 | 0 | my $otp = shift; | |||||||||||
| 93 | 0 | my $message = "id=$self->{apiId}&otp=$otp"; | |||||||||||
| 94 | 0 | return($message); | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | =head2 createSignedUrl | ||||||||||||
| 98 | |||||||||||||
| 99 | Create Signed URL with API-ID, API-KEY and OTP | ||||||||||||
| 100 | |||||||||||||
| 101 | Input : otp | ||||||||||||
| 102 | Output : piece of url containing id, otp and base-64 hmac signature | ||||||||||||
| 103 | |||||||||||||
| 104 | =cut | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | sub createSignedUrl { | ||||||||||
|
Subroutine
createSignedUrl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||
| 108 | 0 | my $otp = shift; | |||||||||||
| 109 | 0 | my $message = $self->createUrl($otp); | |||||||||||
| 110 | 0 | my $apiSignature = $self->signUrl($message); | |||||||||||
| 111 | 0 | $message .= "&h=$apiSignature"; | |||||||||||
| 112 | 0 | return($message); | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 0 | ▶ | sub returnYubiField { | ||||||||||
|
Subroutine
returnYubiField: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||
| 117 | 0 | my $content = shift; | |||||||||||
| 118 | 0 | my $identifier = shift; | |||||||||||
| 119 | 0 | chomp($content); | |||||||||||
| 120 | 0 | ▶ | if ($content =~ m/$identifier=(.*)/i) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 0 | my $return = $1; | |||||||||||
| 122 | 0 | return($return); | |||||||||||
| 123 | } | ||||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | =head2 verifyOnline | ||||||||||||
| 127 | |||||||||||||
| 128 | Verify the OTP with the Yubico server | ||||||||||||
| 129 | |||||||||||||
| 130 | Input : otp | ||||||||||||
| 131 | Output : record with hmac, status and timestamp | ||||||||||||
| 132 | |||||||||||||
| 133 | =cut | ||||||||||||
| 134 | |||||||||||||
| 135 | |||||||||||||
| 136 | 0 | ▶ | sub verifyOnline { | ||||||||||
|
Subroutine
verifyOnline: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||
| 138 | 0 | my $otp = shift; | |||||||||||
| 139 | 0 | my $signed; | |||||||||||
| 140 | 0 | my $url = $self->{apiUrl}; | |||||||||||
| 141 | 0 | my $yubirecord; | |||||||||||
| 142 | my $errorstatus; | ||||||||||||
| 143 | |||||||||||||
| 144 | 0 | ▶ | if ($self->{apiKey}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 0 | $url .= $self->createSignedUrl($otp); | |||||||||||
| 146 | } else { | ||||||||||||
| 147 | 0 | $url .= $self->createUrl($otp); | |||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 0 | my $content = get($url); | |||||||||||
| 151 | |||||||||||||
| 152 | # offer support for more responses later on! | ||||||||||||
| 153 | |||||||||||||
| 154 | 0 | ▶ | if ($content) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | 0 | my %yubiResponses = ("hmac", "h", | |||||||||||
| 156 | "time", "t", | ||||||||||||
| 157 | "status", "status"); | ||||||||||||
| 158 | |||||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | while (my ($humanIdentifier, $yubIdentifier) = each(%yubiResponses)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 161 | 0 | $yubirecord->{$humanIdentifier} = $self->returnYubiField($content,$yubIdentifier); | |||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | } else { | ||||||||||||
| 165 | 0 | $yubirecord->{status} = "Could not retrieve status from Yubico"; | |||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | 0 | return($yubirecord); | |||||||||||
| 169 | } | ||||||||||||
| 170 | |||||||||||||
| 171 | |||||||||||||
| 172 | |||||||||||||
| 173 | 1 | 1; | |||||||||||
| 174 | __END__ | ||||||||||||