]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoDAVAuthenticator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1192 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoDAVAuthenticator.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 "SOGoDAVAuthenticator.h"
35
36 @implementation SOGoDAVAuthenticator
37
38 + (id) sharedSOGoDAVAuthenticator
39 {
40   static SOGoDAVAuthenticator *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: @"SOGoAuthentificationMethod"];
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 = ([authMethod isEqualToString: @"bypass"]
79               && [_login length] > 0);
80
81   return accept;
82 //        || ([_login isEqualToString: @"freebusy"]
83 //            && [_pwd isEqualToString: @"freebusy"]));
84 }
85
86 - (NSString *) passwordInContext: (WOContext *) context
87 {
88   NSString  *auth, *password;
89   NSArray   *creds;
90
91   password = nil;
92   auth = [[context request] headerForKey: @"authorization"];
93   if (auth)
94     {
95       creds = [self parseCredentials: auth];
96       if ([creds count] > 1)
97         password = [creds objectAtIndex: 1];
98     }
99
100   return password;
101 }
102
103 /* create SOGoUser */
104
105 - (SOGoUser *) userInContext: (WOContext *)_ctx
106 {
107   static SOGoUser *anonymous = nil;
108   SOGoUser *user;
109   NSArray *traversalPath;
110   NSString *login;
111
112   if (!anonymous)
113     anonymous
114       = [[SOGoUser alloc] initWithLogin: @"anonymous"
115                           roles: [NSArray arrayWithObject: SoRole_Anonymous]];
116
117   login = [self checkCredentialsInContext:_ctx];
118   if (login)
119     {
120       if ([login isEqualToString: @"anonymous"])
121         {
122           traversalPath = [_ctx objectForKey: @"SoRequestTraversalPath"];
123           user = anonymous;
124         }
125       else
126         {
127           user = [SOGoUser userWithLogin: login
128                            roles: [self rolesForLogin: login]];
129           [user setCurrentPassword: [self passwordInContext: _ctx]];
130         }
131     }
132   else
133     user = nil;
134
135   return user;
136 }
137
138 @end /* SOGoDAVAuthenticator */