migrate to go mod
This commit is contained in:
1 parent
665641c88b
commit
72e6bbe6d7
45 files changed
+784
-1029
No files matched your search
+134
-75
@@ -1,8 +1,8 @@
|
||||
package authenticate
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
"github.com/integration-system/isp-lib/v2/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"github.com/pkg/errors"
|
||||
@@ -18,7 +18,9 @@ import (
|
||||
|
||||
const (
|
||||
// TODO move to isp-lib
|
||||
AdminTokenHeader = "x-auth-admin"
|
||||
AdminTokenHeader = "x-auth-admin" //nolint
|
||||
|
||||
awaitLengthVerifiableHeaders = 4
|
||||
)
|
||||
|
||||
var notExpectedHeaders = []string{
|
||||
@@ -32,9 +34,18 @@ var notExpectedHeaders = []string{
|
||||
|
||||
var auth = authenticate{}
|
||||
|
||||
type authenticate struct {
|
||||
verify veritification.Verify
|
||||
}
|
||||
type (
|
||||
authenticate struct {
|
||||
verify veritification.Verify
|
||||
}
|
||||
|
||||
verifiable struct {
|
||||
appId int32
|
||||
ctx *fasthttp.RequestCtx
|
||||
path string
|
||||
headers map[string]string
|
||||
}
|
||||
)
|
||||
|
||||
func ReceiveConfiguration(conf conf.Cache) {
|
||||
if conf.EnableCache {
|
||||
@@ -53,94 +64,142 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) {
|
||||
ctx.Request.Header.Del(notExpectedHeader)
|
||||
}
|
||||
|
||||
appToken := getParam(utils.ApplicationTokenHeader, &ctx.Request)
|
||||
|
||||
var (
|
||||
err error
|
||||
appId int32 = -1
|
||||
)
|
||||
|
||||
if len(appToken) == 0 {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "application token required")
|
||||
} else if config.GetRemote().(*conf.RemoteConfig).TokensSetting.ApplicationVerify {
|
||||
if appId, err = verifyToken.Application(string(appToken)); err != nil || appId == 0 {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "received invalid application token")
|
||||
}
|
||||
}
|
||||
|
||||
verifiableKeys, err := auth.verify.ApplicationToken(string(appToken))
|
||||
verifiable := newVerifiable(ctx, path)
|
||||
err := verifiable.verifyAppToken()
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return 0, createError("internal Server error", codes.Internal)
|
||||
}
|
||||
if len(verifiableKeys) != 4 {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "received unexpected identities")
|
||||
return 0, err
|
||||
}
|
||||
|
||||
applicationId, err := strconv.Atoi(verifiableKeys[utils.ApplicationIdHeader])
|
||||
err = verifiable.verifyUserToken()
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, errors.WithMessagef(err, "parse appId from redis"))
|
||||
return 0, createError("internal Server error", codes.Internal)
|
||||
}
|
||||
if appId != -1 && int32(applicationId) != appId {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "received unexpected application identity")
|
||||
return 0, err
|
||||
}
|
||||
|
||||
userToken := string(getParam(utils.UserTokenHeader, &ctx.Request))
|
||||
if userToken != "" {
|
||||
userId, err := verifyToken.User(userToken)
|
||||
if err != nil {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "received invalid user token")
|
||||
}
|
||||
verifiableKeys[utils.UserIdHeader] = userId
|
||||
}
|
||||
verifiable.setHeaders()
|
||||
|
||||
verifiableKeys[utils.UserTokenHeader] = userToken
|
||||
verifiableKeys[utils.DeviceTokenHeader] = string(getParam(utils.DeviceTokenHeader, &ctx.Request))
|
||||
verifiableKeys, err = auth.verify.Identity(verifiableKeys, path)
|
||||
err = verifiable.checkInnerMethods()
|
||||
if err != nil {
|
||||
switch err {
|
||||
case veritification.ErrorInvalidUserId:
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, err.Error())
|
||||
case veritification.ErrorPermittedToCallUser, veritification.ErrorPermittedToCallApplication:
|
||||
return 0, createError("forbidden", codes.PermissionDenied, err.Error())
|
||||
default:
|
||||
return 0, createError("internal server error", codes.Internal)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for key, value := range verifiableKeys {
|
||||
if value != "" {
|
||||
ctx.Request.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := routing.InnerMethods[path]; ok {
|
||||
adminToken := string(getParam(AdminTokenHeader, &ctx.Request))
|
||||
if adminToken == "" {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "admin token required")
|
||||
}
|
||||
if verifyToken.Admin(adminToken) != nil {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "received invalid admin token")
|
||||
}
|
||||
}
|
||||
if verifiableKeys[utils.UserIdHeader] == "" {
|
||||
if _, ok := routing.AuthUserMethods[path]; ok {
|
||||
return 0, createError("unauthorized", codes.Unauthenticated, "user token required")
|
||||
}
|
||||
err = verifiable.checkUserMethods()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
//TODO backward capability for isp-config-service 1.x.x
|
||||
uuid := config.Get().(*conf.Configuration).InstanceUuid
|
||||
ctx.Request.Header.Set(utils.InstanceIdHeader, uuid)
|
||||
|
||||
return appId, nil
|
||||
return verifiable.appId, nil
|
||||
}
|
||||
|
||||
func getParam(key string, req *fasthttp.Request) []byte {
|
||||
val := req.Header.Peek(key)
|
||||
func newVerifiable(ctx *fasthttp.RequestCtx, path string) *verifiable {
|
||||
return &verifiable{
|
||||
ctx: ctx,
|
||||
path: path,
|
||||
appId: -1,
|
||||
headers: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (v *verifiable) verifyAppToken() error {
|
||||
appToken := v.getParam(utils.ApplicationTokenHeader)
|
||||
var err error
|
||||
|
||||
if len(appToken) == 0 {
|
||||
return createError("unauthorized", codes.Unauthenticated, "application token required")
|
||||
} else if config.GetRemote().(*conf.RemoteConfig).TokensSetting.ApplicationVerify {
|
||||
if v.appId, err = verifyToken.Application(string(appToken)); err != nil || v.appId == 0 {
|
||||
return createError("unauthorized", codes.Unauthenticated, "received invalid application token")
|
||||
}
|
||||
}
|
||||
|
||||
verifiableHeaders, err := auth.verify.ApplicationToken(string(appToken))
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return createError("internal Server error", codes.Internal)
|
||||
}
|
||||
if len(verifiableHeaders) != awaitLengthVerifiableHeaders {
|
||||
return createError("unauthorized", codes.Unauthenticated, "received unexpected identities")
|
||||
}
|
||||
|
||||
applicationId, err := strconv.Atoi(verifiableHeaders[utils.ApplicationIdHeader])
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, errors.WithMessagef(err, "parse appId from redis"))
|
||||
return createError("internal Server error", codes.Internal)
|
||||
}
|
||||
if v.appId != -1 && int32(applicationId) != v.appId {
|
||||
return createError("unauthorized", codes.Unauthenticated, "received unexpected application identity")
|
||||
}
|
||||
|
||||
v.headers = verifiableHeaders
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *verifiable) verifyUserToken() error {
|
||||
userToken := string(v.getParam(utils.UserTokenHeader))
|
||||
if userToken != "" {
|
||||
userId, err := verifyToken.User(userToken)
|
||||
if err != nil {
|
||||
return createError("unauthorized", codes.Unauthenticated, "received invalid user token")
|
||||
}
|
||||
v.headers[utils.UserIdHeader] = userId
|
||||
}
|
||||
v.headers[utils.UserTokenHeader] = userToken
|
||||
|
||||
v.headers[utils.DeviceTokenHeader] = string(v.getParam(utils.DeviceTokenHeader))
|
||||
|
||||
var err error
|
||||
v.headers, err = auth.verify.Identity(v.headers, v.path)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case veritification.ErrorInvalidUserId:
|
||||
return createError("unauthorized", codes.Unauthenticated, err.Error())
|
||||
case veritification.ErrorPermittedToCallUser, veritification.ErrorPermittedToCallApplication:
|
||||
return createError("forbidden", codes.PermissionDenied, err.Error())
|
||||
default:
|
||||
return createError("internal server error", codes.Internal)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *verifiable) setHeaders() {
|
||||
for key, value := range v.headers {
|
||||
if value != "" {
|
||||
v.ctx.Request.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *verifiable) checkInnerMethods() error {
|
||||
_, ok := routing.InnerMethods[v.path]
|
||||
if ok {
|
||||
adminToken := string(v.getParam(AdminTokenHeader))
|
||||
if adminToken == "" {
|
||||
return createError("unauthorized", codes.Unauthenticated, "admin token required")
|
||||
}
|
||||
if verifyToken.Admin(adminToken) != nil {
|
||||
return createError("unauthorized", codes.Unauthenticated, "received invalid admin token")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *verifiable) checkUserMethods() error {
|
||||
if v.headers[utils.UserIdHeader] == "" {
|
||||
if _, ok := routing.AuthUserMethods[v.path]; ok {
|
||||
return createError("unauthorized", codes.Unauthenticated, "user token required")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *verifiable) getParam(key string) []byte {
|
||||
val := v.ctx.Request.Header.Peek(key)
|
||||
if len(val) != 0 {
|
||||
return val
|
||||
}
|
||||
return req.URI().QueryArgs().Peek(key)
|
||||
return v.ctx.Request.URI().QueryArgs().Peek(key)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package authenticate
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
"isp-gate-service/conf"
|
||||
|
||||
Reference in new issue
Block a user