1 # Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
3 # Copyright (C) 2005-2006 TWiki Contributors.
4 # All Rights Reserved. TWiki Contributors
5 # are listed in the AUTHORS file in the root of this distribution.
6 # NOTE: Please extend that file, not this notice.
8 # Additional copyrights apply to some or all of the code in this
10 # Copyright (C) 2005 Greg Abbas, twiki@abbas.org
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version. For
16 # more details read LICENSE in the root of this distribution.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 # As per the GPL, removal of this notice is prohibited.
26 ---+ package TWiki::LoginManager::TemplateLogin
28 This is a login manager that you can specify in the security setup section of
29 [[%SCRIPTURL{"configure"}%][configure]]. It provides users with a
30 template-based form to enter usernames and passwords, and works with the
31 PasswordManager that you specify to verify those passwords.
33 Subclass of TWiki::LoginManager; see that class for documentation of the
34 methods of this class.
38 package TWiki::LoginManager::TemplateLogin;
39 use base 'TWiki::LoginManager';
47 ---++ ClassMethod new ($session, $impl)
49 Construct the TemplateLogin object
54 my( $class, $session ) = @_;
55 my $this = $class->SUPER::new($session);
56 $session->enterContext( 'can_login' );
57 if ($TWiki::cfg{Sessions}{ExpireCookiesAfter}) {
58 $session->enterContext( 'can_remember_login' );
65 ---++ ObjectMethod forceAuthentication () -> boolean
67 method called when authentication is required - redirects to (...|view)auth
68 Triggered on auth fail
72 sub forceAuthentication {
74 my $twiki = $this->{twiki};
76 unless( $twiki->inContext( 'authenticated' )) {
77 my $query = $twiki->{cgiQuery};
78 # Redirect with passthrough so we don't lose the original query params
79 my $twiki = $this->{twiki};
80 my $topic = $twiki->{topicName};
81 my $web = $twiki->{webName};
82 my $url = $twiki->getScriptUrl( 0, 'login', $web, $topic);
83 $query->param( -name=>'origurl', -value=>$ENV{REQUEST_URI} );
84 $twiki->redirect( $url, 1 );
93 ---++ ObjectMethod loginUrl () -> $loginUrl
95 TODO: why is this not used internally? When is it called, and why
96 Content of a login link
102 my $twiki = $this->{twiki};
103 my $topic = $twiki->{topicName};
104 my $web = $twiki->{webName};
105 return $twiki->getScriptUrl( 0, 'login', $web, $topic,
106 origurl => $ENV{REQUEST_URI} );
111 ---++ ObjectMethod login( $query, $twiki )
113 If a login name and password have been passed in the query, it
114 validates these and if authentic, redirects to the original
115 script. If there is no username in the query or the username/password is
116 invalid (validate returns non-zero) then it prompts again.
118 If a flag to remember the login has been passed in the query, then the
119 corresponding session variable will be set. This will result in the
120 login cookie being preserved across browser sessions.
122 The password handler is expected to return a perl true value if the password
123 is valid. This return value is stored in a session variable called
124 VALIDATION. This is so that password handlers can return extra information
125 about the user, such as a list of TWiki groups stored in a separate
126 database, that can then be displayed by referring to
127 %<nop>SESSION_VARIABLE{"VALIDATION"}%
132 my( $this, $query, $twikiSession ) = @_;
133 my $twiki = $this->{twiki};
134 my $users = $twiki->{users};
136 my $origurl = $query->param( 'origurl' );
137 my $loginName = $query->param( 'username' );
138 my $loginPass = $query->param( 'password' );
139 my $remember = $query->param( 'remember' );
141 # Eat these so there's no risk of accidental passthrough
142 $query->delete('origurl', 'username', 'password');
144 # UserMappings can over-ride where the login template is defined
145 my $loginTemplate = $users->loginTemplateName(); #defaults to login.tmpl
146 my $tmpl = $twiki->templates->readTemplate(
147 $loginTemplate, $twiki->getSkin() );
149 my $banner = $twiki->templates->expandTemplate( 'LOG_IN_BANNER' );
151 my $topic = $twiki->{topicName};
152 my $web = $twiki->{webName};
154 my $cgisession = $this->{_cgisession};
156 $cgisession->param( 'REMEMBER', $remember ) if $cgisession;
157 if( $cgisession && $cgisession->param( 'AUTHUSER' ) &&
158 $loginName && $loginName ne $cgisession->param( 'AUTHUSER' )) {
159 $banner = $twiki->templates->expandTemplate( 'LOGGED_IN_BANNER' );
160 $note = $twiki->templates->expandTemplate( 'NEW_USER_NOTE' );
166 my $validation = $users->checkPassword( $loginName, $loginPass );
167 $error = $users->passwordError();
170 $this->userLoggedIn( $loginName );
171 $cgisession->param( 'VALIDATION', $validation ) if $cgisession;
172 if( !$origurl || $origurl eq $query->url() ) {
173 $origurl = $twiki->getScriptUrl( 0, 'view', $web, $topic );
175 #SUCCESS our user is authenticated..
176 $query->delete('sudo'); #remove the sudo param - its only to tell TemplateLogin that we're using BaseMapper..
177 # Redirect with passthrough
178 $twikiSession->redirect($origurl, 1 );
181 $banner = $twiki->templates->expandTemplate('UNRECOGNISED_USER');
185 # TODO: add JavaScript password encryption in the template
188 $twiki->{prefs}->pushPreferenceValues('SESSION', {ORIGURL=>$origurl, BANNER=>$banner, NOTE=>$note, ERROR=>$error});
190 $tmpl = $twiki->handleCommonTags( $tmpl, $web, $topic );
191 $tmpl = $twiki->renderer->getRenderedVersion( $tmpl, '' );
193 print $twiki->generateHTTPHeaders( $query );