mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 10:56:29 +01:00
This commit is contained in:
parent
c22cb3a188
commit
ab226d925a
|
@ -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
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user