stijnsanders / TMongoWire

Delphi MongoDB driver
MIT License
102 stars 37 forks source link

Could not connect (authenticate) to cloud server (MongoDB 3.0 server) using MongoAuth or MongoAuth3 #27

Closed tblimeira closed 9 years ago

tblimeira commented 9 years ago

Hi There!

I tried to connect and authenticate to a cloud server (MongoDB 3.0), but I always get the same error with MongoAuth or MongoAuth3 units

The error is 'MongoWire: failed to authenticate as username'

I managed to connect to localhost and to another cloudserver (MongoDB 2.69) MongoLab with no problems, but to apollo.modulusmongo.net I could not.

Can you help me? Or give me any hint of what I doing wrong?

procedure TForm1.Button1Click(Sender: TObject); var db:TMongoWire;

q:TMongoWireQuery; d:IBSONDocument; wsUser, wsDatabase, wsPassword: WideString; begin wsDatabase := 'y3gabubO'; db := TMongoWire.Create(wsDatabase); db.Open('apollo.modulusmongo.net');//port 27017 default wsUser := 'uteknikao'; wsPassword := 'ptek#@ni@#kao';

MongoWireAuthenticate(db,wsUser, wsPassword);

d:=BSON; q:=TMongoWireQuery.Create(db); try q.Query('descriptionStationSensors' , nil); while q.Next(d) do ShowMessage(VarToStr(d['id']));

finally q.Free; end; end;

stijnsanders commented 9 years ago

Please check you use only one of either MongoAuth or MongoAuth3, not both. They both declare MongoWireAuthenticate so you need to have only one in a uses clause.

tblimeira commented 9 years ago

Hi there, first I tried only MongoAuth but got the same message. After that I removed MongoAuth and put MongoAuth3 and the same thing happened. Any other advises?

stijnsanders commented 9 years ago

I had a quick try, and it works with me using mongoAuth3.pas:

db:=TMongoWire.Create('y3gabubO');
db.Open('apollo.modulusmongo.net');
MongoWireAuthenticate(db,'uteknikao','ptek#@ni@#kao');

If you want to have a closer look with the debugger, remove the {$D-} and {$L-} lines at the beginning of mongoAuth3.pas, then step into MongoWireAuthenticate

tblimeira commented 9 years ago

Hi thanks for your help, but even doing so it didn't work. I am using delphi 2010.

I removed the {$D-} and {$L-} lines and I get the error at lines (545 and 546)

if VarIsNull(b['conversationId']) then raise EMongoAuthenticationFailed.CreateFmt(ErrMsg,[UserName]);

I could not figure out what is happening. Could you help me?

Local variables in this function

MongoWire ()

UserName 'uteknikao'

Password 'ptek#@ni@#kao'

nonce 's7ozLhC0MU2qxotCqjixDZWBub/XKA5F'

m1 'n=uteknikao,r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5F'

m2 'r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5FZBxNZub8H/yhO4QowsQ0neopsRuncjYf,s=Zv7AcO5mFYGcVnnFgJNL0Q==,i=10000'

m3 'c=biws,r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5FZBxNZub8H/yhO4QowsQ0neopsRuncjYf,p=3mvngj3TytNjt0+VdTs9NW4U/MU='

m4 ''

r '$'#$AB#$8A'~'#$CE'K'#$FA#$19'P'#$A7#$E2'K߸'#$90#$A1'ؘ4'#$EB

s #$B0#$B4#$97'@'#$94#7#$A9#$BB#$96'k'#$FE#$9F#$D2'cH'#$85#$93'9/$'

t #$DE'k�='#$D3#$CA#$D3'c'#$B7'O'#$95'u;=5n'#$14#$FC#$C5

u 'n=uteknikao,r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5F,r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5FZBxNZub8H/yhO4QowsQ0neopsRuncjYf,s=Zv7AcO5mFYGcVnnFgJNL0Q==,i=10000,c=biws,r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5FZBxNZub8H/yhO4QowsQ0neopsRuncjYf'

v Variant array of Byte

i 21

j 101

k 10000

l 104

p 'c=biws,r=s7ozLhC0MU2qxotCqjixDZWBub/XKA5FZBxNZub8H/yhO4QowsQ0neopsRuncjYf,p=3mvngj3TytNjt0+VdTs9NW4U/MU=««««««««'

stijnsanders commented 9 years ago

Could you add bsinUtils to the uses clause and replace line 546 with

raise Exception.Create(BsonToJSon(b));
tblimeira commented 9 years ago

This is the result:

exception

stijnsanders commented 9 years ago

Ah, but this means I have a 32bit/64bit issue in the bit that calculates 'p=' in the m3 value. I'll have to investigate further.

stijnsanders commented 9 years ago

Ah yes I copied the wrong SwapEndian from http://yoy.be/md5 That one is not 64-bits proof, please see my recent commit https://github.com/stijnsanders/TMongoWire/commit/8589d14dce466306cc5a8c96ba8cb19aeafd41c2#diff-0904c9095c070e720d703340a8d68ce7R152 Could you have a try and confirm this fixes it?

tblimeira commented 9 years ago

Hi there sorry for the delay, I just downloaded the MongoAuth3 new file, but I got an error when I tried to authenticate. Error at line 560.

image

stijnsanders commented 9 years ago

Coul you check again? You've downloaded the new version, but the change I asked you to make is not overwritten?!

tblimeira commented 9 years ago

Besides downloading MongoAuth3.pas new version, what changes do I need to do? For now, it gives me the same error, that's why I have put the bsonUtils to see BsonToJSon(b) again

stijnsanders commented 9 years ago

I took the time to run the 32 and 64 versions side by side in the debugger and step through. It appears that Length on a UTF8String doesn't always report the number of bytes (which is actually correct according to the properties of UTF-8). So I switched the hash-functions to RawByteString. (Something introduced post-Delphi-7, but nothing {$IF not Declared can't handle.) Please check and confirm it now works.

tblimeira commented 9 years ago

Hi there, did you commit a new version of MongoAuth3.pas, or do I need to place Length(RawByteString(x)) in each place? If that is what I understood.

Thanks again for your help

tblimeira commented 9 years ago

Just replaced every UFT8String with RawByteString in MongoAuth3.pas and that did the trick, thanks for your support, you really got talent!

LucasMonteiro1 commented 7 years ago

Hello! I tried the above steps and the error persists.

When accessed by the "RoboMongo" database tool is working. captura de tela 2016-11-23 10 32 31 captura de tela 2016-11-23 10 38 57

My source code in delphi:

procedure TForm1.FormCreate(Sender: TObject);
var
  MongoWire : TMongoWire;
  Doc       : IBSONDocument;
begin
  MongoWire := TMongoWire.Create('MongoWire');
  try
    MongoWire.Open('localhost', 27017);
    MongoWireAuthenticate(MongoWire, 'monteiro', '123');
    //Insert
    MongoWire.Insert('db1', BSON(['test','x']) );
    //Get
    Doc := MongoWire.Get('db1',BSON);
    Self.Caption := VarToStr(Doc['test']);
  finally
    MongoWire.Free;
    Application.Terminate;
  end;
end;

Note: If authentication is removed, the code works.

I changed the source code of the unit "mongoAuth3.pas", put what is in the comments above and the following error occurred: captura de tela 2016-11-23 10 36 03

stijnsanders commented 7 years ago

Note: If authentication is removed, the code works.

Are you sure authentication is properly enabled on the server? If you feel like it, remove {$D-}{$L-} from your copy of mongoAuth3.pas and have a step through with the debugger to have a look at the internal values.