func OnHTTPRequestHeaders()

in plugins/wasm-go/extensions/jwt-auth/handler/handler.go [39:150]


func OnHTTPRequestHeaders(ctx wrapper.HttpContext, config cfg.JWTAuthConfig, log wrapper.Log) types.Action {
	var (
		noAllow            = len(config.Allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
		globalAuthNoSet    = config.GlobalAuthCheck() == cfg.GlobalAuthNoSet
		globalAuthSetTrue  = config.GlobalAuthCheck() == cfg.GlobalAuthTrue
		globalAuthSetFalse = config.GlobalAuthCheck() == cfg.GlobalAuthFalse
	)

	// 不需要认证而直接放行的情况:
	// - global_auth == false 且 当前 domain/route 未配置该插件
	// - global_auth 未设置 且 有至少一个 domain/route 配置该插件 且 当前 domain/route 未配置该插件
	if globalAuthSetFalse || (cfg.RuleSet && globalAuthNoSet) {
		if noAllow {
			log.Info("authorization is not required")
			return types.ActionContinue
		}
	}

	header := &proxywasmProvider{}
	actionMap := map[string]func() types.Action{}
	unAuthzConsumer := ""

	// 匹配consumer
	for i := range config.Consumers {
		err := consumerVerify(config.Consumers[i], time.Now(), header, log)
		if err != nil {
			log.Warn(err.Error())
			if v, ok := err.(*ErrDenied); ok {
				actionMap[config.Consumers[i].Name] = v.denied
			}
			continue
		}

		// 全局生效:
		// - global_auth == true 且 当前 domain/route 未配置该插件
		// - global_auth 未设置 且 没有任何一个 domain/route 配置该插件
		if (globalAuthSetTrue && noAllow) || (globalAuthNoSet && !cfg.RuleSet) {
			log.Infof("consumer %q authenticated", config.Consumers[i].Name)
			return authenticated(config.Consumers[i].Name)
		}

		// 全局生效,但当前 domain/route 配置了 allow 列表
		if globalAuthSetTrue && !noAllow {
			if !contains(config.Consumers[i].Name, config.Allow) {
				log.Warnf("jwt verify failed, consumer %q not allow",
					config.Consumers[i].Name)
				actionMap[config.Consumers[i].Name] = deniedUnauthorizedConsumer
				unAuthzConsumer = config.Consumers[i].Name
				continue
			}
			log.Infof("consumer %q authenticated", config.Consumers[i].Name)
			return authenticated(config.Consumers[i].Name)
		}

		// 非全局生效
		if globalAuthSetFalse || (globalAuthNoSet && cfg.RuleSet) {
			if !noAllow { // 配置了 allow 列表
				if !contains(config.Consumers[i].Name, config.Allow) {
					log.Warnf("jwt verify failed, consumer %q not allow",
						config.Consumers[i].Name)
					actionMap[config.Consumers[i].Name] = deniedUnauthorizedConsumer
					unAuthzConsumer = config.Consumers[i].Name
					continue
				}
				log.Infof("consumer %q authenticated", config.Consumers[i].Name)
				return authenticated(config.Consumers[i].Name)
			}
		}

		// switch config.GlobalAuthCheck() {

		// case cfg.GlobalAuthNoSet:
		// 	if !cfg.RuleSet {
		// 		log.Infof("consumer %q authenticated", config.Consumers[i].Name)
		// 		return authenticated(config.Consumers[i].Name)
		// 	}
		// case cfg.GlobalAuthTrue:
		// 	if len(config.Allow) == 0 {
		// 		log.Infof("consumer %q authenticated", config.Consumers[i].Name)
		// 		return authenticated(config.Consumers[i].Name)
		// 	}
		// 	fallthrough // 若 allow 列表不为空,则 fallthrough 到需要检查 allow 列表的逻辑中

		// // 全局生效设置为 false
		// case cfg.GlobalAuthFalse:
		// 	if !contains(config.Consumers[i].Name, config.Allow) {
		// 		log.Warnf("jwt verify failed, consumer %q not allow",
		// 			config.Consumers[i].Name)
		// 		actionMap[config.Consumers[i].Name] = deniedUnauthorizedConsumer
		// 		unAuthzConsumer = config.Consumers[i].Name
		// 		continue
		// 	}
		// 	log.Infof("consumer %q authenticated", config.Consumers[i].Name)
		// 	return authenticated(config.Consumers[i].Name)
		// }
	}

	if len(config.Allow) == 1 {
		if unAuthzConsumer != "" {
			log.Warnf("consumer %q denied", unAuthzConsumer)
			return deniedUnauthorizedConsumer()
		}
		if v, ok := actionMap[config.Allow[0]]; ok {
			log.Warnf("consumer %q denied", config.Allow[0])
			return v()
		}
	}

	// 拒绝兜底
	log.Warnf("all consumers verify failed")
	return deniedNotAllow()
}