mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
446 stars 98 forks source link

Connect with Authentication instruction... (no issue) #314

Closed GorIvanov closed 1 year ago

GorIvanov commented 1 year ago

With this driver, it is possible to connect to each DB by using Mongo Connection String like

var db = Db( "mongodb://localhost:PORT/DBNAME");

If the server with the database is located on any VPS, then anyone can connect to the database, knowing only the server IP and port. For security, you need to enable Authentication on the database.

You can log in using the string connection shown below, but after authorization, it is impossible to go beyond the created database object, you cannot switch to other databases... (like "show dbs", "use users" or "use order" etc)

var db = Db( "mongodb://USER:PASS@localhost:PORT/ADMIN");

The only way to fix this is to use next connection string: "mongodb://USER:PASS@localhost:PORT/DBNAME/?authSource=admin"

So you will be authorized in the administrator database, but in the DB object you will receive "DBNAME" database

realrk95 commented 1 year ago

I don't understand. I'm facing an issue.

MongoDartError (MongoDart Error: Authentication failed.)

I'm using dedicated cluster with 3 nodes. My db name is main:

mongodb+srv://ecom-admin:*****@ecom-new.iv00u.mongodb.net/main

This is working on mongodb compass, but on mongodart it gives the error auth failed. I also tried this:

mongodb+srv://ecom-admin:*****@ecom-new.iv00u.mongodb.net/main?authSource=admin

&

mongodb+srv://ecom-admin:*****@ecom-new.iv00u.mongodb.net/main/?authSource=admin
giorgiofran commented 1 year ago

Maybe the problem is related to the authMechanism. Consider that mongo_dart only supports SCRAM_SHA_1and SCRAM_SHA_256.

realrk95 commented 1 year ago

I checked that, I am using SCRAM only which either is defaulting to SCRAM_SHA_1 or SCRAM_SHA_256. This is working for my serverless instance, but as soon as I shifted to a dedicated cluster (since serverless does not support changeStreams), this problem started happening.

Could it be related to the hyphens in the name of the instance or the name of the db 'main'?

giorgiofran commented 1 year ago

Which is the MongoDb version? And mongo_dart version?

realrk95 commented 1 year ago

MongoDB version is 6.1 Enterprise (on compass) but on Atlas online it shows as 6.0

and mongodart: ^0.8.2

giorgiofran commented 1 year ago

Atlas hosted?

realrk95 commented 1 year ago

Yes, hosted on Atlas. An M10 Cluster Tier, type Replica Set - 3 nodes

giorgiofran commented 1 year ago

It is difficoult to say. I will do some tests, but I have time only next week.

giorgiofran commented 1 year ago

Hi, could you discover the issue? I did some tests, and on my atlas C0 database everything is working fine. I could only receive an authorization message, setting a wrong ?authSource=test auth database. I tryed setting the same auth source as the data database. But in my case passwords settings are stored in admin. As you are specifing the admin database as authSource, could you please check if the authorizations for the username that you set for the connection has been done in the same database where the data is stored?

I had no issues in using a databse named main, and, as far as I know, an hypen in the username or in general in the URL is allowed.

realrk95 commented 1 year ago

@giorgiofran

IT WORKED

mongodb+srv://ecom-admin:*****@ecom-new.iv00u.mongodb.net/main?authSource=admin&authMechanism=SCRAM-SHA-1

You were right the auth mechanism was causing the issue.