mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Fix starting the api auth token cleaner task
This commit is contained in:
parent
545e8e6821
commit
34e076f9ff
2 changed files with 18 additions and 6 deletions
|
@ -37,16 +37,19 @@ type Authenticator func(ctx context.Context, s *http.Server, r *http.Request) (e
|
|||
|
||||
// SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be permitted.
|
||||
func SetAuthenticator(fn Authenticator) error {
|
||||
if module.Online() {
|
||||
return ErrAuthenticationAlreadySet
|
||||
}
|
||||
|
||||
authFnLock.Lock()
|
||||
defer authFnLock.Unlock()
|
||||
|
||||
if authFn == nil {
|
||||
authFn = fn
|
||||
module.NewTask("clean api auth tokens", cleanAuthTokens).Repeat(time.Minute)
|
||||
return nil
|
||||
if authFn != nil {
|
||||
return ErrAuthenticationAlreadySet
|
||||
}
|
||||
|
||||
return ErrAuthenticationAlreadySet
|
||||
authFn = fn
|
||||
return nil
|
||||
}
|
||||
|
||||
func authMiddleware(next http.Handler) http.Handler {
|
||||
|
|
11
api/main.go
11
api/main.go
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/modules"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ var (
|
|||
|
||||
// API Errors
|
||||
var (
|
||||
ErrAuthenticationAlreadySet = errors.New("the authentication function has already been set")
|
||||
ErrAuthenticationAlreadySet = errors.New("the authentication function has already been set (or must be set earlier)")
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -30,6 +31,14 @@ func prep() error {
|
|||
func start() error {
|
||||
logFlagOverrides()
|
||||
go Serve()
|
||||
|
||||
// start api auth token cleaner
|
||||
authFnLock.Lock()
|
||||
defer authFnLock.Unlock()
|
||||
if authFn == nil {
|
||||
module.NewTask("clean api auth tokens", cleanAuthTokens).Repeat(time.Minute)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue