revendor with latest auth lib

This commit is contained in:
Umputun
2018-12-31 01:05:29 -06:00
parent fdef63c61d
commit 189df5b0a4
3 changed files with 27 additions and 23 deletions
+3 -2
View File
@@ -113,7 +113,7 @@
[[projects]]
branch = "master"
digest = "1:9240838c072f500013fa68b709050692dd6ba0095131046639618a296d4f4400"
digest = "1:495f89256b4fef47e64160d109454685afa4949f400d02833207e2af383d1173"
name = "github.com/go-pkgz/auth"
packages = [
".",
@@ -124,7 +124,7 @@
"token",
]
pruneopts = "UT"
revision = "c322626ae89af60b2ad5a8f96ade761b5ed361bb"
revision = "46d3a954882f41fc0efdf551e2c1b070799e0f54"
[[projects]]
digest = "1:1212e114344a5cdcc834ea69e19d456eef230f9784659080fee67e02ba2cb574"
@@ -403,6 +403,7 @@
"github.com/go-chi/render",
"github.com/go-pkgz/auth",
"github.com/go-pkgz/auth/avatar",
"github.com/go-pkgz/auth/logger",
"github.com/go-pkgz/auth/provider",
"github.com/go-pkgz/auth/token",
"github.com/go-pkgz/mongo",
+15 -20
View File
@@ -2,9 +2,7 @@
package middleware
import (
"encoding/base64"
"net/http"
"strings"
"github.com/pkg/errors"
@@ -16,12 +14,21 @@ import (
// Authenticator is top level auth object providing middlewares
type Authenticator struct {
logger.L
JWTService *token.Service
JWTService TokenService
Providers []provider.Service
Validator token.Validator
AdminPasswd string
}
// TokenService defines interface accessing tokens
type TokenService interface {
Parse(tokenString string) (claims token.Claims, err error)
Set(w http.ResponseWriter, claims token.Claims) error
Get(r *http.Request) (claims token.Claims, token string, err error)
IsExpired(claims token.Claims) bool
Reset(w http.ResponseWriter)
}
var adminUser = token.User{
ID: "admin",
Name: "admin",
@@ -51,7 +58,7 @@ func (a *Authenticator) auth(reqAuth bool) func(http.Handler) http.Handler {
h.ServeHTTP(w, r)
return
}
a.Logf("[DEBUG] auth failed, %s", err)
a.Logf("[DEBUG] auth failed, %v", err)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}
@@ -144,25 +151,13 @@ func (a *Authenticator) basicAdminUser(r *http.Request) bool {
return false
}
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
user, passwd, ok := r.BasicAuth()
if !ok {
return false
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
a.Logf("[WARN] admin user auth failed, can't to decode %s, %s", s[1], err)
return false
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
a.Logf("[WARN] admin user auth failed, can't split basic auth %s", string(b))
return false
}
if pair[0] != "admin" || pair[1] != a.AdminPasswd {
a.Logf("[WARN] admin basic auth failed, user/passwd mismatch %+v", pair)
if user != "admin" || passwd != a.AdminPasswd {
a.Logf("[WARN] admin basic auth failed, user/passwd mismatch, %s:%s", user, passwd)
return false
}
+9 -1
View File
@@ -37,7 +37,7 @@ type Service struct {
type Params struct {
logger.L
URL string
JwtService *token.Service
JwtService TokenService
AvatarSaver AvatarSaver
Cid string
Csecret string
@@ -49,6 +49,14 @@ type AvatarSaver interface {
Put(u token.User) (avatarURL string, err error)
}
// TokenService defines interface accessing tokens
type TokenService interface {
Parse(tokenString string) (claims token.Claims, err error)
Set(w http.ResponseWriter, claims token.Claims) error
Get(r *http.Request) (claims token.Claims, token string, err error)
Reset(w http.ResponseWriter)
}
type userData map[string]interface{}
func (u userData) value(key string) string {