1 Star 1 Fork 0

湖底观景 / GolangTraining

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
GoesToEleven 提交于 2016-04-20 17:45 . changes dir structure
package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"io"
"net/http"
)
func getCode(data string) string {
h := hmac.New(sha256.New, []byte("ourkey"))
io.WriteString(h, data)
return fmt.Sprintf("%x", h.Sum(nil))
}
func main() {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie("session-id")
// cookie is not set
if err != nil {
//id, _ := uuid.NewV4()
cookie = &http.Cookie{
Name: "session-id",
}
}
if req.FormValue("email") != "" {
cookie.Value = req.FormValue("email")
}
code := getCode(cookie.Value)
cookie.Value = code + "|" + cookie.Value
// this doesn't run
// need more code added to work
// just shown for example of how to do auth with HMAC
http.SetCookie(res, cookie)
io.WriteString(res, `<!DOCTYPE html>
<html>
<body>
<form method="POST">
`+cookie.Value+`
<input type="email" name="email">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>`)
})
http.ListenAndServe(":9000", nil)
}
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

搜索帮助