-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
52 lines (46 loc) · 1.36 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package auth
import (
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/manyminds/api2go"
)
const (
sessionName = "name"
sessionData = "data"
)
// Auth provides a handler to authenticate requests. In addition to serving the
// routes provided by the API, two additional routes are added for logging in
// and logging out.
type Auth struct {
api *api2go.API
authenticator Authenticator
router *mux.Router
store *sessions.CookieStore
}
// New creates a new handler for the provided API using the provided
// authenticator for requests. The secret key allows for persistent sessions
// and may be set to nil to disable the feature.
func New(api *api2go.API, authenticator Authenticator, secretKey []byte) *Auth {
if secretKey == nil {
secretKey = securecookie.GenerateRandomKey(32)
}
var (
r = mux.NewRouter()
a = &Auth{
api: api,
authenticator: authenticator,
router: r,
store: sessions.NewCookieStore(secretKey),
}
)
r.HandleFunc("/login", a.login).Methods(http.MethodPost)
r.HandleFunc("/logout", a.logout).Methods(http.MethodPost)
r.PathPrefix("/").HandlerFunc(a.initialize)
return a
}
// ServeHTTP responds to requests for API resources.
func (a *Auth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.router.ServeHTTP(w, r)
}