source: TI12-security/trunk/perl/NDG/Security/Client.pm @ 3774

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/perl/NDG/Security/Client.pm@3774
Revision 3774, 2.3 KB checked in by pjkersha, 12 years ago (diff)

ssoclient -> Security::Client

Line 
1#!/usr/bin/env perl
2package ndgsecurity::ssoclient;
3
4use Inline Python => <<'END';
5import base64
6def urlSafeB64Encode(str):
7    return base64.urlsafe_b64encode(str)
8 
9from paste.registry import RegistryManager, StackedObjectProxy
10session = StackedObjectProxy()
11
12from ndg.security.common.pylons.security_util import setSecuritySession
13def pySetSecuritySession(h, sid, u, org, rolesStr):
14    roles = rolesStr.split('r')
15    setSecuritySession(h=h, sid=sid, u=u, org=org, roles=roles)
16END
17
18use Storable qw(freeze thaw);
19use Crypt::CBC;
20use CGI;
21
22sub new 
23{
24    # Constructor
25    my $type = shift;
26    my $cgi = shift;
27    my $encryptionKey = "123456789";
28    my $self = {
29    "cgi"  => undef,
30    "encyptionKey" => $encryptionKey,
31    "cipher" => undef,
32    "cookieName" => "ndg-security",
33    };
34
35    bless $self;
36
37    $self->{cipher} = new Crypt::CBC(-key=>$encryptionKey);
38    $self->{cgi} = $cgi or CGI->new();
39    return $self;
40}
41
42sub sessionHandler
43{
44    my $self = shift;
45    if ($self->{cgi}->param('h'))
46    {
47        my $cookie = self->setSession();
48        my $returnTo = 
49            "http://" . $self->{cgi}->virtual_host() . $self->{cgi}->url(-absolute=>1);
50        $self->{cgi}->redirect(-uri=>$returnTo, -cookie=>$cookie, -nph=>1);
51    }
52}
53
54
55sub makeCookie
56{
57    my $self = shift;
58    my %session = shift;
59    my $serializedSess = freeze(\%session);
60   
61    my $encrSess = $self->{cipher}->encrypt_hex($serializedSess);
62    my $cookie = $self->{cgi}->cookie(
63    #new CGI::Cookie(
64            -name=>$self->{cookieName},
65            -value=>$encrSess,
66            -path=>'/',
67            -expires=>'+8h'
68            );
69           
70    return $cookie;
71}
72
73
74sub getCookie
75{
76    my $self = shift;
77   
78    my $cookie = $self->{cgi}->cookie($self->{cookieName});
79    my $serialisedSess = $self->{cipher}->decrypt_hex($cookie);
80    my %session = thaw($serializedSess);
81    return %session;
82}
83 
84 
85sub setSession
86{
87    my $self = shift;
88   
89    # Process response from IdP
90    my $cgi = $self->{cgi};
91    my @roles = split(',', $cgi->param('roles'));
92    # Separate out NDG Security session args
93    my %session = (
94        h => $cgi->param('h'), 
95        sid => $cgi->param('sid'), 
96        u => $cgi->param('u'), 
97        org => $cgi->param('org'), 
98        roles => @roles);
99       
100    return $self->makeCookie(%session);
101}
102
1031;    # ensure good finish
104__END__
105
106 
Note: See TracBrowser for help on using the repository browser.