first commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"ron"
|
||||
"ron-pets/internal/config"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
app *config.App
|
||||
}
|
||||
|
||||
func New(app *config.App) *Handlers {
|
||||
return &Handlers{
|
||||
app: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (hq *Handlers) HelloWorld(c *ron.Context) {
|
||||
slog.Info("Dummy info message")
|
||||
c.W.Write([]byte("hello world"))
|
||||
}
|
||||
|
||||
func (hq *Handlers) AnotherHelloWorld(c *ron.Context) {
|
||||
c.W.Write([]byte("another hello world"))
|
||||
}
|
||||
|
||||
func (hq *Handlers) HelloWorldJSON(c *ron.Context) {
|
||||
id := c.R.PathValue("id")
|
||||
slog.Info("path value", "id", id)
|
||||
|
||||
c.JSON(200, ron.Data{"message": "hello world"})
|
||||
}
|
||||
|
||||
func (hq *Handlers) HelloWorldHTML(c *ron.Context) {
|
||||
|
||||
//pages := ron.Pages{
|
||||
// TotalElements: len(elements),
|
||||
// ElementsPerPage: 5,
|
||||
//}
|
||||
//
|
||||
//pages.PaginationParams(c.R)
|
||||
//elementsPaginated := pages.PaginateArray(elements)
|
||||
//
|
||||
//td := &ron.TemplateData{
|
||||
// Data: ron.Data{"title": "hello world", "message": "hello world from html", "elements": elementsPaginated},
|
||||
// Pages: pages,
|
||||
//}
|
||||
//
|
||||
//c.HTML(200, "page.index.gohtml", td)
|
||||
}
|
||||
|
||||
func (hq *Handlers) ComponentHTML(c *ron.Context) {
|
||||
c.HTML(200, "component.list.gohtml", nil)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"aidanwoods.dev/go-paseto"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"ron"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserPayload struct {
|
||||
User string `json:"user"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
func (hq *Handlers) CreateToken(c *ron.Context) {
|
||||
token := paseto.NewToken()
|
||||
token.Set("userPayload", UserPayload{User: "pedro", Role: "admin"})
|
||||
token.SetExpiration(time.Now().Add(hq.app.Security.Duration))
|
||||
signed := token.V4Sign(hq.app.Security.AsymmetricKey, nil)
|
||||
|
||||
cookie := http.Cookie{
|
||||
Name: "token",
|
||||
Value: signed,
|
||||
Path: "/",
|
||||
MaxAge: 3600,
|
||||
HttpOnly: true,
|
||||
Secure: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
}
|
||||
|
||||
http.SetCookie(c.W, &cookie)
|
||||
|
||||
c.JSON(http.StatusOK, ron.Data{"token": signed})
|
||||
}
|
||||
|
||||
func (hq *Handlers) ValidateTokenAuthorization(c *ron.Context) {
|
||||
signed := c.R.Header.Get("Authorization")
|
||||
split := strings.Split(signed, "Bearer ")
|
||||
slog.Info("signed", "signed", split[1])
|
||||
parser := paseto.NewParser()
|
||||
token, err := parser.ParseV4Public(hq.app.Security.PublicKey, split[1], nil)
|
||||
if err != nil {
|
||||
slog.Error("error", "err", err)
|
||||
c.JSON(http.StatusUnauthorized, ron.Data{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var userPayload UserPayload
|
||||
token.Get("userPayload", &userPayload)
|
||||
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
"authorized": true,
|
||||
"payload": userPayload,
|
||||
})
|
||||
}
|
||||
|
||||
func (hq *Handlers) ValidateTokenCookie(c *ron.Context) {
|
||||
cookie, err := c.R.Cookie("token")
|
||||
if err != nil {
|
||||
slog.Error("error", "err", err)
|
||||
c.JSON(http.StatusUnauthorized, ron.Data{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
parser := paseto.NewParser()
|
||||
token, err := parser.ParseV4Public(hq.app.Security.PublicKey, cookie.Value, nil)
|
||||
if err != nil {
|
||||
slog.Error("error", "err", err)
|
||||
c.JSON(http.StatusUnauthorized, ron.Data{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var userPayload UserPayload
|
||||
token.Get("userPayload", &userPayload)
|
||||
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
"authorized": true,
|
||||
"payload": userPayload,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user