Переглянути джерело

Cleaned up authentication options

Pierre-Olivier Latour 11 роки тому
батько
коміт
4685fae29c
3 змінених файлів з 31 додано та 11 видалено
  1. 5 1
      GCDWebServer/Core/GCDWebServer.h
  2. 12 4
      GCDWebServer/Core/GCDWebServer.m
  3. 14 6
      Mac/main.m

+ 5 - 1
GCDWebServer/Core/GCDWebServer.h

@@ -46,8 +46,10 @@ extern NSString* const GCDWebServerOption_Port;  // NSNumber / NSUInteger (defau
 extern NSString* const GCDWebServerOption_BonjourName;  // NSString (default is empty string i.e. use computer name)
 extern NSString* const GCDWebServerOption_MaxPendingConnections;  // NSNumber / NSUInteger (default is 16)
 extern NSString* const GCDWebServerOption_ServerName;  // NSString (default is server class name)
+extern NSString* const GCDWebServerOption_AuthenticationMethod;  // One of "GCDWebServerAuthenticationMethod_..." (default is nil i.e. no authentication)
 extern NSString* const GCDWebServerOption_AuthenticationRealm;  // NSString (default is server name)
-extern NSString* const GCDWebServerOption_BasicAuthenticationAccount;  // NSString in the form "user:password" (default is nil i.e. basic authentication disabled)
+extern NSString* const GCDWebServerOption_AuthenticationUser;  // NSString
+extern NSString* const GCDWebServerOption_AuthenticationPassword;  // NSString
 extern NSString* const GCDWebServerOption_ConnectionClass;  // Subclass of GCDWebServerConnection (default is GCDWebServerConnection class)
 extern NSString* const GCDWebServerOption_AutomaticallyMapHEADToGET;  // NSNumber / BOOL (default is YES)
 extern NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval;  // NSNumber / double (default is 1.0 seconds - set to <=0.0 to disable coaslescing of -webServerDidConnect: / -webServerDidDisconnect:)
@@ -55,6 +57,8 @@ extern NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval;  //
 extern NSString* const GCDWebServerOption_AutomaticallySuspendInBackground;  // NSNumber / BOOL (default is YES)
 #endif
 
+extern NSString* const GCDWebServerAuthenticationMethod_Basic;
+
 @class GCDWebServer;
 
 // These methods are always called on main thread

+ 12 - 4
GCDWebServer/Core/GCDWebServer.m

@@ -83,8 +83,10 @@ NSString* const GCDWebServerOption_Port = @"Port";
 NSString* const GCDWebServerOption_BonjourName = @"BonjourName";
 NSString* const GCDWebServerOption_MaxPendingConnections = @"MaxPendingConnections";
 NSString* const GCDWebServerOption_ServerName = @"ServerName";
+NSString* const GCDWebServerOption_AuthenticationMethod = @"AuthenticationMethod";
 NSString* const GCDWebServerOption_AuthenticationRealm = @"AuthenticationRealm";
-NSString* const GCDWebServerOption_BasicAuthenticationAccount = @"BasicAuthenticationAccount";
+NSString* const GCDWebServerOption_AuthenticationUser = @"AuthenticationUser";
+NSString* const GCDWebServerOption_AuthenticationPassword = @"AuthenticationPassword";
 NSString* const GCDWebServerOption_ConnectionClass = @"ConnectionClass";
 NSString* const GCDWebServerOption_AutomaticallyMapHEADToGET = @"AutomaticallyMapHEADToGET";
 NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval = @"ConnectedStateCoalescingInterval";
@@ -92,6 +94,8 @@ NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval = @"Connecte
 NSString* const GCDWebServerOption_AutomaticallySuspendInBackground = @"AutomaticallySuspendInBackground";
 #endif
 
+NSString* const GCDWebServerAuthenticationMethod_Basic = @"Basic";
+
 #ifndef __GCDWEBSERVER_LOGGING_HEADER__
 #ifdef NDEBUG
 GCDWebServerLogLevel GCDLogLevel = kGCDWebServerLogLevel_Info;
@@ -373,9 +377,13 @@ static inline NSString* _EncodeBase64(NSString* string) {
       if (listen(listeningSocket, (int)maxPendingConnections) == 0) {
         LOG_DEBUG(@"Did open listening socket %i", listeningSocket);
         _serverName = [_GetOption(_options, GCDWebServerOption_ServerName, NSStringFromClass([self class])) copy];
-        _authenticationRealm = [_GetOption(_options, GCDWebServerOption_AuthenticationRealm, _serverName) copy];
-        NSString* basicAuthenticationAccount = _GetOption(_options, GCDWebServerOption_BasicAuthenticationAccount, nil);
-        _authenticationBasicAccount = basicAuthenticationAccount.length ? _EncodeBase64(basicAuthenticationAccount) : nil;
+        NSString* authenticationMethod = _GetOption(_options, GCDWebServerOption_AuthenticationMethod, nil);
+        if ([authenticationMethod isEqualToString:GCDWebServerAuthenticationMethod_Basic]) {
+          _authenticationRealm = [_GetOption(_options, GCDWebServerOption_AuthenticationRealm, _serverName) copy];
+          NSString* user = _GetOption(_options, GCDWebServerOption_AuthenticationUser, @"");
+          NSString* password = _GetOption(_options, GCDWebServerOption_AuthenticationPassword, @"");
+          _authenticationBasicAccount = ARC_RETAIN(_EncodeBase64([NSString stringWithFormat:@"%@:%@", user, password]));
+        }
         _connectionClass = _GetOption(_options, GCDWebServerOption_ConnectionClass, [GCDWebServerConnection class]);
         _mapHEADToGET = [_GetOption(_options, GCDWebServerOption_AutomaticallyMapHEADToGET, @YES) boolValue];
         _disconnectDelay = [_GetOption(_options, GCDWebServerOption_ConnectedStateCoalescingInterval, @1.0) doubleValue];

+ 14 - 6
Mac/main.m

@@ -131,10 +131,11 @@ int main(int argc, const char* argv[]) {
     NSString* rootDirectory = NSHomeDirectory();
     NSString* testDirectory = nil;
     NSString* authenticationRealm = nil;
-    NSString* authenticationAccount = nil;
+    NSString* authenticationUser = nil;
+    NSString* authenticationPassword = nil;
     
     if (argc == 1) {
-      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]));
+      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]));
     } else {
       for (int i = 1; i < argc; ++i) {
         if (argv[i][0] != '-') {
@@ -166,9 +167,12 @@ int main(int argc, const char* argv[]) {
         } else if (!strcmp(argv[i], "-authenticationRealm") && (i + 1 < argc)) {
           ++i;
           authenticationRealm = [NSString stringWithUTF8String:argv[i]];
-        } else if (!strcmp(argv[i], "-authenticationAccount") && (i + 1 < argc)) {
+        } else if (!strcmp(argv[i], "-authenticationUser") && (i + 1 < argc)) {
           ++i;
-          authenticationAccount = [NSString stringWithUTF8String:argv[i]];
+          authenticationUser = [NSString stringWithUTF8String:argv[i]];
+        } else if (!strcmp(argv[i], "-authenticationPassword") && (i + 1 < argc)) {
+          ++i;
+          authenticationPassword = [NSString stringWithUTF8String:argv[i]];
         }
       }
     }
@@ -292,8 +296,12 @@ int main(int argc, const char* argv[]) {
         NSMutableDictionary* options = [NSMutableDictionary dictionary];
         [options setObject:@8080 forKey:GCDWebServerOption_Port];
         [options setObject:@"" forKey:GCDWebServerOption_BonjourName];
-        [options setValue:authenticationRealm forKey:GCDWebServerOption_AuthenticationRealm];
-        [options setValue:authenticationAccount forKey:GCDWebServerOption_BasicAuthenticationAccount];
+        if (authenticationUser && authenticationPassword) {
+          [options setObject:GCDWebServerAuthenticationMethod_Basic forKey:GCDWebServerOption_AuthenticationMethod];
+          [options setValue:authenticationRealm forKey:GCDWebServerOption_AuthenticationRealm];
+          [options setObject:authenticationUser forKey:GCDWebServerOption_AuthenticationUser];
+          [options setObject:authenticationPassword forKey:GCDWebServerOption_AuthenticationPassword];
+        }
         if ([webServer runWithOptions:options]) {
           result = 0;
         }