MCPcopy
hub / github.com/dnote/dnote / Auth

Function Auth

pkg/server/middleware/auth.go:66–91  ·  view source on GitHub ↗

Auth is an authentication middleware

(db *gorm.DB, next http.HandlerFunc, p *AuthParams)

Source from the content-addressed store, hash-verified

64
65// Auth is an authentication middleware
66func Auth(db *gorm.DB, next http.HandlerFunc, p *AuthParams) http.HandlerFunc {
67 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68 user, ok, err := AuthWithSession(db, r)
69 if !ok {
70 if p != nil && p.RedirectGuestsToLogin {
71
72 q := url.Values{}
73 q.Set("referrer", r.URL.Path)
74 path := helpers.GetPath("/login", &q)
75
76 http.Redirect(w, r, path, http.StatusFound)
77 return
78 }
79
80 RespondUnauthorized(w)
81 return
82 }
83 if err != nil {
84 DoError(w, "authenticating with session", err, http.StatusInternalServerError)
85 return
86 }
87
88 ctx := context.WithUser(r.Context(), &user)
89 next.ServeHTTP(w, r.WithContext(ctx))
90 })
91}
92
93// TokenAuth is an authentication middleware with token
94func TokenAuth(db *gorm.DB, next http.HandlerFunc, tokenType string, p *AuthParams) http.HandlerFunc {

Callers 2

TestAuthFunction · 0.85
TestWithAccountFunction · 0.85

Calls 6

GetPathFunction · 0.92
WithUserFunction · 0.92
AuthWithSessionFunction · 0.85
RespondUnauthorizedFunction · 0.85
DoErrorFunction · 0.85
ServeHTTPMethod · 0.80

Tested by 2

TestAuthFunction · 0.68
TestWithAccountFunction · 0.68