access.go 3.8 KB
Newer Older
Y
Your Name 已提交
1
package pluginflow
E
eoLinker API Management 已提交
2 3

import (
黄孟柱 已提交
4 5 6 7
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	plugin_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/plugin-manager"
	entity "github.com/eolinker/goku-api-gateway/server/entity/node-entity"
E
eoLinker API Management 已提交
8 9 10
	"reflect"
	"time"
)
Y
Your Name 已提交
11

E
eoLinker API Management 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
var (
	authNames = map[string]string{
		"Oauth2": "goku-oauth2_auth",
		"Apikey": "goku-apikey_auth",
		"Basic":  "goku-basic_auth",
		"Jwt":    "goku-jwt_auth",
	}
	authPluginNames = map[string]string{
		"goku-oauth2_auth": "Oauth2",
		"goku-apikey_auth": "Apikey",
		"goku-basic_auth":  "Basic",
		"goku-jwt_auth":    "Jwt",
	}
)

func getPluginNameByType(authType string) (string, bool) {
	name, has := authNames[authType]
	return name, has
}

Y
Your Name 已提交
32
//AccessFunc 执行插件的Access函数
E
eoLinker API Management 已提交
33
func AccessFunc(ctx *common.Context, handleFunc []*entity.PluginHandlerExce) (bool, int) {
Y
Your Name 已提交
34
	requestID := ctx.RequestId()
E
eoLinker API Management 已提交
35 36 37
	authType := ctx.Request().GetHeader("Authorization-Type")
	authName, _ := getPluginNameByType(authType)
	defer func(ctx *common.Context) {
Y
Your Name 已提交
38
		log.Debug(requestID, " access plugin default: begin")
E
eoLinker API Management 已提交
39
		for _, handler := range plugin_manager.GetDefaultPlugins() {
Y
Your Name 已提交
40
			if handler.PluginObj.Access == nil || reflect.ValueOf(handler.PluginObj.Access).IsNil() {
E
eoLinker API Management 已提交
41 42 43
				continue
			}
			ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
44
			log.Info(requestID, " access plugin:", handler.Name)
Y
Your Name 已提交
45 46
			now := time.Now()
			_, err := handler.PluginObj.Access.Access(ctx)
Y
Your Name 已提交
47
			log.Debug(requestID, " access plugin:", handler.Name, " Duration", time.Since(now))
Y
Your Name 已提交
48
			if err != nil {
Y
Your Name 已提交
49
				log.Warn(requestID, " access plugin:", handler.Name, " error:", err.Error())
E
eoLinker API Management 已提交
50 51
			}
		}
Y
Your Name 已提交
52
		log.Debug(requestID, " access plugin default: end")
E
eoLinker API Management 已提交
53 54 55 56
	}(ctx)
	isAuthSucess := false
	isNeedAuth := false

Y
Your Name 已提交
57
	log.Debug(requestID, " access plugin auth check: begin")
E
eoLinker API Management 已提交
58 59 60 61 62 63 64 65 66 67
	for _, handler := range handleFunc {
		if _, has := authPluginNames[handler.Name]; has {
			isNeedAuth = true
			if handler.Name != authName {
				continue
			}
			if handler.PluginObj.Access == nil || reflect.ValueOf(handler.PluginObj.Access).IsNil() {
				continue
			}
			ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
68
			log.Debug(requestID, " access plugin:", handler.Name, " begin")
Y
Your Name 已提交
69
			now := time.Now()
E
eoLinker API Management 已提交
70
			flag, err := handler.PluginObj.Access.Access(ctx)
Y
Your Name 已提交
71
			log.Debug(requestID, " access plugin:", handler.Name, " Duration", time.Since(now))
E
eoLinker API Management 已提交
72 73 74
			if flag == false {
				// 校验失败
				if err != nil {
Y
Your Name 已提交
75
					log.Warn(requestID, " access auth:[", handler.Name, "] error:", err.Error())
E
eoLinker API Management 已提交
76
				}
Y
Your Name 已提交
77
				log.Info(requestID, " auth [", authName, "] refuse")
E
eoLinker API Management 已提交
78 79 80

				return false, 0
			}
Y
Your Name 已提交
81
			log.Debug(requestID, " auth [", authName, "] pass")
E
eoLinker API Management 已提交
82 83 84
			isAuthSucess = true
		}
	}
Y
Your Name 已提交
85
	log.Debug(requestID, " access plugin auth check: end")
E
eoLinker API Management 已提交
86 87
	// 需要校验但是没有执行校验
	if isNeedAuth && !isAuthSucess {
Y
Your Name 已提交
88
		log.Warn(requestID, " Illegal authorization type:", authType)
E
eoLinker API Management 已提交
89 90 91 92 93
		ctx.SetStatus(403, "403")
		ctx.SetBody([]byte("[ERROR]Illegal authorization type!"))
		return false, 0
	}
	lastIndex := 0
Y
Your Name 已提交
94
	log.Debug(requestID, " access plugin : begin")
E
eoLinker API Management 已提交
95 96 97 98 99 100 101 102 103 104 105 106
	// 执行校验以外的插件
	for index, handler := range handleFunc {
		lastIndex = index
		if _, has := authPluginNames[handler.Name]; has {
			continue
		}

		if handler.PluginObj.Access == nil || reflect.ValueOf(handler.PluginObj.Access).IsNil() {
			continue
		}

		ctx.SetPlugin(handler.Name)
Y
Your Name 已提交
107
		log.Debug(requestID, " access plugin:", handler.Name)
Y
Your Name 已提交
108
		now := time.Now()
E
eoLinker API Management 已提交
109
		flag, err := handler.PluginObj.Access.Access(ctx)
Y
Your Name 已提交
110
		log.Debug(requestID, " access plugin:", handler.Name, " Duration:", time.Since(now))
E
eoLinker API Management 已提交
111
		if err != nil {
Y
Your Name 已提交
112
			log.Warn(requestID, " access plugin:", handler.Name, " error:", err.Error())
E
eoLinker API Management 已提交
113 114
		}
		if flag == false && handler.IsStop {
Y
Your Name 已提交
115
			log.Info(requestID, " access plugin:", handler.Name, " stop")
E
eoLinker API Management 已提交
116 117
			return false, index
		}
Y
Your Name 已提交
118
		log.Debug(requestID, " access plugin:", handler.Name, " continue")
E
eoLinker API Management 已提交
119
	}
Y
Your Name 已提交
120
	log.Debug(requestID, " access plugin : end")
E
eoLinker API Management 已提交
121
	return true, lastIndex
Y
Your Name 已提交
122
}