Airlock
view release on metacpan or search on metacpan
t/91-live-authentik.t view on Meta::CPAN
client_id => $client_id,
scope => 'openid email profile',
sleep => sub { sleep 1 },
on_prompt => sub { }
);
}
# What a person does after the device showed its code: log in, then open the
# verification link, which authentik turns into one run of the authorization
# flow. Returns '' when the device was approved, otherwise what went wrong.
sub approve_in_browser {
my ( $start, %login ) = @_;
my $browser = AuthentikExecutor->new( base_url => $base );
eval { $browser->login( username => $login{username}, password => $login{password},
totp_secret => $login{totp} ); 1 } or return 'the login failed: '.( $@ =~ s/\n.*//sr );
my $who = $browser->whoami;
return 'the login did not make a session' unless $who && $who->{username} eq $login{username};
my $response = $browser->ua->get( $start->{verification_uri_complete} );
my $location = $response->header('Location')
or return 'the verification link answered '.$response->status_line.' instead of a redirect';
my ( $flow ) = $location =~ m{/if/flow/([^/?]+)};
return 'the verification link went to '.$location.' instead of a flow' unless $flow;
my ( $query ) = $location =~ m{\?(.*)\z};
my $challenge = eval { $browser->start( $flow, $query ) } or return 'the flow executor failed: '.( $@ =~ s/\n.*//sr );
return 'the approval flow stopped at '.( $challenge->{component} // 'nothing' ).' ('
.( $challenge->{error_message} // 'no message' ).')'
unless ( $challenge->{component} // '' ) eq 'ak-provider-oauth2-device-code-finish';
return '';
}
# a device of an earlier run would make the password-only login ask for a code
sub forget_totp {
my ( $username ) = @_;
my $user = $api->find_user($username) or return;
for my $device ( @{ $api->list_authenticators( $user->{pk} ) || [] } ) {
next unless ( $device->{type} // '' ) =~ /totp/i;
eval { $api->call( DELETE => '/authenticators/admin/totp/'.$device->{pk}.'/' ) };
}
return;
}
# every login the test makes leaves a session behind, and they pile up over
# runs; the instance is not ours to litter
sub forget_sessions {
my ( $username ) = @_;
my $user = $api->find_user($username) or return;
# /core/authenticated_sessions/ takes no user filter -- the query parameter is
# accepted and ignored -- and reports the owner as a bare pk, not an object.
my $sessions = eval { $api->_paged('/core/authenticated_sessions/') } || [];
for my $session ( @$sessions ) {
next unless defined $session->{user} && $session->{user} == $user->{pk};
eval { $api->call( DELETE => '/core/authenticated_sessions/'.$session->{uuid}.'/' ) };
}
return;
}
sub start_device {
my ( $client ) = @_;
my $start = eval { $client->start };
# authentik throttles the device authorization endpoint itself -- not just the
# token polling RFC 8628 means slow_down for -- to 20 requests an hour per
# client IP, and answers 429 slow_down above that. One full run of this file
# spends four of them, so an instance that only ever serves this suite still
# locks up after five runs in an hour.
BAIL_OUT( 'authentik is throttling the device authorization endpoint (20/hour per IP by '
.'default). Raise AUTHENTIK_THROTTLE__PROVIDERS__OAUTH2__DEVICE on the instance or wait '
.'the hour out; see t/authentik/README.md.' ) if !$start && $@ =~ /slow_down/;
die $@ unless $start;
return $start;
}
sub tidy_up {
return unless $api;
for my $username (qw( airlock-plain airlock-otp )) {
eval { forget_totp($username) };
eval { forget_sessions($username) };
}
return;
}
tidy_up();
END { tidy_up() }
subtest 'discovery and a poll nobody answers' => sub {
my $client = client();
like( $client->device_endpoint, qr{\Ahttp}, 'discovery finds the device authorization endpoint' );
my $start = start_device($client);
ok( length $start->{device_code}, 'device_code' );
ok( length $start->{user_code}, 'user_code' );
like( $start->{verification_uri_complete}, qr{\Ahttp.+\Q$start->{user_code}\E}, 'verification_uri_complete carries the code' );
# A poll that outlives a short deadline proves authentik answered
# authorization_pending the way the client expects; any other answer would
# croak with "poll failed".
ok( !eval { $client->poll( { %$start, interval => 1, expires_in => 3 } ); 1 }, 'polling without approval ends' );
like( $@, qr/the code expired before anyone approved it/, 'because the code ran out, not because of an unexpected answer' );
};
my ( %subject, $totp_secret );
subtest 'enrol TOTP for the second person' => sub {
my $browser = AuthentikExecutor->new( base_url => $base );
ok( eval { $browser->login( username => 'airlock-otp', password => 'airlock-otp-password' ); 1 },
'a password login first' ) or return diag $@;
$totp_secret = eval { $browser->enroll_totp };
ok( $totp_secret, 'the setup flow hands out a secret' ) or return diag $@;
my $user = $api->find_user('airlock-otp');
ok( scalar @{ $api->list_authenticators( $user->{pk} ) }, 'and the device shows up in the API' );
};
for my $case (
{ name => 'password only', username => 'airlock-plain', amr => ['pwd'] },
{ name => 'password and TOTP', username => 'airlock-otp', amr => [qw( pwd mfa )], totp => \$totp_secret }
)
{
subtest 'the whole device flow, logging in with '.$case->{name} => sub {
my %login = ( username => $case->{username}, password => $case->{username}.'-password',
$case->{totp} ? ( totp => ${ $case->{totp} } ) : () );
my $client = client();
my $start = start_device($client);
my $before = time;
is( approve_in_browser( $start, %login ), '', 'a person logs in and grants the device access' ) or return;
my $token = eval { $client->poll( { %$start, interval => 1, expires_in => 30 } ) };
ok( $token && $token->{access_token}, 'the client gets its token' ) or return diag $@;
my $claims = claims( $token->{id_token} );
diag $case->{username}.': acr='.( $claims->{acr} // '(none)' ).' amr='.join( ',', @{ $claims->{amr} || [] } )
.' auth_time='.( $claims->{auth_time} // '(none)' );
is( $claims->{preferred_username}, $case->{username}, 'for the person who logged in' );
is_deeply( $claims->{amr}, $case->{amr}, 'amr says how, with nothing configured for it' );
is( $claims->{acr}, 'goauthentik.io/providers/oauth2/default', 'acr is the one constant value' );
cmp_ok( $claims->{auth_time} // 0, '>=', $before - 60, 'auth_time is the login just made' );
is_deeply( claims( $token->{access_token} )->{amr}, $case->{amr}, 'the access token says the same' );
$subject{ $case->{username} } = Airlock::Upstream::Authentik->new->subject($claims);
};
}
subtest 'the upstream factor on real authentik claims' => sub {
plan skip_all => 'needs both logins' unless $subject{'airlock-plain'} && $subject{'airlock-otp'};
my $factor = Airlock::Upstream::Authentik->new->factor( max_age => 300 );
isnt( $subject{'airlock-plain'}{id}, $subject{'airlock-otp'}{id}, 'two people, two ids' );
is( $factor->verify( $subject{'airlock-plain'} ), 0, 'a password login does not hold' );
is( $factor->verify( $subject{'airlock-otp'} ), 1, 'a login with TOTP does' );
my $stale = Airlock::Upstream::Authentik->new->factor( max_age => 300, now => sub { time + 400 } );
is( $stale->verify( $subject{'airlock-otp'} ), 0, 'but not once it is older than max_age' );
};
( run in 1.788 second using v1.01-cache-2.11-cpan-99fa8243a42 )