Accessing Basic Auth Credentials

I am working on an API using GoLang. All calls to this API will contain public_key

(username) and some will also contain private_key

(password) in the authorization header.

I am trying to figure out how to access the Auth header elements so that I can verify the credentials for the database.

I am using Julien Schmidt and Alice's router to bundle middleware. My setup so far:

func main() {
    session, err := mgo.Dial("conn-string")
    if err != nil {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)

    c := appContext{session.DB("db-name")}
    commonHandlers := alice.New(context.ClearHandler)
    router := NewRouter()
    router.Get("/", commonHandlers.Append(basicAuthHandler).ThenFunc(c.mainHandler))

    http.ListenAndServe(":5000", router)
}

      

but I'm not sure how to proceed with the function basicAuthHandler

below. If present public_key

, I need to check if it is correct. Ditto for private_key

if enabled.

func basicAuthHandler(next http.Handler) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {

    }

    return http.HandlerFunc(fn)
}

      

+3


source to share


1 answer


Try to run the function BasicAuth

as requested:



user, password, ok := r.BasicAuth()
if !ok {
  // could not get basic authentication credentials
}

      

+2


source







All Articles