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
|
package idpmiddleware
import (
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"sync"
"time"
"github.com/pborman/uuid"
"within.website/ln"
"within.website/ln/opname"
)
// hash is a simple wrapper around the MD5 algorithm implementation in the
// Go standard library. It takes in data and a salt and returns the hashed
// representation.
func hash(data string, salt string) string {
output := md5.Sum([]byte(data + salt))
return fmt.Sprintf("%x", output)
}
var (
globalSalt string
)
func init() {
globalSalt = hash(uuid.New(), uuid.New())
}
func verify(ctx context.Context, idpServer, state, code string) *http.Request {
u, err := url.Parse(idpServer)
if err != nil {
panic(err)
}
u.Path = "/auth"
q := u.Query()
q.Set("code", code)
q.Set("state", state)
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
panic(err)
}
req.Header.Set("Accept", "application/json")
req = req.WithContext(ctx)
return req
}
func validate(resp *http.Response) (string, error) {
result := struct {
Me string `json:"me"`
}{}
if resp.StatusCode != 200 {
return "", fmt.Errorf("wanted status 200, got: %d", resp.StatusCode)
}
err := json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", err
}
return result.Me, nil
}
// XeProtect sets defaults for Xe to use.
func XeProtect(selfURL string) func(next http.Handler) http.Handler {
return Protect("https://idp.christine.website", "https://christine.website/", selfURL)
}
// Protect protects a given URL behind your given idp(1) server.
func Protect(idpServer, me, selfURL string) func(next http.Handler) http.Handler {
lock := sync.Mutex{}
codes := map[string]string{}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = opname.With(ctx, "idpmiddleware.Protect.Handler")
if r.URL.Path == "/.within/x/idpmiddleware/challenge" {
v := r.URL.Query()
ctx = ln.WithF(ctx, ln.F{"as": me, "state": v.Get("state"), "code": v.Get("code")})
ln.Log(ctx, ln.Info("login"))
lock.Lock()
defer lock.Unlock()
if cd := v.Get("state"); codes[cd] == cd {
ctx = opname.With(ctx, "verify")
resp, err := http.DefaultClient.Do(verify(ctx, idpServer, v.Get("state"), v.Get("code")))
if err != nil {
ln.Error(ctx, err)
http.Error(w, "nope", http.StatusInternalServerError)
return
}
got, err := validate(resp)
if err != nil {
ln.Error(ctx, err)
http.Error(w, "not valid", http.StatusInternalServerError)
return
}
if me != got {
ln.Error(ctx, errors.New("hacking attempt"))
http.Error(w, "...", http.StatusNotAcceptable)
return
}
ln.Log(ctx, ln.Info("setting cookie"))
http.SetCookie(w, &http.Cookie{
Name: "within-x-idpmiddleware",
Value: hash(me, globalSalt),
HttpOnly: true,
Expires: time.Now().Add(900 * time.Hour),
Path: "/",
SameSite: http.SameSiteLaxMode,
})
delete(codes, cd)
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
http.Redirect(w, r, selfURL, http.StatusTemporaryRedirect)
}
http.Error(w, "Programmer error, maybe you have multiple instances of the IDP middleware?", http.StatusInternalServerError)
return
}
cookie, err := r.Cookie("within-x-idpmiddleware")
if err != nil || cookie.Value != hash(me, globalSalt) {
u, err := url.Parse(idpServer)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
code := uuid.New()
lock.Lock()
codes[code] = code
lock.Unlock()
u.Path = "/auth"
v := url.Values{}
v.Set("me", me)
v.Set("client_id", selfURL)
v.Set("redirect_uri", selfURL+".within/x/idpmiddleware/challenge")
v.Set("state", code)
v.Set("response_type", "id")
u.RawQuery = v.Encode()
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect)
return
}
next.ServeHTTP(w, r)
})
}
}
|