File Coverage

blib/lib/WebService/Cmis/Agent/TokenAuth.pm
Criterion Covered Total %
statement 12 48 25.0
branch 0 14 0.0
condition 0 3 0.0
subroutine 4 8 50.0
pod 4 4 100.0
total 20 77 25.9


line stmt bran cond sub pod time code
1             package WebService::Cmis::Agent::TokenAuth;
2              
3             =head1 NAME
4              
5             WebService::Cmis::Agent::TokenAuth - token-based authentication handler
6              
7             =head1 DESCRIPTION
8              
9             This user agent adds tocken-based authentication for subsequent calls to the
10             CMIS backend while the first one is still performed using HTTP basic auth. Alfresco
11             is one server that implements token-based authentication.
12              
13             my $client = WebService::Cmis::getClient(
14             url => "http://cmis.alfresco.com/service/cmis",
15             useragent => new WebService::Cmis::Agent::TokenAuth(
16             loginUrl => "http://cmis.alfresco.com/service/api/login?u={username}&pw={password}",
17             logoutUrl => "http://cmis.alfresco.com/service/api/login/ticket/{ticket}"
18             )
19             );
20            
21             $client->login(
22             user => "user",
23             password => "password",
24             );
25              
26             my $repo = $client->getRepository;
27              
28             Parent class: L
29              
30             =cut
31              
32 1     1   5561 use strict;
  1         3  
  1         32  
33 1     1   5 use warnings;
  1         2  
  1         25  
34              
35 1     1   5 use WebService::Cmis::Agent ();
  1         2  
  1         34  
36             our @ISA = qw(WebService::Cmis::Agent);
37              
38 1     1   4 use Error qw(:try);
  1         10  
  1         8  
39              
40             =head1 METHODS
41              
42             =over 4
43              
44             =item new(%params)
45              
46             Create a new WebService::Cmis::Agent::TokenAuth. It remembers the session state
47             using a token that is used instead of the normal user credentials to authenticate.
48              
49             Parameters:
50              
51             =over 4
52              
53             =item * user
54              
55             =item * password
56              
57             =item * loginUrl - url used for ticket-based authentication; example:
58              
59             "http://cmis.alfresco.com/service/api/login?u={username}&pw={password}"
60              
61             =item * logoutUrl - url used for ticket-based authentication; example:
62              
63             "http://cmis.alfresco.com/service/api/login/ticket/{ticket}"
64              
65             =back
66              
67             See L for more options.
68              
69             =cut
70              
71             sub new {
72 0     0 1   my ($class, %params) = @_;
73              
74 0           my $user = delete $params{user};
75 0           my $password = delete $params{password};
76 0           my $loginUrl = delete $params{loginUrl};
77 0           my $logoutUrl = delete $params{logoutUrl};
78              
79 0           my $this = $class->SUPER::new(%params);
80              
81 0           $this->{user} = $user;
82 0           $this->{password} = $password;
83 0           $this->{loginUrl} = $loginUrl;
84 0           $this->{logoutUrl} = $logoutUrl;
85              
86 0           return $this;
87             }
88              
89             =item login(%params) -> $ticket
90              
91             logs in to the web service
92              
93             Parameters:
94              
95             =over 4
96              
97             =item * user
98              
99             =item * password
100              
101             =item * ticket
102              
103             =back
104              
105             Login using basic auth. A ticket will be aquired to be
106             used for later logins for the same user.
107              
108             my $ticket = $client->login({
109             user => "user",
110             password => "pasword"
111             });
112              
113             $client->login({
114             user => "user",
115             ticket => "ticket"
116             });
117              
118             =cut
119              
120             sub login {
121 0     0 1   my $this = shift;
122 0           my %params = @_;
123              
124 0 0         $this->{user} = $params{user} if defined $params{user};
125 0 0         $this->{password} = $params{password} if defined $params{password};
126 0 0         $this->{ticket} = $params{ticket} if defined $params{ticket};
127              
128 0           my $loginUrl = $this->{loginUrl};
129              
130 0 0         unless(defined $this->{ticket}) {
131              
132 0           $loginUrl =~ s/{username}/$this->{user}/g;
133 0           $loginUrl =~ s/{password}/$this->{password}/g;
134              
135 0           my $doc = $this->{client}->get($loginUrl);
136 0           $this->{ticket} = $doc->findvalue("ticket");
137              
138 0 0         throw Error::Simple("no ticket found in response: ".$doc->toString(1))
139             unless defined $this->{ticket};
140             }
141              
142 0           return $this->{ticket};
143             }
144              
145             =item logout()
146              
147             logs out of the web service deleting a ticket previously aquired
148              
149             =cut
150              
151             sub logout {
152 0     0 1   my $this = shift;
153              
154 0 0 0       if (defined $this->{logoutUrl} && defined $this->{ticket}) {
155 0           my $logoutUrl = $this->{logoutUrl};
156 0           $logoutUrl =~ s/{ticket}/$this->{ticket}/g;
157 0           $this->{client}->delete($logoutUrl);
158             }
159              
160 0           $this->{user} = undef;
161 0           $this->{password} = undef;
162 0           $this->{ticket} = undef;
163             }
164              
165             =item get_basic_credentials()
166              
167             overrides the method in LWP::UserAgent to implement the given authentication mechanism.
168              
169             =cut
170              
171             sub get_basic_credentials {
172 0     0 1   my $this = shift;
173              
174             #print STDERR "TokenAuth::get_basic_credentials\n";
175 0           my $ticket = $this->{ticket};
176              
177             # TODO: any other "ticket users"?
178 0 0         return ('ROLE_TICKET', $ticket) if defined $ticket;
179              
180             #print STDERR "TokenAuth::fallback\n";
181 0           return ($this->{user}, $this->{password});
182             }
183              
184             =back
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             Copyright 2012-2013 Michael Daum
189              
190             This module is free software; you can redistribute it and/or modify it under
191             the same terms as Perl itself. See F.
192              
193             =cut
194              
195             1;