Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): http cookies properties #5792

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,26 +878,29 @@ func (a *API) Serve(ctx context.Context) error {
// This will returns a cookie with no expiration date that should be dropped by browser when closed.
func (a *API) SetCookieSession(w http.ResponseWriter, name, value string) {
a.setCookie(w, &http.Cookie{
Name: name,
Value: value,
Name: name,
Value: value,
HttpOnly: false,
})
}

// SetCookie on given response writter, automatically add domain and path based on api config.
func (a *API) SetCookie(w http.ResponseWriter, name, value string, expires time.Time) {
func (a *API) SetCookie(w http.ResponseWriter, name, value string, expires time.Time, httpOnly bool) {
a.setCookie(w, &http.Cookie{
Name: name,
Value: value,
Expires: expires,
Name: name,
Value: value,
Expires: expires,
HttpOnly: httpOnly,
})
}

// UnsetCookie on given response writter, automatically add domain and path based on api config.
func (a *API) UnsetCookie(w http.ResponseWriter, name string) {
func (a *API) UnsetCookie(w http.ResponseWriter, name string, httpOnly bool) {
a.setCookie(w, &http.Cookie{
Name: name,
Value: "",
MaxAge: -1,
Name: name,
Value: "",
MaxAge: -1,
HttpOnly: httpOnly,
})
}

Expand All @@ -910,6 +913,12 @@ func (a *API) setCookie(w http.ResponseWriter, c *http.Cookie) {
c.Path = "/"
}
}
c.SameSite = http.SameSiteStrictMode
c.Secure = true
uiURL, _ := url.Parse(a.Config.URL.UI)
if uiURL != nil && uiURL.Hostname() != "" {
c.Domain = uiURL.Hostname()
}
http.SetCookie(w, c)
}

Expand Down
6 changes: 3 additions & 3 deletions engine/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (api *API) postAuthSigninHandler() service.Handler {
}

// Set a cookie with the jwt token
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt)
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt, true)

// Prepare http response
resp := sdk.AuthConsumerSigninResponse{
Expand All @@ -313,7 +313,7 @@ func (api *API) postAuthSignoutHandler() service.Handler {
}

// Delete the jwt cookie value
api.UnsetCookie(w, service.JWTCookieName)
api.UnsetCookie(w, service.JWTCookieName, true)

return service.WriteJSON(w, nil, http.StatusOK)
}
Expand Down Expand Up @@ -356,7 +356,7 @@ func (api *API) postAuthDetachHandler() service.Handler {

// If we just removed the current consumer, clean http cookie.
if consumer.ID == currentConsumer.ID {
api.UnsetCookie(w, service.JWTCookieName)
api.UnsetCookie(w, service.JWTCookieName, true)
}

return service.WriteJSON(w, nil, http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion engine/api/auth_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (api *API) postAuthBuiltinSigninHandler() service.Handler {
}

// Set a cookie with the jwt token
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt)
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt, true)

usr, err := user.LoadByID(ctx, tx, consumer.AuthentifiedUserID)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions engine/api/auth_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (api *API) postAuthLocalSigninHandler() service.Handler {
}

// Set a cookie with the jwt token
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt)
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt, true)

// Prepare http response
resp := sdk.AuthConsumerSigninResponse{
Expand Down Expand Up @@ -353,7 +353,7 @@ func (api *API) postAuthLocalVerifyHandler() service.Handler {
local.CleanVerifyConsumerToken(api.Cache, consumer.ID)

// Set a cookie with the jwt token
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt)
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt, true)

// Prepare http response
resp := sdk.AuthConsumerSigninResponse{
Expand Down Expand Up @@ -521,7 +521,7 @@ func (api *API) postAuthLocalResetHandler() service.Handler {
local.CleanResetConsumerToken(api.Cache, consumer.ID)

// Set a cookie with the jwt token
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt)
api.SetCookie(w, service.JWTCookieName, jwt, session.ExpireAt, true)

// Prepare http response
resp := sdk.AuthConsumerSigninResponse{
Expand Down