More work on the api. Also auth middleware stuff
More work on the placeholder functions for the Linstrom API Additionally, started work on a slightly more sophisticated authentication control system And ran `go generate` again
This commit is contained in:
parent
b9c95a0297
commit
873f52d64f
14 changed files with 637 additions and 300 deletions
|
@ -60,7 +60,7 @@ func LoggingMiddleware(handler http.Handler) http.Handler {
|
|||
|
||||
func passkeyIdToAccountIdTransformerMiddleware(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
s := StorageFromRequest(w, r)
|
||||
s := StorageFromRequest(r)
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
|
@ -100,3 +100,55 @@ func profilingAuthenticationMiddleware(handler http.Handler) http.Handler {
|
|||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Middleware for inserting a logged in account's id into the request context if a session exists
|
||||
// Does not cancel requests ever. If an error occurs, it's treated as if no session is set
|
||||
func checkSessionMiddleware(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("sid")
|
||||
log := hlog.FromRequest(r)
|
||||
if err != nil {
|
||||
// No cookie is ok, this function is only for inserting account id into the context
|
||||
// if one exists, not for checking permissions
|
||||
log.Debug().Msg("No session cookie, passing along")
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
store := StorageFromRequest(r)
|
||||
session, ok := store.GetSession(cookie.Value)
|
||||
if !ok {
|
||||
// Failed to get session from cookie id. Log, then move on as if no session is set
|
||||
log.Warn().
|
||||
Str("session-id", cookie.Value).
|
||||
Msg("Cookie with session id found, but session doesn't exist")
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if session.Expires.Before(time.Now()) {
|
||||
// Session expired. Move on as if no session was set
|
||||
store.DeleteSession(cookie.Value)
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
acc, err := store.FindAccountByPasskeyId(session.UserID)
|
||||
if err != nil {
|
||||
// Failed to get account for passkey id. Log, then move on as if no session is set
|
||||
log.Error().
|
||||
Err(err).
|
||||
Bytes("passkey-id", session.UserID).
|
||||
Msg("Failed to get account with passkey id while checking session. Ignoring session")
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(
|
||||
w,
|
||||
r.WithContext(
|
||||
context.WithValue(
|
||||
r.Context(),
|
||||
ContextKeyActorId,
|
||||
acc.ID,
|
||||
),
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue