Implement LibraryREST auth method
authorJeff Godin <jgodin@tadl.org>
Tue, 28 May 2013 17:56:41 +0000 (13:56 -0400)
committerJeff Godin <jgodin@tadl.org>
Tue, 28 May 2013 17:56:41 +0000 (13:56 -0400)
Initial LibraryREST implementation for Evergreen.

Use this with the experimental code here:
https://github.com/tadl/library-rest-api

Signed-off-by: Jeff Godin <jgodin@tadl.org>
Open-ILS/src/perlmods/MANIFEST
Open-ILS/src/perlmods/lib/OpenILS/LibraryREST.pm [new file with mode: 0644]

index 9f30b97..bbf20de 100644 (file)
@@ -115,6 +115,7 @@ lib/OpenILS/Application/Trigger/Validator/Acq/UserRequestReceived.pm
 lib/OpenILS/Application/Vandelay.pm
 lib/OpenILS/Const.pm
 lib/OpenILS/Event.pm
+lib/OpenILS/LibraryREST.pm
 lib/OpenILS/Perm.pm
 lib/OpenILS/Reporter/Proxy.pm
 lib/OpenILS/Reporter/SQLBuilder.pm
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/LibraryREST.pm b/Open-ILS/src/perlmods/lib/OpenILS/LibraryREST.pm
new file mode 100644 (file)
index 0000000..9d1d10e
--- /dev/null
@@ -0,0 +1,73 @@
+package OpenILS::LibraryREST;
+
+use strict;
+use warnings;
+
+use OpenSRF::System;
+use OpenSRF::AppSession;
+use OpenILS::Utils::Fieldmapper;
+use OpenSRF::Utils::SettingsClient;
+use OpenILS::Application::AppUtils;
+use OpenSRF::Utils qw/:datetime/;
+use DateTime::Format::ISO8601;
+my $U = 'OpenILS::Application::AppUtils';
+
+use Digest::MD5 qw(md5_hex);
+
+my $bsconfig = '/openils/conf/opensrf_core.xml';
+
+sub new {
+    my $class = shift;
+    my $type = ref($class) || $class;
+    my $self = {};
+
+    OpenSRF::System->bootstrap_client(config_file => $bsconfig);
+
+    $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
+
+    Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
+
+    return bless $self, $type;
+}
+
+sub get_auth {
+    my $self = shift;
+    my $user = shift;
+    my $pass = shift;
+
+    my $seed = $U->simplereq(
+                'open-ils.auth',
+                'open-ils.auth.authenticate.init', $user );
+
+    my $response = $U->simplereq(
+            'open-ils.auth',
+            'open-ils.auth.authenticate.complete',
+            {
+                    username => $user,
+                    password => md5_hex($seed . md5_hex($pass)),
+                    type     => 'opac',
+            }
+    );
+
+    if( my $code = $U->event_code($response) ) {
+            my $txt = $response->{textcode};
+            return { error => $txt};
+    }
+
+    my $token = $response->{payload}->{authtoken};
+
+    my $session = $U->simplereq(
+        'open-ils.auth',
+        'open-ils.auth.session.retrieve',
+        $token
+    );
+
+    return {
+        user => $session->id(),
+        token => $response->{payload}->{authtoken},
+    };
+
+    return 0;
+}
+
+1;