-
Notifications
You must be signed in to change notification settings - Fork 112
/
open.go
110 lines (94 loc) · 2.85 KB
/
open.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018-2023 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package open
import (
"context"
"encoding/json"
"os"
"strings"
ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/ocm/provider"
"github.com/cs3org/reva/v2/pkg/ocm/provider/authorizer/registry"
"github.com/cs3org/reva/v2/pkg/utils/cfg"
)
func init() {
registry.Register("open", New)
}
// New returns a new authorizer object.
func New(m map[string]interface{}) (provider.Authorizer, error) {
var c config
if err := cfg.Decode(m, &c); err != nil {
return nil, err
}
f, err := os.ReadFile(c.Providers)
if err != nil {
return nil, err
}
providers := []*ocmprovider.ProviderInfo{}
err = json.Unmarshal(f, &providers)
if err != nil {
return nil, err
}
a := &authorizer{}
a.providers = a.getOCMProviders(providers)
return a, nil
}
type config struct {
// Users holds a path to a file containing json conforming the Users struct
Providers string `mapstructure:"providers"`
}
func (c *config) ApplyDefaults() {
if c.Providers == "" {
c.Providers = "/etc/revad/ocm-providers.json"
}
}
type authorizer struct {
providers []*ocmprovider.ProviderInfo
}
func (a *authorizer) GetInfoByDomain(ctx context.Context, domain string) (*ocmprovider.ProviderInfo, error) {
for _, p := range a.providers {
if strings.Contains(p.Domain, domain) {
return p, nil
}
}
return nil, errtypes.NotFound(domain)
}
func (a *authorizer) IsProviderAllowed(ctx context.Context, provider *ocmprovider.ProviderInfo) error {
return nil
}
func (a *authorizer) ListAllProviders(ctx context.Context) ([]*ocmprovider.ProviderInfo, error) {
return a.providers, nil
}
func (a *authorizer) getOCMProviders(providers []*ocmprovider.ProviderInfo) (po []*ocmprovider.ProviderInfo) {
for _, p := range providers {
_, err := a.getOCMHost(p)
if err == nil {
po = append(po, p)
}
}
return
}
func (a *authorizer) getOCMHost(provider *ocmprovider.ProviderInfo) (string, error) {
for _, s := range provider.Services {
if s.Endpoint.Type.Name == "OCM" {
return s.Host, nil
}
}
return "", errtypes.NotFound("OCM Host")
}