| 1 | package Bio::Graphics::Browser2::Plugin::LDAPAuthenticate; | ||
| 2 | |||
| 3 | # $Id$ | ||
| 4 | use strict; | ||
| 5 | use base 'Bio::Graphics::Browser2::Plugin::AuthPlugin'; | ||
| 6 | |||
| 7 | use Net::LDAP; | ||
| 8 | use Carp 'croak'; | ||
| 9 | #use constant SERVER => 'ldap.res.oicr.on.ca'; | ||
| 10 | #use constant PEOPLE_BASE => 'ou=People,dc=oicr,dc=on,dc=ca'; | ||
| 11 | #use constant GROUPS_BASE => 'ou=Groups,dc=oicr,dc=on,dc=ca'; | ||
| 12 | |||
| 13 | sub authenticate { | ||
| 14 | my $self = shift; | ||
| 15 | my ($name,$password) = $self->credentials; | ||
| 16 | |||
| 17 | my $ldap = $self->_ldap_connect or return; | ||
| 18 | |||
| 19 | # possibly bind to the server if a root DN and password are needed | ||
| 20 | $self->_initial_bind($ldap); | ||
| 21 | |||
| 22 | # do a search to get the user dn with which to bind | ||
| 23 | my $search = $ldap->search( | ||
| 24 | base => $self->people_base, | ||
| 25 | filter => "(&(objectClass=posixAccount)(uid=$name))" | ||
| 26 | ) or die $@; | ||
| 27 | return unless $search->count == 1; | ||
| 28 | |||
| 29 | my $entry = $search->entry(0); | ||
| 30 | |||
| 31 | # now attempt to authenticate with the password | ||
| 32 | my $message = $ldap->bind($entry->dn, password => $password); | ||
| 33 | return if $message->is_error; | ||
| 34 | |||
| 35 | # get user's full name from the gecos and/or cn/sn fields | ||
| 36 | my ($gecos) = $entry->get('gecos'); | ||
| 37 | $gecos =~ s/,+$//; # trailing unused fields | ||
| 38 | my $fullname = join ' ',($entry->get('cn'),$entry->get('sn')); | ||
| 39 | $ldap->unbind; | ||
| 40 | return ($name,$gecos||$fullname||$name); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub user_in_group { | ||
| 44 | my $self = shift; | ||
| 45 | my ($user,$group) = @_; | ||
| 46 | |||
| 47 | my $ldap = $self->_ldap_connect or return; | ||
| 48 | |||
| 49 | # do an anonymous search for the posix group with the | ||
| 50 | # indicated uid member. | ||
| 51 | my $search = $ldap->search( | ||
| 52 | base => $self->groups_base, | ||
| 53 | filter => "(&(objectClass=posixGroup)(memberUid=$user))" | ||
| 54 | ); | ||
| 55 | my @entries = $search->entries; | ||
| 56 | |||
| 57 | my %groups = map {$_->get('cn')=>1} @entries; | ||
| 58 | $ldap->unbind; | ||
| 59 | |||
| 60 | return $groups{$group}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _ldap_connect { | ||
| 64 | my $self = shift; | ||
| 65 | my $ldap = Net::LDAP->new($self->server); | ||
| 66 | unless ($ldap) { | ||
| 67 | warn "Could not connect to server ",$self->server; | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | return $ldap; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _initial_bind { | ||
| 74 | my $self = shift; | ||
| 75 | my $ldap = shift; | ||
| 76 | my $bind_dn = $self->bind_dn or return; | ||
| 77 | my $bind_pass = $self->bind_pass or return; | ||
| 78 | my $message = $ldap->bind($bind_dn,password=>$bind_pass); | ||
| 79 | return unless $message->is_error; | ||
| 80 | my $text = $message->error_text; | ||
| 81 | warn <<END; | ||
| 82 | Error during initial binding to $bind_dn: | ||
| 83 | '$text' | ||
| 84 | All LDAP logins may fail. | ||
| 85 | END | ||
| 86 | ; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub server { | ||
| 90 | return shift->required_setting('ldap server'); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub people_base { | ||
| 94 | return shift->required_setting('people base'); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub groups_base { | ||
| 98 | return shift->required_setting('groups base'); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub bind_dn { | ||
| 102 | return shift->setting('bind dn'); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub bind_pass { | ||
| 106 | return shift->setting('bind pass'); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub required_setting { | ||
| 110 | my $self = shift; | ||
| 111 | my $option = shift; | ||
| 112 | my $value = $self->setting($option); | ||
| 113 | croak "You must set the '$option' option in the [LDAPAuthenticate:plugin] section of GBrowse.conf\n" | ||
| 114 | unless defined $value; | ||
| 115 | return $value; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |