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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘