Skip to content

Commit eed1e1d

Browse files
committed
make cookie and session keys to const
1 parent 9795bd3 commit eed1e1d

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

internal/cookie/keys.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package cookie
2+
3+
const SessionCookieKeyName = "session"
4+
const SecureCookieSessionKeyName = "session"

internal/scookie/scookie.go renamed to internal/cookie/securecookie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package scookie
1+
package cookie
22

33
import (
44
"sandbox-go-api-sqlboiler-rest-auth/internal/config"

internal/handlers/session.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handlers
22

33
import (
44
"net/http"
5+
"sandbox-go-api-sqlboiler-rest-auth/internal/cookie"
56
"sandbox-go-api-sqlboiler-rest-auth/internal/middleware"
67
"sandbox-go-api-sqlboiler-rest-auth/models"
78
"strconv"
@@ -41,7 +42,10 @@ func CreateSession(c echo.Context) error {
4142
return echo.NewHTTPError(http.StatusBadRequest)
4243
}
4344

44-
expirationDays := strconv.Atoi(cc.Config.SessionExpirationDays)
45+
expirationDays, err := strconv.Atoi(cc.Config.SessionExpirationDays)
46+
if err != nil {
47+
return echo.NewHTTPError(http.StatusInternalServerError)
48+
}
4549

4650
var s models.Session
4751
var uid, _ = uuid.NewUUID()
@@ -53,21 +57,21 @@ func CreateSession(c echo.Context) error {
5357
return echo.NewHTTPError(http.StatusInternalServerError)
5458
}
5559

56-
encoded, err := cc.SecureCookie.Encode("session", s.ID)
60+
encoded, err := cc.SecureCookie.Encode(cookie.SecureCookieSessionKeyName, s.ID)
5761
if err != nil {
5862
return echo.NewHTTPError(http.StatusInternalServerError)
5963
}
6064

61-
cookie := &http.Cookie{
62-
Name: "session",
65+
ck := &http.Cookie{
66+
Name: cookie.SessionCookieKeyName,
6367
Value: encoded,
6468
Path: "/",
6569
Expires: time.Now().Add(time.Hour * 24 * time.Duration(expirationDays)),
6670
Secure: !cc.Config.IsDev,
6771
HttpOnly: true,
6872
SameSite: 2,
6973
}
70-
c.SetCookie(cookie)
74+
c.SetCookie(ck)
7175
return c.NoContent(http.StatusNoContent)
7276
}
7377

internal/middleware/middleware.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"fmt"
66
"sandbox-go-api-sqlboiler-rest-auth/internal/config"
7+
"sandbox-go-api-sqlboiler-rest-auth/internal/cookie"
78
"sandbox-go-api-sqlboiler-rest-auth/models"
89
"time"
910

@@ -28,13 +29,13 @@ func SessionRestorer(db *sql.DB, logger *zap.SugaredLogger, sc *securecookie.Sec
2829
return func(next echo.HandlerFunc) echo.HandlerFunc {
2930
return func(c echo.Context) error {
3031
cc := c.(*CustomContext)
31-
cv, err := c.Cookie("session")
32+
cv, err := c.Cookie(cookie.SessionCookieKeyName)
3233
if err != nil {
3334
return next(c)
3435
}
3536

3637
var dv string
37-
err = sc.Decode("session", cv.Value, &dv)
38+
err = sc.Decode(cookie.SecureCookieSessionKeyName, cv.Value, &dv)
3839
if err != nil {
3940
return echo.NewHTTPError(500, "cannot decode cookie", err)
4041
}

internal/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"io/ioutil"
77
"log"
88
"sandbox-go-api-sqlboiler-rest-auth/internal/config"
9-
"sandbox-go-api-sqlboiler-rest-auth/internal/scookie"
9+
"sandbox-go-api-sqlboiler-rest-auth/internal/cookie"
1010

1111
"github.com/labstack/echo/v4"
1212
"go.uber.org/zap"
1313
)
1414

1515
func NewServer(cfg *config.Config, db *sql.DB, l *zap.Logger) *echo.Echo {
16-
sc := scookie.NewSecureCookie(cfg)
16+
sc := cookie.NewSecureCookie(cfg)
1717
e := echo.New()
1818
e.HideBanner = true
1919
e.Debug = cfg.IsDev

0 commit comments

Comments
 (0)