Skip to content

Commit

Permalink
[Backend] Token Cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfex4936 committed Feb 25, 2024
1 parent bef68fe commit 0af61a6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.env
*.sql
*.sql
fly.toml
Dockerfile
2 changes: 1 addition & 1 deletion backend/handlers/auth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func LoginHandler(c *fiber.Ctx) error {
response.Token = token

// Setting the token in a secure cookie
cookie := services.GenerateCookie(token)
cookie := services.GenerateLoginCookie(token)
c.Cookie(&cookie)

return c.JSON(response)
Expand Down
2 changes: 1 addition & 1 deletion backend/handlers/oauth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func GetGoogleCallbackHandler(conf *oauth2.Config) fiber.Handler {
clientAddr := fmt.Sprintf("%s/%s=%s", os.Getenv("CLIENT_ADRR"), os.Getenv("CLIENT_REDIRECT_ENDPOINT"), loginToken)

// Setting the token in a secure cookie
cookie := services.GenerateCookie(loginToken)
cookie := services.GenerateLoginCookie(loginToken)
c.Cookie(&cookie)
return c.Redirect(clientAddr)
}
Expand Down
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
setTokenExpirationTime()
services.AWS_REGION = os.Getenv("AWS_REGION")
services.S3_BUCKET_NAME = os.Getenv("AWS_BUCKET_NAME")
middlewares.TOKEN_COOKIE = os.Getenv("TOKEN_COOKIE")

// Initialize database connection
if err := database.Connect(); err != nil {
Expand Down
37 changes: 18 additions & 19 deletions backend/middlewares/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"chulbong-kr/database"
"database/sql"
"log"
"strings"
"time"

"github.com/gofiber/fiber/v2"
Expand All @@ -20,28 +19,28 @@ import (
// SameSite: "Lax", // or "Strict" depending on your requirements
// })

var TOKEN_COOKIE string

// AuthMiddleware checks for a valid opaque token in the Authorization header
func AuthMiddleware(c *fiber.Ctx) error {
authHeader := c.Get("Authorization")
// check for the cookie
jwtCookie := c.Cookies(TOKEN_COOKIE)
if jwtCookie == "" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "No authorization token provided"})
}
token := jwtCookie

var token string
// // Check if the Authorization header is provided
// if authHeader != "" {
// // Split the Authorization header to extract the token
// parts := strings.SplitN(authHeader, " ", 2)
// if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
// return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Authorization header format must be Bearer {token}"})
// }
// token = parts[1] // The actual token part
// } else {

// Check if the Authorization header is provided
if authHeader != "" {
// Split the Authorization header to extract the token
parts := strings.SplitN(authHeader, " ", 2)
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Authorization header format must be Bearer {token}"})
}
token = parts[1] // The actual token part
} else {
// If Authorization header is missing, check for the cookie
jwtCookie := c.Cookies("jwt")
if jwtCookie == "" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "No authorization token provided"})
}
token = jwtCookie
}
// }

query := `SELECT UserID, ExpiresAt FROM OpaqueTokens WHERE OpaqueToken = ?`
var userID int
Expand Down
5 changes: 3 additions & 2 deletions backend/services/token_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"chulbong-kr/database"
"chulbong-kr/middlewares"
"crypto/rand"
"encoding/base64"
"encoding/hex"
Expand Down Expand Up @@ -58,9 +59,9 @@ func GenerateState() string {
return base64.URLEncoding.EncodeToString(b)
}

func GenerateCookie(value string) fiber.Cookie {
func GenerateLoginCookie(value string) fiber.Cookie {
return fiber.Cookie{
Name: "jwt",
Name: middlewares.TOKEN_COOKIE,
Value: value, // The token generated for the user
Expires: time.Now().Add(24 * time.Hour), // Set the cookie to expire in 24 hours
HTTPOnly: true, // Ensure the cookie is not accessible through client-side scripts
Expand Down

0 comments on commit 0af61a6

Please sign in to comment.