github.com/cs3org/reva/v2@v2.27.7/pkg/ocm/provider/authorizer/open/open.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 open 20 21 import ( 22 "context" 23 "encoding/json" 24 "os" 25 "strings" 26 27 ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1" 28 "github.com/cs3org/reva/v2/pkg/errtypes" 29 "github.com/cs3org/reva/v2/pkg/ocm/provider" 30 "github.com/cs3org/reva/v2/pkg/ocm/provider/authorizer/registry" 31 "github.com/cs3org/reva/v2/pkg/utils/cfg" 32 ) 33 34 func init() { 35 registry.Register("open", New) 36 } 37 38 // New returns a new authorizer object. 39 func New(m map[string]interface{}) (provider.Authorizer, error) { 40 var c config 41 if err := cfg.Decode(m, &c); err != nil { 42 return nil, err 43 } 44 45 f, err := os.ReadFile(c.Providers) 46 if err != nil { 47 return nil, err 48 } 49 providers := []*ocmprovider.ProviderInfo{} 50 err = json.Unmarshal(f, &providers) 51 if err != nil { 52 return nil, err 53 } 54 55 a := &authorizer{} 56 a.providers = a.getOCMProviders(providers) 57 58 return a, nil 59 } 60 61 type config struct { 62 // Users holds a path to a file containing json conforming the Users struct 63 Providers string `mapstructure:"providers"` 64 } 65 66 func (c *config) ApplyDefaults() { 67 if c.Providers == "" { 68 c.Providers = "/etc/revad/ocm-providers.json" 69 } 70 } 71 72 type authorizer struct { 73 providers []*ocmprovider.ProviderInfo 74 } 75 76 func (a *authorizer) GetInfoByDomain(ctx context.Context, domain string) (*ocmprovider.ProviderInfo, error) { 77 for _, p := range a.providers { 78 if strings.Contains(p.Domain, domain) { 79 return p, nil 80 } 81 } 82 return nil, errtypes.NotFound(domain) 83 } 84 85 func (a *authorizer) IsProviderAllowed(ctx context.Context, provider *ocmprovider.ProviderInfo) error { 86 return nil 87 } 88 89 func (a *authorizer) ListAllProviders(ctx context.Context) ([]*ocmprovider.ProviderInfo, error) { 90 return a.providers, nil 91 } 92 93 func (a *authorizer) getOCMProviders(providers []*ocmprovider.ProviderInfo) (po []*ocmprovider.ProviderInfo) { 94 for _, p := range providers { 95 _, err := a.getOCMHost(p) 96 if err == nil { 97 po = append(po, p) 98 } 99 } 100 return 101 } 102 103 func (a *authorizer) getOCMHost(provider *ocmprovider.ProviderInfo) (string, error) { 104 for _, s := range provider.Services { 105 if s.Endpoint.Type.Name == "OCM" { 106 return s.Host, nil 107 } 108 } 109 return "", errtypes.NotFound("OCM Host") 110 }