This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package AuthenticatedFoo; | |
use Mojo::Base 'Mojolicious'; | |
sub setup_routes { | |
my ($self) = @_; | |
my $r = $self->routes; | |
my $auth = \&authenticate; | |
$r->under($auth)->route('/test')->via('GET')->to( controller => 'Test', action => 'test' ); | |
} | |
sub authenticate { | |
my ($c) = @_; | |
# Skip authentication if not enabled | |
return 1 if ! $c->config->{auth}{enabled}; | |
# Validate auth token if possible | |
my $validated_ref = Bar::Auth->validate(jwt => '...', public_key => '...'); | |
return 1 if $validated_ref; | |
# Not authorised | |
$c->render(text => "Request not authorised", status => 401); | |
return undef; | |
} |