Skip to content

Commit

Permalink
📝 feat: auth updates for react native
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jun 13, 2024
1 parent 2dd0789 commit b0f280a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 64 deletions.
20 changes: 1 addition & 19 deletions backend/entities/auth/base/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"log/slog"
"net/http"
"net/url"
"time"

"github.com/GenerateNU/sac/backend/integrations/oauth/soth"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
Expand Down Expand Up @@ -72,17 +70,6 @@ func (h *Handler) Provider(c *fiber.Ctx) error {
}

func (h *Handler) ProviderCallback(c *fiber.Ctx) error {
defer func() {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: "",
Expires: time.Now().Add(-1 * time.Hour), // expire the cookie immediately
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})
}()

gfUser, err := sothic.CompleteUserAuth(c)
if err != nil {
return err
Expand All @@ -102,12 +89,7 @@ func (h *Handler) ProviderCallback(c *fiber.Ctx) error {
return err
}

redirect, err := url.PathUnescape(c.Cookies("redirect", "/"))
if err != nil {
return err
}

return c.Redirect(redirect)
return c.SendStatus(http.StatusOK)
}

func (h *Handler) ProviderLogout(c *fiber.Ctx) error {
Expand Down
4 changes: 3 additions & 1 deletion backend/integrations/oauth/soth/sothic/sothic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strings"

Expand Down Expand Up @@ -72,7 +73,8 @@ func BeginAuthHandler(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}

return c.Redirect(url, fiber.StatusTemporaryRedirect)
c.Set("redirect", url)
return c.SendStatus(http.StatusOK)
}

// SetState sets the state string associated with the given request.
Expand Down
15 changes: 3 additions & 12 deletions backend/middleware/auth/authorize.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package auth

import (
"net/url"
"net/http"
"slices"
"time"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
Expand All @@ -18,16 +17,8 @@ func (m *AuthMiddlewareHandler) Authorize(requiredPermissions ...permission.Perm
return func(c *fiber.Ctx) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: url.PathEscape(c.OriginalURL()),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})

return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down
13 changes: 3 additions & 10 deletions backend/middleware/auth/club.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package auth

import (
"net/http"
"slices"
"time"

"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -16,15 +16,8 @@ import (
func (m *AuthMiddlewareHandler) ClubAuthorizeById(c *fiber.Ctx, extractor ExtractID) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: c.OriginalURL(),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})
return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down
14 changes: 3 additions & 11 deletions backend/middleware/auth/event.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package auth

import (
"net/http"
"slices"
"time"

"github.com/GenerateNU/sac/backend/entities/events"
"github.com/GenerateNU/sac/backend/entities/models"
Expand All @@ -17,16 +17,8 @@ import (
func (m *AuthMiddlewareHandler) EventAuthorizeById(c *fiber.Ctx, extractor ExtractID) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: c.OriginalURL(),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})

return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down
14 changes: 3 additions & 11 deletions backend/middleware/auth/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
"time"
"net/http"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
Expand All @@ -13,16 +13,8 @@ import (
func (m *AuthMiddlewareHandler) UserAuthorizeById(c *fiber.Ctx) error {
strUser, err := sothic.GetFromSession("user", c)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "redirect",
Value: c.OriginalURL(),
Expires: time.Now().Add(5 * time.Minute),
// MARK: secure should be true in prod
// use go build tags to do this
HTTPOnly: true,
})

return c.Redirect("/api/v1/auth/login")
c.Set("redirect", "/api/v1/auth/login")
return c.SendStatus(http.StatusUnauthorized)
}

user := models.UnmarshalUser(strUser)
Expand Down

0 comments on commit b0f280a

Please sign in to comment.