| 1 | package Concierge::Auth::Generators v0.4.5; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Value generation utilities for Concierge::Auth | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Crypt::PRNG qw/rand random_bytes random_string random_string_from/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 10 | gen_uuid | ||||||||||||||||||||||||||||
| 11 | gen_random_id | ||||||||||||||||||||||||||||
| 12 | gen_random_token | ||||||||||||||||||||||||||||
| 13 | gen_random_string | ||||||||||||||||||||||||||||
| 14 | gen_word_phrase | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 18 | str => [qw/gen_random_string gen_word_phrase/], | ||||||||||||||||||||||||||||
| 19 | rand => [qw/gen_random_id gen_random_token gen_random_string/], | ||||||||||||||||||||||||||||
| 20 | tok => [qw/gen_random_id gen_random_token gen_uuid/], | ||||||||||||||||||||||||||||
| 21 | all => [qw/gen_uuid gen_random_id gen_random_token gen_random_string gen_word_phrase/], | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ## Generator response methods | ||||||||||||||||||||||||||||
| 25 | ## These provide non-fatal error handling in generator functions | ||||||||||||||||||||||||||||
| 26 | ## | ||||||||||||||||||||||||||||
| 27 | ## Usage: | ||||||||||||||||||||||||||||
| 28 | ## return g_success($value, $message) # Success with value | ||||||||||||||||||||||||||||
| 29 | ## return g_error($message) # Failure with undef | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 458 | ▶ | sub g_success { | ||||||||||||||||||||||||||
|
Subroutine
g_success: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 458 | my $value = shift; | |||||||||||||||||||||||||||
| 33 | 458 | ▶ | my $message = shift || "Generation successful."; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 'Generation successful.'
MC/DC100%1/1
shift() || 'Generation successful.'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 34 | 458 | ▶ | wantarray ? ($value, $message) : $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | sub g_error { | ||||||||||||||||||||||||||
|
Subroutine
g_error: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | my $message = shift || "Generation failed."; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'Generation failed.'
MC/DC0%0/1
shift() || 'Generation failed.'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | wantarray ? (undef, $message) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ## gen_uuid: generate a version 4 (random) UUID using Crypt::PRNG | ||||||||||||||||||||||||||||
| 43 | ## Returns: UUID string (e.g., "550e8400-e29b-41d4-a716-446655440000") | ||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub gen_uuid { | ||||||||||||||||||||||||||
|
Subroutine
gen_uuid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 4 | my @bytes = unpack('C*', random_bytes(16)); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Set version 4 (bits 12-15 of time_hi_and_version) | ||||||||||||||||||||||||||||
| 48 | 4 | $bytes[6] = ($bytes[6] & 0x0f) | 0x40; | |||||||||||||||||||||||||||
| 49 | # Set variant 1 (bits 6-7 of clock_seq_hi_and_reserved) | ||||||||||||||||||||||||||||
| 50 | 4 | $bytes[8] = ($bytes[8] & 0x3f) | 0x80; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | my $uuid = sprintf( | |||||||||||||||||||||||||||
| 53 | '%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x', | ||||||||||||||||||||||||||||
| 54 | @bytes | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | 4 | return g_success($uuid, "UUID v4 generated."); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ## gen_random_id: generate a random hex ID from cryptographic random bytes | ||||||||||||||||||||||||||||
| 60 | ## Parameters: byte_length (optional, default 20 = 40 hex chars) | ||||||||||||||||||||||||||||
| 61 | ## Returns: hex-encoded random string | ||||||||||||||||||||||||||||
| 62 | 8 | ▶ | sub gen_random_id { | ||||||||||||||||||||||||||
|
Subroutine
gen_random_id: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 8 | ▶ | my $bytes = shift || 20; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 20
MC/DC100%1/1
shift() || 20
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 64 | 8 | ▶ | $bytes = $bytes =~ /^\d+$/ ? $bytes : 20; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 8 | my $id = unpack('H*', random_bytes($bytes)); | |||||||||||||||||||||||||||
| 67 | 8 | return g_success($id, "Random ID generated ($bytes bytes)."); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | ## gen_random_token: generate random alphanumeric token | ||||||||||||||||||||||||||||
| 71 | ## use random_string() from Crypt::PRNG | ||||||||||||||||||||||||||||
| 72 | ## Parameters: length (optional, default 13) | ||||||||||||||||||||||||||||
| 73 | ## Returns: random string of specified length | ||||||||||||||||||||||||||||
| 74 | 10 | ▶ | sub gen_random_token { | ||||||||||||||||||||||||||
|
Subroutine
gen_random_token: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 10 | ▶ | my $length = shift || 13; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 13
MC/DC100%1/1
shift() || 13
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 76 | 10 | ▶ | $length = $length =~ /^\d+$/ ? $length : 13; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 10 | my $token = random_string($length); | |||||||||||||||||||||||||||
| 79 | 10 | return g_success($token, "Random token generated ($length chars)."); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ## gen_random_string: generate random string from optional charset | ||||||||||||||||||||||||||||
| 83 | ## uses random_string() and random_string_from() from Crypt::PRNG | ||||||||||||||||||||||||||||
| 84 | ## Parameters: length, charset (optional) | ||||||||||||||||||||||||||||
| 85 | ## If charset provided and not empty, uses random_string_from | ||||||||||||||||||||||||||||
| 86 | ## Otherwise uses alphanumeric charset | ||||||||||||||||||||||||||||
| 87 | ## Returns: random string of specified length | ||||||||||||||||||||||||||||
| 88 | 328 | ▶ | sub gen_random_string { | ||||||||||||||||||||||||||
|
Subroutine
gen_random_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 328 | my ($length, $charset) = @_; | |||||||||||||||||||||||||||
| 90 | 328 | ▶ | $length = (defined $length and $length =~ /^\d+$/) ? $length : 13; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $length and $length =~ /^\d+$/u
MC/DC100%2/2
defined $length and $length =~ /^\d+$/u
defined $length
$length =~ /^\d+$/u
Truth table100%3/3
A and B
A= defined $lengthB= $length =~ /^\d+$/u
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 328 | ▶ | my $string = ($charset && $charset !~ /^\s*$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$charset and not $charset =~ /^\s*$/u
MC/DC100%2/2
$charset and not $charset =~ /^\s*$/u
$charset
not $charset =~ /^\s*$/u
Truth table100%3/3
A and not(B)
A= $charsetB= not $charset =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 93 | ? random_string_from($charset, $length) | ||||||||||||||||||||||||||||
| 94 | : random_string($length); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 328 | return g_success($string, "Random string generated ($length chars)."); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ## gen_word_phrase: generate multi-word passphrase from dictionary | ||||||||||||||||||||||||||||
| 100 | ## Parameters: | ||||||||||||||||||||||||||||
| 101 | ## num_words: number of words (default 4) | ||||||||||||||||||||||||||||
| 102 | ## min_chars: minimum word length (default 4) | ||||||||||||||||||||||||||||
| 103 | ## max_chars: maximum word length (default 7) | ||||||||||||||||||||||||||||
| 104 | ## word_sep: separator between words (default '') | ||||||||||||||||||||||||||||
| 105 | ## Returns: passphrase string or fallback random phrase | ||||||||||||||||||||||||||||
| 106 | 106 | ▶ | sub gen_word_phrase { | ||||||||||||||||||||||||||
|
Subroutine
gen_word_phrase: called CC=15 SCAR=30.6
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 106 | ▶ | my $num_words = shift || 4; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 4
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 108 | 106 | ▶ | my $min_chars = shift || 4; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 4
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 109 | 106 | ▶ | my $max_chars = shift || 7; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 7
MC/DC100%1/1
shift() || 7
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 110 | 106 | ▶ | my $word_sep = shift || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 106 | my $word_file = '/usr/share/dict/web2'; | |||||||||||||||||||||||||||
| 113 | 106 | my @wordlist; | |||||||||||||||||||||||||||
| 114 | 106 | my $used_fallback = 0; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 106 | ▶ | if (open my $wfh, "<", $word_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | FILE: while (<$wfh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | my $line = $_; | |||||||||||||||||||||||||||
| 119 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | next unless length($line) > $min_chars - 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | next if length($line) > $max_chars; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | push @wordlist => $line; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | close $wfh; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||
| 127 | # Fallback: generate random "words" | ||||||||||||||||||||||||||||
| 128 | 106 | $used_fallback = 1; | |||||||||||||||||||||||||||
| 129 | 106 | for (1..$num_words) { | |||||||||||||||||||||||||||
| 130 | 322 | my $length = $min_chars + int(rand($max_chars - $min_chars + 1)); | |||||||||||||||||||||||||||
| 131 | 322 | my ($word, $msg) = gen_random_string($length); | |||||||||||||||||||||||||||
| 132 | 322 | push @wordlist => lc $word; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 106 | my $list_size = scalar @wordlist; | |||||||||||||||||||||||||||
| 137 | 106 | ▶ | if ($list_size == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | return g_error("No words available for phrase generation."); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 106 | my @rand; | |||||||||||||||||||||||||||
| 142 | my %seen; | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 106 | ▶ | WORD: while (scalar @rand < $num_words) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 580 | my $num = int(rand($list_size)); | |||||||||||||||||||||||||||
| 146 | 580 | ▶ | next WORD if $seen{$num}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 322 | my $wd = $wordlist[$num]; | |||||||||||||||||||||||||||
| 148 | 322 | ▶ | next WORD if $wd =~ /^[A-Z]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 322 | push @rand => ucfirst $wd; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 106 | my $phrase = join $word_sep => @rand; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 106 | ▶ | my $msg = $used_fallback | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | ? "Word phrase generated (fallback mode)." | ||||||||||||||||||||||||||||
| 156 | : "Word phrase generated from dictionary."; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 106 | return g_success($phrase, $msg); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | ## gen_token: deprecated alias for gen_random_token | ||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub gen_token { | ||||||||||||||||||||||||||
|
Subroutine
gen_token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 1 | goto &gen_random_token; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | ## gen_crypt_token: deprecated, now an alias for gen_random_token | ||||||||||||||||||||||||||||
| 167 | 1 | ▶ | sub gen_crypt_token { | ||||||||||||||||||||||||||
|
Subroutine
gen_crypt_token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 1 | goto &gen_random_token; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 7 | 1; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||