summaryrefslogtreecommitdiff
path: root/internal/server/instituteaccess.go
blob: f76323ced6a4b25a551dc20d1f402de9539e47a1 (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
93
94
95
96
97
98
package server

import (
	"errors"
	"fmt"

	"github.com/eduvpn/eduvpn-common/internal/oauth"
	"github.com/eduvpn/eduvpn-common/types"
)

// An instute access server.
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 (servers *Servers) SetInstituteAccess(server Server) error {
	errorMessage := "failed setting institute access server"
	base, baseErr := server.Base()
	if baseErr != nil {
		return types.NewWrappedError(errorMessage, baseErr)
	}

	if base.Type != "institute_access" {
		return types.NewWrappedError(errorMessage, errors.New("not an institute access server"))
	}

	if _, ok := servers.InstituteServers.Map[base.URL]; ok {
		servers.InstituteServers.CurrentURL = base.URL
		servers.IsType = InstituteAccessServerType
	} else {
		return types.NewWrappedError(errorMessage, errors.New("no such institute access server"))
	}
	return nil
}

func (servers *Servers) GetInstituteAccess(url string) (*InstituteAccessServer, error) {
	if server, ok := servers.InstituteServers.Map[url]; ok {
		return server, nil
	}
	return nil, types.NewWrappedError("failed to get institute access server", fmt.Errorf("no institute access server with URL: %s", url))
}

func (servers *Servers) RemoveInstituteAccess(url string) {
	servers.InstituteServers.Remove(url)
}

func (servers *InstituteAccessServers) Remove(url string) {
	// Reset the current url
	if servers.CurrentURL == url {
		servers.CurrentURL = ""
	}

	// Delete the url from the map
	delete(servers.Map, url)
}

func (institute *InstituteAccessServer) TemplateAuth() func(string) string {
	return func(authURL string) string {
		return authURL
	}
}

func (institute *InstituteAccessServer) Base() (*Base, error) {
	return &institute.Basic, nil
}

func (institute *InstituteAccessServer) OAuth() *oauth.OAuth {
	return &institute.Auth
}

func (institute *InstituteAccessServer) init(
	url string,
	displayName map[string]string,
	serverType string,
	supportContact []string,
) error {
	errorMessage := fmt.Sprintf("failed initializing server %s", url)
	institute.Basic.URL = url
	institute.Basic.DisplayName = displayName
	institute.Basic.SupportContact = supportContact
	institute.Basic.Type = serverType
	endpointsErr := institute.Basic.InitializeEndpoints()
	if endpointsErr != nil {
		return types.NewWrappedError(errorMessage, endpointsErr)
	}
	API := institute.Basic.Endpoints.API.V3
	institute.Auth.Init(url, API.Authorization, API.Token)
	return nil
}