-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (182 loc) · 6.58 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
package main
/*
OAuth2 -> SAML federation proxy, with custom Mattermost user mapping.
Mattermost OAuth2 support: https://developers.mattermost.com/integrate/apps/authentication/oauth2/
TikiWiki SAML support: https://doc.tiki.org/SAML
Architecture: https://docs.google.com/drawings/d/1-6bG3IhrlQCuBbebG-6gK_R6q9T9Eyuw_OX5DGoC0iU/edit
Custom user mapping: Mattermost username and e-mail can be changed by the user,
so they are *not* reliable stable user identifiers. The stable user ID is the
internal user ID (eg. "j5rxg5sp538puyifp7dbhiotwo"). TikiWiki uses usernames as
stable user identifier. So, we need an explicit mapping from Mattermost user ID
to TikiWiki username.
Related work:
https://github.com/chaosloth/saml-oauth-bridge
https://github.com/IdentityPython/SATOSA
https://stackoverflow.com/questions/53227919/proxy-on-top-of-oidc-idp-provider-to-accept-saml-requests-from-service-provider
https://www.keycloak.org/docs/latest/server_admin/index.html#_identity_broker_oidc
https://wiki.geant.org/display/GSPP/Technical+Information
https://lemonldap-ng.org/documentation/2.0/federationproxy.html
*/
import (
"bufio"
"crypto/tls"
"crypto/x509"
"flag"
"github.com/crewjam/saml"
"github.com/crewjam/saml/logger"
"golang.org/x/oauth2"
"log"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
)
// https://workos.com/docs/sso/signing-certificates/saml-response-signing-certificate
// openssl req -x509 -newkey rsa:2048 -keyout matterid.key -out matterid.crt -days 3650 -nodes -subj "/CN=auth.foulab.org"
var crtFile = flag.String("crt", "matterid.crt", "IdP certificate")
var keyFile = flag.String("key", "matterid.key", "IdP private key")
var baseURL = flag.String("baseURL", "https://auth.foulab.org/matterid", "IdP base URL (excluding /metadata)")
var serviceProvidersGlob = flag.String("serviceProvidersGlob", "tikiwiki.xml", "Glob for service provider metadata XML. IdP will only accept requests from these SPs.")
// Note: MUST run behind a reverse proxy that adds TLS
var port = flag.Int("port", 8007, "Port for HTTP server")
func readUsermap() map[string]string {
m := map[string]string{}
f, err := os.Open("usermap")
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// TODO: implement comments
fs := strings.Fields(scanner.Text())
if len(fs) == 0 {
continue
}
if len(fs) != 2 {
log.Fatal("Invalid usermap line: %s", scanner.Text())
return nil
}
// TODO: check for duplicates
m[fs[0]] = fs[1]
}
log.Printf("usermap: %d entries", len(m))
return m
}
func readOAuthClientSecret() string {
b, err := os.ReadFile("oauth-client-secret")
if err != nil {
panic(err)
}
return strings.TrimRight(string(b), "\n")
}
// https://blog.kowalczyk.info/article/e00e89c3841e4f8c8c769a78b8a90b47/logging-http-requests-in-go.html
// Request.RemoteAddress contains port, which we want to remove i.e.:
// "[::1]:58292" => "[::1]"
func ipAddrFromRemoteAddr(s string) string {
idx := strings.LastIndex(s, ":")
if idx == -1 {
return s
}
return s[:idx]
}
// requestGetRemoteAddress returns ip address of the client making the request,
// taking into account http proxies
func requestGetRemoteAddress(r *http.Request) string {
hdr := r.Header
hdrRealIP := hdr.Get("X-Real-Ip")
hdrForwardedFor := hdr.Get("X-Forwarded-For")
if hdrRealIP == "" && hdrForwardedFor == "" {
return ipAddrFromRemoteAddr(r.RemoteAddr)
}
if hdrForwardedFor != "" {
// X-Forwarded-For is potentially a list of addresses separated with ","
parts := strings.Split(hdrForwardedFor, ",")
for i, p := range parts {
parts[i] = strings.TrimSpace(p)
}
// TODO: should return first non-local address
return parts[0]
}
return hdrRealIP
}
func main() {
logr := logger.DefaultLogger
flag.Parse()
// OAuth2 client
// https://github.com/douglasmakey/oauth2-example
mattermostOauthConfig := &oauth2.Config{
RedirectURL: *baseURL + "/callback",
ClientID: "8ts1zi7j8jyiffr8bgzbgysc4w",
ClientSecret: readOAuthClientSecret(),
// https://github.com/mattermost/mattermost/blob/773ab352e845e1313c4b6a273ad1aae19e31f58c/app/oauth.go#L176
Scopes: []string{"user"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://test.foulab.org/oauth/authorize",
TokenURL: "https://test.foulab.org/oauth/access_token",
AuthStyle: oauth2.AuthStyleInParams,
},
}
usermap := readUsermap()
sessionProvider := NewMattermostSessionProvider(mattermostOauthConfig, usermap)
// SAML IdP
baseURL, err := url.Parse(*baseURL)
if err != nil {
logr.Fatalf("cannot parse base URL: %v", err)
}
keyPair, err := tls.LoadX509KeyPair(*crtFile, *keyFile)
if err != nil {
logr.Fatalf("cannot load key pair: %v", err)
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
logr.Fatalf("cannot parse certificate: %v", err)
}
metadataURL := *baseURL
metadataURL.Path += "/metadata"
ssoURL := *baseURL
ssoURL.Path += "/sso"
idp := &saml.IdentityProvider{
Key: keyPair.PrivateKey,
Logger: logr,
Certificate: keyPair.Leaf,
MetadataURL: metadataURL,
SSOURL: ssoURL,
ServiceProviderProvider: NewFileServiceProvider(*serviceProvidersGlob),
SessionProvider: sessionProvider,
}
http.HandleFunc(idp.MetadataURL.Path, idp.ServeMetadata)
http.HandleFunc(idp.SSOURL.Path, idp.ServeSSO)
// OAuth2 callback
http.HandleFunc(baseURL.Path+"/callback", func(w http.ResponseWriter, cbr *http.Request) {
sessionProvider.ServeCallback(w, cbr, idp)
})
// Ensure requests are from localhost (from a reverse proxy that hopefully
// adds TLS) -- SAML assertions are bearer tokens and must be protected.
ensureLocalClient := func(w http.ResponseWriter, r *http.Request) {
// Redact query parameter values (eg. OAuth authorization codes)
urlRedacted := regexp.MustCompile(`=[^&]+(&|$)`).ReplaceAllString(r.URL.String(), "=<redacted>$1")
logr.Printf("%s %s %s\n", requestGetRemoteAddress(r), r.Method, urlRedacted)
w.Header().Set("Server", "matterid/0.1")
var a *net.TCPAddr
if a, err = net.ResolveTCPAddr("tcp", r.RemoteAddr); err != nil {
logr.Printf("cannot parse RemoteAddr: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if !a.IP.IsLoopback() {
logr.Printf("RemoteAddr is not loopback: %v", a.IP)
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
http.DefaultServeMux.ServeHTTP(w, r)
}
err = http.ListenAndServe("127.0.0.1:"+strconv.Itoa(*port), http.HandlerFunc(ensureLocalClient))
if err != nil {
logr.Panicf("listen: %v", err)
return
}
}