diff options
| author | Xe <me@christine.website> | 2022-12-29 15:13:38 -0500 |
|---|---|---|
| committer | Xe <me@christine.website> | 2022-12-29 15:13:38 -0500 |
| commit | 3bf8b46e8c8add50dfcd0594d194b318eb580606 (patch) | |
| tree | 9d232b5d5376af8eb2666b7a9e5e12a0720c744e | |
| parent | feb3e3d732ac238c5f8a358aeac36237db71060d (diff) | |
| download | x-3bf8b46e8c8add50dfcd0594d194b318eb580606.tar.xz x-3bf8b46e8c8add50dfcd0594d194b318eb580606.zip | |
delete idp that I don't use
Signed-off-by: Xe <me@christine.website>
| -rw-r--r-- | idp/.gitignore | 4 | ||||
| -rw-r--r-- | idp/README.md | 27 | ||||
| -rw-r--r-- | idp/build.go | 42 | ||||
| -rw-r--r-- | idp/doc.go | 2 | ||||
| -rw-r--r-- | idp/idpmiddleware/_example/test.go | 29 | ||||
| -rw-r--r-- | idp/idpmiddleware/doc.go | 4 | ||||
| -rw-r--r-- | idp/idpmiddleware/middleware.go | 168 | ||||
| -rw-r--r-- | idp/main.go | 290 | ||||
| -rw-r--r-- | idp/translations/en_US.json | 16 | ||||
| -rw-r--r-- | idp/translations/fr_CA.json | 16 | ||||
| -rw-r--r-- | idp/translations/jbo_latn.json | 16 |
11 files changed, 0 insertions, 614 deletions
diff --git a/idp/.gitignore b/idp/.gitignore deleted file mode 100644 index 72b5f4b..0000000 --- a/idp/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -*.cfg -idp -web -*.tar.gz
\ No newline at end of file diff --git a/idp/README.md b/idp/README.md deleted file mode 100644 index 1372b32..0000000 --- a/idp/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# idp - -idp: IDentity Provider - -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/build.go b/idp/build.go deleted file mode 100644 index 4f11cfd..0000000 --- a/idp/build.go +++ /dev/null @@ -1,42 +0,0 @@ -//+build ignore - -// Builds and deploys the application to minipaas. -package main - -import ( - "context" - "log" - "os" - - "within.website/x/internal" - "within.website/x/internal/kahless" - "within.website/x/internal/minipaas" - "within.website/x/internal/yeet" -) - -func main() { - internal.HandleStartup() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - env := append(os.Environ(), []string{"CGO_ENABLED=0", "GOOS=linux"}...) - yeet.ShouldWork(ctx, env, yeet.WD, "go", "build", "-o=web") - yeet.ShouldWork(ctx, env, yeet.WD, "appsluggr", "-web=web") - fin, err := os.Open("slug.tar.gz") - if err != nil { - log.Fatal(err) - } - defer fin.Close() - - fname := "idp-" + yeet.DateTag + ".tar.gz" - pubURL, err := kahless.CopyFile(fname, fin) - if err != nil { - log.Fatal(err) - } - - err = minipaas.Exec("tar:from idp " + pubURL) - if err != nil { - log.Fatal(err) - } -} diff --git a/idp/doc.go b/idp/doc.go deleted file mode 100644 index 4f0a104..0000000 --- a/idp/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Command idp is a primitive identity provider based on TOTP challenge from Google Authenticator codes. -package main diff --git a/idp/idpmiddleware/_example/test.go b/idp/idpmiddleware/_example/test.go deleted file mode 100644 index b63009e..0000000 --- a/idp/idpmiddleware/_example/test.go +++ /dev/null @@ -1,29 +0,0 @@ -//+build ignore - -package main - -import ( - "flag" - "log" - "net/http" - - "within.website/x/idp/idpmiddleware" - "within.website/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/doc.go b/idp/idpmiddleware/doc.go deleted file mode 100644 index e4f06ee..0000000 --- a/idp/idpmiddleware/doc.go +++ /dev/null @@ -1,4 +0,0 @@ -// Package idpmiddleware is a simple HTTP middleware that protects routes using -// idp(1). This only allows users that the given idp(1) instance is configured. -// to allow. -package idpmiddleware diff --git a/idp/idpmiddleware/middleware.go b/idp/idpmiddleware/middleware.go deleted file mode 100644 index 35edc8c..0000000 --- a/idp/idpmiddleware/middleware.go +++ /dev/null @@ -1,168 +0,0 @@ -package idpmiddleware - -import ( - "context" - "crypto/md5" - "encoding/json" - "errors" - "fmt" - "net/http" - "net/url" - "sync" - "time" - - "github.com/pborman/uuid" - "within.website/ln" - "within.website/ln/opname" -) - -// hash is a simple wrapper around the MD5 algorithm implementation in the -// Go standard library. It takes in data and a salt and returns the hashed -// representation. -func hash(data string, salt string) string { - output := md5.Sum([]byte(data + salt)) - return fmt.Sprintf("%x", output) -} - -var ( - globalSalt string -) - -func init() { - globalSalt = hash(uuid.New(), uuid.New()) -} - -func verify(ctx context.Context, idpServer, state, code string) *http.Request { - u, err := url.Parse(idpServer) - if err != nil { - panic(err) - } - - u.Path = "/auth" - q := u.Query() - q.Set("code", code) - q.Set("state", state) - u.RawQuery = q.Encode() - - req, err := http.NewRequest(http.MethodGet, u.String(), nil) - if err != nil { - panic(err) - } - - req.Header.Set("Accept", "application/json") - req = req.WithContext(ctx) - - return req -} - -func validate(resp *http.Response) (string, error) { - result := struct { - Me string `json:"me"` - }{} - - if resp.StatusCode != 200 { - return "", fmt.Errorf("wanted status 200, got: %d", resp.StatusCode) - } - - err := json.NewDecoder(resp.Body).Decode(&result) - if err != nil { - return "", err - } - - return result.Me, nil -} - -// XeProtect sets defaults for Xe to use. -func XeProtect(selfURL string) func(next http.Handler) http.Handler { - return Protect("https://idp.christine.website", "https://christine.website/", selfURL) -} - -// Protect protects a given URL behind your given idp(1) server. -func Protect(idpServer, me, selfURL string) func(next http.Handler) http.Handler { - lock := sync.Mutex{} - codes := map[string]string{} - - 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 == "/.within/x/idpmiddleware/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("state"); codes[cd] == cd { - ctx = opname.With(ctx, "verify") - resp, err := http.DefaultClient.Do(verify(ctx, idpServer, v.Get("state"), v.Get("code"))) - if err != nil { - ln.Error(ctx, err) - http.Error(w, "nope", http.StatusInternalServerError) - return - } - - got, err := validate(resp) - if err != nil { - ln.Error(ctx, err) - http.Error(w, "not valid", http.StatusInternalServerError) - return - } - - if me != got { - ln.Error(ctx, errors.New("hacking attempt")) - http.Error(w, "...", http.StatusNotAcceptable) - return - } - - ln.Log(ctx, ln.Info("setting cookie")) - http.SetCookie(w, &http.Cookie{ - Name: "within-x-idpmiddleware", - Value: hash(me, globalSalt), - HttpOnly: true, - Expires: time.Now().Add(900 * time.Hour), - Path: "/", - SameSite: http.SameSiteLaxMode, - }) - delete(codes, cd) - - w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate") - w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT") - http.Redirect(w, r, selfURL, http.StatusTemporaryRedirect) - } - - http.Error(w, "Programmer error, maybe you have multiple instances of the IDP middleware?", http.StatusInternalServerError) - return - } - - cookie, err := r.Cookie("within-x-idpmiddleware") - if err != nil || cookie.Value != hash(me, globalSalt) { - u, err := url.Parse(idpServer) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - code := uuid.New() - lock.Lock() - codes[code] = code - lock.Unlock() - - u.Path = "/auth" - v := url.Values{} - v.Set("me", me) - v.Set("client_id", selfURL) - v.Set("redirect_uri", selfURL+".within/x/idpmiddleware/challenge") - v.Set("state", code) - v.Set("response_type", "id") - u.RawQuery = v.Encode() - - w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate") - w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT") - http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect) - return - } - - next.ServeHTTP(w, r) - }) - } -} diff --git a/idp/main.go b/idp/main.go deleted file mode 100644 index cdc01f3..0000000 --- a/idp/main.go +++ /dev/null @@ -1,290 +0,0 @@ -package main - -import ( - "encoding/json" - "flag" - "log" - "net/http" - "net/url" - "sync" - "text/template" - "time" - - "within.website/x/i18n" - "within.website/x/idp/idpmiddleware" - "within.website/x/internal" - "github.com/pborman/uuid" - "github.com/xlzd/gotp" - "within.website/ln" - "within.website/ln/ex" -) - -var ( - domain = flag.String("domain", "idp.christine.website", "domain to be hosted from") - otpSecret = flag.String("otp-secret", "", "OTP secret") - port = flag.String("port", "5484", "TCP port to listen on for HTTP") - owner = flag.String("owner", "https://christine.website/", "the me=that is required") - secretGen = flag.Int("secret-gen", 0, "generate a secret of len if set") - defaultLang = flag.String("default-language", "en_US", "default language if none is set") - gitRev = flag.String("git-rev", "", "git revision of runtime (used for dokku detection)") -) - -func main() { - internal.HandleStartup() - - if *secretGen != 0 { - log.Fatal(gotp.RandomSecret(*secretGen)) - } - - translationPath := "./translations" - if *gitRev != "" { - translationPath = "/app/translations" - } - - l := i18n.New(*defaultLang, translationPath) - - i := &idp{ - t: gotp.NewDefaultTOTP(*otpSecret), - l: l, - bearer2me: map[string]string{}, - } - - log.Println(i.t.ProvisioningUri(*domain, *domain)) - - def := idpmiddleware.XeProtect("https://" + *domain + "/")(http.DefaultServeMux) - mux := http.NewServeMux() - mux.HandleFunc("/lang", func(w http.ResponseWriter, r *http.Request) { - locales := i18n.GetLocales(r) - - json.NewEncoder(w).Encode(locales) - }) - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - tr := i.l.TranslationsForRequest(r) - fm := template.FuncMap{ - "T": tr.Value, - } - - t, err := template.New("root").Funcs(fm).Parse(rootPageTemplate) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "text/html") - err = t.Execute(w, nil) - if err != nil { - log.Printf("%v", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - }) - mux.HandleFunc("/auth", i.auth) - mux.HandleFunc("/challenge", i.challenge) - mux.Handle("/.within/", def) - mux.Handle("/debug/", def) - http.ListenAndServe(":"+*port, ex.HTTPLog(mux)) -} - -type idp struct { - t *gotp.TOTP - - l *i18n.L - sync.Mutex - bearer2me map[string]string -} - -// auth implements https://indieweb.org/authorization-endpoint#Open_Source_Authorization_Endpoints -func (i *idp) auth(w http.ResponseWriter, r *http.Request) { - err := r.ParseForm() - if err != nil { - log.Printf("idp: form error in /auth: %v", err) - return - } - - var ( - code, me, state, responseType, redirectURI, clientID string - ) - for k, v := range r.Form { - switch k { - case "state": - state = v[0] - case "me": - me = v[0] - case "response_type": - responseType = v[0] - case "redirect_uri": - redirectURI = v[0] - case "client_id": - clientID = v[0] - case "code": - code = v[0] - } - } - - if code != "" { - i.Lock() - person := i.bearer2me[code] - delete(i.bearer2me, code) - i.Unlock() - - ctx := r.Context() - ln.Log(ctx, ln.F{"state": state, "code": code, "accept": r.Header.Get("Accept"), "person": person}) - - w.Header().Set("Content-Type", r.Header.Get("Accept")) - switch r.Header.Get("Accept") { - case "application/x-www-form-urlencoded": - v := url.Values{} - v.Set("me", person) - - http.Error(w, v.Encode(), http.StatusOK) - case "application/json": - json.NewEncoder(w).Encode(struct { - Me string `json:"me"` - }{ - Me: person, - }) - } - - return - } - - if me != *owner { - http.Error(w, "Not allowed", http.StatusUnauthorized) - return - } - - tr := i.l.TranslationsForRequest(r) - fm := template.FuncMap{ - "T": tr.Value, - } - - t, err := template.New("auth").Funcs(fm).Parse(authPageTemplate) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - err = t.Execute(w, struct { - ClientID, State, Me, ResponseType, RedirectURI string - }{ - ClientID: clientID, - State: state, - Me: me, - ResponseType: responseType, - RedirectURI: redirectURI, - }) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } -} - -func (i *idp) challenge(w http.ResponseWriter, r *http.Request) { - err := r.ParseForm() - if err != nil { - log.Printf("idp: form error in /auth: %v", err) - return - } - - var ( - code, me, state, redirectURI string - ) - for k, v := range r.Form { - switch k { - case "code": - code = v[0] - case "state": - state = v[0] - case "me": - me = v[0] - case "redirect_uri": - redirectURI = v[0] - } - } - - nowCode := i.t.Now() - if code != nowCode { - http.Error(w, "Not allowed", http.StatusUnauthorized) - return - } - - bearerToken := uuid.New() - i.Lock() - i.bearer2me[bearerToken] = me - i.Unlock() - - u, err := url.Parse(redirectURI) - if err != nil { - http.Error(w, "url error", http.StatusBadRequest) - return - } - - time.Sleep(125 * time.Millisecond) - - q := u.Query() - q.Set("state", state) - q.Set("code", bearerToken) - u.RawQuery = q.Encode() - - w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate") - w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT") - http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect) -} - -const rootPageTemplate = `<html> -<head> -<link rel="stylesheet" href="https://unpkg.com/chota@0.5.2/dist/chota.min.css"> -<title>{{ T "errors.forbidden" }}</title> -<meta name=viewport content="width=400"> -<style> -:root { - --color-primary: #da1d50; /* brand color */ - --grid-maxWidth: 40rem; -} -</style> -</head> -<body id="top"> -<div class="container"> -<div class="card"> - <header> - <h4>{{ T "errors.forbidden" }}</h4> - </header> - - <p>{{ T "prose.index" }}</p> -</div> -</div> -</body> -</html>` - -const authPageTemplate = `<html> -<head> -<link rel="stylesheet" href="https://unpkg.com/chota@0.5.2/dist/chota.min.css"> -<title>{{ T "auth_title" }}</title> -<meta name=viewport content="width=400"> -<style> -:root { - --color-primary: #da1d50; /* brand color */ - --grid-maxWidth: 40rem; -} -</style> -</head> -<body id="top"> -<div class="container"> -<div class="card"> - <header> - <h4>{{ T "prose.auth" .ClientID .Me }}</h4> - </header> - <p><form action="/challenge" method="GET"> - {{ T "code" }} <br> - <input type="text" name="code" value="" autofocus><br><br> - <input type="hidden" name="me" value="{{ .Me }}"> - <input type="hidden" name="state" value="{{ .State }}"> - <input type="hidden" name="client_id" value="{{ .ClientID }}"> - <input type="hidden" name="response_type" value="{{ .ResponseType }}"> - <input type="hidden" name="redirect_uri" value="{{ .RedirectURI }}"> - <input class="button primary" type="submit" value="{{ T "submit_button" }}"> -</form></p> -</div> -</div> -</body> -</html>` diff --git a/idp/translations/en_US.json b/idp/translations/en_US.json deleted file mode 100644 index 05c11de..0000000 --- a/idp/translations/en_US.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "errors": { - "not_allowed": "Not Allowed", - "forbidden": "Forbidden", - "url_error": "URL Error" - }, - - "prose": { - "index": "This is a private identity provider supporting IndieAuth for Christine Dodrill. Unauthorized access is forbidden.", - "auth": "Log in to {0} as {1}" - }, - - "auth_title": "Auth", - "submit_button": "Submit", - "code": "Code:" -} diff --git a/idp/translations/fr_CA.json b/idp/translations/fr_CA.json deleted file mode 100644 index d2da1b3..0000000 --- a/idp/translations/fr_CA.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "errors": { - "not_allowed": "Action non autorisée", - "forbidden": "Interdit", - "url_error": "Errur d'URL" - }, - - "prose": { - "index": "Il s'agit d'un fournisseur d'identité privé pour Christine Dodrill. L'accès non autorisé est interdit.", - "auth": "Autoriser à {0} comme {1}" - }, - - "auth_title": "Autoriser", - "submit_button": "Soumettre", - "code": "Code:" -} diff --git a/idp/translations/jbo_latn.json b/idp/translations/jbo_latn.json deleted file mode 100644 index d431db6..0000000 --- a/idp/translations/jbo_latn.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "errors": { - "not_allowed": "na e'akne", - "forbidden": "narplicru", - "url_error": "urli srejvo" - }, - - "prose": { - "index": "ni'o ti samse'u fi lo nu e'ande po la .kristin.dadril. .i le pilno narcru cu na e'akne", - "auth": ".i tolna'e fa zoi .uy. {1} .uy. be zoi .uy. {0} .uy." - }, - - "auth_title": "dubju'o", - "submit_button": "benji", - "code": "selmifra" -} |
