restapi.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package httpservers
  2. import (
  3. "context"
  4. "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  5. log "github.com/sirupsen/logrus"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/metadata"
  8. "google.golang.org/protobuf/encoding/protojson"
  9. "google.golang.org/protobuf/reflect/protoreflect"
  10. "net/http"
  11. gw "github.com/OliveTin/OliveTin/gen/grpc"
  12. config "github.com/OliveTin/OliveTin/internal/config"
  13. cors "github.com/OliveTin/OliveTin/internal/cors"
  14. )
  15. var (
  16. cfg *config.Config
  17. )
  18. func parseHttpHeaderForAuth(req *http.Request) (string, string) {
  19. username, ok := req.Header[cfg.AuthHttpHeaderUsername]
  20. if !ok {
  21. log.Warnf("Config has AuthHttpHeaderUsername set to %v, but it was not found", cfg.AuthHttpHeaderUsername)
  22. return "", ""
  23. }
  24. if cfg.AuthHttpHeaderUserGroup != "" {
  25. usergroup, ok := req.Header[cfg.AuthHttpHeaderUserGroup]
  26. if ok {
  27. log.Debugf("HTTP Header Auth found a username and usergroup")
  28. return username[0], usergroup[0]
  29. } else {
  30. log.Warnf("Config has AuthHttpHeaderUserGroup set to %v, but it was not found", cfg.AuthHttpHeaderUserGroup)
  31. }
  32. }
  33. log.Debugf("HTTP Header Auth found a username, but usergroup is not being used")
  34. return username[0], ""
  35. }
  36. //gocyclo:ignore
  37. func parseRequestMetadata(ctx context.Context, req *http.Request) metadata.MD {
  38. username := ""
  39. usergroup := ""
  40. if cfg.AuthJwtCookieName != "" {
  41. username, usergroup = parseJwtCookie(req)
  42. }
  43. if cfg.AuthHttpHeaderUsername != "" {
  44. username, usergroup = parseHttpHeaderForAuth(req)
  45. }
  46. if len(cfg.AuthOAuth2Providers) > 0 {
  47. username, usergroup = parseOAuth2Cookie(req)
  48. }
  49. if cfg.AuthLocalUsers.Enabled {
  50. username, usergroup = parseLocalUserCookie(req)
  51. }
  52. md := metadata.New(map[string]string{
  53. "username": username,
  54. "usergroup": usergroup,
  55. })
  56. log.Tracef("api request metadata: %+v", md)
  57. return md
  58. }
  59. func forwardResponseHandler(ctx context.Context, w http.ResponseWriter, msg protoreflect.ProtoMessage) error {
  60. forwardResponseHandlerLoginLocalUser(ctx, w, msg)
  61. return nil
  62. }
  63. func SetGlobalRestConfig(config *config.Config) {
  64. cfg = config
  65. }
  66. func startRestAPIServer(globalConfig *config.Config) error {
  67. cfg = globalConfig
  68. log.WithFields(log.Fields{
  69. "address": cfg.ListenAddressRestActions,
  70. }).Info("Starting REST API")
  71. mux := newMux()
  72. return http.ListenAndServe(cfg.ListenAddressRestActions, cors.AllowCors(mux))
  73. }
  74. func newMux() *runtime.ServeMux {
  75. // The MarshalOptions set some important compatibility settings for the webui. See below.
  76. mux := runtime.NewServeMux(
  77. runtime.WithMetadata(parseRequestMetadata),
  78. runtime.WithForwardResponseOption(forwardResponseHandler),
  79. runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
  80. Marshaler: &runtime.JSONPb{
  81. MarshalOptions: protojson.MarshalOptions{
  82. UseProtoNames: false, // eg: canExec for js instead of can_exec from protobuf
  83. EmitUnpopulated: true, // Emit empty fields so that javascript does not get "undefined" when accessing fields with empty values.
  84. },
  85. },
  86. }),
  87. )
  88. ctx := context.Background()
  89. opts := []grpc.DialOption{grpc.WithInsecure()}
  90. err := gw.RegisterOliveTinApiServiceHandlerFromEndpoint(ctx, mux, cfg.ListenAddressGrpcActions, opts)
  91. if err != nil {
  92. log.Panicf("Could not register REST API Handler %v", err)
  93. }
  94. return mux
  95. }