-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfiguration.go
More file actions
74 lines (57 loc) · 1.53 KB
/
configuration.go
File metadata and controls
74 lines (57 loc) · 1.53 KB
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
package authproxy
import (
"encoding/json"
"fmt"
)
type Configuration struct {
AuthenticationContextName string `json:"authContext"`
Upstreams []Upstream `json:"upstreams"`
Users []User `json:"users"`
}
type User struct {
Username string `json:"username"`
Restrict string `json:"restrict"`
}
type Upstream struct {
Prefix string `json:"prefix"`
Location string `json:"location"`
Type string `json:"type"`
}
func (c *Configuration) GetAuthenticationContext() (cx AuthenticationContext, err error) {
if c.AuthenticationContextName == "github" {
cx = NewGithubAuthContext(c)
}
if c.AuthenticationContextName == "auth0" {
cx = NewAuth0AuthContext(c)
}
err = cx.RenderHTMLFile()
return cx, err
}
func NewConfiguration(data []byte) (*Configuration, error) {
config := &Configuration{}
err := json.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("Problem with parsing the confi json file: %s", err.Error())
}
if len(config.Users) == 0 {
return nil, fmt.Errorf("You have no users configured")
}
return config, nil
}
func (c *Configuration) GetRestrictionsForUsername(username string) string {
for _, user := range c.Users {
if user.Username == username {
return user.Restrict
break
}
}
return "NotAllowed"
}
func (c *Configuration) ShouldRestrictUser(username string, method string) bool {
allowedMethod := c.GetRestrictionsForUsername(username)
// Allowed all methods
if allowedMethod == "" {
return true
}
return allowedMethod == method
}