github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/authregistry/authregistry.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 authregistry
    20  
    21  import (
    22  	"context"
    23  
    24  	registrypb "github.com/cs3org/go-cs3apis/cs3/auth/registry/v1beta1"
    25  	"github.com/cs3org/reva/v2/pkg/auth"
    26  	"github.com/cs3org/reva/v2/pkg/auth/registry/registry"
    27  	"github.com/cs3org/reva/v2/pkg/errtypes"
    28  	"github.com/cs3org/reva/v2/pkg/rgrpc"
    29  	"github.com/cs3org/reva/v2/pkg/rgrpc/status"
    30  	"github.com/mitchellh/mapstructure"
    31  	"github.com/rs/zerolog"
    32  	"google.golang.org/grpc"
    33  )
    34  
    35  func init() {
    36  	rgrpc.Register("authregistry", New)
    37  }
    38  
    39  type service struct {
    40  	reg auth.Registry
    41  }
    42  
    43  func (s *service) Close() error {
    44  	return nil
    45  }
    46  
    47  func (s *service) UnprotectedEndpoints() []string {
    48  	return []string{
    49  		"/cs3.auth.registry.v1beta1.RegistryAPI/GetAuthProvider",
    50  		"/cs3.auth.registry.v1beta1.RegistryAPI/ListAuthProviders",
    51  	}
    52  }
    53  
    54  func (s *service) Register(ss *grpc.Server) {
    55  	registrypb.RegisterRegistryAPIServer(ss, s)
    56  }
    57  
    58  type config struct {
    59  	Driver  string                            `mapstructure:"driver"`
    60  	Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
    61  }
    62  
    63  func (c *config) init() {
    64  	if c.Driver == "" {
    65  		c.Driver = "static"
    66  	}
    67  }
    68  
    69  // New creates a new AuthRegistry
    70  func New(m map[string]interface{}, ss *grpc.Server, _ *zerolog.Logger) (rgrpc.Service, error) {
    71  	c, err := parseConfig(m)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	c.init()
    77  
    78  	reg, err := getRegistry(c)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	service := &service{
    84  		reg: reg,
    85  	}
    86  
    87  	return service, nil
    88  }
    89  
    90  func parseConfig(m map[string]interface{}) (*config, error) {
    91  	c := &config{}
    92  	if err := mapstructure.Decode(m, c); err != nil {
    93  		return nil, err
    94  	}
    95  	return c, nil
    96  }
    97  
    98  func getRegistry(c *config) (auth.Registry, error) {
    99  	if f, ok := registry.NewFuncs[c.Driver]; ok {
   100  		return f(c.Drivers[c.Driver])
   101  	}
   102  	return nil, errtypes.NotFound("authregistrysvc: driver not found: " + c.Driver)
   103  }
   104  
   105  func (s *service) ListAuthProviders(ctx context.Context, req *registrypb.ListAuthProvidersRequest) (*registrypb.ListAuthProvidersResponse, error) {
   106  	pinfos, err := s.reg.ListProviders(ctx)
   107  	if err != nil {
   108  		return &registrypb.ListAuthProvidersResponse{
   109  			Status: status.NewInternal(ctx, "error getting list of auth providers"),
   110  		}, nil
   111  	}
   112  
   113  	res := &registrypb.ListAuthProvidersResponse{
   114  		Status:    status.NewOK(ctx),
   115  		Providers: pinfos,
   116  	}
   117  	return res, nil
   118  }
   119  
   120  func (s *service) GetAuthProviders(ctx context.Context, req *registrypb.GetAuthProvidersRequest) (*registrypb.GetAuthProvidersResponse, error) {
   121  	pinfo, err := s.reg.GetProvider(ctx, req.Type)
   122  	if err != nil {
   123  		return &registrypb.GetAuthProvidersResponse{
   124  			Status: status.NewInternal(ctx, "error getting auth provider for type: "+req.Type),
   125  		}, nil
   126  	}
   127  
   128  	res := &registrypb.GetAuthProvidersResponse{
   129  		Status:    status.NewOK(ctx),
   130  		Providers: []*registrypb.ProviderInfo{pinfo},
   131  	}
   132  	return res, nil
   133  }