Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6c21fd13

Von Moritz Bunkus vor fast 12 Jahren hinzugefügt

  • ID 6c21fd13caa00ecee7acac38ac6395948dad20a7
  • Vorgänger 0e451e1b
  • Nachfolger adc9b96c

Automatische Authentifizierung bestehender Sessions über Session-ID + API-Token

Wird für CRM-Menü benötigt.

Unterschiede anzeigen:

SL/Auth.pm
592 592
  $cookie = $sth->fetchrow_hashref;
593 593
  $sth->finish;
594 594

  
595
  if (!$cookie || $cookie->{is_expired} || ($cookie->{ip_address} ne $ENV{REMOTE_ADDR})) {
595
  # The session ID provided is valid in the following cases:
596
  #  1. session ID exists in the database
597
  #  2. hasn't expired yet
598
  #  3. if form field '{AUTH}api_token' is given: form field must equal database column 'auth.session.api_token' for the session ID
599
  #  4. if form field '{AUTH}api_token' is NOT given then: the requestee's IP address must match the stored IP address
600
  $self->{api_token}   = $cookie->{api_token} if $cookie;
601
  my $api_token_cookie = $self->get_api_token_cookie;
602
  my $cookie_is_bad    = !$cookie || $cookie->{is_expired};
603
  $cookie_is_bad     ||= $api_token_cookie && ($api_token_cookie ne $cookie->{api_token}) if  $api_token_cookie;
604
  $cookie_is_bad     ||= $cookie->{ip_address} ne $ENV{REMOTE_ADDR}                       if !$api_token_cookie;
605
  if ($cookie_is_bad) {
596 606
    $self->destroy_session();
597 607
    $main::lxdebug->leave_sub();
598 608
    return $cookie ? SESSION_EXPIRED : SESSION_NONE;
......
791 801
    do_query($::form, $dbh, qq|INSERT INTO auth.session (id, ip_address, mtime) VALUES (?, ?, now())|, $session_id, $ENV{REMOTE_ADDR});
792 802
  }
793 803

  
804
  if ($self->{column_information}->has('api_token', 'session')) {
805
    my ($stored_api_token) = $dbh->selectrow_array(qq|SELECT api_token FROM auth.session WHERE id = ?|, undef, $session_id);
806
    do_query($::form, $dbh, qq|UPDATE auth.session SET api_token = ? WHERE id = ?|, $self->_create_session_id, $session_id) unless $stored_api_token;
807
  }
808

  
794 809
  my @values_to_save = grep    { $_->{fetched} }
795 810
                       values %{ $self->{SESSION} };
796 811
  if (@values_to_save) {
......
927 942
}
928 943

  
929 944
sub get_session_cookie_name {
930
  my $self = shift;
945
  my ($self, %params) = @_;
931 946

  
932
  return $self->{cookie_name} || 'lx_office_erp_session_id';
947
  $params{type}     ||= 'id';
948
  my $name            = $self->{cookie_name} || 'lx_office_erp_session_id';
949
  $name              .= '_api_token' if $params{type} eq 'api_token';
950

  
951
  return $name;
933 952
}
934 953

  
935 954
sub get_session_id {
936 955
  return $session_id;
937 956
}
938 957

  
958
sub get_api_token_cookie {
959
  my ($self) = @_;
960

  
961
  $::request->{cgi}->cookie($self->get_session_cookie_name(type => 'api_token'));
962
}
963

  
939 964
sub session_tables_present {
940 965
  $main::lxdebug->enter_sub();
941 966

  

Auch abrufbar als: Unified diff