本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
856B

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routers
  5. import (
  6. "crypto/subtle"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/prometheus/client_golang/prometheus/promhttp"
  10. )
  11. // Metrics validate auth token and render prometheus metrics
  12. func Metrics(ctx *context.Context) {
  13. if setting.Metrics.Token == "" {
  14. promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
  15. return
  16. }
  17. header := ctx.Req.Header.Get("Authorization")
  18. if header == "" {
  19. ctx.Error(401)
  20. return
  21. }
  22. got := []byte(header)
  23. want := []byte("Bearer " + setting.Metrics.Token)
  24. if subtle.ConstantTimeCompare(got, want) != 1 {
  25. ctx.Error(401)
  26. return
  27. }
  28. promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
  29. }
上海开阖软件有限公司 沪ICP备12045867号-1