mirror of
https://github.com/safing/portbase
synced 2025-09-02 10:40:39 +00:00
Merge pull request #59 from safing/fix/api-auth-token-cleaner
Fix starting the api auth token cleaner task
This commit is contained in:
commit
56a7b8c16e
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.
|
// 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 {
|
||||||
|
|
11
api/main.go
11
api/main.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue