MSP-325 add ids return from authentication
This commit is contained in:
1 parent
7b5c6165f2
commit
ed7a49ef04
3 files changed
+41
-20
No files matched your search
@@ -42,6 +42,7 @@ type (
|
||||
|
||||
verifiable struct {
|
||||
appId int32
|
||||
adminId int64
|
||||
ctx *fasthttp.RequestCtx
|
||||
path string
|
||||
headers map[string]string
|
||||
@@ -60,7 +61,7 @@ func ReceiveConfiguration(conf conf.Cache) {
|
||||
}
|
||||
}
|
||||
|
||||
func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) {
|
||||
func Do(ctx *fasthttp.RequestCtx, path string) (int32, int64, error) {
|
||||
for _, notExpectedHeader := range notExpectedHeaders {
|
||||
ctx.Request.Header.Del(notExpectedHeader)
|
||||
}
|
||||
@@ -68,27 +69,27 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) {
|
||||
verifiable := newVerifiable(ctx, path)
|
||||
err := verifiable.verifyAppToken()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
err = verifiable.verifyUserToken()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
verifiable.setHeaders()
|
||||
|
||||
err = verifiable.checkInnerMethods()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
err = verifiable.checkUserMethods()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return verifiable.appId, nil
|
||||
return verifiable.appId, verifiable.adminId, nil
|
||||
}
|
||||
|
||||
func newVerifiable(ctx *fasthttp.RequestCtx, path string) *verifiable {
|
||||
@@ -96,6 +97,7 @@ func newVerifiable(ctx *fasthttp.RequestCtx, path string) *verifiable {
|
||||
ctx: ctx,
|
||||
path: path,
|
||||
appId: -1,
|
||||
adminId: -1,
|
||||
headers: make(map[string]string),
|
||||
}
|
||||
}
|
||||
@@ -179,9 +181,11 @@ func (v *verifiable) checkInnerMethods() error {
|
||||
if adminToken == "" {
|
||||
return createError("unauthorized", codes.Unauthenticated, "admin token required")
|
||||
}
|
||||
if verifyToken.Admin(adminToken) != nil {
|
||||
adminId, err := verifyToken.Admin(adminToken)
|
||||
if err != nil {
|
||||
return createError("unauthorized", codes.Unauthenticated, "received invalid admin token")
|
||||
}
|
||||
v.adminId = adminId
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+15
-3
@@ -23,12 +23,24 @@ type userClaims struct {
|
||||
UserId int64
|
||||
}
|
||||
|
||||
type adminClaims struct {
|
||||
*jwt.StandardClaims
|
||||
Id int64
|
||||
}
|
||||
|
||||
type token struct{}
|
||||
|
||||
func (t token) Admin(token string) error {
|
||||
func (t token) Admin(token string) (int64, error) {
|
||||
secret := config.GetRemote().(*conf.RemoteConfig).TokensSetting.AdminSecret
|
||||
_, err := t.parse(token, secret, jwt.MapClaims{})
|
||||
return err
|
||||
parsed, err := t.parse(token, secret, &adminClaims{StandardClaims: &jwt.StandardClaims{}})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
claims, ok := parsed.Claims.(*adminClaims)
|
||||
if !ok {
|
||||
return 0, errInvalidToken
|
||||
}
|
||||
return claims.Id, nil
|
||||
}
|
||||
|
||||
func (t token) Application(token string) (int32, error) {
|
||||
|
||||
+15
-10
@@ -41,7 +41,7 @@ func New(logger log.Logger) *Handler {
|
||||
func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) {
|
||||
currentTime := time.Now()
|
||||
|
||||
method, resp := h.authenticateAccountingProxy(ctx)
|
||||
_, _, method, resp := h.authenticateAccountingProxy(ctx)
|
||||
|
||||
executionTime := time.Since(currentTime) / execution
|
||||
|
||||
@@ -69,26 +69,31 @@ func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (string, domain.ProxyResponse) {
|
||||
initialPath := string(ctx.Path())
|
||||
func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (int32, int64, string, domain.ProxyResponse) {
|
||||
var (
|
||||
appId int32 = -1
|
||||
adminId int64 = -1
|
||||
initialPath = string(ctx.Path())
|
||||
)
|
||||
|
||||
p, path := proxy.Find(initialPath)
|
||||
if p == nil {
|
||||
msg := fmt.Sprintf("unknown proxy for '%s'", initialPath)
|
||||
utils.WriteError(ctx, msg, codes.NotFound, nil)
|
||||
return initialPath, domain.Create().SetError(errors.New(msg))
|
||||
return appId, adminId, initialPath, domain.Create().SetError(errors.New(msg))
|
||||
}
|
||||
|
||||
if !p.SkipExistCheck() {
|
||||
if _, ok := routing.AllMethods[path]; !ok {
|
||||
msg := "not implemented"
|
||||
utils.WriteError(ctx, msg, codes.Unimplemented, nil)
|
||||
return path, domain.Create().SetError(errors.New(msg))
|
||||
return appId, adminId, path, domain.Create().SetError(errors.New(msg))
|
||||
}
|
||||
}
|
||||
|
||||
if !p.SkipAuth() {
|
||||
applicationId, err := authenticate.Do(ctx, path)
|
||||
var err error
|
||||
appId, adminId, err = authenticate.Do(ctx, path)
|
||||
if err != nil {
|
||||
message := "unknown error"
|
||||
status := codes.Unknown
|
||||
@@ -99,17 +104,17 @@ func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (string,
|
||||
details = e.Details()
|
||||
}
|
||||
utils.WriteError(ctx, message, status, details)
|
||||
return path, domain.Create().SetError(err)
|
||||
return appId, adminId, path, domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
if !accounting.AcceptRequest(applicationId, path) {
|
||||
if !accounting.AcceptRequest(appId, path) {
|
||||
err := errAccounting
|
||||
utils.WriteError(ctx, "too many requests", codes.ResourceExhausted, nil)
|
||||
return path, domain.Create().SetError(err)
|
||||
return appId, adminId, path, domain.Create().SetError(err)
|
||||
}
|
||||
}
|
||||
|
||||
return path, p.ProxyRequest(ctx, path)
|
||||
return appId, adminId, path, p.ProxyRequest(ctx, path)
|
||||
}
|
||||
|
||||
func (Handler) setMetricStatus(statusCode int) string {
|
||||
|
||||
Reference in new issue
Block a user