still need to pull in old code

This commit is contained in:
Risotto Bias 2025-01-12 04:47:44 -07:00
parent 9673984551
commit 4ebab3cb80
9 changed files with 117 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

28
example_test.go Normal file
View file

@ -0,0 +1,28 @@
package whoisthis_test
import (
"net/http"
"git.bivouac.wiki/use/whoisthis"
)
func Deny(w http.ResponseWriter, r *http.Request) {
http.Error(w, "please sign-in", http.StatusUnauthorized)
}
func lookup(r *http.Request) (string, error) {
user := "abcd"
return user, nil
}
func HomeRoute(w http.ResponseWriter, r *http.Request) {
}
func ExampleSession() {
wis := whoisthis.New(store, cache, lookup, Deny)
mux := http.NewServeMux()
mux.Handle("/home", wis.Authed(HomeRoute()))
}

25
init.go Normal file
View file

@ -0,0 +1,25 @@
package whoisthis
import "net/http"
type kv interface {
Get(string) any
Set(string) any
}
type WhoIsThis struct {
cache kv
store kv
LookupReqFn LookupFn
DenyFn respond
}
type LookupFn func(r *http.Request) (string, error)
type respond func(w http.ResponseWriter, r *http.Request)
func New(store kv, cache kv, lookup LookupFn, deny respond) WhoIsThis {
return WhoIsThis{
store: store,
cache: cache,
LookupReqFn: lookup,
DenyFn: deny,
}
}

1
login.go Normal file
View file

@ -0,0 +1 @@
package whoisthis

9
login/login.go Normal file
View file

@ -0,0 +1,9 @@
package login
type LoginResult string
type LoginMethod interface {
Login()
Logout()
Recover()
}

1
logout.go Normal file
View file

@ -0,0 +1 @@
package whoisthis

25
middleware.go Normal file
View file

@ -0,0 +1,25 @@
package whoisthis
import (
"context"
"net/http"
)
type CtxKey string
const ContextUserId CtxKey = "userid"
func (wis WhoIsThis) Authed(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Our middleware logic goes here...
user, err := wis.LookupReqFn(r)
if err != nil {
http.Error(w, "please sign-in", http.StatusUnauthorized)
return
}
ctxWithUser := context.WithValue(r.Context(), ContextUserId, user)
rWithUser := r.WithContext(ctxWithUser)
next.ServeHTTP(w, rWithUser)
})
}

1
recover.go Normal file
View file

@ -0,0 +1 @@
package whoisthis

6
session/session.go Normal file
View file

@ -0,0 +1,6 @@
package session
type SessionMethod interface {
Start()
Check()
}