Authy is a go library that acts as an oauth authentication middleware for net/http, it aims to provide drop-in support for most OAuth 1 (not implemented yet) and 2 providers. It is inspired from node.js libraries such as grant or everyauth.
The current OAuth implementation is kinda rough and basic but should do the trick.
The current list of providers is a verbatim adaptation of the one provided by grant.
With martini:
server.go
package main
import (
"encoding/json"
"github.com/go-martini/martini"
"github.com/gophergala/authy/martini"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"os"
)
type Config struct {
Secret string `json:"secret"`
Authy authy.Config `json:"authy"`
}
func readConfig() (Config, error) {
f, err := os.Open("config.json")
if err != nil {
return Config{}, err
}
decoder := json.NewDecoder(f)
var config Config
decoder.Decode(&config)
return config, nil
}
func main() {
// read app config (and authy config)
config, err := readConfig()
if err != nil {
panic(err)
}
// setup Martini
m := martini.Classic()
m.Use(sessions.Sessions("authy", sessions.NewCookieStore([]byte(config.Secret))))
// register our middleware
m.Use(authy.Authy(config.Authy))
m.Use(render.Renderer())
// see the LoginRequired middleware, automatically redirect to the login page if necessary
m.Get("/generic_callback", authy.LoginRequired(), func(token authy.Token, r render.Render) {
r.HTML(200, "callback", token)
})
m.Run()
}
templates/callback.tmpl
<html>
<body>
<h2>{{.Value}} <small>({{.Scope}})</small></h2>
</body>
</html>
config.json
{
"authy": {
"login_page": "/login",
"callback": "/generic_callback",
"providers": {
"github": {
"key": "my-app-key",
"secret": "my-app-secret",
"scope": ["repo", "user:email"]
}
}
}
}