Apache2-AuthAny

 view release on metacpan or  search on metacpan

examples/demo/index.php  view on Meta::CPAN

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Apache2::AuthAny (optionally) uses a database table (userIdent) to
resolve the identities provided by the identity providers. Multiple
provider identities can resolve to a single AuthAny identity.
This AuthAny identity can then be used for
authorization purposes.
</p>
 
<h2>Trial logins</h2>
<p>
To make it possible to show the authentication and authorization
capabilities to anyone coming to this site, several "basic auth" accounts/passwords have been set
up. The password field should be left blank
 
<ul>
  <li><b>aatest1</b> - This user name is not in the identity table. The access provided will be similar
                       to what you will get if an unknown user (you) logs in with Google or Shibboleth.
  </li>
  <li><b>aatest2</b> - This user name IS in our userIdent and user
                       tables. The user's name and roles are available.
  </li>
  <li><b>aatest3</b> - This user name is linked to the same user as "aatest2".</li>

lib/Apache2/AuthAny/DB.pm  view on Meta::CPAN

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
my $dbHandle;
our $VERSION = '0.201';
 
sub new {
    my $class = shift;
    my $self = {};
 
    unless ($dbHandle) {
        my $dbUser = $ENV{AUTH_ANY_DB_USER} || die "Env variable AUTH_ANY_DB_USER required";
        my $dbPasswordFile = $ENV{AUTH_ANY_DB_PW_FILE} || die "Env variable AUTH_ANY_DB_PW_FILE required";
        open(PWD, "<$dbPasswordFile") || die "Could not read password file, '$dbPasswordFile'. $!";
        my $dbPassword = <PWD>;
        close(PWD) || die "ouch $!";
        chomp $dbPassword;      #remove the trailing new line
        die "Could not get password" unless $dbPassword;
        my $dbName = $ENV{AUTH_ANY_DB_NAME} || die "Env variable AUTH_ANY_DB_NAME required";
        my $db;
        $db = $ENV{AUTH_ANY_DB} || "mysql";
        my $dsn = "database=$dbName";
        my $dbHost = $ENV{AUTH_ANY_DB_HOST};
 
        $dsn .= ";host=$dbHost" if $dbHost;
        $dbHandle = DBI->connect("DBI:$db:$dsn", $dbUser, $dbPassword) or die "user: $dbUser, errstr: $DBI::errstr";
        $dbHandle->do('SET CHARACTER SET utf8');
    }

lib/Apache2/AuthAny/DB.pm  view on Meta::CPAN

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
                      AND authId = ? AND authProvider = ? limit 1';
 
    $self->useDB();
    return $dbHandle->selectrow_hashref($getUserSql, undef, $authId, $authProvider);
}
 
sub getBasicUser {
    my $self = shift;
    my ($user) = @_;
 
    my $sql = 'select user, password from basicAuth WHERE user = ?';
 
    $self->useDB();
    return $dbHandle->selectrow_hashref($sql, undef, $user);
}
 
sub getUserRoles {
    my $self = shift;
    my ($UID) = @_;
 
    my $getRoleSql = 'select role from userRole WHERE UID = ?';

lib/Apache2/AuthAny/RequestConfig.pm  view on Meta::CPAN

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
sub get_scripted_pid {
    my $r = shift;
    my $cf = shift;
    if ($cf->{AuthAnyBasicAuthUserFile}) {
        unless (open(HTPASSWD, $cf->{AuthAnyBasicAuthUserFile})) {
            my $msg = "Cannot read  '$cf->{AuthAnyBasicAuthUserFile}' $!";
            die $msg;
        }
 
        my ($http_user, $http_password) = get_user_and_password($r);
        if ($http_user && $http_password) {
 
            my $stored_passwd;
            while (<HTPASSWD>) {
                chomp;
                my ($username, $crypt_passwd) = split(":", $_, 2);
                if ($username eq $http_user) {
                    if (crypt($http_password, $crypt_passwd) eq $crypt_passwd) {
                        $r->log->info("RequestConfig: From HTTP header: $username");
                        return {PID => 'unused',
                                SID => 'unused',
                                logoutKey => 'unused',
                                state => 'authenticated',
                                scripted => 1,
                                authId => $username,
                                authProvider => 'basic',
                                last => 2298416724, # time in the future
                               };
                    } else {
                        my $msg = "RequestConfig: Basic user found in " .
                          "$cf->{AuthAnyBasicAuthUserFile}, however password is incorrect";
                        $r->log->warn($msg);
                        last;
                    }
                }
            }
        }
    }
}
 
sub get_user_and_password {
    my $r = shift;
    my $Authorization = $r->headers_in->{Authorization};
    if ($Authorization) {
        my ($type, $hash) = split " ", $Authorization;
        my $u_and_p = decode_base64($hash);
        if ($u_and_p) {
            my ($user, $password) = split(/:/, $u_and_p, 2);
            return ($user, $password);
        }
    }
    return undef;
}
 
1;



( run in 0.369 second using v1.01-cache-2.11-cpan-87723dcf8b7 )