rename to badger

This commit is contained in:
Milo Schwartz 2024-09-29 11:45:41 -04:00
parent bc95a7e182
commit 5d4a0ca7c3
No known key found for this signature in database
5 changed files with 85 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
go.sum

10
.traefik.yml Normal file
View file

@ -0,0 +1,10 @@
displayName: gerbil
type: middleware
import: github.com/fosrl/gerbil
summary: Middleware auth bouncer for Fossorial
testData:
apiAddress: http://pangolin:3001
validToken: abc123

View file

@ -1 +1,3 @@
# badger
Custom Traefik plugin middleware for auth

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/fosrl/badger
go 1.21.5

69
main.go Normal file
View file

@ -0,0 +1,69 @@
package badger
import (
"context"
"net/http"
"time"
)
type Config struct {
APIAddress string `json:"apiAddress"`
ValidToken string `json:"validToken"`
}
func CreateConfig() *Config {
return &Config{}
}
type Badger struct {
next http.Handler
name string
apiAdress string
validToken string
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &Badger{
next: next,
name: name,
apiAdress: config.APIAddress,
validToken: config.ValidToken,
}, nil
}
// THIS IS AN EAXMPLE FOR TESTING
var usedTokens = make(map[string]bool)
const cookieName = "access_token"
const cookieDuration = 1 * time.Minute
func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if _, err := req.Cookie(cookieName); err == nil {
p.next.ServeHTTP(rw, req)
return
}
queryToken := req.URL.Query().Get("token")
if queryToken == "" {
http.Error(rw, "Missing token", http.StatusUnauthorized)
return
}
if queryToken != p.validToken || usedTokens[queryToken] {
http.Error(rw, "Invalid or already used token", http.StatusUnauthorized)
return
}
usedTokens[queryToken] = true
expiration := time.Now().Add(cookieDuration)
http.SetCookie(rw, &http.Cookie{
Name: cookieName,
Value: "temporary-access",
Expires: expiration,
Path: "/",
})
p.next.ServeHTTP(rw, req)
}