作者 yangfu

basic auth

... ... @@ -4,6 +4,7 @@ go 1.15
require (
github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
github.com/abbot/go-http-auth v0.4.0
//github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
github.com/astaxie/beego v1.12.2
github.com/dgrijalva/jwt-go v3.2.0+incompatible
... ...
... ... @@ -21,4 +21,6 @@ func init() {
beego.InsertFilter("/v1/project_module_version/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
beego.InsertFilter("/v1/project_module_files/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
beego.InsertFilter("/v1/rbac/*", beego.BeforeExec, middleware.InspectRoleAccess("/role/*"))
beego.InsertFilter("/log", beego.BeforeStatic, middleware.BasicAuth())
}
... ...
... ... @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/astaxie/beego/context"
//"github.com/opentracing/opentracing-go"
auth "github.com/abbot/go-http-auth"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex"
... ... @@ -154,3 +155,20 @@ func AllowCors() func(ctx *context.Context) {
// }
// defer sp.Finish()
//}
func secret(user, pwd string) string {
if user == "admin" {
// password is "hello"
return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
}
return ""
}
func BasicAuth() func(ctx *context.Context) {
return func(ctx *context.Context) {
a := auth.NewBasicAuthenticator("example.com", secret)
if username := a.CheckAuth(ctx.Request); username == "" {
a.RequireAuth(ctx.ResponseWriter, ctx.Request)
}
}
}
... ...