github.com/cs3org/reva/v2@v2.27.7/internal/http/services/ocmd/ocm.go (about) 1 // Copyright 2018-2023 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package ocmd 20 21 import ( 22 "net/http" 23 24 "github.com/cs3org/reva/v2/pkg/appctx" 25 "github.com/cs3org/reva/v2/pkg/rhttp/global" 26 "github.com/cs3org/reva/v2/pkg/sharedconf" 27 "github.com/cs3org/reva/v2/pkg/utils/cfg" 28 "github.com/go-chi/chi/v5" 29 "github.com/rs/zerolog" 30 ) 31 32 func init() { 33 global.Register("ocmd", New) 34 } 35 36 type config struct { 37 Prefix string `mapstructure:"prefix"` 38 GatewaySvc string `mapstructure:"gatewaysvc" validate:"required"` 39 ExposeRecipientDisplayName bool `mapstructure:"expose_recipient_display_name"` 40 } 41 42 func (c *config) ApplyDefaults() { 43 c.GatewaySvc = sharedconf.GetGatewaySVC(c.GatewaySvc) 44 if c.Prefix == "" { 45 c.Prefix = "ocm" 46 } 47 } 48 49 type svc struct { 50 Conf *config 51 router chi.Router 52 } 53 54 // New returns a new ocmd object, that implements 55 // the OCM APIs specified in https://cs3org.github.io/OCM-API/docs.html 56 func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) { 57 var c config 58 if err := cfg.Decode(m, &c); err != nil { 59 return nil, err 60 } 61 62 r := chi.NewRouter() 63 s := &svc{ 64 Conf: &c, 65 router: r, 66 } 67 68 if err := s.routerInit(); err != nil { 69 return nil, err 70 } 71 72 return s, nil 73 } 74 75 func (s *svc) routerInit() error { 76 sharesHandler := new(sharesHandler) 77 invitesHandler := new(invitesHandler) 78 notifHandler := new(notifHandler) 79 80 if err := sharesHandler.init(s.Conf); err != nil { 81 return err 82 } 83 if err := invitesHandler.init(s.Conf); err != nil { 84 return err 85 } 86 if err := notifHandler.init(s.Conf); err != nil { 87 return err 88 } 89 90 s.router.Post("/shares", sharesHandler.CreateShare) 91 s.router.Post("/invite-accepted", invitesHandler.AcceptInvite) 92 s.router.Post("/notifications", notifHandler.Notifications) 93 return nil 94 } 95 96 // Close performs cleanup. 97 func (s *svc) Close() error { 98 return nil 99 } 100 101 func (s *svc) Prefix() string { 102 return s.Conf.Prefix 103 } 104 105 func (s *svc) Unprotected() []string { 106 return []string{"/invite-accepted", "/shares", "/notifications"} 107 } 108 109 func (s *svc) Handler() http.Handler { 110 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 111 log := appctx.GetLogger(r.Context()) 112 log.Debug().Str("path", r.URL.Path).Msg("ocm routing") 113 114 // unset raw path, otherwise chi uses it to route and then fails to match percent encoded path segments 115 r.URL.RawPath = "" 116 s.router.ServeHTTP(w, r) 117 }) 118 }