]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoWebAuthenticator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1192 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 - (SOGoUser *) userInContext: (WOContext *)_ctx
96 {
97   static SOGoUser *anonymous = nil;
98   SOGoUser *user;
99
100   if (!anonymous)
101     anonymous
102       = [[SOGoUser alloc] initWithLogin: @"anonymous"
103                           roles: [NSArray arrayWithObject: SoRole_Anonymous]];
104
105   user = (SOGoUser *) [super userInContext: _ctx];
106   if (!user)
107     user = anonymous;
108
109   return user;
110 }
111
112 - (NSString *) passwordInContext: (WOContext *) context
113 {
114   NSArray *creds;
115   NSString *auth, *password;
116   
117   auth = [[context request] cookieValueForKey:
118                               [self cookieNameInContext: context]];
119   creds = [self parseCredentials: auth];
120   if ([creds count] > 1)
121     password = [creds objectAtIndex: 1];
122   else
123     password = nil;
124
125   return password;
126 }
127
128 /* create SOGoUser */
129
130 - (SOGoUser *) userWithLogin: (NSString *) login
131                     andRoles: (NSArray *) roles
132                    inContext: (WOContext *) ctx
133 {
134   /* the actual factory method */
135   return [SOGoUser userWithLogin: login roles: roles];
136 }
137
138 - (WOResponse *) preprocessCredentialsInContext: (WOContext *) context
139 {
140   /*
141     This is called by SoObjectRequestHandler prior doing any significant
142     processing to allow the authenticator to reject invalid requests.
143   */
144   WOResponse *response;
145   NSString *auth;
146   
147   auth = [[context request]
148            cookieValueForKey: [self cookieNameInContext:context]];
149   if ([auth isEqualToString: @"discard"])
150     {
151       [context setObject: [NSArray arrayWithObject: SoRole_Anonymous]
152                forKey: @"SoAuthenticatedRoles"];
153       response = nil;
154     }
155   else
156     response = [super preprocessCredentialsInContext: context];
157
158   return response;
159 }
160
161 - (void) setupAuthFailResponse: (WOResponse *) response
162                     withReason: (NSString *) reason
163                      inContext: (WOContext *) context
164 {
165   WOComponent *page;
166   WOCookie *authCookie;
167   NSCalendarDate *date;
168
169   page = [[WOApplication application] pageWithName: @"SOGoRootPage"
170                                       forRequest: [context request]];
171   [[SoDefaultRenderer sharedRenderer] renderObject: page
172                                       inContext: context];
173   authCookie = [WOCookie cookieWithName: [self cookieNameInContext: context]
174                          value: @"discard"];
175   [authCookie setPath: @"/"];
176   date = [NSCalendarDate calendarDate];
177   [authCookie setExpires: [date yesterday]];
178   [response addCookie: authCookie];
179 }
180
181 @end /* SOGoWebAuthenticator */