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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失