Guest User

Untitled

a guest
Jul 9th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package Imos::AuthCookieHandler;
  2.  
  3. use strict;
  4. use diagnostics;
  5. use Apache2::Const qw(:common HTTP_FORBIDDEN);
  6. use Apache2::AuthCookie;
  7. use Apache2::RequestRec;
  8. use Apache2::RequestIO;
  9. use vars qw($VERSION @ISA);
  10.  
  11. # MD5 hashing and DBI
  12. use Digest::SHA qw(sha1 sha1_hex);
  13. use DBI;
  14.  
  15. $VERSION = substr(q$Revision: 220 $, 10);
  16. @ISA = qw(Apache2::AuthCookie);
  17.  
  18. sub authen_cred ($$\@) {
  19. my $self = shift;
  20. my $r = shift;
  21. my @creds = @_;
  22.  
  23. my $dbh = DBI->connect("dbi:SQLite:dbname=" . $r->dir_config->get('DatabasePath'), "", "");
  24. unless ($dbh) {
  25. $r->server->log_error("Database connection failed: " . $DBI::errstr);
  26. $r->print("Es ist ein unerwarteter Fehler aufgetreten");
  27. die;
  28. }
  29.  
  30. my $q = "SELECT user_id, login FROM users WHERE login = ? AND password = ?";
  31. my $sth = $dbh->prepare($q);
  32. unless ($sth) {
  33. $r->server->log_error("Preparing failed for query=(" . $q . "): " . $DBI::errstr);
  34. $r->print("Es ist ein unerwarteter Fehler aufgetreten");
  35. die;
  36. }
  37.  
  38. # Generate password hash
  39. my $sha = Digest::SHA->new;
  40. $sha->add($creds[1]);
  41.  
  42. $sth->execute($creds[0], $sha->hexdigest);
  43. my $rd = $sth->fetchrow_hashref;
  44.  
  45. # Found database entry?
  46. return unless $rd;
  47.  
  48. # We got a entry
  49. my $hash = generate_hash;
  50. my $cookie = $rd->{'user_id']} . ":" . join(":", $rd);
  51.  
  52. my $q = "INSERT INTO tickets (user_id, hash) VALUES (?, ?)";
  53.  
  54.  
  55. return $cookie;
  56. }
  57.  
  58. sub authen_ses_key ($$$) {
  59. my ($self, $r, $cookie) = @_;
  60. my @parts = split(/:/, $cookie);
  61. $r->server->log_error(@parts);
  62. $r->server->log_error("user=" . $parts[1] . " cookie=$cookie");
  63.  
  64. if ($parts[0] eq 1) {
  65. return $parts[0];
  66. } else {
  67. return;
  68. }
  69. }
  70.  
  71. sub generate_hash {
  72. my @a = ("a".."z", "A".."Z", 0..9); say join "", map {@a[rand @a]} 1..8;
  73.  
  74. return @a;
  75. }
  76.  
  77. 1;
Add Comment
Please, Sign In to add comment