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 - (NSString *) passwordInContext: (WOContext *) context
98 NSString *auth, *password;
100 auth = [[context request] cookieValueForKey:
101 [self cookieNameInContext: context]];
102 creds = [self parseCredentials: auth];
103 if ([creds count] > 1)
104 password = [creds objectAtIndex: 1];
111 /* create SOGoUser */
113 - (SOGoUser *) userWithLogin: (NSString *) login
114 andRoles: (NSArray *) roles
115 inContext: (WOContext *) ctx
117 /* the actual factory method */
118 return [SOGoUser userWithLogin: login roles: roles];
121 - (WOResponse *) preprocessCredentialsInContext: (WOContext *) context
124 This is called by SoObjectRequestHandler prior doing any significant
125 processing to allow the authenticator to reject invalid requests.
127 WOResponse *response;
130 auth = [[context request]
131 cookieValueForKey: [self cookieNameInContext:context]];
132 if ([auth isEqualToString: @"discard"])
134 [context setObject: [NSArray arrayWithObject: SoRole_Anonymous]
135 forKey: @"SoAuthenticatedRoles"];
139 response = [super preprocessCredentialsInContext: context];
144 - (void) setupAuthFailResponse: (WOResponse *) response
145 withReason: (NSString *) reason
146 inContext: (WOContext *) context
149 WOCookie *authCookie;
150 NSCalendarDate *date;
152 page = [[WOApplication application] pageWithName: @"SOGoRootPage"
153 forRequest: [context request]];
154 [[SoDefaultRenderer sharedRenderer] renderObject: page
156 authCookie = [WOCookie cookieWithName: [self cookieNameInContext: context]
158 [authCookie setPath: @"/"];
159 date = [NSCalendarDate calendarDate];
160 [authCookie setExpires: [date yesterday]];
161 [response addCookie: authCookie];
164 @end /* SOGoWebAuthenticator */