mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-14 19:20:37 +03:00
No need to check for first time when authenticating. One less SQL call per request
This commit is contained in:
parent
d54129ecd2
commit
59b99d2206
@ -169,15 +169,6 @@ func validateLogin(userRepo model.UserRepository, userName, password string) (*m
|
|||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func contextWithUser(ctx context.Context, ds model.DataStore, username string) (context.Context, error) {
|
|
||||||
user, err := ds.User(ctx).FindByUsername(username)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(ctx, "Authenticated username not found in DB", "username", username)
|
|
||||||
return ctx, err
|
|
||||||
}
|
|
||||||
return request.WithUser(ctx, *user), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method maps the custom authorization header to the default 'Authorization', used by the jwtauth library
|
// This method maps the custom authorization header to the default 'Authorization', used by the jwtauth library
|
||||||
func authHeaderMapper(next http.Handler) http.Handler {
|
func authHeaderMapper(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -216,14 +207,16 @@ func UsernameFromReverseProxyHeader(r *http.Request) string {
|
|||||||
return username
|
return username
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticateRequest(ds model.DataStore, r *http.Request, findUsernameFns ...func(r *http.Request) string) (context.Context, error) {
|
func contextWithUser(ctx context.Context, ds model.DataStore, username string) (context.Context, error) {
|
||||||
ctx := r.Context()
|
user, err := ds.User(ctx).FindByUsername(username)
|
||||||
c, err := ds.User(ctx).CountAll()
|
if err == nil {
|
||||||
firstTime := c == 0 && err == nil
|
return request.WithUser(ctx, *user), nil
|
||||||
if firstTime {
|
|
||||||
return nil, ErrNoUsers
|
|
||||||
}
|
}
|
||||||
|
log.Error(ctx, "Authenticated username not found in DB", "username", username)
|
||||||
|
return ctx, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func authenticateRequest(ds model.DataStore, r *http.Request, findUsernameFns ...func(r *http.Request) string) (context.Context, error) {
|
||||||
var username string
|
var username string
|
||||||
for _, fn := range findUsernameFns {
|
for _, fn := range findUsernameFns {
|
||||||
username = fn(r)
|
username = fn(r)
|
||||||
@ -242,10 +235,6 @@ func Authenticator(ds model.DataStore) func(next http.Handler) http.Handler {
|
|||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, err := authenticateRequest(ds, r, UsernameFromToken, UsernameFromReverseProxyHeader)
|
ctx, err := authenticateRequest(ds, r, UsernameFromToken, UsernameFromReverseProxyHeader)
|
||||||
if err == ErrNoUsers {
|
|
||||||
_ = rest.RespondWithJSON(w, http.StatusUnauthorized, map[string]string{"message": ErrNoUsers.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = rest.RespondWithError(w, http.StatusUnauthorized, "Not authenticated")
|
_ = rest.RespondWithError(w, http.StatusUnauthorized, "Not authenticated")
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user