Skip to content

Commit 221009b

Browse files
authored
Merge pull request #229 from ruessej/main
feat: Add a option to disable httpOnly cookies
2 parents 21b70e4 + 6085c2d commit 221009b

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

server/constants/env.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ const (
4949
EnvKeySenderEmail = "SENDER_EMAIL"
5050
// EnvKeyIsEmailServiceEnabled key for env variable IS_EMAIL_SERVICE_ENABLED
5151
EnvKeyIsEmailServiceEnabled = "IS_EMAIL_SERVICE_ENABLED"
52+
// EnvKeyAppCookieSecure key for env variable APP_COOKIE_SECURE
53+
EnvKeyAppCookieSecure = "APP_COOKIE_SECURE"
54+
// EnvKeyAdminCookieSecure key for env variable ADMIN_COOKIE_SECURE
55+
EnvKeyAdminCookieSecure = "ADMIN_COOKIE_SECURE"
5256
// EnvKeyJwtType key for env variable JWT_TYPE
5357
EnvKeyJwtType = "JWT_TYPE"
5458
// EnvKeyJwtSecret key for env variable JWT_SECRET

server/cookie/admin_cookie.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@ package cookie
33
import (
44
"net/url"
55

6+
log "github.com/sirupsen/logrus"
7+
68
"github.com/authorizerdev/authorizer/server/constants"
9+
"github.com/authorizerdev/authorizer/server/memorystore"
710
"github.com/authorizerdev/authorizer/server/parsers"
811
"github.com/gin-gonic/gin"
912
)
1013

1114
// SetAdminCookie sets the admin cookie in the response
1215
func SetAdminCookie(gc *gin.Context, token string) {
13-
secure := true
14-
httpOnly := true
16+
adminCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAdminCookieSecure)
17+
if err != nil {
18+
log.Debug("Error while getting admin cookie secure from env variable: %v", err)
19+
adminCookieSecure = true
20+
}
21+
22+
secure := adminCookieSecure
23+
httpOnly := adminCookieSecure
1524
hostname := parsers.GetHost(gc)
1625
host, _ := parsers.GetHostParts(hostname)
1726
gc.SetCookie(constants.AdminCookieName, token, 3600, "/", host, secure, httpOnly)
@@ -35,8 +44,14 @@ func GetAdminCookie(gc *gin.Context) (string, error) {
3544

3645
// DeleteAdminCookie sets the response cookie to empty
3746
func DeleteAdminCookie(gc *gin.Context) {
38-
secure := true
39-
httpOnly := true
47+
adminCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAdminCookieSecure)
48+
if err != nil {
49+
log.Debug("Error while getting admin cookie secure from env variable: %v", err)
50+
adminCookieSecure = true
51+
}
52+
53+
secure := adminCookieSecure
54+
httpOnly := adminCookieSecure
4055
hostname := parsers.GetHost(gc)
4156
host, _ := parsers.GetHostParts(hostname)
4257
gc.SetCookie(constants.AdminCookieName, "", -1, "/", host, secure, httpOnly)

server/cookie/cookie.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ import (
44
"net/http"
55
"net/url"
66

7+
log "github.com/sirupsen/logrus"
8+
79
"github.com/authorizerdev/authorizer/server/constants"
10+
"github.com/authorizerdev/authorizer/server/memorystore"
811
"github.com/authorizerdev/authorizer/server/parsers"
912
"github.com/gin-gonic/gin"
1013
)
1114

1215
// SetSession sets the session cookie in the response
1316
func SetSession(gc *gin.Context, sessionID string) {
14-
secure := true
15-
httpOnly := true
17+
appCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAppCookieSecure)
18+
if err != nil {
19+
log.Debug("Error while getting app cookie secure from env variable: %v", err)
20+
appCookieSecure = true
21+
}
22+
23+
secure := appCookieSecure
24+
httpOnly := appCookieSecure
1625
hostname := parsers.GetHost(gc)
1726
host, _ := parsers.GetHostParts(hostname)
1827
domain := parsers.GetDomainName(hostname)
@@ -30,8 +39,14 @@ func SetSession(gc *gin.Context, sessionID string) {
3039

3140
// DeleteSession sets session cookies to expire
3241
func DeleteSession(gc *gin.Context) {
33-
secure := true
34-
httpOnly := true
42+
appCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAppCookieSecure)
43+
if err != nil {
44+
log.Debug("Error while getting app cookie secure from env variable: %v", err)
45+
appCookieSecure = true
46+
}
47+
48+
secure := appCookieSecure
49+
httpOnly := appCookieSecure
3550
hostname := parsers.GetHost(gc)
3651
host, _ := parsers.GetHostParts(hostname)
3752
domain := parsers.GetDomainName(hostname)

server/env/env.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func InitAllEnv() error {
7979
osOrganizationLogo := os.Getenv(constants.EnvKeyOrganizationLogo)
8080

8181
// os bool vars
82+
osAppCookieSecure := os.Getenv(constants.EnvKeyAppCookieSecure)
83+
osAdminCookieSecure := os.Getenv(constants.EnvKeyAdminCookieSecure)
8284
osDisableBasicAuthentication := os.Getenv(constants.EnvKeyDisableBasicAuthentication)
8385
osDisableEmailVerification := os.Getenv(constants.EnvKeyDisableEmailVerification)
8486
osDisableMagicLinkLogin := os.Getenv(constants.EnvKeyDisableMagicLinkLogin)
@@ -417,6 +419,40 @@ func InitAllEnv() error {
417419
envData[constants.EnvKeyOrganizationLogo] = osOrganizationLogo
418420
}
419421

422+
if _, ok := envData[constants.EnvKeyAppCookieSecure]; !ok {
423+
if osAppCookieSecure == "" {
424+
envData[constants.EnvKeyAppCookieSecure] = true
425+
} else {
426+
envData[constants.EnvKeyAppCookieSecure] = osAppCookieSecure == "true"
427+
}
428+
}
429+
if osAppCookieSecure != "" {
430+
boolValue, err := strconv.ParseBool(osAppCookieSecure)
431+
if err != nil {
432+
return err
433+
}
434+
if boolValue != envData[constants.EnvKeyAppCookieSecure].(bool) {
435+
envData[constants.EnvKeyAppCookieSecure] = boolValue
436+
}
437+
}
438+
439+
if _, ok := envData[constants.EnvKeyAdminCookieSecure]; !ok {
440+
if osAdminCookieSecure == "" {
441+
envData[constants.EnvKeyAdminCookieSecure] = true
442+
} else {
443+
envData[constants.EnvKeyAdminCookieSecure] = osAdminCookieSecure == "true"
444+
}
445+
}
446+
if osAdminCookieSecure != "" {
447+
boolValue, err := strconv.ParseBool(osAdminCookieSecure)
448+
if err != nil {
449+
return err
450+
}
451+
if boolValue != envData[constants.EnvKeyAdminCookieSecure].(bool) {
452+
envData[constants.EnvKeyAdminCookieSecure] = boolValue
453+
}
454+
}
455+
420456
if _, ok := envData[constants.EnvKeyDisableBasicAuthentication]; !ok {
421457
envData[constants.EnvKeyDisableBasicAuthentication] = osDisableBasicAuthentication == "true"
422458
}

0 commit comments

Comments
 (0)