-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
456 lines (395 loc) · 11.5 KB
/
main.go
File metadata and controls
456 lines (395 loc) · 11.5 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"flag"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"sync"
"time"
)
var (
signingKey *ecdsa.PrivateKey
keyID string
codes pendingMap[authCode]
tokens pendingMap[tokenInfo]
cfg config
)
type config struct {
Listen string
Upstream string
Issuer string
ClientID string
ClientSecret string
}
type authCode struct {
Email string
UserID string
Nonce string
}
type tokenInfo struct {
Email string
UserID string
}
func main() {
flag.StringVar(&cfg.Listen, "listen", envOr("LISTEN", ":8000"), "listen address")
flag.StringVar(&cfg.Upstream, "upstream", os.Getenv("UPSTREAM"), "upstream URL to proxy to")
flag.StringVar(&cfg.Issuer, "issuer", os.Getenv("ISSUER"), "OIDC issuer URL")
flag.StringVar(&cfg.ClientID, "client-id", os.Getenv("CLIENT_ID"), "OIDC client ID")
flag.StringVar(&cfg.ClientSecret, "client-secret", os.Getenv("CLIENT_SECRET"), "OIDC client secret")
flag.Parse()
if cfg.Upstream == "" {
log.Fatal("--upstream is required")
}
if cfg.Issuer == "" {
log.Fatal("--issuer is required")
}
if cfg.ClientID == "" {
log.Fatal("--client-id is required")
}
if cfg.ClientSecret == "" {
log.Fatal("--client-secret is required")
}
var err error
signingKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalf("generating signing key: %v", err)
}
keyID = computeKeyID(&signingKey.PublicKey)
go sweepLoop()
upstreamURL, err := url.Parse(cfg.Upstream)
if err != nil {
log.Fatalf("parsing upstream URL: %v", err)
}
proxy := httputil.NewSingleHostReverseProxy(upstreamURL)
mux := http.NewServeMux()
mux.HandleFunc("/_oidc/.well-known/openid-configuration", handleDiscovery)
mux.HandleFunc("/_oidc/authorize", handleAuthorize)
mux.HandleFunc("/_oidc/token", handleToken)
mux.HandleFunc("/_oidc/userinfo", handleUserInfo)
mux.HandleFunc("/_oidc/jwks", handleJWKS)
mux.Handle("/", proxy)
log.Printf("exe-oidc-proxy listening on %s, upstream %s, issuer %s", cfg.Listen, cfg.Upstream, cfg.Issuer)
if err := http.ListenAndServe(cfg.Listen, mux); err != nil {
log.Fatal(err)
}
}
func handleDiscovery(w http.ResponseWriter, r *http.Request) {
doc := map[string]any{
"issuer": cfg.Issuer,
"authorization_endpoint": cfg.Issuer + "/authorize",
"token_endpoint": cfg.Issuer + "/token",
"userinfo_endpoint": cfg.Issuer + "/userinfo",
"jwks_uri": cfg.Issuer + "/jwks",
"response_types_supported": []string{"code"},
"subject_types_supported": []string{"public"},
"id_token_signing_alg_values_supported": []string{"ES256"},
"scopes_supported": []string{"openid", "email", "profile"},
"token_endpoint_auth_methods_supported": []string{"client_secret_post", "client_secret_basic"},
"claims_supported": []string{"sub", "email", "iss", "aud", "exp", "iat", "nonce"},
"grant_types_supported": []string{"authorization_code"},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(doc)
}
func handleAuthorize(w http.ResponseWriter, r *http.Request) {
email := r.Header.Get("X-Exedev-Email")
userID := r.Header.Get("X-Exedev-Userid")
if email == "" {
// Not authenticated — bounce through exe.dev login.
// Reconstruct the full authorize URL so we come back here after login.
loginURL := "/__exe.dev/login?redirect=" + url.QueryEscape(r.URL.RequestURI())
http.Redirect(w, r, loginURL, http.StatusFound)
return
}
redirectURI := r.URL.Query().Get("redirect_uri")
state := r.URL.Query().Get("state")
nonce := r.URL.Query().Get("nonce")
responseMode := r.URL.Query().Get("response_mode")
if redirectURI == "" {
http.Error(w, "missing redirect_uri", http.StatusBadRequest)
return
}
code := randomString(32)
codes.Store(code, authCode{Email: email, UserID: userID, Nonce: nonce}, 60*time.Second)
u, err := url.Parse(redirectURI)
if err != nil {
http.Error(w, "invalid redirect_uri", http.StatusBadRequest)
return
}
// Build the response parameters
params := url.Values{}
params.Set("code", code)
if state != "" {
params.Set("state", state)
}
// Use fragment or query based on response_mode
if responseMode == "fragment" {
u.Fragment = params.Encode()
} else {
u.RawQuery = params.Encode()
}
http.Redirect(w, r, u.String(), http.StatusFound)
}
func handleToken(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
// Authenticate the client via post body or basic auth.
if !authenticateClient(r) {
w.Header().Set("WWW-Authenticate", "Basic")
http.Error(w, "invalid client credentials", http.StatusUnauthorized)
return
}
code := r.FormValue("code")
ac, ok := codes.LoadAndDelete(code)
if !ok {
http.Error(w, `{"error":"invalid_grant"}`, http.StatusBadRequest)
return
}
accessToken := randomString(32)
tokens.Store(accessToken, tokenInfo{Email: ac.Email, UserID: ac.UserID}, 24*time.Hour)
log.Printf("token: issued access_token=%q for %s", accessToken, ac.Email)
// Derive a display name from email
name := ac.Email
if idx := strings.Index(ac.Email, "@"); idx > 0 {
name = ac.Email[:idx]
}
now := time.Now()
idToken := jwtToken{
Header: jwtHeader{Alg: "ES256", Typ: "JWT", Kid: keyID},
Claims: map[string]any{
"iss": cfg.Issuer,
"sub": ac.UserID,
"aud": cfg.ClientID,
"email": ac.Email,
"name": name,
"preferred_username": name,
"groups": []string{"admin"},
"iat": now.Unix(),
"exp": now.Add(24 * time.Hour).Unix(),
},
}
if ac.Nonce != "" {
idToken.Claims["nonce"] = ac.Nonce
}
signed, err := idToken.Sign(signingKey)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
json.NewEncoder(w).Encode(map[string]any{
"access_token": accessToken,
"id_token": signed,
"token_type": "bearer",
"expires_in": 86400,
})
}
func handleUserInfo(w http.ResponseWriter, r *http.Request) {
// Log all headers for debugging
log.Printf("userinfo: method=%s", r.Method)
for name, values := range r.Header {
log.Printf("userinfo: header %s=%v", name, values)
}
log.Printf("userinfo: query=%s", r.URL.RawQuery)
accessToken := extractBearerToken(r)
// Also check query parameter and form body (some clients send it there)
if accessToken == "" {
accessToken = r.URL.Query().Get("access_token")
}
if accessToken == "" {
r.ParseForm()
accessToken = r.FormValue("access_token")
}
log.Printf("userinfo: token=%q", accessToken)
if accessToken == "" {
log.Printf("userinfo: no bearer token in header, query, or form")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
info, ok := tokens.Load(accessToken)
if !ok {
log.Printf("userinfo: token not found in store")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
log.Printf("userinfo: found user %s", info.Email)
// Derive a display name from email (part before @)
name := info.Email
if idx := strings.Index(info.Email, "@"); idx > 0 {
name = info.Email[:idx]
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"sub": info.UserID,
"email": info.Email,
"name": name,
"preferred_username": name,
"groups": []string{"admin"},
})
}
func handleJWKS(w http.ResponseWriter, r *http.Request) {
pub := &signingKey.PublicKey
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"keys": []map[string]any{{
"kty": "EC",
"crv": "P-256",
"use": "sig",
"kid": keyID,
"alg": "ES256",
"x": base64URLEncode(pub.X.Bytes(), 32),
"y": base64URLEncode(pub.Y.Bytes(), 32),
}},
})
}
// authenticateClient checks client credentials from POST body or Basic auth.
func authenticateClient(r *http.Request) bool {
// Try POST body first.
if r.FormValue("client_id") == cfg.ClientID && r.FormValue("client_secret") == cfg.ClientSecret {
return true
}
// Try Basic auth.
if user, pass, ok := r.BasicAuth(); ok {
return user == cfg.ClientID && pass == cfg.ClientSecret
}
return false
}
func extractBearerToken(r *http.Request) string {
auth := r.Header.Get("Authorization")
// Handle both "Bearer" and "bearer" (case-insensitive)
if len(auth) > 7 && strings.EqualFold(auth[:6], "bearer") && auth[6] == ' ' {
return auth[7:]
}
return ""
}
func envOr(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
// --- JWT signing (no dependencies) ---
type jwtHeader struct {
Alg string `json:"alg"`
Typ string `json:"typ"`
Kid string `json:"kid"`
}
type jwtToken struct {
Header jwtHeader
Claims map[string]any
}
func (t *jwtToken) Sign(key *ecdsa.PrivateKey) (string, error) {
headerJSON, err := json.Marshal(t.Header)
if err != nil {
return "", err
}
claimsJSON, err := json.Marshal(t.Claims)
if err != nil {
return "", err
}
unsigned := base64url(headerJSON) + "." + base64url(claimsJSON)
hash := sha256.Sum256([]byte(unsigned))
sigR, sigS, err := ecdsa.Sign(rand.Reader, key, hash[:])
if err != nil {
return "", err
}
// ECDSA signature for ES256: r || s, each 32 bytes, big-endian.
sig := make([]byte, 64)
rBytes := sigR.Bytes()
sBytes := sigS.Bytes()
copy(sig[32-len(rBytes):32], rBytes)
copy(sig[64-len(sBytes):64], sBytes)
return unsigned + "." + base64url(sig), nil
}
func base64url(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}
// base64URLEncode encodes a big-endian integer as base64url, zero-padded to size bytes.
func base64URLEncode(b []byte, size int) string {
padded := make([]byte, size)
copy(padded[size-len(b):], b)
return base64url(padded)
}
func computeKeyID(pub *ecdsa.PublicKey) string {
h := sha256.New()
h.Write(pub.X.Bytes())
h.Write(pub.Y.Bytes())
return base64url(h.Sum(nil)[:8])
}
// --- Pending map with expiry ---
type pendingEntry[T any] struct {
Value T
Expiry time.Time
}
type pendingMap[T any] struct {
m sync.Map
}
func (p *pendingMap[T]) Store(key string, val T, ttl time.Duration) {
p.m.Store(key, pendingEntry[T]{Value: val, Expiry: time.Now().Add(ttl)})
}
func (p *pendingMap[T]) Load(key string) (T, bool) {
v, ok := p.m.Load(key)
if !ok {
var zero T
return zero, false
}
e := v.(pendingEntry[T])
if time.Now().After(e.Expiry) {
p.m.Delete(key)
var zero T
return zero, false
}
return e.Value, true
}
func (p *pendingMap[T]) LoadAndDelete(key string) (T, bool) {
v, ok := p.m.LoadAndDelete(key)
if !ok {
var zero T
return zero, false
}
e := v.(pendingEntry[T])
if time.Now().After(e.Expiry) {
var zero T
return zero, false
}
return e.Value, true
}
func (p *pendingMap[T]) Sweep() {
now := time.Now()
p.m.Range(func(key, value any) bool {
if e, ok := value.(pendingEntry[T]); ok && now.After(e.Expiry) {
p.m.Delete(key)
}
return true
})
}
func sweepLoop() {
for {
time.Sleep(time.Minute)
codes.Sweep()
tokens.Sweep()
}
}
func randomString(n int) string {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return base64url(b)
}