]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoAuthenticator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1088 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   if ((self = [super init]))
51     {
52       authMethod = [[NSUserDefaults standardUserDefaults]
53                      stringForKey: @"AuthentificationMethod"];
54       [authMethod retain];
55     }
56
57   return self;
58 }
59
60 - (void) dealloc
61 {
62   [authMethod release];
63   [super dealloc];
64 }
65
66 - (BOOL) checkLogin: (NSString *) _login
67            password: (NSString *) _pwd
68 {
69   BOOL accept;
70   LDAPUserManager *um;
71
72   if ([authMethod isEqualToString: @"LDAP"])
73     {
74       um = [LDAPUserManager sharedUserManager];
75       accept = [um checkLogin: _login andPassword: _pwd];
76     }
77   else
78     accept = ([_login length] > 0);
79
80   return accept;
81 //        || ([_login isEqualToString: @"freebusy"]
82 //            && [_pwd isEqualToString: @"freebusy"]));
83 }
84
85 - (NSString *) _passwordInContext: (WOContext *) context
86 {
87   NSString  *auth, *password;
88   NSArray   *creds;
89
90   password = nil;
91   auth = [[context request] headerForKey: @"authorization"];
92   if (auth)
93     {
94       creds = [self parseCredentials: auth];
95       if ([creds count] > 1)
96         password = [creds objectAtIndex: 1];
97     }
98   
99   return password;
100 }
101
102 /* create SOGoUser */
103
104 - (SOGoUser *) userInContext: (WOContext *)_ctx
105 {
106   static SOGoUser *anonymous = nil;
107   SOGoUser *user;
108   NSArray *traversalPath;
109   NSString *login;
110
111   if (!anonymous)
112     anonymous
113       = [[SOGoUser alloc] initWithLogin: @"anonymous"
114                           roles: [NSArray arrayWithObject: SoRole_Anonymous]];
115 //   if (!freebusy)
116 //     freebusy
117 //       = [[SOGoUser alloc] initWithLogin: @"freebusy"
118 //                           roles: [NSArray arrayWithObject: SOGoRole_FreeBusy]];
119
120   login = [self checkCredentialsInContext:_ctx];
121   if (login)
122     {
123       if ([login isEqualToString: @"anonymous"])
124         {
125           traversalPath = [_ctx objectForKey: @"SoRequestTraversalPath"];
126 //           if ([[traversalPath lastObject] isEqualToString: @"freebusy.ifb"])
127 //             user = freebusy;
128 //           else
129           user = anonymous;
130         }
131       else
132         {
133           user = [SOGoUser userWithLogin: login
134                            roles: [self rolesForLogin: login]];
135           [user setCurrentPassword: [self _passwordInContext: _ctx]];
136         }
137     }
138   else
139     user = nil;
140
141   return user;
142 }
143
144 // - (BOOL) renderException: (NSException *) exception
145 //                inContext: (WOContext *) context
146 // {
147 //   id renderedException;
148 //   WOComponent *tmpComponent;
149 //   WOResponse *response;
150 //   BOOL rc;
151
152 //   rc = [super renderException: exception inContext: context];
153 //   if (!rc)
154 //     {
155 //       tmpComponent = [WOComponent new];
156 //       renderedException = [tmpComponent pageWithName: @"UIxException"];
157 //       if (renderedException)
158 //         {
159 //           rc = YES;
160 //           response = [context response];
161 //           [response setHeader: @"text/html" forKey: @"content-type"];
162 //           [renderedException setClientObject: exception];
163 //           [context setPage: renderedException];
164 //           [renderedException appendToResponse: response
165 //                              inContext: context];
166 //         }
167 //       [tmpComponent release];
168 //     }
169
170 //   return rc;
171 // }
172
173 @end /* SOGoAuthenticator */