Skip to content

Commit 094498a

Browse files
committed
set blank cookie on logout always
1 parent 994a294 commit 094498a

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

coderd/users.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
8585
// TODO: @emyrk this currently happens outside the database tx used to create
8686
// the user. Maybe I add this ability to grant roles in the createUser api
8787
// and add some rbac bypass when calling api functions this way??
88-
// Add the admin role to this first user
88+
// Add the admin role to this first user.
8989
_, err = api.Database.UpdateUserRoles(r.Context(), database.UpdateUserRolesParams{
9090
GrantedRoles: []string{rbac.RoleAdmin(), rbac.RoleMember()},
9191
ID: user.ID,
@@ -109,7 +109,7 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
109109
statusFilter = r.URL.Query().Get("status")
110110
)
111111

112-
// Reading all users across the site
112+
// Reading all users across the site.
113113
if !api.Authorize(rw, r, rbac.ActionRead, rbac.ResourceUser) {
114114
return
115115
}
@@ -162,7 +162,7 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
162162

163163
// Creates a new user.
164164
func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
165-
// Create the user on the site
165+
// Create the user on the site.
166166
if !api.Authorize(rw, r, rbac.ActionCreate, rbac.ResourceUser) {
167167
return
168168
}
@@ -408,11 +408,11 @@ func (api *API) userRoles(rw http.ResponseWriter, r *http.Request) {
408408
return
409409
}
410410

411-
// Only include ones we can read from RBAC
411+
// Only include ones we can read from RBAC.
412412
memberships = AuthorizeFilter(api, r, rbac.ActionRead, memberships)
413413

414414
for _, mem := range memberships {
415-
// If we can read the org member, include the roles
415+
// If we can read the org member, include the roles.
416416
if err == nil {
417417
resp.OrganizationRoles[mem.OrganizationID] = mem.Roles
418418
}
@@ -422,7 +422,7 @@ func (api *API) userRoles(rw http.ResponseWriter, r *http.Request) {
422422
}
423423

424424
func (api *API) putUserRoles(rw http.ResponseWriter, r *http.Request) {
425-
// User is the user to modify
425+
// User is the user to modify.
426426
user := httpmw.UserParam(r)
427427
roles := httpmw.UserRoles(r)
428428

@@ -470,7 +470,7 @@ func (api *API) putUserRoles(rw http.ResponseWriter, r *http.Request) {
470470
// updateSiteUserRoles will ensure only site wide roles are passed in as arguments.
471471
// If an organization role is included, an error is returned.
472472
func (api *API) updateSiteUserRoles(ctx context.Context, args database.UpdateUserRolesParams) (database.User, error) {
473-
// Enforce only site wide roles
473+
// Enforce only site wide roles.
474474
for _, r := range args.GrantedRoles {
475475
if _, ok := rbac.IsOrgRole(r); ok {
476476
return database.User{}, xerrors.Errorf("must only update site wide roles")
@@ -504,7 +504,7 @@ func (api *API) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
504504
return
505505
}
506506

507-
// Only return orgs the user can read
507+
// Only return orgs the user can read.
508508
organizations = AuthorizeFilter(api, r, rbac.ActionRead, organizations)
509509

510510
publicOrganizations := make([]codersdk.Organization, 0, len(organizations))
@@ -584,7 +584,7 @@ func (api *API) postOrganizationsByUser(rw http.ResponseWriter, r *http.Request)
584584
CreatedAt: database.Now(),
585585
UpdatedAt: database.Now(),
586586
Roles: []string{
587-
// Also assign member role incase they get demoted from admin
587+
// Also assign member role incase they get demoted from admin.
588588
rbac.RoleOrgMember(organization.ID),
589589
rbac.RoleOrgAdmin(organization.ID),
590590
},
@@ -650,7 +650,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
650650
})
651651
}
652652

653-
// Creates a new session key, used for logging in via the CLI
653+
// Creates a new session key, used for logging in via the CLI.
654654
func (api *API) postAPIKey(rw http.ResponseWriter, r *http.Request) {
655655
user := httpmw.UserParam(r)
656656

@@ -669,9 +669,19 @@ func (api *API) postAPIKey(rw http.ResponseWriter, r *http.Request) {
669669
httpapi.Write(rw, http.StatusCreated, codersdk.GenerateAPIKeyResponse{Key: sessionToken})
670670
}
671671

672-
// Clear the user's session cookie
672+
// Clear the user's session cookie.
673673
func (api *API) postLogout(rw http.ResponseWriter, r *http.Request) {
674-
// Delete the session token from database
674+
// Get a blank token cookie.
675+
cookie := &http.Cookie{
676+
// MaxAge < 0 means to delete the cookie now.
677+
MaxAge: -1,
678+
Name: httpmw.SessionTokenKey,
679+
Path: "/",
680+
}
681+
682+
http.SetCookie(rw, cookie)
683+
684+
// Delete the session token from database.
675685
apiKey := httpmw.APIKey(r)
676686
err := api.Database.DeleteAPIKeyByID(r.Context(), apiKey.ID)
677687
if err != nil {
@@ -681,15 +691,6 @@ func (api *API) postLogout(rw http.ResponseWriter, r *http.Request) {
681691
return
682692
}
683693

684-
// Get a blank token cookie
685-
cookie := &http.Cookie{
686-
// MaxAge < 0 means to delete the cookie now
687-
MaxAge: -1,
688-
Name: httpmw.SessionTokenKey,
689-
Path: "/",
690-
}
691-
692-
http.SetCookie(rw, cookie)
693694
httpapi.Write(rw, http.StatusOK, httpapi.Response{
694695
Message: "Logged out!",
695696
})
@@ -771,7 +772,7 @@ func (api *API) createUser(ctx context.Context, req codersdk.CreateUserRequest)
771772
req.OrganizationID = organization.ID
772773
orgRoles = append(orgRoles, rbac.RoleOrgAdmin(req.OrganizationID))
773774
}
774-
// Always also be a member
775+
// Always also be a member.
775776
orgRoles = append(orgRoles, rbac.RoleOrgMember(req.OrganizationID))
776777

777778
params := database.InsertUserParams{
@@ -817,7 +818,7 @@ func (api *API) createUser(ctx context.Context, req codersdk.CreateUserRequest)
817818
UserID: user.ID,
818819
CreatedAt: database.Now(),
819820
UpdatedAt: database.Now(),
820-
// By default give them membership to the organization
821+
// By default give them membership to the organization.
821822
Roles: orgRoles,
822823
})
823824
if err != nil {

0 commit comments

Comments
 (0)