dongluan5740 2015-05-19 13:48
浏览 23
已采纳

访问基本身份验证凭据

I'm working on an API using GoLang. All calls to this API will contain a public_key (username) and some will also contain a private_key (password) in the Authorization header.

I'm trying to figure out how to access the Auth header details so that I can check the credentials against a database.

I'm using Julien Schmidt's router and Alice to chain middleware. My setup so far is:

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 continue with the basicAuthHandler function below. If the public_key is present I need to check that it's valid. The same for the private_key if that's included.

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

    }

    return http.HandlerFunc(fn)
}
  • 写回答

1条回答 默认 最新

  • dsepcxw181184853 2015-05-19 13:56
    关注

    Try running the function BasicAuth on the request:

    user, password, ok := r.BasicAuth()
    if !ok {
      // could not get basic authentication credentials
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)