| … | |
… | |
| 22 | |
22 | |
| 23 | =cut |
23 | =cut |
| 24 | |
24 | |
| 25 | use strict; |
25 | use strict; |
| 26 | use warnings; |
26 | use warnings; |
|
|
27 | use Apache::Cookie; |
|
|
28 | use Data::Dumper; |
| 27 | |
29 | |
| 28 | sub new($$$) { |
30 | sub new($$$) { |
| 29 | my $invocant = shift; |
31 | my $invocant = shift; |
| 30 | my $class = ref($invocant) || $invocant; |
32 | my $class = ref($invocant) || $invocant; |
| 31 | my $self = {}; |
33 | my $self = {}; |
| … | |
… | |
| 86 | $self->{db}->deleteKey($userID); |
88 | $self->{db}->deleteKey($userID); |
| 87 | return 0; |
89 | return 0; |
| 88 | } |
90 | } |
| 89 | } |
91 | } |
| 90 | |
92 | |
|
|
93 | sub checkCookie { |
|
|
94 | my ($self, $user, $key) = @_; |
|
|
95 | my $r = $self->{r}; |
|
|
96 | my %cookies = Apache::Cookie->fetch; |
|
|
97 | my $cookie = $cookies{WeBWorKAuthentication}; |
|
|
98 | if ($cookie) { |
|
|
99 | my ($user, $key) = $cookie->value =~ m/^user=([^&]*)&key=([^&]*)$/; |
|
|
100 | return $user, $key; |
|
|
101 | } |
|
|
102 | } |
|
|
103 | |
|
|
104 | sub sendCookie { |
|
|
105 | my ($self, $user, $key) = @_; |
|
|
106 | my $r = $self->{r}; |
|
|
107 | my $ce = $self->{ce}; |
|
|
108 | my $cookie = Apache::Cookie->new($r, |
|
|
109 | -name => "WeBWorKAuthentication", |
|
|
110 | -value => "user=$user&key=$key", |
|
|
111 | -expires => "+30D", |
|
|
112 | -domain => $r->hostname, |
|
|
113 | -path => $ce->{webworkURLRoot}, |
|
|
114 | -secure => 0, |
|
|
115 | ); |
|
|
116 | $r->headers_out->set("Set-Cookie" => $cookie->as_string); |
|
|
117 | } |
|
|
118 | |
| 91 | # verify will return 1 if the person is who they say the are. If the |
119 | # verify will return 1 if the person is who they say the are. If the |
| 92 | # verification failed because of of invalid authentication data, a note will be |
120 | # verification failed because of of invalid authentication data, a note will be |
| 93 | # written in the request explaining why it failed. If the request failed because |
121 | # written in the request explaining why it failed. If the request failed because |
| 94 | # no authentication data was provided, however, no note will be written, as this |
122 | # no authentication data was provided, however, no note will be written, as this |
| 95 | # is expected to happen whenever someone types in a URL manually, and is not |
123 | # is expected to happen whenever someone types in a URL manually, and is not |
| … | |
… | |
| 115 | # This block is here so we can "last" out of it when we've |
143 | # This block is here so we can "last" out of it when we've |
| 116 | # decided whether we're going to succeed or fail. |
144 | # decided whether we're going to succeed or fail. |
| 117 | |
145 | |
| 118 | # no authentication data was given. this is OK. |
146 | # no authentication data was given. this is OK. |
| 119 | unless (defined $user or defined $passwd or defined $key) { |
147 | unless (defined $user or defined $passwd or defined $key) { |
|
|
148 | # check to see if a cookie was sent by the browser. if so, use the |
|
|
149 | # user and key from the cookie for authentication. note that the |
|
|
150 | # cookie is only used if no credentials are sent as parameters. |
|
|
151 | my ($cookieUser, $cookieKey) = $self->checkCookie; |
|
|
152 | if ($cookieUser and $cookieKey) { |
|
|
153 | $r->param("user", $cookieUser); |
|
|
154 | $r->param("key", $cookieKey); |
|
|
155 | $user = $cookieUser; |
|
|
156 | $key = $cookieKey; |
|
|
157 | } else { |
| 120 | $failWithoutError = 1; |
158 | $failWithoutError = 1; |
| 121 | last VERIFY; |
159 | last VERIFY; |
|
|
160 | } |
| 122 | } |
161 | } |
| 123 | |
162 | |
| 124 | if (defined $user and $force_passwd_authen) { |
163 | if (defined $user and $force_passwd_authen) { |
| 125 | $failWithoutError = 1; |
164 | $failWithoutError = 1; |
| 126 | last VERIFY; |
165 | last VERIFY; |
| … | |
… | |
| 231 | } elsif ($failWithoutError) { |
270 | } elsif ($failWithoutError) { |
| 232 | # authentication failed, but not in a bad way |
271 | # authentication failed, but not in a bad way |
| 233 | return 0; |
272 | return 0; |
| 234 | } else { |
273 | } else { |
| 235 | # autentication succeeded! |
274 | # autentication succeeded! |
|
|
275 | # send a cookie with the user and key that were accepted. |
|
|
276 | if ($r->param("send_cookie")) { |
|
|
277 | $self->sendCookie($r->param("user"), $r->param("key")); |
|
|
278 | } |
| 236 | return 1; |
279 | return 1; |
| 237 | } |
280 | } |
| 238 | |
281 | |
| 239 | # Whatever you do, don't delete this! |
282 | # Whatever you do, don't delete this! |
| 240 | critical($r); |
283 | critical($r); |
|
|
284 | # One time, I deleted it, and my mother broke her back, my cat died, and |
|
|
285 | # the Pope got a tummy ache. When I replaced the line, I received eternal |
|
|
286 | # salvation and a check for USD 500. |
| 241 | } |
287 | } |
| 242 | |
288 | |
| 243 | 1; |
289 | 1; |
| 244 | |
290 | |
| 245 | __END__ |
291 | __END__ |