github.com/cs3org/reva/v2@v2.27.7/pkg/auth/registry/static/static.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 static
    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/sharedconf"
    29  	"github.com/mitchellh/mapstructure"
    30  )
    31  
    32  func init() {
    33  	registry.Register("static", New)
    34  }
    35  
    36  type config struct {
    37  	Rules map[string]string `mapstructure:"rules"`
    38  }
    39  
    40  func (c *config) init() {
    41  	if len(c.Rules) == 0 {
    42  		c.Rules = map[string]string{
    43  			"basic": sharedconf.GetGatewaySVC(""),
    44  		}
    45  	}
    46  }
    47  
    48  type reg struct {
    49  	rules map[string]string
    50  }
    51  
    52  func (r *reg) ListProviders(ctx context.Context) ([]*registrypb.ProviderInfo, error) {
    53  	providers := []*registrypb.ProviderInfo{}
    54  	for k, v := range r.rules {
    55  		providers = append(providers, &registrypb.ProviderInfo{
    56  			ProviderType: k,
    57  			Address:      v,
    58  		})
    59  	}
    60  	return providers, nil
    61  }
    62  
    63  func (r *reg) GetProvider(ctx context.Context, authType string) (*registrypb.ProviderInfo, error) {
    64  	for k, v := range r.rules {
    65  		if k == authType {
    66  			return &registrypb.ProviderInfo{
    67  				ProviderType: k,
    68  				Address:      v,
    69  			}, nil
    70  		}
    71  	}
    72  	return nil, errtypes.NotFound("static: auth type not found: " + authType)
    73  }
    74  
    75  func parseConfig(m map[string]interface{}) (*config, error) {
    76  	c := &config{}
    77  	if err := mapstructure.Decode(m, c); err != nil {
    78  		return nil, err
    79  	}
    80  	return c, nil
    81  }
    82  
    83  // New returns an implementation of the auth.Registry interface.
    84  func New(m map[string]interface{}) (auth.Registry, error) {
    85  	c, err := parseConfig(m)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	c.init()
    90  	return &reg{rules: c.Rules}, nil
    91  }