MSP-325 add ids return from authentication

This commit is contained in:
DeniskaRediska committed 2021-10-22 11:00:49 +03:00
1 parent 7b5c6165f2
commit ed7a49ef04
3 files changed
+41 -20

No files matched your search

+11 -7
View File
@@ -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
View File
@@ -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) {