Permalink
Browse files

move DJabberd::Authen::HTDigest to own directory

git-svn-id: http://code.sixapart.com/svn/djabberd/trunk@712 43dd9337-660f-0410-bd32-e7601923a1a1
  • Loading branch information...
0 parents commit 7c449c42f59811f1596d6e2aa53cc1cc870de1b4 @bradfitz bradfitz committed Sep 28, 2006
Showing with 113 additions and 0 deletions.
  1. +65 −0 lib/DJabberd/Authen/HTDigest.pm
  2. +47 −0 t/htdigest.t
  3. +1 −0 t/htdigest.t.conf
@@ -0,0 +1,65 @@
+package DJabberd::Authen::HTDigest;
+use strict;
+use base 'DJabberd::Authen';
+
+use DJabberd::Log;
+our $logger = DJabberd::Log->get_logger;
+use Digest::MD5 qw(md5_hex);
+sub log {
+ $logger;
+}
+
+sub set_config_htdigest {
+ my ($self, $htdigest) = @_;
+ $self->{htdigest} = $htdigest;
+}
+
+sub set_config_realm {
+ my ($self, $realm) = @_;
+ $self->{realm} = $realm;
+}
+
+sub finalize {
+ my $self = shift;
+ $logger->error_die("No htdigest file") unless $self->{htdigest};
+ $logger->error_die("htdigest file '$self->{htdigest}' does not exist") unless -e $self->{htdigest};
+ $logger->error_die("htdigest file '$self->{htdigest} not readable") unless -r $self->{htdigest};
+ $logger->error_die("No realm specified") unless $self->{realm};
+}
+
+sub can_retrieve_cleartext { 0 }
+
+sub check_cleartext {
+ my ($self, $cb, %args) = @_;
+ my $username = $args{username};
+ my $password = $args{password};
+ my $conn = $args{conn};
+ unless ($username =~ /^\w+$/) {
+ $cb->reject;
+ return;
+ }
+
+
+ open(my $htdigest, "<$self->{htdigest}") || $logger->logwarn("Cannot open file '$self->{htdigest}': $!");
+ while(<$htdigest>) {
+ chomp;
+ next unless /^$username:$self->{realm}:(.+)$/;
+ my $md5_orig = $1;
+ my $md5_computed = md5_hex("$username:$self->{realm}:$password");
+ if($md5_orig eq $md5_computed) {
+ $cb->accept;
+ $logger->debug("User '$username' successfully logged in");
+ return 1;
+ } else {
+ $cb->reject();
+ $logger->info("User '$username' denied, '$md5_orig' ne '$md5_computed'");
+ return 0;
+ }
+ }
+
+ $logger->info("User '$username' denied, does not exist in '$self->{htdigest}");
+ $cb->reject;
+ return 1;
+}
+
+1;
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+
+use strict;
+use Test::More tests => 4;
+use lib 't/lib';
+BEGIN { $ENV{LOGLEVEL} ||= "FATAL" }
+BEGIN { require 'djabberd-test.pl' }
+
+use DJabberd::Authen::HTDigest;
+
+my $server = Test::DJabberd::Server->new(id => 1);
+$server->start([
+ DJabberd::RosterStorage::InMemoryOnly->new(),
+ DJabberd::Authen::HTDigest->new(realm => 'djabberd', htdigest => 't/htdigest.t.conf')
+ ]);
+
+eval {
+ my $client = Test::DJabberd::Client->new(server => $server, name => "testuser");
+ $client->login("foobar");
+};
+like($@, qr/bad password/, "bad password");
+
+eval {
+ my $client = Test::DJabberd::Client->new(server => $server, name => "testuser2");
+ $client->login("foo");
+};
+like($@, qr/bad password/, "correct password wrong username");
+
+
+my $client = Test::DJabberd::Client->new(server => $server, name => "testuser");
+$client->login("foo");
+pass("Client logged in");
+
+
+eval {
+ my $server2 = Test::DJabberd::Server->new(id => 1);
+ $server2->{plugins} = [
+ DJabberd::RosterStorage::InMemoryOnly->new(),
+ DJabberd::Authen::HTDigest->new(realm => 'djabberd', htdigest => 't/htdigest.t.conf.shouldnotbehere')
+ ];
+ $server2->start();
+};
+like($@, qr/does not exist/, "File should not be here, ignore previous line");
+
+
+
@@ -0,0 +1 @@
+testuser:djabberd:b62e499692e11fbc358497c545643b3c

0 comments on commit 7c449c4

Please sign in to comment.