You are not logged in.
Pages: 1
Hello,
currently the TMongoClient class only features one constructor, defined like this:
constructor Create(const Host: RawUTF8; Port: Integer=MONGODB_DEFAULTPORT;
const SecondaryHostCSV: RawUTF8=''; const SecondaryPortCSV: RawUTF8=''); overload;
But, even though it's very easy to use, the above is somewhat limited.
Why not adding an overloaded constructor like the following:
constructor Create(const MongoDBConnString: RawUTF8); overload;
That would allow an expert user to create the TMongoClient object by passing a standard MongoDB connection string, as specified here:
http://docs.mongodb.org/manual/referenc … on-string/
You'd be able to do beautiful things like this:
fClient := TMongoClient.Create('mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000');
Offline
No answer to this? I thought it was a good idea...
Offline
In my opinion it is good idea but .....
... sometimes something you need do it yourself
it is open source
Lazarus x64 Linux
Offline
Marius, I agree. And if you look at the source code you will actually find my contributions.
But this is a "delicate" addition, hence my request to the higher level person.
Offline
Anyone willing to pick up this proposal?
Offline
Hello,
I tested the MongoDB sample and it works just fine with a local MongoDB.
But I would also like to connect to a remote MongoDB on MongoLab in the following format:
"mongodb://<dbuser>:<dbpassword>@ds04542.mongolab.com:44542/mymongodb"
Does somebody have a solution or maybe a workaround for this?!
I tried a static change in Create method but as soon I assign a Database (fDB := fClient.Database['mymongodb'];) I get an exception "Query failure".
Thanks for any help...
Michael
Offline
Here is implementation of TMongoClient.OpenAuth method:
uses
SynCrypto;
...
function TMongoClient.OpenAuth(const DatabaseName, UserName,
PassWord: RawUTF8): TMongoDatabase;
var res,bson: variant;
err,nonce,key: RawUTF8;
begin
if (self=nil) or (UserName='') or (PassWord='') then
result := nil else begin
result := TMongoDatabase(fDatabases.GetObjectByName(DatabaseName));
if result=nil then begin // not already opened -> try now from primary host
if not fConnections[0].Opened then begin
fConnections[0].Open;
// step 1
bson := BSONVariant('{getnonce:1}');
err := fConnections[0].RunCommand(DatabaseName,bson,res);
if err<>'' then
raise EMongoException.CreateUTF8('%.OpenAuth("%") step1 error: %',[self,DatabaseName,err]);
// step 2
nonce := _Safe(res)^.GetValueOrRaiseException('nonce');
key := MD5(nonce + UserName + MD5(UserName + ':mongo:' + PassWord));
bson := BSONVariant('{authenticate:1,user:?,nonce:?,key:?}',[],[UserName,nonce,key]);
err := fConnections[0].RunCommand(DatabaseName,bson,res);
if err<>'' then
raise EMongoException.CreateUTF8('%.OpenAuth("%") step2 error: %',[self,DatabaseName,err]);
end;
result := TMongoDatabase.Create(Self,DatabaseName);
fDatabases.AddObject(DatabaseName,result);
end;
end;
if result=nil then
raise EMongoException.CreateUTF8('%.Open("%") unknown DB',[self,DatabaseName]);
end;
Tested on localhost with MongoDB 2.6 and Delphi 2007.
Based on MongoDB docs: Implement Authentication in a Driver
Last edited by zed (2015-07-17 10:44:04)
Offline
Included in http://synopse.info/fossil/info/59481a85f9
Thanks a lot zed, for the patch!
Online
Bad news: MongoDB 3.x has 5 (!) auth mechanisms, and MONGODB-CR that I implement is deprecated. Now default is SCRAM-SHA-1 and MongoLab use it.
I found implementations of all mechanisms in one place in PyMongo driver: auth.py and SCRAM is terrible.
Last edited by zed (2015-07-17 21:17:08)
Offline
Sounds not so difficult to implement.
Online
I've implemented SCRAM-SHA1 authentication.
See http://synopse.info/fossil/info/5e2e99181e
Feedback is welcome!
BTW, our implementation is much easier to follow (and also much faster) than TMongoWire unit (which would not be able to handle password bigger than 64 chars, I'm afraid).
I tried to stick to https://tools.ietf.org/html/rfc5802 official definition.
Online
I test it with mongod 2.6, 3.0.4 and with MongoLab and it works! ab, you’re awesome.
Auto-detect authentication mechanism by server version is a good idea, but to be on the safe side, сan you make option to use MONGODB-CR with MongoDB 3.xx?
Offline
Indeed.
I've added a ForceMongoDBCR optional parameter to OpenAuth() method.
See http://synopse.info/fossil/info/f8536c7fba
Online
Thanks a lot.
Offline
Pages: 1