Handle Mongo Connection with Percent-Encoding in Go

Kecci
3 min readFeb 25, 2021

--

connection() : auth error: sasl conversation error: unable to authenticate using mechanism “SCRAM-SHA-1”: (AuthenticationFailed) Authentication failed.

Problem

I was tried to connect with mongo db with simple connection with mongo-go-driver. Here the code main.go :

package mainimport (
"context"
"fmt"
"net/url"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
var ctx = context.Background()func main() {
driver := "mongodb"
host := "127.0.0.1"
port := "27017"
username := "kecci"
password := "p@sword%20"
URL := url.URL{
Scheme: driver,
Host: fmt.Sprintf("%s:%s", host, port),
}
if username != "" || password != "" {
URL.User = url.UserPassword(username, password)
}
fmt.Printf("URI: %s \n", URL.String())
clientOptions := options.Client().ApplyURI(URL.String())
clientOptions.SetDirect(true)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
fmt.Printf("error when mongo.Connect(ctx, clientOptions), %v, %v", clientOptions, err)
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("ping success")
}

Then I got Error looks like in this output:

mongodb://kecci:p%40sword%2520@127.0.0.1:27017
connection() : auth error: sasl conversation error: unable to authenticate using mechanism “SCRAM-SHA-1”: (AuthenticationFailed) Authentication failed.

My username and password is include symbolic characters. Which it’s use format URL Encode or Percent-Encode by RF3986.

MongoDB Documentation:

MongoDB Documentation for Connection String

I use package net/url to generate URL (ofcourse with URL Encode).

Which mean, I do “Double Encode”

Let’s see how the comparison before, after, and expected format:

Before parsed to URL   : p@sword%20
After parsed to URL : p%40sword%2520
Expected parsed to URL : p%40sword%20

As you can see the symbol of % parsed to %25. Which is it cause of the Wrong Password.

Which is it cause of the Wrong Password.

Solving

So now I should handle it with url.QueryUnescape from net/url package golang.

And here how I solve it inmain.go :

package mainimport (
"context"
"fmt"
"net/url"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
var ctx = context.Background()func main() {
driver := "mongodb"
host := "127.0.0.1"
port := "27017"
username, err := url.QueryUnescape("kecci")
if err != nil {
username = "kecci"
}
password, err := url.QueryUnescape("p@sword%20")
if err != nil {
password = "p@sword%20"
}
URL := url.URL{
Scheme: driver,
Host: fmt.Sprintf("%s:%s", host, port),
}
if username != "" || password != "" {
URL.User = url.UserPassword(username, password)
}
fmt.Printf("URI: %s \n", URL.String())
clientOptions := options.Client().ApplyURI(URL.String())
clientOptions.SetDirect(true)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
fmt.Printf("error when mongo.Connect(ctx, clientOptions), %v, %v", clientOptions, err)
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("ping success")
}

output:

mongodb://kecci:p%40sword%20@127.0.0.1:27017
ping success

Conclusion

What I’m trying to do to solve it is :

  1. Try to Decode URL / Unescape Query of my password & username. If decode got an error, we re-set the username & password before.
  2. After username & password was clear without Encode URL / Percent-Encode. We proceed the URL.String() to get the final format.
  3. Done. You can try connect to your mongo db with the right URI.

I also put the code on my github: https://github.com/kecci/mongo-connection-go

That’s all. Have a good day.

Thank You.

Source

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kecci
Kecci

Written by Kecci

Coders + Librarian @Github

No responses yet

Write a response