...
1
2
3
4
5
6
7
8
9
10
11
12
13 package couchdb
14
15 import (
16 "context"
17 "encoding/json"
18 "net/http"
19
20 "github.com/go-kivik/kivik/v4/driver"
21 )
22
23 type session struct {
24 Data json.RawMessage
25 Info authInfo `json:"info"`
26 UserCtx userContext `json:"userCtx"`
27 }
28
29 type authInfo struct {
30 AuthenticationMethod string `json:"authenticated"`
31 AuthenticationDB string `json:"authentiation_db"`
32 AuthenticationHandlers []string `json:"authentication_handlers"`
33 }
34
35 type userContext struct {
36 Name string `json:"name"`
37 Roles []string `json:"roles"`
38 }
39
40 func (s *session) UnmarshalJSON(data []byte) error {
41 type alias session
42 var a alias
43 if err := json.Unmarshal(data, &a); err != nil {
44 return err
45 }
46 *s = session(a)
47 s.Data = data
48 return nil
49 }
50
51 func (c *client) Session(ctx context.Context) (*driver.Session, error) {
52 s := &session{}
53 err := c.DoJSON(ctx, http.MethodGet, "/_session", nil, s)
54 return &driver.Session{
55 RawResponse: s.Data,
56 Name: s.UserCtx.Name,
57 Roles: s.UserCtx.Roles,
58 AuthenticationMethod: s.Info.AuthenticationMethod,
59 AuthenticationDB: s.Info.AuthenticationDB,
60 AuthenticationHandlers: s.Info.AuthenticationHandlers,
61 }, err
62 }
63
View as plain text