/
auth.go
49 lines (44 loc) · 1.48 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
package auth // import "github.com/flimzy/kivik/auth"
import (
"encoding/json"
"net/http"
"github.com/flimzy/kivik/authdb"
)
// Handler is an auth handler.
type Handler interface {
// MethodName identifies the handler. It is only called once on server
// start up.
MethodName() string
// Authenticate authenticates the HTTP request. On success, a user context
// must be returned. Any error will immediately terminate the authentication
// process, returning an error to the client. In particular, this means that
// an "unauthorized" error must not be returned if fallthrough is intended.
// If a response is sent, execution does not continue. This allows handlers
// to expose their own API endpoints (for example, the default cookie auth
// handler adds POST /_session and DELETE /_session handlers).
Authenticate(http.ResponseWriter, *http.Request) (*authdb.UserContext, error)
}
// Session represents an authenticated session.
type Session struct {
AuthMethod string
AuthDB string
Handlers []string
User *authdb.UserContext
}
// MarshalJSON satisfies the json.Marshaler interface.
func (s *Session) MarshalJSON() ([]byte, error) {
user := s.User
if user == nil {
user = &authdb.UserContext{}
}
result := map[string]interface{}{
"info": map[string]interface{}{
"authenticated": s.AuthMethod,
"authentication_db": s.AuthDB,
"authentication_handlers": s.Handlers,
},
"ok": true,
"userCtx": user,
}
return json.Marshal(result)
}