github.com/go-kivik/kivik/v4@v4.3.2/couchdb/session.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 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 }