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

Method handleSessionPOST

rest/session_api.go:44–85  ·  view source on GitHub ↗

POST /_session creates a login session and sets its cookie

()

Source from the content-addressed store, hash-verified

42
43// POST /_session creates a login session and sets its cookie
44func (h *handler) handleSessionPOST() error {
45 err := h.checkLoginCORS()
46 if err != nil {
47 return err
48 }
49
50 oneTime := h.getBoolQuery("one_time")
51
52 // NOTE: handleSessionPOST doesn't handle creating users from OIDC - checkPublicAuth calls out into AuthenticateUntrustedJWT.
53 // Therefore, if by this point `h.user` is guest, this isn't creating a session from OIDC.
54 if h.db.Options.DisablePasswordAuthentication && (h.user == nil || h.user.Name() == "") {
55 return ErrLoginRequired
56 }
57 user, err := h.getUserFromSessionRequestBody()
58
59 ttl := defaultSessionTTL
60 if oneTime {
61 ttl = oneTimeSessionTTL
62 }
63 var sessionID string
64 // If we fail to get a user from the body and we've got a non-GUEST authenticated user, create the session based on that user
65 if user == nil && h.user != nil && h.user.Name() != "" {
66 sessionID, err = h.makeSessionWithTTL(h.user, ttl, oneTime)
67 if err != nil {
68 return err
69 }
70 user = h.user
71 } else if err != nil {
72 return err
73 } else {
74 sessionID, err = h.makeSessionWithTTL(user, ttl, oneTime)
75 if err != nil {
76 return err
77 }
78 }
79 if oneTime {
80 h.writeJSON(h.formatSessionResponse(user, sessionID))
81 } else {
82 h.writeJSON(h.formatSessionResponse(user, ""))
83 }
84 return nil
85}
86
87func (h *handler) getUserFromSessionRequestBody() (auth.User, error) {
88

Callers

nothing calls this directly

Calls 7

checkLoginCORSMethod · 0.95
getBoolQueryMethod · 0.95
makeSessionWithTTLMethod · 0.95
writeJSONMethod · 0.95
formatSessionResponseMethod · 0.95
NameMethod · 0.65

Tested by

no test coverage detected