-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (198 loc) · 5.6 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/gorilla/sessions"
)
const (
UserHeader = "ASCSA-User"
GroupsHeader = "ASCSA-Groups"
)
type AuthProxy struct {
conf Config
cookieStore *sessions.CookieStore
vhosts map[string][]Proxy
}
type Proxy struct {
Path string
Group string
Handler ProxyHandler
}
type ProxyHandler interface {
Serve(w http.ResponseWriter, r *http.Request, session *sessions.Session)
}
func NewAuthProxy(conf Config) *AuthProxy {
cookieStore := sessions.NewCookieStore(conf.SessionKey)
cookieStore.Options.Secure = true
vhosts := make(map[string][]Proxy)
for _, p := range conf.Proxies {
src, err := url.Parse(p.Src)
if err != nil {
log.Fatalf("Invalid src URL in config: %s", p.Src)
}
switch p.Dst {
case "|signin":
signin := Proxy{
Path: src.Path,
Handler: &SigninHandler{
TenantId: conf.TenantId,
ClientId: conf.ClientId,
},
}
signinCallback := Proxy{
Path: src.Path + "-callback",
Handler: &SigninCallbackHandler{
TenantId: conf.TenantId,
ClientId: conf.ClientId,
ClientSecret: conf.ClientSecret,
},
}
vhosts[src.Host] = append(vhosts[src.Host], signinCallback, signin)
case "|signout":
proxy := Proxy{Path: src.Path, Handler: &SignoutHandler{}}
vhosts[src.Host] = append(vhosts[src.Host], proxy)
default:
dst, err := url.Parse(p.Dst)
if err != nil {
log.Fatalf("Invalid dst URL in config: %s", p.Dst)
}
proxy := Proxy{
Path: src.Path,
Group: p.Group,
Handler: NewReverseHandler(dst),
}
vhosts[src.Host] = append(vhosts[src.Host], proxy)
}
}
return &AuthProxy{
conf: conf,
cookieStore: cookieStore,
vhosts: vhosts,
}
}
func (ap *AuthProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Ignore errors, we'll just get a new session on error
session, _ := ap.cookieStore.Get(r, "session")
userName, _ := session.Values["username"].(string)
displayName, _ := session.Values["displayname"].(string)
groups, _ := session.Values["groups"].([]string)
if userName != "" {
r.Header.Set(UserHeader, fmt.Sprintf("%s %s", userName, displayName))
r.Header.Set(GroupsHeader, strings.Join(groups, " "))
} else {
r.Header.Set(UserHeader, "")
r.Header.Set(GroupsHeader, "")
}
for _, proxy := range ap.vhosts[r.Host] {
if strings.HasPrefix(r.URL.Path, proxy.Path) {
if ap.canAccess(groups, proxy.Group) {
proxy.Handler.Serve(w, r, session)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
return
}
}
http.Error(w, "Not Found", http.StatusNotFound)
}
func (ap *AuthProxy) canAccess(groups []string, group string) bool {
if group == "" {
return true // All groups allowed
} else if group == "*" {
return len(groups) > 0 // Any (non-public) group allowed
} else {
for _, group := range groups {
if group == group {
return true
}
}
}
return false
}
type ReverseHandler struct {
*httputil.ReverseProxy
}
func NewReverseHandler(dst *url.URL) *ReverseHandler {
return &ReverseHandler{
ReverseProxy: httputil.NewSingleHostReverseProxy(dst),
}
}
func (p *ReverseHandler) Serve(w http.ResponseWriter, r *http.Request, session *sessions.Session) {
log.Printf("%s %s: %s", r.Method, r.URL, r.RemoteAddr)
p.ServeHTTP(w, r)
}
type SigninHandler struct {
TenantId string
ClientId string
ClientSecret string
}
func (h *SigninHandler) Serve(w http.ResponseWriter, r *http.Request, session *sessions.Session) {
log.Printf("%s %s: %s", r.Method, r.URL, r.RemoteAddr)
redirectURI := fmt.Sprintf("https://%s%s-callback", r.Host, r.URL.Path)
u := AuthorizationCodeRequestURL(h.TenantId, h.ClientId, redirectURI)
http.Redirect(w, r, u, http.StatusFound)
}
type SigninCallbackHandler struct {
TenantId string
ClientId string
ClientSecret string
}
func (h *SigninCallbackHandler) Serve(w http.ResponseWriter, r *http.Request, session *sessions.Session) {
// Do not log the URL query, since it contains sensitive info
log.Printf("%s %s: %s", r.Method, r.URL.Path, r.RemoteAddr)
code := r.URL.Query().Get("code")
redirectURI := fmt.Sprintf("https://%s%s", r.Host, r.URL.Path)
token, err := RequestAccessToken(h.TenantId, h.ClientId, h.ClientSecret, code, redirectURI)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
userName, displayName, err := GetUser(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
groups, err := GetUserGroups(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Do not allow spaces in groups
for i, group := range groups {
groups[i] = strings.ReplaceAll(group, " ", "")
}
session.Values["username"] = userName
session.Values["displayname"] = displayName
session.Values["groups"] = groups
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
type SignoutHandler struct{}
func (h *SignoutHandler) Serve(w http.ResponseWriter, r *http.Request, session *sessions.Session) {
log.Printf("%s %s: %s", r.Method, r.URL, r.RemoteAddr)
session.Values = nil
session.Save(r, w)
bookmark := r.URL.Query().Get("bookmark")
if bookmark == "" {
bookmark = fmt.Sprintf("https://%s/", r.Host)
}
http.Redirect(w, r, bookmark, http.StatusFound)
}
func main() {
confPath := flag.String("conf", "", "Path to config file")
flag.Parse()
if *confPath == "" {
log.Fatal("Missing configuration file")
}
conf, err := ReadConfig(*confPath)
if err != nil {
log.Fatalf("Error reading configuration file: %s", err)
}
h := NewAuthProxy(conf)
log.Fatal(ListenAndServe(conf, h))
}