Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for password-protected databases

* CouchEmbeddedServer gets the database's admin credential so requests will be authorized.
* CouchChangeTracker does basic auth if necessary to fetch the _changes feed.

Change-Id: I1682bed1d5a462c340b91c72420651cb5953c513
  • Loading branch information...
commit 4ef4ae5c4aaaaa3f470901dc056f21c886c25d93 1 parent 8552e8e
@snej snej authored
View
18 Couch/CouchChangeTracker.m
@@ -78,12 +78,20 @@ - (BOOL) start {
NSAssert(!_trackingInput, @"Already started");
NSURL* url = _database.URL;
- _trackingRequest = [[NSString stringWithFormat:
+ NSMutableString* request = [NSMutableString stringWithFormat:
@"GET /%@/_changes?feed=continuous&heartbeat=300000&since=%u HTTP/1.1\r\n"
- @"Host: %@\r\n"
- @"\r\n",
- _database.relativePath, _lastSequenceNumber, url.host] copy];
- COUCHLOG2(@"%@: Starting with request:\n%@", self, _trackingRequest);
+ @"Host: %@\r\n",
+ _database.relativePath, _lastSequenceNumber, url.host];
+ NSURLCredential* credential = [_database credentialForOperation: nil];
+ if (credential) {
+ NSString* auth = [NSString stringWithFormat: @"%@:%@",
+ credential.user, credential.password];
+ auth = [RESTBody base64WithData: [auth dataUsingEncoding: NSUTF8StringEncoding]];
+ [request appendFormat: @"Authorization: Basic %@", auth];
+ }
+ COUCHLOG2(@"%@: Starting with request:\n%@", self, request);
+ [request appendString: @"\r\n"];
+ _trackingRequest = [request copy];
/* Why are we using raw TCP streams rather than NSURLConnection? Good question.
NSURLConnection seems to have some kind of bug with reading the output of _changes, maybe
View
3  Couch/CouchEmbeddedServer.h
@@ -31,6 +31,9 @@
(If you call -start:, the block will still be called.) */
- (id) initWithURL:(NSURL *)url;
+/** The underlying CouchbaseMobile object that manages the embedded server. */
+@property (readonly) CouchbaseMobile* couchbase;
+
/** Starts the server, asynchronously.
@param onStartBlock A block to be called when the server finishes starting up (or fails to). At that point you can start to access databases, etc.
@return YES if startup began, NO if a fatal error occurred. */
View
5 Couch/CouchEmbeddedServer.m
@@ -75,6 +75,9 @@ - (void)dealloc {
}
+@synthesize couchbase = _couchbase;
+
+
- (NSURL*)URL {
NSURL* url = [super URL];
NSAssert(url.port != 0, @"Can't use CouchEmbeddedServer till it's started up");
@@ -113,6 +116,8 @@ -(void)couchbaseMobile:(CouchbaseMobile*)couchbase didStart:(NSURL*)serverURL {
NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
NSString* notificationToPost;
if (firstStart) {
+ if ([couchbase respondsToSelector: @selector(adminCredential)])
+ self.credential = couchbase.adminCredential;
self.tracksActiveOperations = YES;
#if TARGET_OS_IPHONE
UIApplication* app = [UIApplication sharedApplication];
View
17 Couch/CouchbaseMobile.h
@@ -42,11 +42,12 @@
@private
id<CouchbaseDelegate> _delegate;
CFAbsoluteTime _timeStarted;
- NSString* _documentsDirectory;
+ NSString* _rootDirectory;
NSString* _bundlePath;
NSString* _iniFilePath;
NSURL* _serverURL;
NSError* _error;
+ uint8_t _logLevel;
BOOL _autoRestart;
BOOL _started;
}
@@ -80,6 +81,10 @@
/** Defaults to YES, set to NO to prevent auto-restart behavior when app returns from background */
@property (assign) BOOL autoRestart;
+/** A credential containing the admin username and password of the server.
+ These are required in any requests sent to the server. The password is generated randomly on first launch. */
+@property (readonly) NSURLCredential* adminCredential;
+
#pragma mark CONFIGURATION:
/** Initializes the instance with a nonstandard location for the runtime resources.
@@ -87,6 +92,11 @@
require resources to go elsewhere, so in that case you might need to use a custom path.) */
- (id) initWithBundlePath: (NSString*)bundlePath;
+/** The root directory where Couchbase Mobile will store data files.
+ This defaults to ~/CouchbaseMobile.
+ You may NOT change this after starting the server. */
+@property (copy) NSString* rootDirectory;
+
/** The directory where CouchDB writes its log files. */
@property (readonly) NSString* logDirectory;
@@ -105,6 +115,11 @@
emptying the file at this path before calling -start.*/
@property (readonly) NSString* localIniFilePath;
+/** Controls the amount of logging by Erlang and CouchDB.
+ Defaults to 0, meaning none.
+ 1 logs errors only, 2 also logs CouchDB info (like HTTP requests), 3 logs Erlang 'progress'. */
+@property uint8_t logLevel;
+
/** Copies a database file into the databaseDirectory if no such file exists there already.
Call this before -start, to set up initial contents of one or more databases on first run. */
- (BOOL) installDefaultDatabase: (NSString*)databasePath;
Please sign in to comment.
Something went wrong with that request. Please try again.