2007-09-04 17:39:51 +02:00
|
|
|
/* SOGoWebAuthenticator.m - this file is part of SOGo
|
|
|
|
*
|
2009-09-25 16:42:33 +02:00
|
|
|
* Copyright (C) 2007-2009 Inverse inc.
|
2007-09-04 17:39:51 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2007-09-07 00:17:56 +02:00
|
|
|
#import <Foundation/NSCalendarDate.h>
|
2007-09-04 17:39:51 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
|
|
|
|
2007-09-05 19:57:17 +02:00
|
|
|
#import <NGObjWeb/SoDefaultRenderer.h>
|
2007-09-04 17:39:51 +02:00
|
|
|
#import <NGObjWeb/WOApplication.h>
|
|
|
|
#import <NGObjWeb/WOContext.h>
|
2007-09-07 00:17:56 +02:00
|
|
|
#import <NGObjWeb/WOCookie.h>
|
2007-09-04 17:39:51 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2007-09-07 00:17:56 +02:00
|
|
|
#import <NGExtensions/NSCalendarDate+misc.h>
|
2008-07-24 20:00:21 +02:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2007-09-04 17:39:51 +02:00
|
|
|
#import <NGLdap/NGLdapConnection.h>
|
|
|
|
|
|
|
|
#import <UI/MainUI/SOGoRootPage.h>
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
#import "SOGoUserManager.h"
|
2007-09-04 17:39:51 +02:00
|
|
|
#import "SOGoPermissions.h"
|
|
|
|
#import "SOGoUser.h"
|
|
|
|
|
|
|
|
#import "SOGoWebAuthenticator.h"
|
|
|
|
|
|
|
|
@implementation SOGoWebAuthenticator
|
|
|
|
|
|
|
|
+ (id) sharedSOGoWebAuthenticator
|
|
|
|
{
|
|
|
|
static SOGoWebAuthenticator *auth = nil;
|
|
|
|
|
|
|
|
if (!auth)
|
|
|
|
auth = [self new];
|
|
|
|
|
|
|
|
return auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) checkLogin: (NSString *) _login
|
|
|
|
password: (NSString *) _pwd
|
|
|
|
{
|
2009-11-20 21:25:13 +01:00
|
|
|
return [[SOGoUserManager sharedUserManager] checkLogin: _login
|
|
|
|
andPassword: _pwd];
|
2007-09-04 17:39:51 +02:00
|
|
|
}
|
|
|
|
|
2007-10-19 00:48:34 +02:00
|
|
|
- (SOGoUser *) userInContext: (WOContext *)_ctx
|
|
|
|
{
|
|
|
|
static SOGoUser *anonymous = nil;
|
|
|
|
SOGoUser *user;
|
|
|
|
|
|
|
|
if (!anonymous)
|
|
|
|
anonymous
|
|
|
|
= [[SOGoUser alloc] initWithLogin: @"anonymous"
|
|
|
|
roles: [NSArray arrayWithObject: SoRole_Anonymous]];
|
|
|
|
|
|
|
|
user = (SOGoUser *) [super userInContext: _ctx];
|
|
|
|
if (!user)
|
|
|
|
user = anonymous;
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2007-09-04 17:39:51 +02:00
|
|
|
- (NSString *) passwordInContext: (WOContext *) context
|
|
|
|
{
|
|
|
|
NSArray *creds;
|
|
|
|
NSString *auth, *password;
|
|
|
|
|
|
|
|
auth = [[context request] cookieValueForKey:
|
|
|
|
[self cookieNameInContext: context]];
|
|
|
|
creds = [self parseCredentials: auth];
|
|
|
|
if ([creds count] > 1)
|
|
|
|
password = [creds objectAtIndex: 1];
|
|
|
|
else
|
|
|
|
password = nil;
|
|
|
|
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create SOGoUser */
|
|
|
|
|
|
|
|
- (SOGoUser *) userWithLogin: (NSString *) login
|
|
|
|
andRoles: (NSArray *) roles
|
|
|
|
inContext: (WOContext *) ctx
|
|
|
|
{
|
|
|
|
/* the actual factory method */
|
|
|
|
return [SOGoUser userWithLogin: login roles: roles];
|
|
|
|
}
|
|
|
|
|
2007-09-07 00:17:56 +02:00
|
|
|
- (WOResponse *) preprocessCredentialsInContext: (WOContext *) context
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is called by SoObjectRequestHandler prior doing any significant
|
|
|
|
processing to allow the authenticator to reject invalid requests.
|
|
|
|
*/
|
|
|
|
WOResponse *response;
|
|
|
|
NSString *auth;
|
|
|
|
|
|
|
|
auth = [[context request]
|
|
|
|
cookieValueForKey: [self cookieNameInContext:context]];
|
|
|
|
if ([auth isEqualToString: @"discard"])
|
|
|
|
{
|
|
|
|
[context setObject: [NSArray arrayWithObject: SoRole_Anonymous]
|
|
|
|
forKey: @"SoAuthenticatedRoles"];
|
|
|
|
response = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
response = [super preprocessCredentialsInContext: context];
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-09-04 17:39:51 +02:00
|
|
|
- (void) setupAuthFailResponse: (WOResponse *) response
|
|
|
|
withReason: (NSString *) reason
|
|
|
|
inContext: (WOContext *) context
|
|
|
|
{
|
2007-09-05 19:57:17 +02:00
|
|
|
WOComponent *page;
|
2007-09-07 00:17:56 +02:00
|
|
|
WOCookie *authCookie;
|
|
|
|
NSCalendarDate *date;
|
2007-09-04 17:39:51 +02:00
|
|
|
|
|
|
|
page = [[WOApplication application] pageWithName: @"SOGoRootPage"
|
2007-09-05 00:03:32 +02:00
|
|
|
forRequest: [context request]];
|
2007-09-05 19:57:17 +02:00
|
|
|
[[SoDefaultRenderer sharedRenderer] renderObject: page
|
|
|
|
inContext: context];
|
2007-09-07 00:17:56 +02:00
|
|
|
authCookie = [WOCookie cookieWithName: [self cookieNameInContext: context]
|
|
|
|
value: @"discard"];
|
|
|
|
[authCookie setPath: @"/"];
|
|
|
|
date = [NSCalendarDate calendarDate];
|
|
|
|
[authCookie setExpires: [date yesterday]];
|
|
|
|
[response addCookie: authCookie];
|
2007-09-04 17:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end /* SOGoWebAuthenticator */
|