Skip to content

Commit

Permalink
auth improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bueti committed Nov 13, 2023
1 parent 2e30582 commit ea77108
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func main() {

sessionManager := scs.New()
sessionManager.Store = postgresstore.New(dbd)
sessionManager.Lifetime = 7 * 24 * time.Hour
sessionManager.Lifetime = 14 * 24 * time.Hour

app := &application{
sessionManager: sessionManager,
Expand Down
75 changes: 42 additions & 33 deletions cmd/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,57 @@ import (

func (app *application) authenticate(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if app.isAuthenticated(c) {
return next(c)
if c.Request().Header.Get(echo.HeaderContentType) == echo.MIMEApplicationJSON {
return app.jsonAuthenticate(c, next)
}
authorizationHeader := c.Request().Header.Get("Authorization")
if authorizationHeader == "" {
return c.JSON(http.StatusUnauthorized, "Unauthorized")
if !app.isAuthenticated(c) {
return c.Render(http.StatusUnauthorized, "login.tmpl.html", app.newTemplateData(c))
}
c.Request().Header.Set("Cache-Control", "no-store")
return next(c)

headerParts := strings.Split(authorizationHeader, " ")
if len(headerParts) != 2 || headerParts[0] != "Bearer" {
return c.JSON(http.StatusBadRequest, "Bad Request")
}
}
}

token := headerParts[1]
claims, err := jwt.HMACCheck([]byte(token), []byte(app.config.signingKey))
if err != nil {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
if !claims.Valid(time.Now()) {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
if claims.Issuer != "shrink.ch" {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
if !claims.AcceptAudience("shrink.ch") {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
func (app *application) jsonAuthenticate(c echo.Context, next echo.HandlerFunc) error {
authorizationHeader := c.Request().Header.Get("Authorization")
if authorizationHeader == "" {
return c.JSON(http.StatusUnauthorized, "Unauthorized")
}

userID, err := uuid.Parse(claims.Subject)
if err != nil {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
headerParts := strings.Split(authorizationHeader, " ")
if len(headerParts) != 2 || headerParts[0] != "Bearer" {
return c.JSON(http.StatusBadRequest, "Bad Request")
}

user, err := app.models.Users.GetByID(userID)
if err != nil {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
token := headerParts[1]
claims, err := jwt.HMACCheck([]byte(token), []byte(app.config.signingKey))
if err != nil {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
if !claims.Valid(time.Now()) {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
if claims.Issuer != "shrink.ch" {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}
if !claims.AcceptAudience("shrink.ch") {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}

c.Set("user", user)
userID, err := uuid.Parse(claims.Subject)
if err != nil {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}

return next(c)
user, err := app.models.Users.GetByID(userID)
if err != nil {
return c.JSON(http.StatusBadRequest, "Invalid Token")
}

c.Set("user", user)

return next(c)
}

func (app *application) requireRole(role string) echo.MiddlewareFunc {
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (app *application) deleteUrlHandler(c echo.Context) error {
urlUUID, err := uuid.Parse(c.Param("id"))
if err != nil {
app.sessionManager.Put(c.Request().Context(), "flash_error", "Bad Request?!")
return c.Render(http.StatusBadRequest, "dashboard.tmpl.html", app.newTemplateData(c))
return app.dashboardHandler(c)
}

err = app.models.Urls.Delete(urlUUID)
Expand Down
3 changes: 1 addition & 2 deletions ui/html/pages/dashboard.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ <h3 class="mt-8 text-xl font-bold text-gray-900">Your URLs</h3>
<tbody>
<tr>
<td class="px-4 py-2">
<a href="{{ printf " %.25s" .Original }}" class="text-indigo-600 hover:underline">{{ .Original
}}</a>
<a href="{{ printf " %.25s" .Original }}" class="text-indigo-600 hover:underline">{{ .Original }}</a>
</td>
<td class="px-4 py-2">
<a href="{{ .ShortUrl }}" class="text-indigo-600 hover:underline">{{ .ShortUrl }}</a>
Expand Down

0 comments on commit ea77108

Please sign in to comment.