photoprism/internal/api/session.go
Michael Mayer fed1d8ad95 Auth: Accept access token as passwd with fail rate limit #782 #808 #3943
Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-14 18:28:17 +01:00

37 lines
990 B
Go

package api
import (
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/get"
"github.com/photoprism/photoprism/internal/server/limiter"
"github.com/photoprism/photoprism/pkg/rnd"
)
// Session finds the client session for the specified auth token, or returns nil if not found.
func Session(clientIp, authToken string) *entity.Session {
// Skip authentication when running in public mode.
if get.Config().Public() {
return get.Session().Public()
}
// Fail if the auth token does not have a supported format.
if !rnd.IsAuthAny(authToken) {
return nil
}
// Fail if authentication error rate limit is exceeded.
if clientIp != "" && limiter.Auth.Reject(clientIp) {
return nil
}
// Find the session based on the hashed auth token, or return nil otherwise.
if s, err := entity.FindSession(rnd.SessionID(authToken)); err != nil {
if clientIp != "" {
limiter.Auth.Reserve(clientIp)
}
return nil
} else {
return s
}
}