1 /* SOGoWebAuthenticator.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSUserDefaults.h>
28 #import <NGObjWeb/SoDefaultRenderer.h>
29 #import <NGObjWeb/WOApplication.h>
30 #import <NGObjWeb/WOContext.h>
31 #import <NGObjWeb/WOCookie.h>
32 #import <NGObjWeb/WORequest.h>
33 #import <NGObjWeb/WOResponse.h>
34 #import <NGExtensions/NSCalendarDate+misc.h>
35 #import <NGLdap/NGLdapConnection.h>
37 #import <UI/MainUI/SOGoRootPage.h>
39 #import "LDAPUserManager.h"
40 #import "SOGoPermissions.h"
43 #import "SOGoWebAuthenticator.h"
45 @implementation SOGoWebAuthenticator
47 + (id) sharedSOGoWebAuthenticator
49 static SOGoWebAuthenticator *auth = nil;
59 if ((self = [super init]))
61 authMethod = [[NSUserDefaults standardUserDefaults]
62 stringForKey: @"SOGoAuthentificationMethod"];
75 - (BOOL) checkLogin: (NSString *) _login
76 password: (NSString *) _pwd
81 if ([authMethod isEqualToString: @"LDAP"])
83 um = [LDAPUserManager sharedUserManager];
84 accept = [um checkLogin: _login andPassword: _pwd];
87 accept = ([authMethod isEqualToString: @"bypass"]
88 && [_login length] > 0);
91 // || ([_login isEqualToString: @"freebusy"]
92 // && [_pwd isEqualToString: @"freebusy"]));
95 - (SOGoUser *) userInContext: (WOContext *)_ctx
97 static SOGoUser *anonymous = nil;
102 = [[SOGoUser alloc] initWithLogin: @"anonymous"
103 roles: [NSArray arrayWithObject: SoRole_Anonymous]];
105 user = (SOGoUser *) [super userInContext: _ctx];
112 - (NSString *) passwordInContext: (WOContext *) context
115 NSString *auth, *password;
117 auth = [[context request] cookieValueForKey:
118 [self cookieNameInContext: context]];
119 creds = [self parseCredentials: auth];
120 if ([creds count] > 1)
121 password = [creds objectAtIndex: 1];
128 /* create SOGoUser */
130 - (SOGoUser *) userWithLogin: (NSString *) login
131 andRoles: (NSArray *) roles
132 inContext: (WOContext *) ctx
134 /* the actual factory method */
135 return [SOGoUser userWithLogin: login roles: roles];
138 - (WOResponse *) preprocessCredentialsInContext: (WOContext *) context
141 This is called by SoObjectRequestHandler prior doing any significant
142 processing to allow the authenticator to reject invalid requests.
144 WOResponse *response;
147 auth = [[context request]
148 cookieValueForKey: [self cookieNameInContext:context]];
149 if ([auth isEqualToString: @"discard"])
151 [context setObject: [NSArray arrayWithObject: SoRole_Anonymous]
152 forKey: @"SoAuthenticatedRoles"];
156 response = [super preprocessCredentialsInContext: context];
161 - (void) setupAuthFailResponse: (WOResponse *) response
162 withReason: (NSString *) reason
163 inContext: (WOContext *) context
166 WOCookie *authCookie;
167 NSCalendarDate *date;
169 page = [[WOApplication application] pageWithName: @"SOGoRootPage"
170 forRequest: [context request]];
171 [[SoDefaultRenderer sharedRenderer] renderObject: page
173 authCookie = [WOCookie cookieWithName: [self cookieNameInContext: context]
175 [authCookie setPath: @"/"];
176 date = [NSCalendarDate calendarDate];
177 [authCookie setExpires: [date yesterday]];
178 [response addCookie: authCookie];
181 @end /* SOGoWebAuthenticator */