]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoAuthenticator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1087 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoAuthenticator.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSString.h>
24 #import <Foundation/NSUserDefaults.h>
25
26 #import <NGObjWeb/WOContext.h>
27 #import <NGObjWeb/WOResponse.h>
28 #import <NGLdap/NGLdapConnection.h>
29
30 #import "LDAPUserManager.h"
31 #import "SOGoPermissions.h"
32 #import "SOGoUser.h"
33
34 #import "SOGoAuthenticator.h"
35
36 @implementation SOGoAuthenticator
37
38 + (id) sharedSOGoAuthenticator
39 {
40   static SOGoAuthenticator *auth = nil;
41  
42   if (!auth)
43     auth = [self new];
44   
45   return auth;
46 }
47
48 - (id) init
49 {
50   NSUserDefaults *ud;
51
52   if ((self = [super init]))
53     {
54       ud = [NSUserDefaults standardUserDefaults];
55
56       authMethod = [[ud stringForKey:@"AuthentificationMethod"] retain];
57     }
58
59   return self;
60 }
61
62 - (void) dealloc
63 {
64   [authMethod release];
65   [super dealloc];
66 }
67
68 - (BOOL) checkLogin: (NSString *) _login
69            password: (NSString *) _pwd
70 {
71   BOOL accept;
72   LDAPUserManager *um;
73
74   if ([authMethod isEqualToString: @"LDAP"])
75     {
76       um = [LDAPUserManager sharedUserManager];
77       accept = [um checkLogin: _login andPassword: _pwd];
78     }
79   else
80     accept = ([_login length] > 0);
81
82   return (accept
83           || ([_login isEqualToString: @"freebusy"]
84               && [_pwd isEqualToString: @"freebusy"]));
85 }
86
87 - (NSString *) _passwordInContext: (WOContext *) context
88 {
89   NSString  *auth, *password;
90   NSArray   *creds;
91
92   password = nil;
93   auth = [[context request] headerForKey:@"authorization"];
94   if (auth)
95     {
96       creds = [self parseCredentials: auth];
97       if ([creds count] > 1)
98         password = [creds objectAtIndex: 1];
99     }
100   
101   return password;
102 }
103
104 /* create SOGoUser */
105
106 - (SOGoUser *) userInContext: (WOContext *)_ctx
107 {
108   static SOGoUser *anonymous = nil, *freebusy;
109   SOGoUser *user;
110   NSArray *traversalPath;
111   NSString *login;
112
113   if (!anonymous)
114     anonymous
115       = [[SOGoUser alloc] initWithLogin: @"anonymous"
116                           roles: [NSArray arrayWithObject: SoRole_Anonymous]];
117   if (!freebusy)
118     freebusy
119       = [[SOGoUser alloc] initWithLogin: @"freebusy"
120                           roles: [NSArray arrayWithObject: SOGoRole_FreeBusy]];
121
122   login = [self checkCredentialsInContext:_ctx];
123   if (login)
124     {
125       if ([login isEqualToString: @"anonymous"])
126         {
127           traversalPath = [_ctx objectForKey: @"SoRequestTraversalPath"];
128           if ([[traversalPath lastObject] isEqualToString: @"freebusy.ifb"])
129             user = freebusy;
130           else
131             user = anonymous;
132         }
133       else
134         {
135           user = [SOGoUser userWithLogin: login
136                            roles: [self rolesForLogin: login]];
137           [user setCurrentPassword: [self _passwordInContext: _ctx]];
138         }
139     }
140   else
141     user = nil;
142
143   return user;
144 }
145
146 // - (BOOL) renderException: (NSException *) exception
147 //                inContext: (WOContext *) context
148 // {
149 //   id renderedException;
150 //   WOComponent *tmpComponent;
151 //   WOResponse *response;
152 //   BOOL rc;
153
154 //   rc = [super renderException: exception inContext: context];
155 //   if (!rc)
156 //     {
157 //       tmpComponent = [WOComponent new];
158 //       renderedException = [tmpComponent pageWithName: @"UIxException"];
159 //       if (renderedException)
160 //         {
161 //           rc = YES;
162 //           response = [context response];
163 //           [response setHeader: @"text/html" forKey: @"content-type"];
164 //           [renderedException setClientObject: exception];
165 //           [context setPage: renderedException];
166 //           [renderedException appendToResponse: response
167 //                              inContext: context];
168 //         }
169 //       [tmpComponent release];
170 //     }
171
172 //   return rc;
173 // }
174
175 @end /* SOGoAuthenticator */