IndieAuth #25

Merged
h4kor merged 19 commits from auth into master 2022-11-07 19:38:21 +00:00
7 changed files with 57 additions and 0 deletions
Showing only changes of commit 1072f48e9f - Show all commits

View File

@ -59,6 +59,26 @@ func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Requ
} }
} }
func userAuthHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user, err := getUserFromRepo(repo, ps)
if err != nil {
println("Error getting user: ", err.Error())
notFoundHandler(repo)(w, r)
return
}
html, err := owl.RenderUserAuthPage(user)
if err != nil {
println("Error rendering auth page: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
println("Rendering auth page for user", user.Name())
w.Write([]byte(html))
}
}
func userWebmentionHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { func userWebmentionHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user, err := getUserFromRepo(repo, ps) user, err := getUserFromRepo(repo, ps)

View File

@ -14,6 +14,7 @@ func Router(repo *owl.Repository) http.Handler {
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir())) router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", repoIndexHandler(repo)) router.GET("/", repoIndexHandler(repo))
router.GET("/user/:user/", userIndexHandler(repo)) router.GET("/user/:user/", userIndexHandler(repo))
router.GET("/user/:user/auth/", userAuthHandler(repo))
router.GET("/user/:user/media/*filepath", userMediaHandler(repo)) router.GET("/user/:user/media/*filepath", userMediaHandler(repo))
router.GET("/user/:user/index.xml", userRSSHandler(repo)) router.GET("/user/:user/index.xml", userRSSHandler(repo))
router.GET("/user/:user/posts/:post/", postHandler(repo)) router.GET("/user/:user/posts/:post/", postHandler(repo))
@ -27,6 +28,7 @@ func SingleUserRouter(repo *owl.Repository) http.Handler {
router := httprouter.New() router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir())) router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", userIndexHandler(repo)) router.GET("/", userIndexHandler(repo))
router.GET("/auth/", userAuthHandler(repo))
router.GET("/media/*filepath", userMediaHandler(repo)) router.GET("/media/*filepath", userMediaHandler(repo))
router.GET("/index.xml", userRSSHandler(repo)) router.GET("/index.xml", userRSSHandler(repo))
router.GET("/posts/:post/", postHandler(repo)) router.GET("/posts/:post/", postHandler(repo))

5
embed/auth.html Normal file
View File

@ -0,0 +1,5 @@
<form action="" method="post">
<label for="password">Password</label>
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Login">
</form>

View File

@ -27,6 +27,9 @@
<link rel="stylesheet" href="/static/pico.min.css"> <link rel="stylesheet" href="/static/pico.min.css">
<link rel="webmention" href="{{ .User.WebmentionUrl }}"> <link rel="webmention" href="{{ .User.WebmentionUrl }}">
{{ if .User.AuthUrl }}
<link rel="authorization_endpoint" href="{{ .User.AuthUrl}}">
{{ end }}
<style> <style>
header { header {
background-color: {{.User.Config.HeaderColor}}; background-color: {{.User.Config.HeaderColor}};

View File

@ -107,7 +107,18 @@ func RenderIndexPage(user User) (string, error) {
Title: "Index", Title: "Index",
Content: template.HTML(postHtml), Content: template.HTML(postHtml),
}) })
}
func RenderUserAuthPage(user User) (string, error) {
authHtml, err := renderEmbedTemplate("embed/auth.html", user)
if err != nil {
return "", err
}
return renderIntoBaseTemplate(user, PageContent{
Title: "Auth",
Content: template.HTML(authHtml),
})
} }
func RenderUserList(repo Repository) (string, error) { func RenderUserList(repo Repository) (string, error) {

View File

@ -285,3 +285,11 @@ func TestAddFaviconIfExist(t *testing.T) {
result, _ := owl.RenderIndexPage(user) result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "favicon.png") assertions.AssertContains(t, result, "favicon.png")
} }
func TestRenderUserAuth(t *testing.T) {
user := getTestUser()
user.ResetPassword("test")
result, err := owl.RenderUserAuthPage(user)
assertions.AssertNoError(t, err, "Error rendering user auth page")
assertions.AssertContains(t, result, "<form")
}

View File

@ -44,6 +44,14 @@ func (user User) FullUrl() string {
return url return url
} }
func (user User) AuthUrl() string {
if user.Config().PassworHash == "" {
return ""
}
url, _ := url.JoinPath(user.FullUrl(), "auth/")
return url
}
func (user User) WebmentionUrl() string { func (user User) WebmentionUrl() string {
url, _ := url.JoinPath(user.FullUrl(), "webmention/") url, _ := url.JoinPath(user.FullUrl(), "webmention/")
return url return url