Gerasimos (Makis) Maropoulos 2020-08-15 15:40:41 +03:00
parent c22cb3a188
commit ab226d925a
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
2 changed files with 12 additions and 2 deletions

View File

@ -6,6 +6,7 @@ package basicauth
import ( import (
"encoding/base64" "encoding/base64"
"strconv" "strconv"
"sync"
"time" "time"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
@ -22,6 +23,7 @@ type (
Username string Username string
logged bool logged bool
expires time.Time expires time.Time
mu sync.RWMutex
} }
encodedUsers []*encodedUser encodedUsers []*encodedUser
@ -117,11 +119,19 @@ func (b *basicAuthMiddleware) Serve(ctx *context.Context) {
// all ok // all ok
if b.expireEnabled { if b.expireEnabled {
if !auth.logged { if !auth.logged {
auth.mu.Lock()
auth.expires = time.Now().Add(b.config.Expires) auth.expires = time.Now().Add(b.config.Expires)
auth.logged = true auth.logged = true
auth.mu.Unlock()
} }
if time.Now().After(auth.expires) { auth.mu.RLock()
expired := time.Now().After(auth.expires)
auth.mu.RUnlock()
if expired {
auth.mu.Lock()
auth.logged = false
auth.mu.Unlock()
b.askForCredentials(ctx) // ask for authentication again b.askForCredentials(ctx) // ask for authentication again
ctx.StopExecution() ctx.StopExecution()
return return

View File

@ -20,7 +20,7 @@ type Config struct {
Users map[string]string Users map[string]string
// Realm http://tools.ietf.org/html/rfc2617#section-1.2. Default is "Authorization Required" // Realm http://tools.ietf.org/html/rfc2617#section-1.2. Default is "Authorization Required"
Realm string Realm string
// Expires expiration duration, default is 0 never expires // Expires expiration duration, default is 0 never expires.
Expires time.Duration Expires time.Duration
// OnAsk fires each time the server asks to the client for credentials in order to gain access and continue to the next handler. // OnAsk fires each time the server asks to the client for credentials in order to gain access and continue to the next handler.