github.com/cs3org/reva/v2@v2.27.7/internal/http/services/wellknown/wellknown.go (about)

     1  // Copyright 2018-2024 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 wellknown
    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/utils/cfg"
    27  	"github.com/go-chi/chi/v5"
    28  	"github.com/rs/zerolog"
    29  )
    30  
    31  func init() {
    32  	global.Register("wellknown", New)
    33  }
    34  
    35  type svc struct {
    36  	router chi.Router
    37  	Conf   *config
    38  }
    39  
    40  type config struct {
    41  	OCMProvider OcmProviderConfig `mapstructure:"ocmprovider"`
    42  }
    43  
    44  // New returns a new wellknown object.
    45  func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
    46  	var c config
    47  	if err := cfg.Decode(m, &c); err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	r := chi.NewRouter()
    52  	s := &svc{
    53  		router: r,
    54  		Conf:   &c,
    55  	}
    56  	if err := s.routerInit(); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return s, nil
    61  }
    62  
    63  func (s *svc) routerInit() error {
    64  	wkocmHandler := new(wkocmHandler)
    65  	wkocmHandler.init(&s.Conf.OCMProvider)
    66  	s.router.Get("/.well-known/ocm", wkocmHandler.Ocm)
    67  	s.router.Get("/ocm-provider", wkocmHandler.Ocm)
    68  	return nil
    69  }
    70  
    71  func (s *svc) Close() error {
    72  	return nil
    73  }
    74  
    75  func (s *svc) Prefix() string {
    76  	return ""
    77  }
    78  
    79  func (s *svc) Unprotected() []string {
    80  	return []string{"/", "/.well-known/ocm", "/ocm-provider"}
    81  }
    82  
    83  func (s *svc) Handler() http.Handler {
    84  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    85  		log := appctx.GetLogger(r.Context())
    86  		log.Debug().Str("path", r.URL.Path).Msg(".well-known routing")
    87  
    88  		// unset raw path, otherwise chi uses it to route and then fails to match percent encoded path segments
    89  		r.URL.RawPath = ""
    90  		s.router.ServeHTTP(w, r)
    91  	})
    92  }