]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoWebAuthenticator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1174 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoWebAuthenticator.m
1 /* SOGoWebAuthenticator.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSUserDefaults.h>
27
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>
36
37 #import <UI/MainUI/SOGoRootPage.h>
38
39 #import "LDAPUserManager.h"
40 #import "SOGoPermissions.h"
41 #import "SOGoUser.h"
42
43 #import "SOGoWebAuthenticator.h"
44
45 @implementation SOGoWebAuthenticator
46
47 + (id) sharedSOGoWebAuthenticator
48 {
49   static SOGoWebAuthenticator *auth = nil;
50  
51   if (!auth)
52     auth = [self new];
53
54   return auth;
55 }
56
57 - (id) init
58 {
59   if ((self = [super init]))
60     {
61       authMethod = [[NSUserDefaults standardUserDefaults]
62                      stringForKey: @"SOGoAuthentificationMethod"];
63       [authMethod retain];
64     }
65
66   return self;
67 }
68
69 - (void) dealloc
70 {
71   [authMethod release];
72   [super dealloc];
73 }
74
75 - (BOOL) checkLogin: (NSString *) _login
76            password: (NSString *) _pwd
77 {
78   BOOL accept;
79   LDAPUserManager *um;
80
81   if ([authMethod isEqualToString: @"LDAP"])
82     {
83       um = [LDAPUserManager sharedUserManager];
84       accept = [um checkLogin: _login andPassword: _pwd];
85     }
86   else
87     accept = ([authMethod isEqualToString: @"bypass"]
88               && [_login length] > 0);
89
90   return accept;
91 //        || ([_login isEqualToString: @"freebusy"]
92 //            && [_pwd isEqualToString: @"freebusy"]));
93 }
94
95 - (NSString *) passwordInContext: (WOContext *) context
96 {
97   NSArray *creds;
98   NSString *auth, *password;
99   
100   auth = [[context request] cookieValueForKey:
101                               [self cookieNameInContext: context]];
102   creds = [self parseCredentials: auth];
103   if ([creds count] > 1)
104     password = [creds objectAtIndex: 1];
105   else
106     password = nil;
107
108   return password;
109 }
110
111 /* create SOGoUser */
112
113 - (SOGoUser *) userWithLogin: (NSString *) login
114                     andRoles: (NSArray *) roles
115                    inContext: (WOContext *) ctx
116 {
117   /* the actual factory method */
118   return [SOGoUser userWithLogin: login roles: roles];
119 }
120
121 - (WOResponse *) preprocessCredentialsInContext: (WOContext *) context
122 {
123   /*
124     This is called by SoObjectRequestHandler prior doing any significant
125     processing to allow the authenticator to reject invalid requests.
126   */
127   WOResponse *response;
128   NSString *auth;
129   
130   auth = [[context request]
131            cookieValueForKey: [self cookieNameInContext:context]];
132   if ([auth isEqualToString: @"discard"])
133     {
134       [context setObject: [NSArray arrayWithObject: SoRole_Anonymous]
135                forKey: @"SoAuthenticatedRoles"];
136       response = nil;
137     }
138   else
139     response = [super preprocessCredentialsInContext: context];
140
141   return response;
142 }
143
144 - (void) setupAuthFailResponse: (WOResponse *) response
145                     withReason: (NSString *) reason
146                      inContext: (WOContext *) context
147 {
148   WOComponent *page;
149   WOCookie *authCookie;
150   NSCalendarDate *date;
151
152   page = [[WOApplication application] pageWithName: @"SOGoRootPage"
153                                       forRequest: [context request]];
154   [[SoDefaultRenderer sharedRenderer] renderObject: page
155                                       inContext: context];
156   authCookie = [WOCookie cookieWithName: [self cookieNameInContext: context]
157                          value: @"discard"];
158   [authCookie setPath: @"/"];
159   date = [NSCalendarDate calendarDate];
160   [authCookie setExpires: [date yesterday]];
161   [response addCookie: authCookie];
162 }
163
164 @end /* SOGoWebAuthenticator */