| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Jifty::Plugin::Authentication::CAS::Action::CASLogin - process CAS login plugin | ||
| 7 | |||
| 8 | =cut | ||
| 9 | |||
| 10 | package Jifty::Plugin::Authentication::CAS::Action::CASLogin; | ||
| 11 | use base qw/Jifty::Action/; | ||
| 12 | |||
| 13 | |||
| 14 | =head2 arguments | ||
| 15 | |||
| 16 | Return the ticket form field | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | sub arguments { | ||
| 21 | return ( | ||
| 22 | { | ||
| 23 | ticket => { | ||
| 24 | label => 'cas ticket', | ||
| 25 | ajax_validates => 1, | ||
| 26 | }, | ||
| 27 | |||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | } | ||
| 32 | |||
| 33 | =head2 validate_ticket ST | ||
| 34 | |||
| 35 | for ajax_validates | ||
| 36 | Makes sure that the ticket submitted is legal. | ||
| 37 | |||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub validate_ticket { | ||
| 42 | my $self = shift; | ||
| 43 | my $ticket = shift; | ||
| 44 | |||
| 45 | if ( $ticket && $ticket !~ /^[A-Za-z0-9-]+$/ ) { | ||
| 46 | return $self->validation_error( | ||
| 47 | ticket => _("That doesn't look like a valid ticket.") ); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | return $self->validation_ok('ticket'); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | =head2 take_action | ||
| 56 | |||
| 57 | Actually check the user's password. If it's right, log them in. | ||
| 58 | Otherwise, throw an error. | ||
| 59 | |||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub take_action { | ||
| 64 | my $self = shift; | ||
| 65 | my $ticket = $self->argument_value('ticket'); | ||
| 66 | |||
| 67 | my ($plugin) = Jifty->find_plugin('Jifty::Plugin::Authentication::CAS'); | ||
| 68 | |||
| 69 | # my $service_url = ($ENV{SERVER_PORT} == 443)?'https://':'http://'. | ||
| 70 | # $ENV{HTTP_HOST}.'/caslogin'; | ||
| 71 | |||
| 72 | my $service_url = Jifty->web->url.'/caslogin'; | ||
| 73 | if ( Jifty->web->request->continuation ) { | ||
| 74 | $service_url .= '?J:C='.Jifty->web->request->continuation_id; | ||
| 75 | }; | ||
| 76 | |||
| 77 | if (! $ticket) { | ||
| 78 | my $login_url = $plugin->CAS->login_url( $service_url ); | ||
| 79 | Jifty->web->_redirect($login_url); | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $r = $plugin->CAS->service_validate($service_url,$ticket); | ||
| 84 | my $username; | ||
| 85 | if ($r->is_success) { | ||
| 86 | $username = $r->user(); | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | Jifty->log->info("CAS error: $ticket $username"); | ||
| 90 | return; | ||
| 91 | }; | ||
| 92 | |||
| 93 | my ($name,$email); | ||
| 94 | #TODO add a ldap conf to find name and email | ||
| 95 | $email = $username.'@'.$plugin->domain() if ($plugin->domain()); | ||
| 96 | |||
| 97 | # Load up the user | ||
| 98 | my $current_user = Jifty->app_class('CurrentUser'); | ||
| 99 | my $user = ($email) ? $current_user->new( email => $email) # load by email to mix authentication | ||
| 100 | : $current_user->new( cas_id => $username ); # else load by cas_id | ||
| 101 | |||
| 102 | # Autocreate the user if necessary | ||
| 103 | if ( not $user->id ) { | ||
| 104 | my $action = Jifty->web->new_action( | ||
| 105 | class => 'CreateUser', | ||
| 106 | current_user => $current_user->superuser, | ||
| 107 | arguments => { | ||
| 108 | cas_id => $username | ||
| 109 | } | ||
| 110 | ); | ||
| 111 | $action->run; | ||
| 112 | |||
| 113 | if ( not $action->result->success ) { | ||
| 114 | # Should this be less "friendly"? | ||
| 115 | $self->result->error(_("Sorry, something weird happened (we couldn't create a user for you). Try again later.")); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | $user = $current_user->new( cas_id => $username ); | ||
| 120 | } | ||
| 121 | |||
| 122 | my $u = $user->user_object; | ||
| 123 | |||
| 124 | |||
| 125 | # Update, just in case | ||
| 126 | $u->__set( column => 'cas_id', value => $username ) if (!$u->cas_id); | ||
| 127 | $u->__set( column => 'name', value => $username ) if (!$u->name); | ||
| 128 | $u->__set( column => 'name', value => $name ) if ($name); | ||
| 129 | $u->__set( column => 'email', value => $email ) if ($email); | ||
| 130 | |||
| 131 | # Actually do the signin thing. | ||
| 132 | Jifty->web->current_user( $user ); | ||
| 133 | Jifty->web->session->set_cookie; | ||
| 134 | |||
| 135 | # Success! | ||
| 136 | $self->report_success; | ||
| 137 | |||
| 138 | return 1; | ||
| 139 | }; | ||
| 140 | |||
| 141 | =head2 report_success | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub report_success { | ||
| 146 | my $self = shift; | ||
| 147 | $self->result->message(_("Hi %1!", Jifty->web->current_user->user_object->name )); | ||
| 148 | }; | ||
| 149 | |||
| 150 | |||
| 151 | 1; |