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
111
112
113
114
|
package server
import (
"github.com/eduvpn/eduvpn-common/internal/discovery"
"github.com/eduvpn/eduvpn-common/internal/oauth"
"github.com/go-errors/errors"
)
type InstituteAccessServer struct {
// An instute access server has its own OAuth
Auth oauth.OAuth `json:"oauth"`
// Embed the server base
Basic Base `json:"base"`
}
type InstituteAccessServers struct {
Map map[string]*InstituteAccessServer `json:"map"`
CurrentURL string `json:"current_url"`
}
func (ss *Servers) SetInstituteAccess(srv Server) error {
b, err := srv.Base()
if err != nil {
return err
}
if b.Type != "institute_access" {
return errors.Errorf("not an institute access server, url: %s, type: %s", b.URL, b.Type)
}
if _, ok := ss.InstituteServers.Map[b.URL]; ok {
ss.InstituteServers.CurrentURL = b.URL
ss.IsType = InstituteAccessServerType
} else {
return errors.Errorf("institute access server with url: %s, is not yet configured", b.URL)
}
return nil
}
func (ss *Servers) GetInstituteAccess(url string) (*InstituteAccessServer, error) {
if srv, ok := ss.InstituteServers.Map[url]; ok {
return srv, nil
}
return nil, errors.Errorf("no institute access server with URL: %s", url)
}
func (ss *Servers) RemoveInstituteAccess(url string) {
ss.InstituteServers.Remove(url)
}
func (iass *InstituteAccessServers) Remove(url string) {
// Reset the current url
if iass.CurrentURL == url {
iass.CurrentURL = ""
}
// Delete the url from the map
delete(iass.Map, url)
}
func (ias *InstituteAccessServer) TemplateAuth() func(string) string {
return func(authURL string) string {
return authURL
}
}
func (ias *InstituteAccessServer) Base() (*Base, error) {
return &ias.Basic, nil
}
func (ias *InstituteAccessServer) OAuth() *oauth.OAuth {
return &ias.Auth
}
func (ias *InstituteAccessServer) RefreshEndpoints(_ *discovery.Discovery) error {
// Re-initialize the endpoints
b, err := ias.Base()
if err != nil {
return err
}
err = b.InitializeEndpoints()
if err != nil {
return err
}
// update OAuth
auth := ias.OAuth()
if auth != nil {
auth.BaseAuthorizationURL = b.Endpoints.API.V3.Authorization
auth.TokenURL = b.Endpoints.API.V3.Token
}
return nil
}
func (ias *InstituteAccessServer) init(
url string,
name map[string]string,
srvType string,
supportContact []string,
) error {
ias.Basic.URL = url
ias.Basic.DisplayName = name
ias.Basic.SupportContact = supportContact
ias.Basic.Type = srvType
err := ias.Basic.InitializeEndpoints()
if err != nil {
return err
}
API := ias.Basic.Endpoints.API.V3
ias.Auth.Init(API.Authorization, API.Token)
return nil
}
|