Skip to content

Commit 4685fae

Browse files
committed
Cleaned up authentication options
1 parent 8619799 commit 4685fae

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

GCDWebServer/Core/GCDWebServer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ extern NSString* const GCDWebServerOption_Port; // NSNumber / NSUInteger (defau
4646
extern NSString* const GCDWebServerOption_BonjourName; // NSString (default is empty string i.e. use computer name)
4747
extern NSString* const GCDWebServerOption_MaxPendingConnections; // NSNumber / NSUInteger (default is 16)
4848
extern NSString* const GCDWebServerOption_ServerName; // NSString (default is server class name)
49+
extern NSString* const GCDWebServerOption_AuthenticationMethod; // One of "GCDWebServerAuthenticationMethod_..." (default is nil i.e. no authentication)
4950
extern NSString* const GCDWebServerOption_AuthenticationRealm; // NSString (default is server name)
50-
extern NSString* const GCDWebServerOption_BasicAuthenticationAccount; // NSString in the form "user:password" (default is nil i.e. basic authentication disabled)
51+
extern NSString* const GCDWebServerOption_AuthenticationUser; // NSString
52+
extern NSString* const GCDWebServerOption_AuthenticationPassword; // NSString
5153
extern NSString* const GCDWebServerOption_ConnectionClass; // Subclass of GCDWebServerConnection (default is GCDWebServerConnection class)
5254
extern NSString* const GCDWebServerOption_AutomaticallyMapHEADToGET; // NSNumber / BOOL (default is YES)
5355
extern NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval; // NSNumber / double (default is 1.0 seconds - set to <=0.0 to disable coaslescing of -webServerDidConnect: / -webServerDidDisconnect:)
5456
#if TARGET_OS_IPHONE
5557
extern NSString* const GCDWebServerOption_AutomaticallySuspendInBackground; // NSNumber / BOOL (default is YES)
5658
#endif
5759

60+
extern NSString* const GCDWebServerAuthenticationMethod_Basic;
61+
5862
@class GCDWebServer;
5963

6064
// These methods are always called on main thread

GCDWebServer/Core/GCDWebServer.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,19 @@ @interface GCDWebServerHandler () {
8383
NSString* const GCDWebServerOption_BonjourName = @"BonjourName";
8484
NSString* const GCDWebServerOption_MaxPendingConnections = @"MaxPendingConnections";
8585
NSString* const GCDWebServerOption_ServerName = @"ServerName";
86+
NSString* const GCDWebServerOption_AuthenticationMethod = @"AuthenticationMethod";
8687
NSString* const GCDWebServerOption_AuthenticationRealm = @"AuthenticationRealm";
87-
NSString* const GCDWebServerOption_BasicAuthenticationAccount = @"BasicAuthenticationAccount";
88+
NSString* const GCDWebServerOption_AuthenticationUser = @"AuthenticationUser";
89+
NSString* const GCDWebServerOption_AuthenticationPassword = @"AuthenticationPassword";
8890
NSString* const GCDWebServerOption_ConnectionClass = @"ConnectionClass";
8991
NSString* const GCDWebServerOption_AutomaticallyMapHEADToGET = @"AutomaticallyMapHEADToGET";
9092
NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval = @"ConnectedStateCoalescingInterval";
9193
#if TARGET_OS_IPHONE
9294
NSString* const GCDWebServerOption_AutomaticallySuspendInBackground = @"AutomaticallySuspendInBackground";
9395
#endif
9496

97+
NSString* const GCDWebServerAuthenticationMethod_Basic = @"Basic";
98+
9599
#ifndef __GCDWEBSERVER_LOGGING_HEADER__
96100
#ifdef NDEBUG
97101
GCDWebServerLogLevel GCDLogLevel = kGCDWebServerLogLevel_Info;
@@ -373,9 +377,13 @@ - (BOOL)_start {
373377
if (listen(listeningSocket, (int)maxPendingConnections) == 0) {
374378
LOG_DEBUG(@"Did open listening socket %i", listeningSocket);
375379
_serverName = [_GetOption(_options, GCDWebServerOption_ServerName, NSStringFromClass([self class])) copy];
376-
_authenticationRealm = [_GetOption(_options, GCDWebServerOption_AuthenticationRealm, _serverName) copy];
377-
NSString* basicAuthenticationAccount = _GetOption(_options, GCDWebServerOption_BasicAuthenticationAccount, nil);
378-
_authenticationBasicAccount = basicAuthenticationAccount.length ? _EncodeBase64(basicAuthenticationAccount) : nil;
380+
NSString* authenticationMethod = _GetOption(_options, GCDWebServerOption_AuthenticationMethod, nil);
381+
if ([authenticationMethod isEqualToString:GCDWebServerAuthenticationMethod_Basic]) {
382+
_authenticationRealm = [_GetOption(_options, GCDWebServerOption_AuthenticationRealm, _serverName) copy];
383+
NSString* user = _GetOption(_options, GCDWebServerOption_AuthenticationUser, @"");
384+
NSString* password = _GetOption(_options, GCDWebServerOption_AuthenticationPassword, @"");
385+
_authenticationBasicAccount = ARC_RETAIN(_EncodeBase64([NSString stringWithFormat:@"%@:%@", user, password]));
386+
}
379387
_connectionClass = _GetOption(_options, GCDWebServerOption_ConnectionClass, [GCDWebServerConnection class]);
380388
_mapHEADToGET = [_GetOption(_options, GCDWebServerOption_AutomaticallyMapHEADToGET, @YES) boolValue];
381389
_disconnectDelay = [_GetOption(_options, GCDWebServerOption_ConnectedStateCoalescingInterval, @1.0) doubleValue];

Mac/main.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ int main(int argc, const char* argv[]) {
131131
NSString* rootDirectory = NSHomeDirectory();
132132
NSString* testDirectory = nil;
133133
NSString* authenticationRealm = nil;
134-
NSString* authenticationAccount = nil;
134+
NSString* authenticationUser = nil;
135+
NSString* authenticationPassword = nil;
135136

136137
if (argc == 1) {
137-
fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | webDAV | webUploader | streamingResponse] [-record] [-root directory] [-tests directory] [-authenticationRealm realm] [-authenticationAccount user:password]\n\n", basename((char*)argv[0]));
138+
fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | webDAV | webUploader | streamingResponse] [-record] [-root directory] [-tests directory] [-authenticationRealm realm] [-authenticationUser user] [-authenticationPassword password]\n\n", basename((char*)argv[0]));
138139
} else {
139140
for (int i = 1; i < argc; ++i) {
140141
if (argv[i][0] != '-') {
@@ -166,9 +167,12 @@ int main(int argc, const char* argv[]) {
166167
} else if (!strcmp(argv[i], "-authenticationRealm") && (i + 1 < argc)) {
167168
++i;
168169
authenticationRealm = [NSString stringWithUTF8String:argv[i]];
169-
} else if (!strcmp(argv[i], "-authenticationAccount") && (i + 1 < argc)) {
170+
} else if (!strcmp(argv[i], "-authenticationUser") && (i + 1 < argc)) {
170171
++i;
171-
authenticationAccount = [NSString stringWithUTF8String:argv[i]];
172+
authenticationUser = [NSString stringWithUTF8String:argv[i]];
173+
} else if (!strcmp(argv[i], "-authenticationPassword") && (i + 1 < argc)) {
174+
++i;
175+
authenticationPassword = [NSString stringWithUTF8String:argv[i]];
172176
}
173177
}
174178
}
@@ -292,8 +296,12 @@ int main(int argc, const char* argv[]) {
292296
NSMutableDictionary* options = [NSMutableDictionary dictionary];
293297
[options setObject:@8080 forKey:GCDWebServerOption_Port];
294298
[options setObject:@"" forKey:GCDWebServerOption_BonjourName];
295-
[options setValue:authenticationRealm forKey:GCDWebServerOption_AuthenticationRealm];
296-
[options setValue:authenticationAccount forKey:GCDWebServerOption_BasicAuthenticationAccount];
299+
if (authenticationUser && authenticationPassword) {
300+
[options setObject:GCDWebServerAuthenticationMethod_Basic forKey:GCDWebServerOption_AuthenticationMethod];
301+
[options setValue:authenticationRealm forKey:GCDWebServerOption_AuthenticationRealm];
302+
[options setObject:authenticationUser forKey:GCDWebServerOption_AuthenticationUser];
303+
[options setObject:authenticationPassword forKey:GCDWebServerOption_AuthenticationPassword];
304+
}
297305
if ([webServer runWithOptions:options]) {
298306
result = 0;
299307
}

0 commit comments

Comments
 (0)