forked from upstream/filebrowser
+10








3c02eedca6
Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: Gabriel Górski <me@glaeqen.eu> Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com> Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com> Co-authored-by: carloda <35763669+carloda@users.noreply.github.com> Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com> Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com> Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com> Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com> Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com> Co-authored-by: Juan Soler <juansoler@gmail.com> Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net> Co-authored-by: Neil <kylelzzz02@icloud.com> Co-authored-by: Graham <info@quantumx-apps.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/database/access"
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
|
)
|
|
|
|
// Auther is the authentication interface.
|
|
type Auther interface {
|
|
// Auth is called to authenticate a request.
|
|
Auth(r *http.Request, userStore *users.Storage) (*users.User, error)
|
|
}
|
|
|
|
const FB_ISSUER = "FileBrowser Quantum" // don't change
|
|
|
|
// IsRevokedApiToken checks if a token is in the revoked list.
|
|
func IsRevokedApiToken(accessStore *access.Storage, token string) bool {
|
|
if accessStore == nil {
|
|
return false
|
|
}
|
|
return accessStore.IsTokenRevoked(token)
|
|
}
|
|
|
|
// RevokeApiToken adds a token to the revoked list.
|
|
func RevokeApiToken(accessStore *access.Storage, token string) error {
|
|
if accessStore == nil {
|
|
return fmt.Errorf("access storage not available")
|
|
}
|
|
return accessStore.RevokeToken(token)
|
|
}
|
|
|
|
func MakeSignedTokenAPI(user *users.User, name string, duration time.Duration, perms users.Permissions, minimal bool) (string, users.AuthToken, error) {
|
|
if _, ok := user.Tokens[name]; ok {
|
|
return "", users.AuthToken{}, fmt.Errorf("key already exists with same name %v ", name)
|
|
}
|
|
now := time.Now()
|
|
expires := now.Add(duration)
|
|
// Create minimal token with only JWT standard claims
|
|
claim := users.AuthToken{
|
|
MinimalAuthToken: users.MinimalAuthToken{
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
IssuedAt: jwt.NewNumericDate(now),
|
|
ExpiresAt: jwt.NewNumericDate(expires),
|
|
Issuer: FB_ISSUER,
|
|
},
|
|
},
|
|
}
|
|
if !minimal {
|
|
claim.Permissions = users.SanitizeTokenPermissions(perms)
|
|
claim.BelongsTo = user.ID
|
|
}
|
|
signedClaims := jwt.Claims(claim)
|
|
if minimal {
|
|
// Sign only standard JWT claims. The full AuthToken struct always JSON-marshals
|
|
// a zero Permissions object, which bloats the token past WebDAV client limits.
|
|
signedClaims = claim.MinimalAuthToken
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, signedClaims)
|
|
tokenString, err := token.SignedString([]byte(settings.Config.Auth.Key))
|
|
if err != nil {
|
|
return "", users.AuthToken{}, err
|
|
}
|
|
return tokenString, claim, nil
|
|
|
|
}
|