Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-504

how to NewClient with SCRAM-SHA-1, usename, password and database

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 0.0.9
    • Component/s: Authentication, Connections
    • Labels:
      None
    • Environment:
      linux

      package main

      import (
      "context"
      "fmt"
      "github.com/apex/log"
      "github.com/mongodb/mongo-go-driver/bson"
      "github.com/mongodb/mongo-go-driver/mongo"
      "github.com/mongodb/mongo-go-driver/mongo/clientopt"
      "time"
      )

      func main()

      { f1() }

      func f1() {
      appname := clientopt.AppName("myapp")
      conTimeout := clientopt.ConnectTimeout(time.Second)
      sockTimout := clientopt.SocketTimeout(time.Second)
      serverSTimeout := clientopt.ServerSelectionTimeout(time.Second * 3)
      single := clientopt.Single(true)
      auth := clientopt.Auth(clientopt.Credential

      {AuthMechanism: "SCRAM-SHA-1", AuthSource: "xxx", Username: "xxx", Password: "as@xxx"}

      )

      client, err := mongo.NewClientWithOptions("mongodb://localhost:27017", single, serverSTimeout, appname, sockTimout, conTimeout, auth)
      if err != nil

      { log.WithError(err).Fatal("error connecting") }
      err = client.Connect(context.Background())
      if err != nil { log.WithError(err).Fatal("error connecting") }

      fmt.Println(client.ListDatabases(context.Background(), nil))

      collection := client.Database("xxx").Collection("test")

      cur, err := collection.Find(context.Background(), nil)
      if err != nil

      { log.WithError(err).Fatal("failed to find collection") }

      defer cur.Close(context.Background())
      for cur.Next(context.Background()) {
      elem := bson.NewDocument()
      err := cur.Decode(elem)
      if err != nil

      { log.WithError(err).Fatal("failed to decode element") }

      // do something with elem....
      fmt.Printf("%v\n", elem.ToExtJSON(true))
      }
      if err := cur.Err(); err != nil

      { log.WithError(err).Fatal("failed to get cursor") }

      }

      than go run main.go, it outputs

      command-line-arguments

      {[] 0}

      auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": Authentication failed.
      2018/07/20 17:48:35 fatal failed to find collection error=auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": Authentication failed.
      exit status 1

      how to make it works

            Assignee:
            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            Reporter:
            jackylee orange.jackylee
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: