| 1 | package Vigil::Token; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Bytes::Random::Secure qw(random_bytes); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Math::BigInt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | our $VERSION = '2.0.0'; | |||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | sub new { bless {}, shift } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | #Token of digits only | ||||||||||
| 15 | 1 | ▶ | sub otp { | ||||||||
|
Subroutine
otp: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 16 | 1 | my ($self) = @_; | |||||||||
| 17 | |||||||||||
| 18 | 1 | my $min = 100_000; | |||||||||
| 19 | 1 | my $max = 999_999; | |||||||||
| 20 | 1 | my $range = $max - $min + 1; | |||||||||
| 21 | |||||||||||
| 22 | 1 | my $num; | |||||||||
| 23 | 1 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | my $bytes = random_bytes(4); # 32-bit random | |||||||||
| 25 | 1 | $num = unpack("N", $bytes); # big-endian 32-bit integer | |||||||||
| 26 | } while ($num >= int(2**32 / $range) * $range); # rejection sampling | ||||||||||
| 27 | |||||||||||
| 28 | 1 | return $num % $range + $min; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 2 | ▶ | sub custom_token { | ||||||||
|
Subroutine
custom_token: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 32 | 2 | my ($self, $length) = @_; | |||||||||
| 33 | 2 | ▶ | die "custom_token requires a positive length" unless $length > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | # Compute bytes needed to get at least $length Base64 chars | ||||||||||
| 35 | 2 | my $num_bytes = int(($length * 6 + 7) / 8); # ceil(length*6/8) | |||||||||
| 36 | 2 | my $bytes = random_bytes($num_bytes); | |||||||||
| 37 | 2 | my $token = encode_base64($bytes, ''); | |||||||||
| 38 | 2 | $token =~ tr[+/][-_]; # Base64URL | |||||||||
| 39 | 2 | $token =~ s/=+$//; # remove padding | |||||||||
| 40 | 2 | return substr($token, 0, $length); # exact length | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | use overload | ||||||||||
| 44 | 2 | ▶ | '&{}' => \&as_coderef, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 2 | fallback => 1; | |||||||||
| 46 | |||||||||||
| 47 | 1 | ▶ | sub as_coderef { | ||||||||
|
Subroutine
as_coderef: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 48 | 1 | my ($self) = @_; | |||||||||
| 49 | 1 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | my @args = @_; | |||||||||
| 51 | 1 | return $self->custom_token(@args); | |||||||||
| 52 | 1 | }; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | 1; | |||||||||
| 56 | |||||||||||
| 57 | __END__ | ||||||||||