From 4ebab3cb8079060ae713158a48dfc2008f75d641 Mon Sep 17 00:00:00 2001 From: risotto Date: Sun, 12 Jan 2025 04:47:44 -0700 Subject: [PATCH] still need to pull in old code --- LICENSE | 21 +++++++++++++++++++++ example_test.go | 28 ++++++++++++++++++++++++++++ init.go | 25 +++++++++++++++++++++++++ login.go | 1 + login/login.go | 9 +++++++++ logout.go | 1 + middleware.go | 25 +++++++++++++++++++++++++ recover.go | 1 + session/session.go | 6 ++++++ 9 files changed, 117 insertions(+) create mode 100644 LICENSE create mode 100644 example_test.go create mode 100644 init.go create mode 100644 login.go create mode 100644 login/login.go create mode 100644 logout.go create mode 100644 middleware.go create mode 100644 recover.go create mode 100644 session/session.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2fdc5c2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright © 2024 NAME HERE + +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. diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..15bb529 --- /dev/null +++ b/example_test.go @@ -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())) +} diff --git a/init.go b/init.go new file mode 100644 index 0000000..fa97c6b --- /dev/null +++ b/init.go @@ -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, + } +} diff --git a/login.go b/login.go new file mode 100644 index 0000000..7264ce3 --- /dev/null +++ b/login.go @@ -0,0 +1 @@ +package whoisthis diff --git a/login/login.go b/login/login.go new file mode 100644 index 0000000..b2b458d --- /dev/null +++ b/login/login.go @@ -0,0 +1,9 @@ +package login + +type LoginResult string + +type LoginMethod interface { + Login() + Logout() + Recover() +} diff --git a/logout.go b/logout.go new file mode 100644 index 0000000..7264ce3 --- /dev/null +++ b/logout.go @@ -0,0 +1 @@ +package whoisthis diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..c82921a --- /dev/null +++ b/middleware.go @@ -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) + }) +} diff --git a/recover.go b/recover.go new file mode 100644 index 0000000..7264ce3 --- /dev/null +++ b/recover.go @@ -0,0 +1 @@ +package whoisthis diff --git a/session/session.go b/session/session.go new file mode 100644 index 0000000..7b77bc4 --- /dev/null +++ b/session/session.go @@ -0,0 +1,6 @@ +package session + +type SessionMethod interface { + Start() + Check() +}