Merge pull request #59 from safing/fix/api-auth-token-cleaner

Fix starting the api auth token cleaner task
This commit is contained in:
Daniel 2020-06-27 14:50:53 +02:00 committed by GitHub
commit 56a7b8c16e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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. // SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be permitted.
func SetAuthenticator(fn Authenticator) error { func SetAuthenticator(fn Authenticator) error {
if module.Online() {
return ErrAuthenticationAlreadySet
}
authFnLock.Lock() authFnLock.Lock()
defer authFnLock.Unlock() defer authFnLock.Unlock()
if authFn == nil { if authFn != nil {
authFn = fn return ErrAuthenticationAlreadySet
module.NewTask("clean api auth tokens", cleanAuthTokens).Repeat(time.Minute)
return nil
} }
return ErrAuthenticationAlreadySet authFn = fn
return nil
} }
func authMiddleware(next http.Handler) http.Handler { func authMiddleware(next http.Handler) http.Handler {

View file

@ -3,6 +3,7 @@ package api
import ( import (
"context" "context"
"errors" "errors"
"time"
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
) )
@ -13,7 +14,7 @@ var (
// API Errors // API Errors
var ( 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() { func init() {
@ -30,6 +31,14 @@ func prep() error {
func start() error { func start() error {
logFlagOverrides() logFlagOverrides()
go Serve() 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 return nil
} }