Fix starting the api auth token cleaner task

This commit is contained in:
Daniel 2020-06-26 22:41:47 +02:00
parent 545e8e6821
commit 34e076f9ff
2 changed files with 18 additions and 6 deletions

View file

@ -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 {

View file

@ -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
}