| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Util::RandomString; |
|
2
|
2
|
|
|
2
|
|
2957
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
16
|
|
|
3
|
2
|
|
|
2
|
|
1601
|
use Session::Token; |
|
|
2
|
|
|
|
|
2554
|
|
|
|
2
|
|
|
|
|
2426
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.08_1'; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our (%generator, %setting, %default, %param); |
|
8
|
|
|
|
|
|
|
our $read_config; |
|
9
|
|
|
|
|
|
|
our $ok = 1; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Register plugin |
|
12
|
|
|
|
|
|
|
sub register { |
|
13
|
4
|
|
|
4
|
1
|
1380
|
my ($plugin, $mojo, $param) = @_; |
|
14
|
|
|
|
|
|
|
|
|
15
|
4
|
|
|
|
|
8
|
$ok--; |
|
16
|
|
|
|
|
|
|
|
|
17
|
4
|
50
|
|
|
|
21
|
if (ref $param ne 'HASH') { |
|
18
|
0
|
|
|
|
|
0
|
$mojo->log->fatal(__PACKAGE__ . ' expects a hash reference'); |
|
19
|
0
|
|
|
|
|
0
|
return; |
|
20
|
|
|
|
|
|
|
}; |
|
21
|
|
|
|
|
|
|
|
|
22
|
4
|
50
|
|
|
|
16
|
if ($param) { |
|
23
|
4
|
|
|
|
|
26
|
$param{$_} = $param->{$_} foreach keys %$param; |
|
24
|
|
|
|
|
|
|
}; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Load parameter from Config file |
|
28
|
4
|
100
|
|
|
|
16
|
unless ($read_config) { |
|
29
|
2
|
100
|
|
|
|
13
|
if (my $config_param = $mojo->config('Util-RandomString')) { |
|
30
|
1
|
|
|
|
|
24
|
%param = ( %$config_param, %param ); |
|
31
|
|
|
|
|
|
|
}; |
|
32
|
2
|
|
|
|
|
24
|
$read_config = 1; |
|
33
|
|
|
|
|
|
|
}; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Reseed on fork |
|
37
|
|
|
|
|
|
|
Mojo::IOLoop->timer( |
|
38
|
|
|
|
|
|
|
0 => sub { |
|
39
|
4
|
|
|
4
|
|
588
|
my %created = (); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Create generators by param |
|
42
|
4
|
|
|
|
|
16
|
foreach (keys %param) { |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Named generator |
|
45
|
20
|
100
|
100
|
|
|
103
|
if (ref $param{$_} && ref $param{$_} eq 'HASH') { |
|
46
|
|
|
|
|
|
|
|
|
47
|
14
|
50
|
|
|
|
37
|
next if $created{$_}; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Construct object |
|
50
|
14
|
50
|
|
|
|
22
|
unless ($generator{$_} = Session::Token->new( |
|
51
|
14
|
|
|
|
|
63
|
%{ $param{$_} } |
|
52
|
|
|
|
|
|
|
)) { |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Unable to construct object |
|
55
|
0
|
|
|
|
|
0
|
$mojo->log->fatal(qq!Unable to create generator for "$_"!); |
|
56
|
0
|
|
|
|
|
0
|
next; |
|
57
|
|
|
|
|
|
|
}; |
|
58
|
14
|
|
|
|
|
2677
|
$setting{$_} = { %{$param{$_}} }; |
|
|
14
|
|
|
|
|
64
|
|
|
59
|
14
|
|
|
|
|
41
|
$created{$_} = 1; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Default parameter |
|
63
|
|
|
|
|
|
|
else { |
|
64
|
6
|
|
|
|
|
16
|
$default{$_} = $param{$_}; |
|
65
|
|
|
|
|
|
|
}; |
|
66
|
|
|
|
|
|
|
}; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Plugin registered |
|
69
|
4
|
|
|
|
|
15
|
$ok++; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Create default generator |
|
72
|
4
|
100
|
|
|
|
18
|
unless (exists $generator{default}) { |
|
73
|
2
|
|
|
|
|
9
|
$generator{default} = Session::Token->new( %default ); |
|
74
|
|
|
|
|
|
|
}; |
|
75
|
4
|
|
|
|
|
47
|
}); |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Establish 'random_string' helper |
|
79
|
|
|
|
|
|
|
$mojo->helper( |
|
80
|
|
|
|
|
|
|
random_string => sub { |
|
81
|
224
|
|
|
224
|
|
407742
|
my $c = shift; |
|
82
|
224
|
|
|
|
|
470
|
my $gen = $_[0]; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# One tick for loop until the plugin is registered |
|
85
|
224
|
|
100
|
|
|
961
|
Mojo::IOLoop->one_tick until Mojo::IOLoop->is_running || $ok > 0; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Generate from generator |
|
88
|
224
|
100
|
|
|
|
4550
|
unless ($_[1]) { |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Generator doesn't exist |
|
91
|
207
|
100
|
100
|
|
|
853
|
if ($gen && !exists $generator{$gen}) { |
|
92
|
2
|
|
|
|
|
10
|
$c->app->log->warn(qq!RandomString generator "$gen" is unknown!); |
|
93
|
2
|
|
|
|
|
36
|
return ''; |
|
94
|
|
|
|
|
|
|
}; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Get from generator |
|
97
|
205
|
|
100
|
|
|
1619
|
return $generator{$gen || 'default'}->get; |
|
98
|
|
|
|
|
|
|
}; |
|
99
|
|
|
|
|
|
|
|
|
100
|
17
|
100
|
|
|
|
62
|
$gen = @_ % 2 ? shift : 'default'; |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Overwrite configuration |
|
103
|
17
|
|
|
|
|
56
|
my %overwrite = @_; |
|
104
|
|
|
|
|
|
|
|
|
105
|
17
|
100
|
|
|
|
56
|
if (exists $overwrite{entropy}) { |
|
|
|
100
|
|
|
|
|
|
|
106
|
3
|
|
100
|
|
|
12
|
$overwrite{length} //= undef; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
elsif (exists $overwrite{length}) { |
|
109
|
9
|
|
50
|
|
|
39
|
$overwrite{entropy} //= undef; |
|
110
|
|
|
|
|
|
|
}; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Take default configuration |
|
113
|
17
|
100
|
|
|
|
57
|
if ($gen eq 'default') { |
|
|
|
100
|
|
|
|
|
|
|
114
|
7
|
|
|
|
|
32
|
return Session::Token->new(%default, %overwrite)->get; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Overwrite specific configuration |
|
118
|
|
|
|
|
|
|
elsif ($setting{ $gen }) { |
|
119
|
8
|
|
|
|
|
28
|
return Session::Token->new( %{ $setting{ $gen } }, %overwrite)->get; |
|
|
8
|
|
|
|
|
63
|
|
|
120
|
|
|
|
|
|
|
}; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Generator is unknown |
|
123
|
2
|
|
|
|
|
11
|
$c->app->log->warn(qq!RandomString generator "$gen" is unknown!); |
|
124
|
2
|
|
|
|
|
318
|
return ''; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
4
|
100
|
|
|
|
417
|
) unless exists $mojo->renderer->helpers->{random_string}; |
|
127
|
|
|
|
|
|
|
}; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
__END__ |