Debian Codegraph @

lemonldap-ng-common/t/60-U2F-Migrate.t

raw
1 use warnings;
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl Lemonldap-NG-Manager.t'
5
6 #########################
7
8 # change 'tests => 1' to 'tests => last_test_to_print';
9
10 use Test::More;
11 use Test::Output;
12 use File::Path;
13 use JSON;
14
15 BEGIN {
16 use_ok('Lemonldap::NG::Common::Session');
17 use_ok('Lemonldap::NG::Common::CliSessions');
18 }
19
20 #########################
21
22 SKIP: {
23 eval "use Authen::WebAuthn::Test; use Authen::WebAuthn;";
24 if ($@) {
25 skip 'Authen::WebAuthn not found';
26 }
27 my $dir;
28 my $cli;
29
30 sub setup_sessions {
31 use File::Temp;
32 $dir = File::Temp::tempdir();
33 my $sessionsdir = "$dir/sessions";
34 my $psessionsdir = "$dir/psessions";
35 mkdir $sessionsdir;
36 mkdir $psessionsdir;
37
38 $cli = Lemonldap::NG::Common::CliSessions->new(
39 conf => {
40 globalStorage => "Apache::Session::File",
41 globalStorageOptions => {
42 Directory => $sessionsdir,
43 LockDirectory => $sessionsdir,
44 },
45 persistentStorage => "Apache::Session::File",
46 persistentStorageOptions => {
47 Directory => $psessionsdir,
48 LockDirectory => $psessionsdir,
49 },
50 }
51 );
52
53 # Provision test sessions
54 my @psessionsOpts = (
55 storageModule => "Apache::Session::File",
56 storageModuleOptions => {
57 Directory => $psessionsdir,
58 LockDirectory => $psessionsdir,
59 },
60 kind => 'Persistent',
61 force => 1,
62 );
63
64 #dwho
65 Lemonldap::NG::Common::Session->new( {
66 @psessionsOpts,
67 id => "5efe8af397fc3577e05b483aca964f1b",
68 force => 1,
69 info => {
70 "_2fDevices" => to_json( [ {
71 'type' => 'UBK',
72 'epoch' => 1588691690,
73 '_yubikey' => 'cccccceijfnf',
74 'name' => 'Imported automatically'
75 },
76 {
77 'name' => 'U2F-1',
78 'type' => 'U2F',
79 'epoch' => 1588691728,
80 '_keyHandle' =>
81 '4aS6vXlFQpG5XZSoad6auM9fFu7Q1wazQYwfPtPKN_Hll6Up_ceeWkOgqxm49swWq4Vvcg5UlX0sQQhuRe8heA',
82 '_userKey' =>
83 'BMgMqKPL2PhsjCNW78UEQyNF8zlJtrAAPtWMUDBp9VfDRF5oL2xkwFuyXRMPtRZ7lNfGijDrMc06bDNfp478sQQ',
84 },
85 {
86 'name' => 'U2F-2',
87 'type' => 'U2F',
88 'epoch' => 1588691730,
89 '_keyHandle' =>
90 'F1Kk9V_O7KDPIx-mqp6CIjbz7ljA-ihWVWyoP1xYBe_HPLHR74aTLanmn0b4vI8DumiBWO1DAle3k6N55cXreg',
91 '_userKey' =>
92 'BAE_svIcxLfm2Knart7DI1ScfBnCt-OFKDWugp3YMO14tamwuc_wN0vSh1D_0DV4Ao3S5GNQZXxtjtUADHTwXHA',
93 },
94 {
95 _credentialId => 'noconflict',
96 'name' => 'Existing WebAuthn',
97 'type' => 'WebAuthn',
98 'epoch' => 1588691798
99 }
100 ]
101 ),
102 "_oidcConsents" => to_json( [ {
103 'scope' => 'openid email',
104 'rp' => 'rp-example',
105 'epoch' => 1589288341
106 },
107 {
108 'scope' => 'openid email',
109 'epoch' => 1589291482,
110 'rp' => 'rp-example2'
111 }
112 ]
113 ),
114 "_session_uid" => "dwho",
115 }
116 }
117 );
118
119 # rtyler
120 Lemonldap::NG::Common::Session->new( {
121 @psessionsOpts,
122 id => "8d3bc3b0e14ea2a155f275aa7c07ebee",
123 force => 1,
124 info => {
125 "_session_uid" => "rtyler",
126 "_2fDevices" => to_json( [ {
127 'type' => 'UBK',
128 'epoch' => 1588691690,
129 '_yubikey' => 'cccccceijfnf',
130 'name' => 'Imported automatically'
131 },
132 {
133 'name' => 'U2F-3',
134 'type' => 'U2F',
135 'epoch' => 1588691734,
136 '_keyHandle' =>
137 '4suXv5Cf10vbJEP72mVkLpBjhSqy5niOgfc0X_MjdxZ_g2e-V8biC6WyCTpF_kGV1FCa06YlcryPCtWUuUST_g',
138 '_userKey' =>
139 'BIXrgc12iGGOYIGyWKd8WeOGCKyTkFA7jXkjlLS0i1MA3vy8gDocfqYCngXMzBAmtGI7FfMlbkG6DJeSubdxAVc',
140 },
141 ]
142 ),
143 }
144 }
145 );
146 }
147
148 sub getJson {
149 my @args = @_;
150 my ($str) = Test::Output::output_from( sub { $cli->run(@args); } );
151 return from_json($str);
152 }
153
154 sub getLines {
155 my @args = @_;
156 my ($str) = Test::Output::output_from( sub { $cli->run(@args); } );
157 return [ split /\n/, $str ];
158 }
159
160 setup_sessions();
161 my $res;
162
163 # Migrate U2F
164 $cli->run( "secondfactors", {}, "migrateu2f", "dwho" );
165
166 $res = getJson( "secondfactors", {}, "get", "rtyler" );
167 is( values %{$res}, 2, "Still 2 devices" );
168 is( ( grep { $_->{type} eq "WebAuthn" } values %{$res} ),
169 0, "No WebAuthn sessions created" );
170
171 $res = getJson( "secondfactors", {}, "get", "dwho" );
172 is( values %{$res}, 6, "Expect 6 devices after migration" );
173 is( ( grep { $_->{type} eq "U2F" } values %{$res} ),
174 2, "U2F still present" );
175 is( ( grep { $_->{type} eq "UBK" } values %{$res} ),
176 1, "UBK still in place" );
177 is( ( grep { $_->{type} eq "WebAuthn" } values %{$res} ),
178 3, "New WebAuthn device" );
179 my $migratedid = "MTU4ODY5MTcyODo6V2ViQXV0aG46OlUyRi0x";
180 is( $res->{$migratedid}->{_signCount}, 0, "migrated signcount" );
181 is(
182 $res->{$migratedid}->{_credentialId},
183 '4aS6vXlFQpG5XZSoad6auM9fFu7Q1wazQYwfPtPKN_Hll6Up_ceeWkOgqxm49swWq4Vvcg5UlX0sQQhuRe8heA',
184 "migrated credential ID"
185 );
186 is(
187 $res->{$migratedid}->{_credentialPublicKey},
188 'pQECAyYgASFYIMgMqKPL2PhsjCNW78UEQyNF8zlJtrAAPtWMUDBp9VfDIlggRF5oL2xkwFuyXRMPtRZ7lNfGijDrMc06bDNfp478sQQ',
189 "migrated credential key"
190 );
191 is( $res->{$migratedid}->{epoch}, '1588691728', "migrated epoch" );
192 is( $res->{$migratedid}->{name}, "U2F-1", "migrated name" );
193
194 # Check idempotence
195 $cli->run( "secondfactors", {}, "migrateu2f", "dwho" );
196 $res = getJson( "secondfactors", {}, "get", "dwho" );
197 is( values %{$res}, 6, "Expect still 6 devices after rerunning migration" );
198 is( ( grep { $_->{type} eq "U2F" } values %{$res} ),
199 2, "U2F still in place" );
200 is( ( grep { $_->{type} eq "UBK" } values %{$res} ),
201 1, "UBK still in place" );
202 is( ( grep { $_->{type} eq "WebAuthn" } values %{$res} ),
203 3, "Same WebAuthn devices" );
204
205 rmtree $dir;
206 setup_sessions();
207
208 # Migrate all
209 $cli->run( "secondfactors", { all => 1 }, "migrateu2f" );
210 $res = getJson( "secondfactors", {}, "get", "dwho" );
211 is( values %{$res}, 6, "Expect 6 devices after migration" );
212 is( ( grep { $_->{type} eq "U2F" } values %{$res} ),
213 2, "U2F still in place" );
214 is( ( grep { $_->{type} eq "UBK" } values %{$res} ),
215 1, "UBK still in place" );
216 is( ( grep { $_->{type} eq "WebAuthn" } values %{$res} ),
217 3, "New WebAuthn device" );
218
219 $res = getJson( "secondfactors", {}, "get", "rtyler" );
220 is( values %{$res}, 3, "Expect 3 devices after migration" );
221 is( ( grep { $_->{type} eq "U2F" } values %{$res} ),
222 1, "U2F still in place" );
223 is( ( grep { $_->{type} eq "UBK" } values %{$res} ),
224 1, "UBK still in place" );
225 is( ( grep { $_->{type} eq "WebAuthn" } values %{$res} ),
226 1, "New WebAuthn device" );
227
228 rmtree $dir;
229 }
230 done_testing();