/
SOGoProxyAuthenticator.m
186 lines (153 loc) · 4.91 KB
/
SOGoProxyAuthenticator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* SOGoProxyAuthenticator.h - this file is part of SOGo
*
* Copyright (C) 2009-2013 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSString.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/WOContext.h>
#import <NGObjWeb/WORequest.h>
#import <NGObjWeb/WOResponse.h>
#import <NGExtensions/NGBase64Coding.h>
#import <NGExtensions/NSObject+Logs.h>
#import "SOGoPermissions.h"
#import "SOGoSystemDefaults.h"
#import "SOGoUser.h"
#import "SOGoProxyAuthenticator.h"
@implementation SOGoProxyAuthenticator
+ (id) sharedSOGoProxyAuthenticator
{
static SOGoProxyAuthenticator *auth = nil;
if (!auth)
auth = [self new];
return auth;
}
- (BOOL) checkLogin: (NSString *) _login
password: (NSString *) _pwd
{
return YES;
}
/* create SOGoUser */
- (NSString *) checkCredentialsInContext: (WOContext *) context
{
NSString *remoteUser;
if ([[SOGoSystemDefaults sharedSystemDefaults] trustProxyAuthentication])
{
remoteUser = @"anonymous";
}
else
{
/* If such a header is not provided by the proxy, SOPE will attempt to
deduce it from the "Authorization" header. */
remoteUser = [[context request] headerForKey: @"x-webobjects-remote-user"];
}
return remoteUser;
}
- (WOResponse *) unauthorized: (NSString *) reason
inContext: (WOContext *) context
{
WOResponse *r;
if (![reason length])
reason = @"Unauthorized";
r = [context response];
[r setStatus: 403 /* unauthorized */];
[r setHeader: @"text/plain; charset=utf-8" forKey: @"content-type"];
[r appendContentString: reason];
return r;
}
- (SOGoUser *) userInContext: (WOContext *) context
{
SOGoUser *user;
NSString *login;
login = [self checkCredentialsInContext: context];
if ([login length])
user = [SOGoUser userWithLogin: login
roles: [NSArray arrayWithObject:
SoRole_Authenticated]];
else
user = nil;
return user;
}
#warning the DAV authenticator is pretty similar to this one. We should enable \
the use of the former for Basic auth type through some defaults.
- (NSString *) passwordInContext: (WOContext *) context
{
NSString *password, *authType, *authorization, *pair, *pairStart;
WORequest *rq;
password = @"";
rq = [context request];
authType = [rq headerForKey: @"x-webobjects-auth-type"];
if ([authType isEqualToString: @"Basic"])
{
authorization = [rq headerForKey: @"authorization"];
if ([authorization hasPrefix: @"Basic "])
{
pair = [[authorization substringFromIndex: 6]
stringByDecodingBase64];
pairStart = [NSString stringWithFormat: @"%@:",
[self checkCredentialsInContext: context]];
if ([pair hasPrefix: pairStart])
password = [pair substringFromIndex: [pairStart length]];
else
[self errorWithFormat: @"the username in the 'authorization'"
@" header does not have the expected value"];
}
else
[self errorWithFormat:
@"'authorization' header does not have the expected value"];
}
else if (authType)
[self errorWithFormat: @"unrecognized authentication type: '%@'",
authType];
else
[self warnWithFormat: @"no authentication type found, skipped"];
return password;
}
- (NSString *) imapPasswordInContext: (WOContext *) context
forURL: (NSURL *) server
forceRenew: (BOOL) renew
{
return (renew ? nil : [self passwordInContext: context]);
}
- (WOResponse *) preprocessCredentialsInContext: (WOContext *) context
{
WOResponse *r;
if ([self userInContext: context])
{
[context setObject: [NSArray arrayWithObject: SoRole_Authenticated]
forKey: @"SoAuthenticatedRoles"];
r = nil;
}
else
r = [self unauthorized: nil inContext: context];
return r;
}
- (BOOL) renderException: (NSException *) e
inContext: (WOContext *) context
{
BOOL rc;
if ([e httpStatus] == 401)
{
[self unauthorized: [e reason] inContext: context];
rc = YES;
}
else
rc = NO;
return rc;
}
@end /* SOGoProxyAuthenticator */