| 1 | package Catalyst::Authentication::Realm::Progressive; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use base 'Catalyst::Authentication::Realm'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =for Pod::Coverage setup_credential setup_store | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Catalyst::Authentication::Realm::Progressive - Authenticate against multiple realms | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | This Realm allows an application to use a single authenticate() call during | ||||||||||||||||||||
| 18 | which multiple realms are used and tried incrementally until one performs | ||||||||||||||||||||
| 19 | a successful authentication is accomplished. | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | A simple use case is a Temporary Password that looks and acts exactly as a | ||||||||||||||||||||
| 22 | regular password. Without changing the authentication code, you can | ||||||||||||||||||||
| 23 | authenticate against multiple realms. | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Another use might be to support a legacy website authentication system, trying | ||||||||||||||||||||
| 26 | the current auth system first, and upon failure, attempting authentication against | ||||||||||||||||||||
| 27 | the legacy system. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head2 EXAMPLE | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | If your application has multiple realms to authenticate, such as a temporary | ||||||||||||||||||||
| 32 | password realm and a normal realm, you can configure the progressive realm as | ||||||||||||||||||||
| 33 | the default, and configure it to iteratively call the temporary realm and then | ||||||||||||||||||||
| 34 | the normal realm. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | __PACKAGE__->config( | ||||||||||||||||||||
| 37 | 'Plugin::Authentication' => { | ||||||||||||||||||||
| 38 | default_realm => 'progressive', | ||||||||||||||||||||
| 39 | realms => { | ||||||||||||||||||||
| 40 | progressive => { | ||||||||||||||||||||
| 41 | class => 'Progressive', | ||||||||||||||||||||
| 42 | realms => [ 'temp', 'normal' ], | ||||||||||||||||||||
| 43 | # Modify the authinfo passed into authenticate by merging | ||||||||||||||||||||
| 44 | # these hashes into the realm's authenticate call: | ||||||||||||||||||||
| 45 | authinfo_munge => { | ||||||||||||||||||||
| 46 | normal => { 'type' => 'normal' }, | ||||||||||||||||||||
| 47 | temp => { 'type' => 'temporary' }, | ||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||
| 50 | normal => { | ||||||||||||||||||||
| 51 | credential => { | ||||||||||||||||||||
| 52 | class => 'Password', | ||||||||||||||||||||
| 53 | password_field => 'secret', | ||||||||||||||||||||
| 54 | password_type => 'hashed', | ||||||||||||||||||||
| 55 | password_hash_type => 'SHA-1', | ||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||
| 57 | store => { | ||||||||||||||||||||
| 58 | class => 'DBIx::Class', | ||||||||||||||||||||
| 59 | user_model => 'Schema::Person::Identity', | ||||||||||||||||||||
| 60 | id_field => 'id', | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||
| 63 | temp => { | ||||||||||||||||||||
| 64 | credential => { | ||||||||||||||||||||
| 65 | class => 'Password', | ||||||||||||||||||||
| 66 | password_field => 'secret', | ||||||||||||||||||||
| 67 | password_type => 'hashed', | ||||||||||||||||||||
| 68 | password_hash_type => 'SHA-1', | ||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||
| 70 | store => { | ||||||||||||||||||||
| 71 | class => 'DBIx::Class', | ||||||||||||||||||||
| 72 | user_model => 'Schema::Person::Identity', | ||||||||||||||||||||
| 73 | id_field => 'id', | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Then, in your controller code, to attempt authentication against both realms | ||||||||||||||||||||
| 81 | you just have to do a simple authenticate call: | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | if ( $c->authenticate({ id => $username, password => $password }) ) { | ||||||||||||||||||||
| 84 | if ( $c->user->type eq 'temporary' ) { | ||||||||||||||||||||
| 85 | # Force user to change password | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head1 CONFIGURATION | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =over | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =item realms | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | An array reference consisting of each realm to attempt authentication against, | ||||||||||||||||||||
| 96 | in the order listed. If the realm does not exist, calling authenticate will | ||||||||||||||||||||
| 97 | die. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =item authinfo_munge | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | A hash reference keyed by realm names, with values being hash references to | ||||||||||||||||||||
| 102 | merge into the authinfo call that is subsequently passed into the realm's | ||||||||||||||||||||
| 103 | authenticate method. This is useful if your store uses the same class for each | ||||||||||||||||||||
| 104 | realm, separated by some other token (in the L<EXAMPLE> authinfo_mungesection, | ||||||||||||||||||||
| 105 | the 'realm' is a column on C<Schema::Person::Identity> that will be either | ||||||||||||||||||||
| 106 | 'temp' or 'local', to ensure the query to fetch the user finds the right | ||||||||||||||||||||
| 107 | Identity record for that realm. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head1 METHODS | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head2 new ($realmname, $config, $app) | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Constructs an instance of this realm. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head2 authenticate | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | This method iteratively calls each realm listed in the C<realms> configuration | ||||||||||||||||||||
| 120 | key. It returns after the first successful authentication call is done. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | ▶ | sub authenticate { | ||||||||||||||||||
|
Subroutine
authenticate: called CC=8 SCAR=22.5
Pod
documented
| |||||||||||||||||||||
| 125 | 2 | my ( $self, $c, $authinfo ) = @_; | |||||||||||||||||||
| 126 | 2 | my $realms = $self->config->{realms}; | |||||||||||||||||||
| 127 | 2 | ▶ | carp "No realms to authenticate against, check configuration" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | unless $realms; | ||||||||||||||||||||
| 129 | 2 | ▶ | carp "Realms configuration must be an array reference" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | unless ref $realms eq 'ARRAY'; | ||||||||||||||||||||
| 131 | 2 | foreach my $realm_name ( @$realms ) { | |||||||||||||||||||
| 132 | 3 | my $realm = $c->get_auth_realm( $realm_name ); | |||||||||||||||||||
| 133 | 3 | ▶ | carp "Unable to find realm: $realm_name, check configuration" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | unless $realm; | ||||||||||||||||||||
| 135 | 3 | my $auth = { %$authinfo }; | |||||||||||||||||||
| 136 | 3 | ▶ | $auth->{realm} ||= $realm->name; | ||||||||||||||||||
|
Condition33%1/3
$auth->{"realm"} ||= $realm->name
MC/DC0%0/2
$auth->{"realm"} ||= $realm->name
$auth->{"realm"}
$realm->name
Truth table33%1/3
A ||= B
A= $auth->{"realm"}B= $realm->name
| |||||||||||||||||||||
| 137 | 3 | ▶ | if ( my $info = $self->config->{authinfo_munge}->{$realm->name} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 0 | $auth = Catalyst::Utils::merge_hashes($auth, $info); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | 3 | ▶ | if ( my $obj = $realm->authenticate( $c, $auth ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 2 | $c->set_authenticated( $obj, $realm->name ); | |||||||||||||||||||
| 142 | 2 | return $obj; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 0 | return; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | ## we don't want our own store or credential, so avoid setting them up | ||||||||||||||||||||
| 149 | 1 | ▶ | sub setup_store {} | ||||||||||||||||||
|
Subroutine
setup_store: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 150 | 1 | ▶ | sub setup_credential {} | ||||||||||||||||||
|
Subroutine
setup_credential: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head1 AUTHORS | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | J. Shirley C<< <jshirley@cpan.org> >> | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Jay Kuri C<< <jayk@cpan.org> >> | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Copyright (c) 2008 the aforementioned authors. All rights reserved. This program | ||||||||||||||||||||
| 161 | is free software; you can redistribute it and/or modify it under the same terms | ||||||||||||||||||||
| 162 | as Perl itself. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 1 | 1; | |||||||||||||||||||