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)
| 915 | // authentication fails. |
| 916 | // NOTE: checkPublicAuth is not used for the admin interface. |
| 917 | func (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. |
no test coverage detected