inject refresh cache to auth, revendor go-pkgz/auth to 0.4
This commit is contained in:
Generated
+4
-3
@@ -112,18 +112,19 @@
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:1bd48d32651b8806f269540fabe6b91b900c093c1dcd98c369ca9880c71390f5"
|
||||
digest = "1:d31c72099b9e195785933d11441bc92cce9b2c421f8fca712a7eaf36a8e55a47"
|
||||
name = "github.com/go-pkgz/auth"
|
||||
packages = [
|
||||
".",
|
||||
"avatar",
|
||||
"logger",
|
||||
"middleware",
|
||||
"provider",
|
||||
"token",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "272bd70168efc2706130177a3e900eafd3cd5030"
|
||||
version = "v0.3.4"
|
||||
revision = "92256b685196d26a346dfbedea12898fe589b0eb"
|
||||
version = "v0.4.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:1933dabfb0e07548ed9684fdef857cc6f2413bbbb74ba838a7c1f77407b8fd90"
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
bolt "github.com/coreos/bbolt"
|
||||
"github.com/go-pkgz/lgr"
|
||||
log "github.com/go-pkgz/lgr"
|
||||
acache "github.com/patrickmn/go-cache"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/go-pkgz/auth"
|
||||
@@ -551,7 +552,25 @@ func (s *ServerCommand) makeAuthenticator(ds *service.DataStore, avas avatar.Sto
|
||||
AvatarResizeLimit: s.Avatar.RszLmt,
|
||||
AvatarRoutePath: "/api/v1/avatar",
|
||||
Logger: lgr.Default(),
|
||||
RefreshCache: newAuthRefreshCache(),
|
||||
})
|
||||
s.addAuthProviders(authenticator)
|
||||
return authenticator
|
||||
}
|
||||
|
||||
// authRefreshCache used by authenticator to minimize repeatable token refreshes
|
||||
type authRefreshCache struct {
|
||||
*acache.Cache
|
||||
}
|
||||
|
||||
func newAuthRefreshCache() *authRefreshCache {
|
||||
return &authRefreshCache{Cache: acache.New(5*time.Minute, 10*time.Minute)}
|
||||
}
|
||||
|
||||
func (c *authRefreshCache) Get(key interface{}) (interface{}, bool) {
|
||||
return c.Cache.Get(key.(string))
|
||||
}
|
||||
|
||||
func (c *authRefreshCache) Set(key, value interface{}) {
|
||||
c.Cache.Set(key.(string), value, acache.DefaultExpiration)
|
||||
}
|
||||
|
||||
+10
-10
@@ -6,11 +6,11 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-pkgz/lgr"
|
||||
"github.com/go-pkgz/rest"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/go-pkgz/auth/avatar"
|
||||
"github.com/go-pkgz/auth/logger"
|
||||
"github.com/go-pkgz/auth/middleware"
|
||||
"github.com/go-pkgz/auth/provider"
|
||||
"github.com/go-pkgz/auth/token"
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
|
||||
// Service provides higher level wrapper allowing to construct everything and get back token middleware
|
||||
type Service struct {
|
||||
logger lgr.L
|
||||
logger logger.L
|
||||
opts Opts
|
||||
jwtService *token.Service
|
||||
providers []provider.Service
|
||||
@@ -53,10 +53,10 @@ type Opts struct {
|
||||
AvatarResizeLimit int // resize avatar's limit in pixels
|
||||
AvatarRoutePath string // avatar routing prefix, i.e. "/api/v1/avatar", default `/avatar`
|
||||
|
||||
AdminPasswd string // if presented, allows basic auth with user admin and given password
|
||||
AudienceReader token.Audience // list of allowed aud values, default (empty) allows any
|
||||
RefreshFactor int // estimated number of request client sends in parallel during token refresh.
|
||||
Logger lgr.L // logger interface, default is no logging at all
|
||||
AdminPasswd string // if presented, allows basic auth with user admin and given password
|
||||
AudienceReader token.Audience // list of allowed aud values, default (empty) allows any
|
||||
Logger logger.L // logger interface, default is no logging at all
|
||||
RefreshCache middleware.RefreshCache // optional cache to keep refreshed tokens
|
||||
}
|
||||
|
||||
// NewService initializes everything
|
||||
@@ -66,9 +66,9 @@ func NewService(opts Opts) (res *Service) {
|
||||
opts: opts,
|
||||
logger: opts.Logger,
|
||||
authMiddleware: middleware.Authenticator{
|
||||
Validator: opts.Validator,
|
||||
AdminPasswd: opts.AdminPasswd,
|
||||
RefreshFactor: opts.RefreshFactor,
|
||||
Validator: opts.Validator,
|
||||
AdminPasswd: opts.AdminPasswd,
|
||||
RefreshCache: opts.RefreshCache,
|
||||
},
|
||||
issuer: opts.Issuer,
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func NewService(opts Opts) (res *Service) {
|
||||
}
|
||||
|
||||
if opts.Logger == nil {
|
||||
res.logger = lgr.NoOp
|
||||
res.logger = logger.NoOp
|
||||
}
|
||||
|
||||
jwtService := token.NewService(token.Opts{
|
||||
|
||||
+2
-2
@@ -12,18 +12,18 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-pkgz/lgr"
|
||||
"github.com/go-pkgz/rest"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/image/draw"
|
||||
|
||||
"github.com/go-pkgz/auth/logger"
|
||||
"github.com/go-pkgz/auth/token"
|
||||
)
|
||||
|
||||
// Proxy provides http handler for avatars from avatar.Store
|
||||
// On user login token will call Put and it will retrieve and save picture locally.
|
||||
type Proxy struct {
|
||||
lgr.L
|
||||
logger.L
|
||||
Store Store
|
||||
RoutePath string
|
||||
URL string
|
||||
|
||||
-2
@@ -6,10 +6,8 @@ require (
|
||||
github.com/coreos/bbolt v1.3.0
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
|
||||
github.com/go-pkgz/lgr v0.2.2
|
||||
github.com/go-pkgz/mongo v1.0.0
|
||||
github.com/go-pkgz/rest v1.2.0
|
||||
github.com/hashicorp/golang-lru v0.5.0
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/nullrocks/identicon v0.0.0-20180626043057-7875f45b0022
|
||||
github.com/pkg/errors v0.8.1
|
||||
|
||||
-2
@@ -11,8 +11,6 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is=
|
||||
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
|
||||
github.com/go-pkgz/lgr v0.2.2 h1:HSOqMVoetAfvA40Gpy/X/HGyV0UUafIMPgp+SdZends=
|
||||
github.com/go-pkgz/lgr v0.2.2/go.mod h1:hBM1NM/SoYdlrykgdgJWGrZ/TM/XaZIjRbJfx7NkMm8=
|
||||
github.com/go-pkgz/mongo v1.0.0 h1:9jijAK7prCRMetiyTu3c1rv/2lMypzuf2DWcVpTlwzw=
|
||||
github.com/go-pkgz/mongo v1.0.0/go.mod h1:R9si/F2aJsjz4MUxhzuppIHY8yLV3YCeuCpgcI50cu4=
|
||||
github.com/go-pkgz/rest v1.2.0 h1:75GVv25NmkV2l4dBr/io/ZApJ6zWQu5aZ4wFJA6QQCw=
|
||||
|
||||
+15
-27
@@ -3,32 +3,28 @@ package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/go-pkgz/lgr"
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/go-pkgz/auth/logger"
|
||||
"github.com/go-pkgz/auth/provider"
|
||||
"github.com/go-pkgz/auth/token"
|
||||
)
|
||||
|
||||
// Authenticator is top level auth object providing middlewares
|
||||
type Authenticator struct {
|
||||
lgr.L
|
||||
JWTService TokenService
|
||||
Providers []provider.Service
|
||||
Validator token.Validator
|
||||
AdminPasswd string
|
||||
RefreshFactor int
|
||||
|
||||
refresh struct {
|
||||
cache *lru.Cache
|
||||
once sync.Once
|
||||
}
|
||||
logger.L
|
||||
JWTService TokenService
|
||||
Providers []provider.Service
|
||||
Validator token.Validator
|
||||
AdminPasswd string
|
||||
RefreshCache RefreshCache
|
||||
}
|
||||
|
||||
const refreshCacheSize = 1000
|
||||
type RefreshCache interface {
|
||||
Get(key interface{}) (value interface{}, ok bool)
|
||||
Set(key, value interface{})
|
||||
}
|
||||
|
||||
// TokenService defines interface accessing tokens
|
||||
type TokenService interface {
|
||||
@@ -124,16 +120,8 @@ func (a *Authenticator) auth(reqAuth bool) func(http.Handler) http.Handler {
|
||||
// refreshExpiredToken makes a new token with passed claims
|
||||
func (a *Authenticator) refreshExpiredToken(w http.ResponseWriter, claims token.Claims, tkn string) (token.Claims, error) {
|
||||
|
||||
a.refresh.once.Do(func() {
|
||||
var e error
|
||||
if a.refresh.cache, e = lru.New(refreshCacheSize); e != nil {
|
||||
a.Logf("[WARN] can't make refresh cache, %v", e)
|
||||
a.refresh.cache = nil
|
||||
}
|
||||
})
|
||||
|
||||
if a.refresh.cache != nil {
|
||||
if c, ok := a.refresh.cache.Get(tkn); ok {
|
||||
if a.RefreshCache != nil {
|
||||
if c, ok := a.RefreshCache.Get(tkn); ok {
|
||||
// already in cache
|
||||
return c.(token.Claims), nil
|
||||
}
|
||||
@@ -145,8 +133,8 @@ func (a *Authenticator) refreshExpiredToken(w http.ResponseWriter, claims token.
|
||||
return token.Claims{}, err
|
||||
}
|
||||
|
||||
if a.refresh.cache != nil {
|
||||
a.refresh.cache.Add(tkn, c)
|
||||
if a.RefreshCache != nil {
|
||||
a.RefreshCache.Set(tkn, c)
|
||||
}
|
||||
|
||||
a.Logf("[DEBUG] token refreshed for %+v", claims.User)
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/go-pkgz/lgr"
|
||||
"github.com/go-pkgz/auth/logger"
|
||||
"github.com/nullrocks/identicon"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/oauth2"
|
||||
@@ -26,7 +26,7 @@ const devAuthPort = 8084
|
||||
// can run in interactive and non-interactive mode. In interactive mode login attempts will show login form to select
|
||||
// desired user name, this is the mode used for development. Non-interactive mode for tests only.
|
||||
type DevAuthServer struct {
|
||||
lgr.L
|
||||
logger.L
|
||||
Provider Oauth2Handler
|
||||
Automatic bool
|
||||
username string // unsafe, but fine for dev
|
||||
|
||||
+2
-2
@@ -5,16 +5,16 @@ import (
|
||||
"net/http"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"github.com/go-pkgz/lgr"
|
||||
"github.com/go-pkgz/rest"
|
||||
|
||||
"github.com/go-pkgz/auth/logger"
|
||||
"github.com/go-pkgz/auth/token"
|
||||
)
|
||||
|
||||
// DirectHandler implements non-oauth2 provider authorizing user in traditional way with storage
|
||||
// with users and hashes
|
||||
type DirectHandler struct {
|
||||
lgr.L
|
||||
logger.L
|
||||
CredChecker CredChecker
|
||||
ProviderName string
|
||||
TokenService TokenService
|
||||
|
||||
+3
-3
@@ -9,10 +9,10 @@ import (
|
||||
"time"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"github.com/go-pkgz/lgr"
|
||||
"github.com/go-pkgz/rest"
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
"github.com/go-pkgz/auth/logger"
|
||||
"github.com/go-pkgz/auth/token"
|
||||
)
|
||||
|
||||
@@ -32,7 +32,7 @@ type Oauth2Handler struct {
|
||||
|
||||
// Params to make initialized and ready to use provider
|
||||
type Params struct {
|
||||
lgr.L
|
||||
logger.L
|
||||
URL string
|
||||
JwtService TokenService
|
||||
Cid string
|
||||
@@ -54,7 +54,7 @@ func (u userData) value(key string) string {
|
||||
// initOauth2Handler makes oauth2 handler for given provider
|
||||
func initOauth2Handler(p Params, service Oauth2Handler) Oauth2Handler {
|
||||
if p.L == nil {
|
||||
p.L = lgr.NoOp
|
||||
p.L = logger.NoOp
|
||||
}
|
||||
p.Logf("[INFO] init oauth2 service %s", service.name)
|
||||
service.Params = p
|
||||
|
||||
Reference in New Issue
Block a user