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

Method checkPublicAuth

rest/handler.go:917–949  ·  view source on GitHub ↗

checkPublicAuth verifies that the current request is authenticated for the given database. Returns an HTTPError if authentication fails. NOTE: checkPublicAuth is not used for the admin interface.

(dbCtx *db.DatabaseContext)

Source from the content-addressed store, hash-verified

915// authentication fails.
916// NOTE: checkPublicAuth is not used for the admin interface.
917func (h *handler) checkPublicAuth(dbCtx *db.DatabaseContext) (err error) {
918
919 h.user = nil
920 if dbCtx == nil {
921 return nil
922 }
923
924 start := time.Now()
925 auditFields, err := h.setUserForPublicAuth(dbCtx)
926 dbCtx.DbStats.Security().TotalAuthTime.Add(time.Since(start).Nanoseconds())
927 if err != nil {
928 dbCtx.DbStats.Security().AuthFailedCount.Add(1)
929 if errors.Is(err, ErrInvalidLogin) {
930 base.Audit(h.ctx(), base.AuditIDPublicUserAuthenticationFailed, auditFields)
931 }
932 return
933 }
934 dbCtx.DbStats.Security().AuthSuccessCount.Add(1)
935
936 username := ""
937 if h.isGuest() {
938 username = base.GuestUsername
939 } else if h.user != nil {
940 username = h.user.Name()
941 }
942 roleNames, err := getSGUserRolesForAudit(dbCtx, h.user)
943 if err != nil {
944 base.InfofCtx(h.ctx(), base.KeyHTTP, "Unable to retrieve user roles for audit logging, will be omitted from audit entry: %v", err)
945 }
946 h.rqCtx = base.UserLogCtx(h.ctx(), username, base.UserDomainSyncGateway, roleNames)
947 base.Audit(h.ctx(), base.AuditIDPublicUserAuthenticated, auditFields)
948 return err
949}
950
951// setUserForPublicAuth sets h.user based on the authentication information in the request. Returns an error if the user
952// can not authenticate successfully, and returns AuditFields even in the case that there is an error in the request.

Callers 1

Calls 11

setUserForPublicAuthMethod · 0.95
ctxMethod · 0.95
isGuestMethod · 0.95
AuditFunction · 0.92
InfofCtxFunction · 0.92
UserLogCtxFunction · 0.92
getSGUserRolesForAuditFunction · 0.85
SecurityMethod · 0.80
NameMethod · 0.65
AddMethod · 0.45
SinceMethod · 0.45

Tested by

no test coverage detected