aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-01-12 17:45:55 -0800
committerChristine Dodrill <me@christine.website>2019-01-12 17:45:55 -0800
commit10a92ff91a17731b2cef6e9a5aa3de8fe455d721 (patch)
treecc5f6588cd4990d02b4ec3bf533129fd8b259ecd
parent4aa86399c798cdeb4db325132e75b7499dc0c9a5 (diff)
downloadx-10a92ff91a17731b2cef6e9a5aa3de8fe455d721.tar.xz
x-10a92ff91a17731b2cef6e9a5aa3de8fe455d721.zip
idp: make middleware better, usage directions
-rw-r--r--idp/README.md25
-rw-r--r--idp/idpmiddleware/_example/test.go29
-rw-r--r--idp/idpmiddleware/middleware.go26
3 files changed, 73 insertions, 7 deletions
diff --git a/idp/README.md b/idp/README.md
new file mode 100644
index 0000000..3ca1425
--- /dev/null
+++ b/idp/README.md
@@ -0,0 +1,25 @@
+# idp
+
+This is a poor man's implementation of [indieauth's authorization endpoint](https://indieweb.org/authorization-endpoint). Setup and usage is simple:
+
+- `go build -o idp .`
+- Set up a domain in your nginx/caddy/whatever configuration for the `idp(1)` server
+- Set up `idp(1)` to listen on a local port: `./idp -port 5484`
+- Generate a TOTP key: `./idp -secret-gen 16`
+- Make a config file
+- Configure idp to start on boot: `idp -config /etc/within.x/idp.conf`
+- Add `<link rel="authorization_endpoint" href="https://idp.christine.website/auth">` to your homepage/index.html where `idp.christine.website` is your domain from the second step
+- Test [here](https://indielogin.com)
+
+## Config File
+
+```
+// Example idp(1) config file
+
+port 9040
+otp-secret 4S62BZNFXXSZLCRO
+owner https://example.com
+domain idp.example.com
+```
+
+Put this in `/etc/within.x/idp.conf`
diff --git a/idp/idpmiddleware/_example/test.go b/idp/idpmiddleware/_example/test.go
new file mode 100644
index 0000000..1d28ecb
--- /dev/null
+++ b/idp/idpmiddleware/_example/test.go
@@ -0,0 +1,29 @@
+//+build ignore
+
+package main
+
+import (
+ "flag"
+ "log"
+ "net/http"
+
+ "github.com/Xe/x/idp/idpmiddleware"
+ "github.com/Xe/x/internal"
+ "within.website/ln/ex"
+)
+
+var (
+ port = flag.String("port", "6060", "TCP port")
+ server = flag.String("server", "https://idp.christine.website", "idp server")
+ me = flag.String("me", "https://christine.website/", "self-identity")
+ selfURL = flag.String("self-url", "http://127.0.0.1:6060/", "redirect URL for the self.")
+)
+
+func main() {
+ internal.HandleStartup()
+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ http.Error(w, "success!", http.StatusOK)
+ })
+
+ log.Fatal(http.ListenAndServe(":"+*port, ex.HTTPLog(idpmiddleware.Protect(*server, *me, *selfURL)(http.DefaultServeMux))))
+}
diff --git a/idp/idpmiddleware/middleware.go b/idp/idpmiddleware/middleware.go
index eaec2ce..b7a961b 100644
--- a/idp/idpmiddleware/middleware.go
+++ b/idp/idpmiddleware/middleware.go
@@ -6,7 +6,10 @@ import (
"sync"
"time"
+ "github.com/kr/pretty"
"github.com/pborman/uuid"
+ "within.website/ln"
+ "within.website/ln/opname"
)
// Protect protects a given URL behind your given idp(1) server.
@@ -16,24 +19,32 @@ func Protect(idpServer, me, selfURL string) func(next http.Handler) http.Handler
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ ctx := r.Context()
+ ctx = opname.With(ctx, "idpmiddleware.Protect.Handler")
if r.URL.Path == "/auth/challenge" {
v := r.URL.Query()
+ ctx = ln.WithF(ctx, ln.F{"as": me, "state": v.Get("state"), "code": v.Get("code")})
+ ln.Log(ctx, ln.Info("login"))
lock.Lock()
- defer lock.Unlock()
- if cd := v.Get("code"); codes[cd] == cd {
+ pretty.Println(codes)
+ if cd := v.Get("state"); codes[cd] == cd {
+ ln.Log(ctx, ln.Info("setting cookie"))
http.SetCookie(w, &http.Cookie{
- Name: "idp",
+ Name: "auth",
Value: me,
HttpOnly: true,
Expires: time.Now().Add(900 * time.Hour),
+ Path: "/",
+ SameSite: http.SameSiteLaxMode,
})
- http.Redirect(w, r, selfURL, http.StatusTemporaryRedirect)
- return
+ http.Error(w, "success", http.StatusOK)
}
+ lock.Unlock()
+ return
}
- cookie, err := r.Cookie("idp")
+ cookie, err := r.Cookie("auth")
if err != nil {
u, err := url.Parse(idpServer)
if err != nil {
@@ -50,9 +61,10 @@ func Protect(idpServer, me, selfURL string) func(next http.Handler) http.Handler
v := url.Values{}
v.Set("me", me)
v.Set("client_id", selfURL)
- v.Set("redirect_uri", selfURL+"/auth/challenge")
+ v.Set("redirect_uri", selfURL+"auth/challenge")
v.Set("state", code)
v.Set("response_type", "id")
+ u.RawQuery = v.Encode()
http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect)
return