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

     1  // Copyright 2018-2021 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 siteacc
    20  
    21  import (
    22  	"net/http"
    23  
    24  	"github.com/cs3org/reva/v2/pkg/siteacc"
    25  	"github.com/cs3org/reva/v2/pkg/siteacc/config"
    26  	"github.com/mitchellh/mapstructure"
    27  	"github.com/pkg/errors"
    28  	"github.com/rs/zerolog"
    29  
    30  	"github.com/cs3org/reva/v2/pkg/rhttp/global"
    31  )
    32  
    33  func init() {
    34  	global.Register(serviceName, New)
    35  }
    36  
    37  type svc struct {
    38  	conf *config.Configuration
    39  	log  *zerolog.Logger
    40  
    41  	siteacc *siteacc.SiteAccounts
    42  }
    43  
    44  const (
    45  	serviceName = "siteacc"
    46  )
    47  
    48  // Close is called when this service is being stopped.
    49  func (s *svc) Close() error {
    50  	return nil
    51  }
    52  
    53  // Prefix returns the main endpoint of this service.
    54  func (s *svc) Prefix() string {
    55  	return s.conf.Prefix
    56  }
    57  
    58  // Unprotected returns all endpoints that can be queried without prior authorization.
    59  func (s *svc) Unprotected() []string {
    60  	return s.siteacc.GetPublicEndpoints()
    61  }
    62  
    63  // Handler serves all HTTP requests.
    64  func (s *svc) Handler() http.Handler {
    65  	return s.siteacc.RequestHandler()
    66  }
    67  
    68  func parseConfig(m map[string]interface{}) (*config.Configuration, error) {
    69  	conf := &config.Configuration{}
    70  	if err := mapstructure.Decode(m, &conf); err != nil {
    71  		return nil, errors.Wrap(err, "error decoding configuration")
    72  	}
    73  	applyDefaultConfig(conf)
    74  	conf.Cleanup()
    75  
    76  	if conf.Webserver.URL == "" {
    77  		return nil, errors.Errorf("no webserver URL specified")
    78  	}
    79  
    80  	return conf, nil
    81  }
    82  
    83  func applyDefaultConfig(conf *config.Configuration) {
    84  	if conf.Prefix == "" {
    85  		conf.Prefix = serviceName
    86  	}
    87  
    88  	if conf.Storage.Driver == "" {
    89  		conf.Storage.Driver = "file"
    90  	}
    91  
    92  	if conf.Mentix.DataEndpoint == "" {
    93  		conf.Mentix.DataEndpoint = "/sites"
    94  	}
    95  
    96  	if conf.Mentix.SiteRegistrationEndpoint == "" {
    97  		conf.Mentix.SiteRegistrationEndpoint = "/sitereg"
    98  	}
    99  
   100  	// Enforce a minimum session timeout of 1 minute (and default to 5 minutes)
   101  	if conf.Webserver.SessionTimeout < 60 {
   102  		conf.Webserver.SessionTimeout = 5 * 60
   103  	}
   104  }
   105  
   106  // New returns a new Site Accounts service.
   107  func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
   108  	// Prepare the configuration
   109  	conf, err := parseConfig(m)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	// Create the site accounts instance
   115  	siteacc, err := siteacc.New(conf, log)
   116  	if err != nil {
   117  		return nil, errors.Wrap(err, "error creating the site accounts service")
   118  	}
   119  
   120  	// Create the service
   121  	s := &svc{
   122  		conf:    conf,
   123  		log:     log,
   124  		siteacc: siteacc,
   125  	}
   126  	return s, nil
   127  }