Goth包为Go Web应用程序提供了一种简单、清晰且符合习惯的方式来编写身份验证包。
与其他类似的包不同,Goth允许您编写OAuth、OAuth2或任何其他协议提供商,只要它们实现了Provider和Session接口。
这个包的灵感来自https://github.com/intridea/omniauth。
$ go get github.com/markbates/goth
请查看examples文件夹,其中有一个可运行的应用程序,允许用户通过Twitter、Facebook、Google Plus等进行身份验证。
要运行示例,您可以从GitHub克隆源代码
$ git clone git@github.com:markbates/goth.git
或者使用
$ go get github.com/markbates/goth
$ cd goth/examples $ go get -v $ go build $ ./examples
现在打开您的浏览器,访问http://localhost:3000查看示例。
要实际使用不同的提供商,请确保设置环境变量。examples/main.go文件中给出了示例。
默认情况下,gothic使用gorilla/sessions包中的CookieStore来存储会话数据。
按照配置,这个默认存储(gothic.Store)将生成具有以下Options的cookie:
&Options{ Path: "/", Domain: "", MaxAge: 86400 * 30, HttpOnly: true, Secure: false, }
要为您的应用程序定制这些字段,您可以在启动时覆盖gothic.Store变量。
以下代码片段展示了一种实现方法:
key := "" // 替换为您的SESSION_SECRET或类似内容 maxAge := 86400 * 30 // 30天 isProd := false // 通过https提供服务时设置为true store := sessions.NewCookieStore([]byte(key)) store.MaxAge(maxAge) store.Options.Path = "/" store.Options.HttpOnly = true // HttpOnly应始终启用 store.Options.Secure = isProd gothic.Store = store
如果问题附带了拉取请求,那么解决的可能性会大大提高。
我是否希望看到更多的提供商?当然!您是否愿意贡献一个?希 望是的!
gofmt -s -w ./确保代码库遵守Go编码标准