forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorizations.go
132 lines (112 loc) · 2.88 KB
/
authorizations.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package auth
import (
"errors"
"fmt"
"log"
"net/url"
"regexp"
"time"
"github.com/nsqio/nsq/internal/http_api"
)
type Authorization struct {
Topic string `json:"topic"`
Channels []string `json:"channels"`
Permissions []string `json:"permissions"`
}
type State struct {
TTL int `json:"ttl"`
Authorizations []Authorization `json:"authorizations"`
Identity string `json:"identity"`
IdentityURL string `json:"identity_url"`
Expires time.Time
}
func (a *Authorization) HasPermission(permission string) bool {
for _, p := range a.Permissions {
if permission == p {
return true
}
}
return false
}
func (a *Authorization) IsAllowed(topic, channel string) bool {
if channel != "" {
if !a.HasPermission("subscribe") {
return false
}
} else {
if !a.HasPermission("publish") {
return false
}
}
topicRegex := regexp.MustCompile(a.Topic)
if !topicRegex.MatchString(topic) {
return false
}
for _, c := range a.Channels {
channelRegex := regexp.MustCompile(c)
if channelRegex.MatchString(channel) {
return true
}
}
return false
}
func (a *State) IsAllowed(topic, channel string) bool {
for _, aa := range a.Authorizations {
if aa.IsAllowed(topic, channel) {
return true
}
}
return false
}
func (a *State) IsExpired() bool {
if a.Expires.Before(time.Now()) {
return true
}
return false
}
func QueryAnyAuthd(authd []string, remoteIP, tlsEnabled, authSecret string) (*State, error) {
for _, a := range authd {
authState, err := QueryAuthd(a, remoteIP, tlsEnabled, authSecret)
if err != nil {
log.Printf("Error: failed auth against %s %s", a, err)
continue
}
return authState, nil
}
return nil, errors.New("Unable to access auth server")
}
func QueryAuthd(authd, remoteIP, tlsEnabled, authSecret string) (*State, error) {
v := url.Values{}
v.Set("remote_ip", remoteIP)
v.Set("tls", tlsEnabled)
v.Set("secret", authSecret)
endpoint := fmt.Sprintf("http://%s/auth?%s", authd, v.Encode())
var authState State
client := http_api.NewClient(nil)
if err := client.GETV1(endpoint, &authState); err != nil {
return nil, err
}
// validation on response
for _, auth := range authState.Authorizations {
for _, p := range auth.Permissions {
switch p {
case "subscribe", "publish":
default:
return nil, fmt.Errorf("unknown permission %s", p)
}
}
if _, err := regexp.Compile(auth.Topic); err != nil {
return nil, fmt.Errorf("unable to compile topic %q %s", auth.Topic, err)
}
for _, channel := range auth.Channels {
if _, err := regexp.Compile(channel); err != nil {
return nil, fmt.Errorf("unable to compile channel %q %s", channel, err)
}
}
}
if authState.TTL <= 0 {
return nil, fmt.Errorf("invalid TTL %d (must be >0)", authState.TTL)
}
authState.Expires = time.Now().Add(time.Duration(authState.TTL) * time.Second)
return &authState, nil
}