-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48ec0b1
commit e1d0e70
Showing
9 changed files
with
175 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package handlers | ||
|
||
import ( | ||
"html/template" | ||
"io/fs" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/justinas/nosurf" | ||
"jirku.sk/mcmamina/pkg/middleware" | ||
) | ||
|
||
type AdminHandlers struct { | ||
CssPathGetter CSSPathGetter | ||
Recaptcha RecaptchaValidator | ||
Log *slog.Logger | ||
loginTmpl *template.Template | ||
} | ||
|
||
func (h *AdminHandlers) InitTmpl(tmpl *template.Template, file fs.FS) *AdminHandlers { | ||
var err error | ||
h.loginTmpl, err = getTmpl(tmpl, "admin.tmpl", file) | ||
if err != nil { | ||
h.Log.Error("cloning template: %w", err) | ||
} | ||
return h | ||
} | ||
|
||
func (h *AdminHandlers) DashboardGet(w http.ResponseWriter, r *http.Request) { | ||
model := createModel("Prihlásenie", "/login", "activities", h.CssPathGetter) | ||
model["csrfTokenField"] = nosurf.FormFieldName | ||
model["csrfToken"] = nosurf.Token(r) | ||
model["user"] = middleware.GetUser(r) | ||
model["recaptchaKey"] = h.Recaptcha.Key() | ||
|
||
if err := h.loginTmpl.ExecuteTemplate(w, "page", model); err != nil { | ||
h.Log.Error("page executing context", err) | ||
http.Redirect(w, r, "/error", http.StatusInternalServerError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/gorilla/sessions" | ||
"jirku.sk/mcmamina/pkg/models" | ||
) | ||
|
||
const SessionName = "session" | ||
|
||
type UserCookie int | ||
|
||
const UserCookieKey UserCookie = 0 | ||
|
||
func AuthMiddleware(store sessions.Store) mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
session, err := store.Get(r, SessionName) | ||
if err != nil || session.Values["user"] != nil { | ||
if user, ok := session.Values["user"].(models.UserLogin); ok { | ||
ctx = context.WithValue(ctx, UserCookieKey, user) | ||
} | ||
} | ||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} | ||
} | ||
|
||
func AuthorizeMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
user := GetUser(r) | ||
if user == nil { | ||
// TODO: handle redirect back to original page | ||
http.Redirect(w, r, "/prihlasenie", http.StatusFound) | ||
} else { | ||
next.ServeHTTP(w, r) | ||
} | ||
}) | ||
} | ||
|
||
func StoreUser(w http.ResponseWriter, r *http.Request, user *models.UserLogin, store sessions.Store) error { | ||
session := sessions.NewSession(store, SessionName) | ||
if user == nil { | ||
session.Values["user"] = nil | ||
} else { | ||
session.Values["user"] = &user | ||
} | ||
|
||
session.Options.MaxAge = 60 * 60 * 24 * 10 | ||
session.Options.HttpOnly = true | ||
session.Options.Path = "/" | ||
session.Save(r, w) | ||
return nil | ||
} | ||
|
||
func GetUser(r *http.Request) *models.UserLogin { | ||
result := r.Context().Value(UserCookieKey) | ||
if result == nil { | ||
return nil | ||
} else if result, ok := result.(models.UserLogin); ok { | ||
return &result | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
type UserLogin struct { | ||
Sub string `json:"sub"` | ||
Email string `json:"email"` | ||
Name string `json:"name"` | ||
GivenName string `json:"given_name"` | ||
FamilyName string `json:"family_name"` | ||
Picture string `json:"picture"` | ||
EmailVerified bool `json:"email_verified"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{- define "content" -}} | ||
{{- template "layout.start" .}} | ||
|
||
{{- template "com_cards.fullwidth.start" dict "m" "mb-0 mt-12"}} | ||
{{- template "com_cards.cardcontent.start" "flex flex-col md:flex-row gap-3 md:gap-4 lg:gap-10 justify-center"}} | ||
|
||
<h1 class="text-3xl">Dobrý deň, {{.user.Name}}!</h1> | ||
|
||
{{- template "com_cards.cardcontent.end" -}} | ||
{{- template "com_cards.fullwidth.end" -}} | ||
|
||
{{- template "layout.end" .}} | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters