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