| 1 | package Mojolicious::Plugin::Fondation::Auth; | ||||||||||
| 2 | 2 | $Mojolicious::Plugin::Fondation::Auth::VERSION = '0.04'; | |||||||||
| 3 | # ABSTRACT: Fondation authentication plugin — DBIx-backed login/logout | ||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Mojolicious::Plugin::Fondation::Auth::Provider::DBIx; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | sub fondation_meta { | ||||||||
|
Subroutine
fondation_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 9 | return { | ||||||||||
| 10 | 2 | dependencies => ['Fondation::User', 'Fondation::Problem'], | |||||||||
| 11 | after => ['Fondation::User::UI::Bootstrap'], | ||||||||||
| 12 | defaults => { | ||||||||||
| 13 | model => 'user', # model name from Fondation::User, overridable | ||||||||||
| 14 | provider => 'Mojolicious::Plugin::Fondation::Auth::Provider::DBIx', | ||||||||||
| 15 | username_column => 'username', | ||||||||||
| 16 | password_column => 'password', | ||||||||||
| 17 | timeout_sessions => 1800, | ||||||||||
| 18 | session_key => 'fondation', | ||||||||||
| 19 | }, | ||||||||||
| 20 | setup => { | ||||||||||
| 21 | label => 'Authentication', | ||||||||||
| 22 | description => 'DBIx-backed login/logout with session management', | ||||||||||
| 23 | parameters => [ | ||||||||||
| 24 | { | ||||||||||
| 25 | key => 'model', | ||||||||||
| 26 | label => 'Model Name', | ||||||||||
| 27 | type => 'string', | ||||||||||
| 28 | default => 'user', | ||||||||||
| 29 | placeholder => 'Model name from Fondation::User', | ||||||||||
| 30 | }, | ||||||||||
| 31 | { | ||||||||||
| 32 | key => 'provider', | ||||||||||
| 33 | label => 'Auth Provider Class', | ||||||||||
| 34 | type => 'string', | ||||||||||
| 35 | default => 'Mojolicious::Plugin::Fondation::Auth::Provider::DBIx', | ||||||||||
| 36 | placeholder => 'Perl class implementing validate_user, load_user, auth_form', | ||||||||||
| 37 | }, | ||||||||||
| 38 | { | ||||||||||
| 39 | key => 'username_column', | ||||||||||
| 40 | label => 'Username Column', | ||||||||||
| 41 | type => 'string', | ||||||||||
| 42 | default => 'username', | ||||||||||
| 43 | }, | ||||||||||
| 44 | { | ||||||||||
| 45 | key => 'password_column', | ||||||||||
| 46 | label => 'Password Column', | ||||||||||
| 47 | type => 'string', | ||||||||||
| 48 | default => 'password', | ||||||||||
| 49 | }, | ||||||||||
| 50 | { | ||||||||||
| 51 | key => 'timeout_sessions', | ||||||||||
| 52 | label => 'Session Timeout (seconds)', | ||||||||||
| 53 | type => 'integer', | ||||||||||
| 54 | default => 1800, | ||||||||||
| 55 | }, | ||||||||||
| 56 | { | ||||||||||
| 57 | key => 'session_key', | ||||||||||
| 58 | label => 'Session Key', | ||||||||||
| 59 | type => 'string', | ||||||||||
| 60 | default => 'fondation', | ||||||||||
| 61 | }, | ||||||||||
| 62 | ], | ||||||||||
| 63 | }, | ||||||||||
| 64 | }; | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | sub register ($self, $app, $config) { | ||||||||
|
Subroutine
register: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | |||||||||||
| 69 | # ── Session timeout ────────────────────────────────────────────── | ||||||||||
| 70 | 2 | ▶ | if (my $timeout = $config->{timeout_sessions}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 2 | $app->sessions->default_expiration($timeout); | |||||||||
| 72 | 2 | $self->log->debug("session timeout set to $timeout seconds"); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | # ── Provider ───────────────────────────────────────────────────── | ||||||||||
| 76 | 2 | my $provider_class = $config->{provider}; | |||||||||
| 77 | 2 | $self->log->debug("using provider $provider_class"); | |||||||||
| 78 | 2 | my $provider = $provider_class->new( | |||||||||
| 79 | %$config, | ||||||||||
| 80 | app => $app, | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | # ── Authentication plugin ──────────────────────────────────────── | ||||||||||
| 84 | $app->plugin('Authentication' => { | ||||||||||
| 85 | session_key => $config->{session_key}, | ||||||||||
| 86 | 11 | ▶ | load_user => sub ($app, $uid) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 87 | 11 | return $provider->load_user($app, $uid); | |||||||||
| 88 | }, | ||||||||||
| 89 | 6 | ▶ | validate_user => sub ($c, $username, $password, $extra = {}) { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 90 | 6 | return $provider->validate_user($c, $username, $password, $extra); | |||||||||
| 91 | }, | ||||||||||
| 92 | 2 | }); | |||||||||
| 93 | |||||||||||
| 94 | # ── Helpers ────────────────────────────────────────────────────── | ||||||||||
| 95 | 2 | ▶ | $app->helper(auth_form => sub ($c) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 2 | return $provider->auth_form($c); | |||||||||
| 97 | 2 | }); | |||||||||
| 98 | |||||||||||
| 99 | # ── Routes ─────────────────────────────────────────────────────── | ||||||||||
| 100 | 2 | my $r = $app->routes; | |||||||||
| 101 | |||||||||||
| 102 | # ── Route condition: fondation.authenticated ──────────────────── | ||||||||||
| 103 | # Overrides the no-op fallback registered by Fondation core. | ||||||||||
| 104 | 11 | ▶ | $app->routes->add_condition('fondation.authenticated' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 105 | 11 | my ($route, $c, $captures, $required) = @_; | |||||||||
| 106 | 11 | my $auth = $c->is_user_authenticated; | |||||||||
| 107 | 11 | ▶ | my $pass = $required ? $auth : !$auth; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 11 | ▶ | return 1 if $pass; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 1 | $c->res->code(403); | |||||||||
| 110 | 1 | ▶ | $c->stash('fondation.denied' => { status => 403, title => 'Forbidden' }) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | unless $c->stash('fondation.denied'); | ||||||||||
| 112 | 1 | return undef; | |||||||||
| 113 | 2 | }); | |||||||||
| 114 | |||||||||||
| 115 | $r->get('/login') | ||||||||||
| 116 | ->requires('fondation.authenticated' => 0) | ||||||||||
| 117 | 2 | ▶ | ->to(cb => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 118 | 2 | my $c = shift; | |||||||||
| 119 | 2 | $c->render('login'); | |||||||||
| 120 | 2 | }); | |||||||||
| 121 | |||||||||||
| 122 | $r->post('/login') | ||||||||||
| 123 | ->requires('fondation.authenticated' => 0) | ||||||||||
| 124 | 6 | ▶ | ->to(cb => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 125 | 6 | my $c = shift; | |||||||||
| 126 | 6 | my $username = $c->param('username'); | |||||||||
| 127 | 6 | my $password = $c->param('password'); | |||||||||
| 128 | |||||||||||
| 129 | 6 | ▶ | if ($c->authenticate($username, $password)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 4 | my $user = $c->current_user; | |||||||||
| 131 | 4 | my $username = $user->{username}; | |||||||||
| 132 | 4 | $c->flash(message => $c->l('Logged in as') . " $username", message_class => 'alert-success'); | |||||||||
| 133 | 4 | $c->redirect_to('/'); | |||||||||
| 134 | } | ||||||||||
| 135 | else { | ||||||||||
| 136 | 2 | $c->flash(message => $c->l('Login failed'), message_class => 'alert-danger'); | |||||||||
| 137 | 2 | $c->redirect_to('/login'); | |||||||||
| 138 | } | ||||||||||
| 139 | 2 | }); | |||||||||
| 140 | |||||||||||
| 141 | $r->get('/logout') | ||||||||||
| 142 | ->requires('fondation.authenticated' => 1) | ||||||||||
| 143 | 2 | ▶ | ->to(cb => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 144 | 2 | my $c = shift; | |||||||||
| 145 | 2 | $c->logout; | |||||||||
| 146 | 2 | $c->flash(message => $c->l('Logged out'), message_class => 'alert-success'); | |||||||||
| 147 | 2 | $c->redirect_to('/'); | |||||||||
| 148 | 2 | }); | |||||||||
| 149 | |||||||||||
| 150 | 2 | return $self; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 2 | 1; | |||||||||
| 154 | |||||||||||
| 155 | __END__ | ||||||||||