| 1 | package OpenInteract::Auth; | ||
| 2 | |||
| 3 | # $Id: Auth.pm,v 1.18 2002/06/04 11:29:54 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Data::Dumper qw( Dumper ); | ||
| 7 | |||
| 8 | $OpenInteract::Auth::VERSION = sprintf("%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/); | ||
| 9 | |||
| 10 | |||
| 11 | # Authenticate a user -- after calling this method if | ||
| 12 | # $R->{auth}{logged_in} is true then $R->{auth}{user} will be a | ||
| 13 | # user object. | ||
| 14 | |||
| 15 | sub user { | ||
| 16 | my ( $class ) = @_; | ||
| 17 | my $R = OpenInteract::Request->instance; | ||
| 18 | |||
| 19 | my ( $uid ); | ||
| 20 | |||
| 21 | # Check to see if the user is in the session | ||
| 22 | |||
| 23 | my $user_refresh = $R->CONFIG->{session_info}{cache_user}; | ||
| 24 | if ( $user_refresh > 0 ) { | ||
| 25 | if ( my $user = $R->{session}{_oi_cache}{user} ) { | ||
| 26 | if ( time < $R->{session}{_oi_cache}{user_refresh_on} ) { | ||
| 27 | $R->DEBUG && $R->scrib( 1, "Got user from session ok" ); | ||
| 28 | $R->{auth}{user} = $user; | ||
| 29 | $R->{auth}{logged_in} = 1; | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | # If we need to refresh the user object, pull the $uid out | ||
| 34 | # so we know what to refresh... | ||
| 35 | $R->DEBUG && $R->scrib( 1, "User session cache expired; refreshing from db" ); | ||
| 36 | $uid = $user->id; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | $uid ||= $class->fetch_user_id; | ||
| 41 | if ( $uid ) { | ||
| 42 | $R->DEBUG && $R->scrib( 1, "Found uid [$uid]; fetching user" ); | ||
| 43 | |||
| 44 | $R->{auth}{user} = eval { $class->fetch_user( $uid ) }; | ||
| 45 | |||
| 46 | # If there's a failure fetching the user, we need to ensure that | ||
| 47 | # this user_id is not passed back to us again so we don't keep | ||
| 48 | # going through this process... | ||
| 49 | |||
| 50 | if ( $@ or ! $R->{auth}{user} ) { | ||
| 51 | return $class->fetch_user_failed( $uid ); | ||
| 52 | } | ||
| 53 | |||
| 54 | $R->DEBUG && $R->scrib( 1, "User found [$R->{auth}{user}{login_name}]" ); | ||
| 55 | $R->{auth}{logged_in} = 1; | ||
| 56 | |||
| 57 | $class->check_first_login; | ||
| 58 | if ( $user_refresh > 0 ) { | ||
| 59 | $class->set_cached_user; | ||
| 60 | } | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | $R->DEBUG && $R->scrib( 1, "No uid found in session. Finding login info." ); | ||
| 64 | |||
| 65 | # If no user info found, check to see if the user logged in | ||
| 66 | |||
| 67 | $class->login_user; | ||
| 68 | |||
| 69 | # If not, create an 'empty' user and we're done | ||
| 70 | |||
| 71 | unless ( $R->{auth}{user} ) { | ||
| 72 | $R->DEBUG && $R->scrib( 1, "Creating the not-logged-in user." ); | ||
| 73 | $class->create_nologin_user; | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | $R->{auth}{logged_in} = 1; | ||
| 78 | $class->remember_login; | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | # Just grab the user_id from somewhere | ||
| 83 | |||
| 84 | sub fetch_user_id { | ||
| 85 | my ( $class ) = @_; | ||
| 86 | my $R = OpenInteract::Request->instance; | ||
| 87 | return $R->{session}{user_id}; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | # Use the user_id to create a user (don't use eval {} around the | ||
| 92 | # fetch(), this should die if it fails) | ||
| 93 | |||
| 94 | sub fetch_user { | ||
| 95 | my ( $class, $uid ) = @_; | ||
| 96 | my $R = OpenInteract::Request->instance; | ||
| 97 | return $R->user->fetch( $uid, { skip_security => 1 } ); | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | # What to do if the user fetch fails | ||
| 102 | |||
| 103 | sub fetch_user_failed { | ||
| 104 | my ( $class, $uid ) = @_; | ||
| 105 | my $R = OpenInteract::Request->instance; | ||
| 106 | OpenInteract::Error->set( SPOPS::Error->get ); | ||
| 107 | $R->throw({ code => 311 }); | ||
| 108 | $R->{session}{user_id} = undef; | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | # If there's a removal date, then this is the user's first login | ||
| 114 | |||
| 115 | # TODO: Check if this is working, if it's needed, ... | ||
| 116 | |||
| 117 | sub check_first_login { | ||
| 118 | my ( $class ) = @_; | ||
| 119 | my $R = OpenInteract::Request->instance; | ||
| 120 | if ( $R->{auth}{user}{removal_date} ) { | ||
| 121 | $R->DEBUG && $R->scrib( 1, "First login for user! Do some cleanup." ); | ||
| 122 | $R->{auth}{user}{removal_date} = undef; | ||
| 123 | |||
| 124 | # blank out the removal date -- note that this doesn't seem to | ||
| 125 | # work properly, and put the user in the public group | ||
| 126 | |||
| 127 | eval { | ||
| 128 | $R->{auth}{user}->save; | ||
| 129 | $R->{auth}{user}->make_public; | ||
| 130 | }; | ||
| 131 | |||
| 132 | # need to check for save/security errors here | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | # If no user found elsewhere, see if a login_name and password were | ||
| 138 | # passed in; if so, try and login the user and track the info | ||
| 139 | |||
| 140 | sub login_user { | ||
| 141 | my ( $class ) = @_; | ||
| 142 | my $R = OpenInteract::Request->instance; | ||
| 143 | my $CONFIG = $R->CONFIG; | ||
| 144 | |||
| 145 | my $login_field = $CONFIG->{login}{login_field}; | ||
| 146 | my $password_field = $CONFIG->{login}{password_field}; | ||
| 147 | unless ( $login_field and $password_field ) { | ||
| 148 | $R->throw({ code => 205, type => 'system' }); | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | my $login_name = $R->apache->param( $login_field ); | ||
| 153 | return undef unless ( $login_name ); | ||
| 154 | $R->DEBUG && $R->scrib( 1, "Found login name from form: ($login_name)" ); | ||
| 155 | |||
| 156 | my $user = eval { $R->user->fetch_by_login_name( $login_name, | ||
| 157 | { return_single => 1, | ||
| 158 | skip_security => 1 } ) }; | ||
| 159 | if ( $@ ) { | ||
| 160 | my $ei = SPOPS::Error->get; | ||
| 161 | $R->scrib( 0, "Error when fetching by login name: $ei->{system_msg}\n" ); | ||
| 162 | } | ||
| 163 | unless ( $user ) { | ||
| 164 | $R->scrib( 0, "User with login ($login_name) not found. Throwing auth error" ); | ||
| 165 | $R->throw({ code => 401, | ||
| 166 | type => 'authenticate', | ||
| 167 | extra => { login_name => $login_name } }); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | # Check the password | ||
| 172 | |||
| 173 | my $password = $R->apache->param( $password_field ); | ||
| 174 | $R->DEBUG && $R->scrib( 5, "Password entered: ($password)" ); | ||
| 175 | unless ( $user->check_password( $password ) ) { | ||
| 176 | $R->scrib( 0, "Password check for ($login_name) failed. Throwing auth error" ); | ||
| 177 | $R->throw({ code => 402, | ||
| 178 | type => 'authenticate', | ||
| 179 | extra => { login_name => $login_name } }); | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | $R->DEBUG && $R->scrib( 1, "Passwords matched; UID ($user->{user_id})" ); | ||
| 183 | |||
| 184 | # Persist the user ID via the session (whether the session is | ||
| 185 | # transient is handled in 'remember_login()') | ||
| 186 | |||
| 187 | $R->{session}{user_id} = $user->id; | ||
| 188 | $R->{auth}{user} = $user; | ||
| 189 | $class->set_cached_user; | ||
| 190 | |||
| 191 | return $R->{auth}{user}; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | sub set_cached_user { | ||
| 196 | my ( $class ) = @_; | ||
| 197 | my $R = OpenInteract::Request->instance; | ||
| 198 | my $user_refresh = $R->CONFIG->{session_info}{cache_user}; | ||
| 199 | return unless ( $user_refresh > 0 ); | ||
| 200 | $R->{session}{_oi_cache}{user} = $R->{auth}{user}; | ||
| 201 | $R->{session}{_oi_cache}{user_refresh_on} = time + ( $user_refresh * 60 ); | ||
| 202 | $R->DEBUG && $R->scrib( 1, "Set user to session cache, expires ", | ||
| 203 | "in [$user_refresh] minutes" ); | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | # If we created a user, make the expiration transient unless told otherwise. | ||
| 208 | |||
| 209 | sub remember_login { | ||
| 210 | my ( $class ) = @_; | ||
| 211 | my $R = OpenInteract::Request->instance; | ||
| 212 | return unless ( $R->{auth}{user} ); | ||
| 213 | return if ( $R->CONFIG->{login}{always_remember} ); | ||
| 214 | |||
| 215 | my $remember_field = $R->CONFIG->{login}{remember_field}; | ||
| 216 | if ( ! $remember_field or ! $R->apache->param( $remember_field ) ) { | ||
| 217 | $R->{session}{expiration} = undef; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | # Create a 'dummy' user | ||
| 222 | |||
| 223 | sub create_nologin_user { | ||
| 224 | my ( $class ) = @_; | ||
| 225 | my $R = OpenInteract::Request->instance; | ||
| 226 | $R->{auth}{logged_in} = 0; | ||
| 227 | delete $R->{session}{user_id}; | ||
| 228 | return $R->{auth}{user} = $R->user->new({ login_name => 'anonymous', | ||
| 229 | first_name => 'Anonymous', | ||
| 230 | last_name => 'User', | ||
| 231 | user_id => 99999 }); | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | # If the user is logged in, retrieve the groups he/she/it belongs to | ||
| 236 | |||
| 237 | sub group { | ||
| 238 | my ( $class ) = @_; | ||
| 239 | my $R = OpenInteract::Request->instance; | ||
| 240 | unless ( $R->{auth}{logged_in} ) { | ||
| 241 | $R->DEBUG && $R->scrib( 1, "No logged-in user found, not retrieving groups." ); | ||
| 242 | return; | ||
| 243 | } | ||
| 244 | $R->DEBUG && $R->scrib( 1, "Authenticated user exists; getting groups." ); | ||
| 245 | |||
| 246 | # Check to see if the group is in the session | ||
| 247 | my $group_refresh = $R->CONFIG->{session_info}{cache_group}; | ||
| 248 | if ( $group_refresh > 0 ) { | ||
| 249 | if ( my $groups = $R->{session}{_oi_cache}{group} ) { | ||
| 250 | if ( time < $R->{session}{_oi_cache}{group_refresh_on} ) { | ||
| 251 | $R->DEBUG && $R->scrib( 1, "Got groups from session ok" ); | ||
| 252 | $R->{auth}{group} = $groups; | ||
| 253 | return; | ||
| 254 | } | ||
| 255 | $R->DEBUG && $R->scrib( 1, "Group session cache expired; refreshing from db" ); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | $R->{auth}{group} = eval { $R->{auth}{user}->group }; | ||
| 260 | if ( $@ ) { | ||
| 261 | OpenInteract::Error->set( SPOPS::Error->get ); | ||
| 262 | $R->throw({ code => 309 }); | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | if ( $group_refresh > 0 ) { | ||
| 266 | $R->{session}{_oi_cache}{group} = $R->{auth}{group}; | ||
| 267 | $R->{session}{_oi_cache}{group_refresh_on} = time + ( $group_refresh * 60 ); | ||
| 268 | $R->DEBUG && $R->scrib( 1, "Set groups to session cache, expires ", | ||
| 269 | "in [$group_refresh] minutes" ); | ||
| 270 | } | ||
| 271 | $R->DEBUG && $R->scrib( 2, "Retrieved groups: ", | ||
| 272 | join( ', ', map { "($_->{name})" } @{ $R->{auth}{group} } ) ); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | sub is_admin { | ||
| 278 | my ( $class ) = @_; | ||
| 279 | my $R = OpenInteract::Request->instance; | ||
| 280 | |||
| 281 | return unless ( $R->{auth}{logged_in} ); | ||
| 282 | return unless ( ref $R->{auth}{group} eq 'ARRAY' ); | ||
| 283 | |||
| 284 | my $CONFIG = $R->CONFIG; | ||
| 285 | |||
| 286 | if ( $R->{auth}{user}->id eq $CONFIG->{default_objects}{superuser} ) { | ||
| 287 | return $R->{auth}{is_admin}++; | ||
| 288 | } | ||
| 289 | |||
| 290 | my $site_admin_id = $CONFIG->{default_objects}{site_admin_group}; | ||
| 291 | my $supergroup_id = $CONFIG->{default_objects}{supergroup}; | ||
| 292 | foreach my $group ( @{ $R->{auth}{group} } ) { | ||
| 293 | my $group_id = $group->id; | ||
| 294 | if ( $group_id eq $site_admin_id or $group_id eq $supergroup_id ) { | ||
| 295 | return $R->{auth}{is_admin}++ ; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | ######################################## | ||
| 302 | # CUSTOM AUTH METHOD | ||
| 303 | |||
| 304 | sub custom_handler { | ||
| 305 | my ( $class ) = @_; | ||
| 306 | my $R = OpenInteract::Request->instance; | ||
| 307 | my $CONFIG = $R->CONFIG; | ||
| 308 | my $custom_class = $CONFIG->{login}{custom_handler}; | ||
| 309 | return unless ( $custom_class ); | ||
| 310 | |||
| 311 | eval "require $custom_class"; | ||
| 312 | if ( $@ ) { | ||
| 313 | $R->scrib( 0, "Tried to use custom login handler [$custom_class]", | ||
| 314 | "but requiring the class failed: $@" ); | ||
| 315 | return; | ||
| 316 | } | ||
| 317 | my $custom_method = $CONFIG->{login}{custom_method} | ||
| 318 | || 'handler'; | ||
| 319 | $R->scrib( 1, "Custom login handler/method being used: ", | ||
| 320 | "[$custom_class] [$custom_method]" ); | ||
| 321 | eval { $custom_class->$custom_method() }; | ||
| 322 | if ( $@ ) { | ||
| 323 | $R->scrib( 0, "Custom login handler died with: $@" ); | ||
| 324 | $class->custom_handler_failed; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | sub custom_handler_failed { | ||
| 329 | my ( $class ) = @_; | ||
| 330 | my $R = OpenInteract::Request->instance; | ||
| 331 | my $CONFIG = $R->CONFIG; | ||
| 332 | my $custom_class = $CONFIG->{login}{custom_handler}; | ||
| 333 | my $fail_method = $CONFIG->{login}{custom_fail_method}; | ||
| 334 | return unless ( $fail_method ); | ||
| 335 | eval { $custom_class->$fail_method() }; | ||
| 336 | if ( $@ ) { | ||
| 337 | $R->scrib( 0, "Caught error from failure method [$custom_class]", | ||
| 338 | "[$fail_method]: $@" ); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | 1; | ||
| 344 | |||
| 345 | __END__ |