Skip to content

Commit efa68f5

Browse files
committed
idk some bad code
1 parent bb1e0af commit efa68f5

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

cmd/mini-server/main.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,57 @@ func main() {
190190
}{tmpauth.MinValidationTime().UnixMilli()})
191191
})
192192

193+
http.HandleFunc("/header-evaluate", func(w http.ResponseWriter, r *http.Request) {
194+
w.Header().Set("Content-Type", "text/plain")
195+
196+
var headerOption tmpauth.HeaderOption
197+
err := json.NewDecoder(r.Body).Decode(&headerOption)
198+
if err != nil {
199+
log.Println("error decoding header option:", err)
200+
http.Error(w, err.Error(), http.StatusBadRequest)
201+
return
202+
}
203+
204+
configID := r.Header.Get(tmpauth.ConfigIDHeader)
205+
if configID == "" {
206+
log.Println("missing config ID")
207+
http.Error(w, "missing config ID", http.StatusBadRequest)
208+
return
209+
}
210+
211+
token := r.Header.Get(tmpauth.TokenHeader)
212+
if token == "" {
213+
log.Println("missing tmpauth token")
214+
http.Error(w, "missing tmpauth token", http.StatusBadRequest)
215+
return
216+
}
217+
218+
ta, ok := tmpauthInstances[configID]
219+
if !ok {
220+
log.Println("invalid config ID:", configID)
221+
http.Error(w, "invalid config ID", http.StatusPreconditionFailed)
222+
return
223+
}
224+
225+
cachedToken, err := ta.ParseWrappedAuthJWT(token)
226+
if err != nil {
227+
log.Println("error parsing token:", err)
228+
http.Error(w, err.Error(), http.StatusBadRequest)
229+
return
230+
}
231+
232+
result, err := headerOption.Evaluate(cachedToken.UserDescriptor)
233+
if err != nil {
234+
log.Println("error evaluating header:", err)
235+
http.Error(w, err.Error(), http.StatusInternalServerError)
236+
return
237+
}
238+
239+
w.WriteHeader(http.StatusOK)
240+
w.Write([]byte(result))
241+
return
242+
})
243+
193244
http.HandleFunc("/tmpauth/whomst", func(w http.ResponseWriter, r *http.Request) {
194245
configID := r.Header.Get(tmpauth.ConfigIDHeader)
195246
if configID == "" {

token.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package tmpauth
22

33
import (
4+
"bytes"
45
"crypto/sha256"
56
"encoding/base64"
67
"encoding/hex"
78
"encoding/json"
89
"errors"
910
"fmt"
11+
"io"
1012
"math/rand"
1113
"net/http"
1214
"net/url"
@@ -288,8 +290,38 @@ func (t *Tmpauth) SetHeaders(token *CachedToken, headers http.Header) error {
288290
headers.Set(headerName, val)
289291
} else {
290292
if t.miniServerHost != "" {
291-
return errors.New("tmpauth: cannot set headers when using mini server " +
292-
"endpoint, mini server has a bad implementation")
293+
headerConfig, err := json.Marshal(headerOption)
294+
if err != nil {
295+
return fmt.Errorf("tmpauth: failed to marshal header option: %w", err)
296+
}
297+
298+
req, err := http.NewRequest(http.MethodGet, t.miniServerHost+"/header-evaluate",
299+
bytes.NewReader(headerConfig))
300+
if err != nil {
301+
return fmt.Errorf("tmpauth: invalid mini server request: %w", err)
302+
}
303+
304+
req.Header.Set(ConfigIDHeader, t.miniConfigID)
305+
req.Header.Set(TokenHeader, token.RawToken)
306+
307+
req.Header.Set("Content-Type", "application/jwt")
308+
resp, err := t.miniClient(req, 0)
309+
if err != nil {
310+
return fmt.Errorf("tmpauth: mini request failed: %w", err)
311+
}
312+
313+
body, err := io.ReadAll(resp.Body)
314+
if err != nil {
315+
return fmt.Errorf("tmpauth: read all failed: %w", err)
316+
}
317+
318+
if resp.StatusCode != http.StatusOK {
319+
return fmt.Errorf("tmpauth: mini server returned %v: %v", resp.Status, string(body))
320+
}
321+
322+
headers.Set(headerName, string(body))
323+
headersToCache = append(headersToCache, [2]string{headerOption.Format, string(body)})
324+
return nil
293325
}
294326

295327
value, err := headerOption.Evaluate(token.UserDescriptor)

0 commit comments

Comments
 (0)