| 1 | #!/usr/bin/perl -Ilib -w | ||
| 2 | |||
| 3 | package Net::Server::Framework::Auth; | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Switch; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Net::Server::Framework::DB; | ||
| 10 | use Net::Server::Framework::Crypt; | ||
| 11 | |||
| 12 | our ($VERSION) = '1.0'; | ||
| 13 | our $DB = 'framework'; | ||
| 14 | |||
| 15 | sub authenticate { | ||
| 16 | my ( $user, $token, $mode ) = @_; | ||
| 17 | switch ($mode) { | ||
| 18 | case /client/i { return ( _token( $user, $token ) ); } | ||
| 19 | case /server/i { return ( _check( $user, $token ) ); } | ||
| 20 | case /userpass/i { return ( _userpass( $user, $token ) ); } | ||
| 21 | else { carp "2003"; } | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | sub make_pass { | ||
| 26 | my $pass = shift; | ||
| 27 | return Net::Server::Framework::Crypt::hash($pass); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _check { | ||
| 31 | my ( $user, $token ) = @_; | ||
| 32 | my $dbh = Net::Server::Framework::DB::dbconnect($DB); | ||
| 33 | my $res = Net::Server::Framework::DB::get( { dbh => $dbh, key => 'auth', term => $user } ); | ||
| 34 | if ( my $pass = $res->{$user}->{password} ) { | ||
| 35 | my $string = Net::Server::Framework::Crypt::decrypt( $token, $pass, 'blowfish', 'a' ); | ||
| 36 | my ( $u, $time ) = split( /-/, $string, 2 ); | ||
| 37 | if ( $u eq $user ) { | ||
| 38 | |||
| 39 | # more than one day time difference is too much | ||
| 40 | if ( ( ( $time + 86400 ) gt time ) | ||
| 41 | and ( time gt( $time - 86400 ) ) ) | ||
| 42 | { | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | return 2200; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _token { | ||
| 51 | my ( $user, $pass ) = @_; | ||
| 52 | |||
| 53 | my $string = $user . "-" . time; | ||
| 54 | my $token = Net::Server::Framework::Crypt::encrypt( $string, $pass, 'blowfish', 'a' ); | ||
| 55 | chomp($token); | ||
| 56 | return $token; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _userpass { | ||
| 60 | my ( $user, $token ) = @_; | ||
| 61 | my $dbh = Net::Server::Framework::DB::dbconnect($DB); | ||
| 62 | my $res = Net::Server::Framework::DB::get( { dbh => $dbh, key => 'auth', term => $user } ); | ||
| 63 | if ( my $pass = $res->{$user}->{password} ) { | ||
| 64 | if ( $token eq $pass ) { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | return 2200; | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | Net::Server::Framework::Auth - authentication for Net::Server::Framework | ||
| 76 | based daemons | ||
| 77 | |||
| 78 | |||
| 79 | =head1 VERSION | ||
| 80 | |||
| 81 | This documentation refers to C<Net::Server::Framework::Auth> version 1.0. | ||
| 82 | |||
| 83 | |||
| 84 | =head1 SYNOPSIS | ||
| 85 | |||
| 86 | The Authentication part of the C<Net::Server::Framework> | ||
| 87 | |||
| 88 | A typical invocation looks like this: | ||
| 89 | |||
| 90 | if ( !defined ($error = Net::Server::Framework::Auth::authenticate( | ||
| 91 | $c->{user}, $c->{pass}, 'userpass' ))) | ||
| 92 | { | ||
| 93 | # this is authenticated | ||
| 94 | } else { | ||
| 95 | # throw an error | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | =head1 DESCRIPTION | ||
| 100 | |||
| 101 | This is a lib that is used to authenticate clients connecting to the | ||
| 102 | daemon. | ||
| 103 | |||
| 104 | |||
| 105 | =head1 BASIC METHODS | ||
| 106 | |||
| 107 | =head2 authenticate | ||
| 108 | |||
| 109 | This function authenticates a user against a stored password hash. | ||
| 110 | |||
| 111 | =head2 make_pass | ||
| 112 | |||
| 113 | This function creates a password hash secure enough to store it in a | ||
| 114 | database. | ||
| 115 | |||
| 116 | =head1 COMMANDS | ||
| 117 | |||
| 118 | The commands accepted by the lib are: | ||
| 119 | |||
| 120 | =head2 client | ||
| 121 | |||
| 122 | =head2 server | ||
| 123 | |||
| 124 | =head2 userpass | ||
| 125 | |||
| 126 | |||
| 127 | =head1 CONFIGURATION AND ENVIRONMENT | ||
| 128 | |||
| 129 | The library needs a working etc/db.conf file and a configured $DB | ||
| 130 | variable. | ||
| 131 | |||
| 132 | =head1 BUGS AND LIMITATIONS | ||
| 133 | |||
| 134 | There are no known bugs in this module. | ||
| 135 | Please report problems to | ||
| 136 | Lenz Gschwendtner ( <lenz@springtimesoft.com> ) | ||
| 137 | Patches are welcome. | ||
| 138 | |||
| 139 | =head1 AUTHOR | ||
| 140 | |||
| 141 | Lenz Gschwendtner ( <lenz@springtimesoft.com> ) | ||
| 142 | |||
| 143 | =head1 LICENCE AND COPYRIGHT | ||
| 144 | |||
| 145 | Copyright (c) | ||
| 146 | 2007 Lenz Gschwerndtner ( <lenz@springtimesoft.comn> ) | ||
| 147 | All rights reserved. | ||
| 148 | |||
| 149 | This module is free software; you can redistribute it and/or | ||
| 150 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 151 | |||
| 152 | This program is distributed in the hope that it will be useful, | ||
| 153 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 154 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||