Files
boringproxy/utils.go
Anders Pitman 5cd911f310 Automatically create admin user on first start
Also changed order that extractToken looks for tokens. Used to
be cookies then headers then query. Now in reverse, to make it
easier to override, ie for replacing cookies during login.
2020-10-13 09:48:03 -06:00

68 lines
1.4 KiB
Go

package main
import (
"crypto/rand"
"encoding/json"
"errors"
"io/ioutil"
"math/big"
"net/http"
"strings"
)
func saveJson(data interface{}, filePath string) error {
jsonStr, err := json.MarshalIndent(data, "", " ")
if err != nil {
return errors.New("Error serializing JSON")
} else {
err := ioutil.WriteFile(filePath, jsonStr, 0644)
if err != nil {
return errors.New("Error saving JSON")
}
}
return nil
}
// Looks for auth token in query string, then headers, then cookies
func extractToken(tokenName string, r *http.Request) (string, error) {
query := r.URL.Query()
queryToken := query.Get(tokenName)
if queryToken != "" {
return queryToken, nil
}
tokenHeader := r.Header.Get(tokenName)
if tokenHeader != "" {
return tokenHeader, nil
}
authHeader := r.Header.Get("Authorization")
if authHeader != "" {
tokenHeader := strings.Split(authHeader, " ")[1]
return tokenHeader, nil
}
tokenCookie, err := r.Cookie(tokenName)
if err == nil {
return tokenCookie.Value, nil
}
return "", errors.New("No token found")
}
const chars string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func genRandomCode(length int) (string, error) {
id := ""
for i := 0; i < length; i++ {
randIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
return "", err
}
id += string(chars[randIndex.Int64()])
}
return id, nil
}