restapi.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package httpservers
  2. import (
  3. "net/http"
  4. "strings"
  5. log "github.com/sirupsen/logrus"
  6. // apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  7. config "github.com/OliveTin/OliveTin/internal/config"
  8. )
  9. func parseHttpHeaderForAuth(cfg *config.Config, req *http.Request) (string, string) {
  10. username, ok := req.Header[cfg.AuthHttpHeaderUsername]
  11. if !ok {
  12. log.Warnf("Config has AuthHttpHeaderUsername set to %v, but it was not found", cfg.AuthHttpHeaderUsername)
  13. return "", ""
  14. }
  15. if cfg.AuthHttpHeaderUserGroup != "" {
  16. usergroup, ok := req.Header[cfg.AuthHttpHeaderUserGroup]
  17. if ok {
  18. log.Debugf("HTTP Header Auth found a username and usergroup")
  19. return username[0], usergroup[0]
  20. } else {
  21. log.Warnf("Config has AuthHttpHeaderUserGroup set to %v, but it was not found", cfg.AuthHttpHeaderUserGroup)
  22. }
  23. }
  24. log.Debugf("HTTP Header Auth found a username, but usergroup is not being used")
  25. return username[0], ""
  26. }
  27. //gocyclo:ignore
  28. func parseJwtHeader(cfg *config.Config, req *http.Request) (string, string) {
  29. // JWTs in the Authorization header are usually prefixed with "Bearer " which is not part of the JWT token.
  30. return parseJwt(cfg, strings.TrimPrefix(req.Header.Get(cfg.AuthJwtHeader), "Bearer "))
  31. }