MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / createUserSession

Method createUserSession

rest/session_api.go:208–250  ·  view source on GitHub ↗

ADMIN API: Generates a login session for a user and returns the session ID and cookie name.

()

Source from the content-addressed store, hash-verified

206
207// ADMIN API: Generates a login session for a user and returns the session ID and cookie name.
208func (h *handler) createUserSession() error {
209 h.assertAdminOnly()
210 var params struct {
211 Name string `json:"name"`
212 TTL int `json:"ttl"`
213 }
214 params.TTL = int(defaultSessionTTL / time.Second)
215 err := h.readJSONInto(&params)
216 if err != nil {
217 return err
218 } else if params.Name == "" || params.Name == base.GuestUsername || !auth.IsValidPrincipalName(params.Name) {
219 return base.HTTPErrorf(http.StatusBadRequest, "Invalid or missing user name")
220 }
221 authenticator := h.db.Authenticator(h.ctx())
222 user, err := authenticator.GetUser(params.Name)
223 if user == nil {
224 if err == nil {
225 err = base.HTTPErrorf(http.StatusNotFound, "No such user %q", params.Name)
226 }
227 return err
228 }
229
230 ttl := time.Duration(params.TTL) * time.Second
231 if ttl < 1.0 {
232 return base.HTTPErrorf(http.StatusBadRequest, "Invalid or missing ttl")
233 }
234
235 oneTime := false
236 session, err := authenticator.CreateSession(h.ctx(), user, ttl, oneTime)
237 if err != nil {
238 return err
239 }
240 var response struct {
241 SessionID string `json:"session_id"`
242 Expires string `json:"expires"`
243 CookieName string `json:"cookie_name"`
244 }
245 response.SessionID = session.ID
246 response.Expires = session.Expiration.UTC().Format(time.RFC3339)
247 response.CookieName = authenticator.SessionCookieName
248 h.writeJSON(response)
249 return nil
250}
251
252func (h *handler) getUserSession() error {
253

Callers

nothing calls this directly

Calls 9

assertAdminOnlyMethod · 0.95
readJSONIntoMethod · 0.95
ctxMethod · 0.95
writeJSONMethod · 0.95
IsValidPrincipalNameFunction · 0.92
HTTPErrorfFunction · 0.92
GetUserMethod · 0.80
CreateSessionMethod · 0.80
AuthenticatorMethod · 0.45

Tested by

no test coverage detected