summaryrefslogtreecommitdiff
path: root/internal/server/instituteaccess.go
blob: 56ed1cfe99ea7d51420ccc5ed1f7ea63d2624967 (plain)
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
package server

import (
	"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")
	}

	if _, ok := ss.InstituteServers.Map[b.URL]; ok {
		ss.InstituteServers.CurrentURL = b.URL
		ss.IsType = InstituteAccessServerType
	} else {
		return errors.Errorf("no such institute access server")
	}
	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) 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(url, API.Authorization, API.Token)
	return nil
}