Skip to content

Commit

Permalink
fix: dont crash if users have no roles when refreshing token (#511)
Browse files Browse the repository at this point in the history
* fix: dont crash if users have no roles when refreshing token

* asd
  • Loading branch information
dbarrosop authored Apr 30, 2024
1 parent 72d7e29 commit 8bb4dcf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions go/controller/post_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestPostToken(t *testing.T) { //nolint:maintidx
),
}),
).Return([]sql.RefreshTokenAndGetUserRolesRow{
{Role: "user", RefreshTokenID: tokenID},
{Role: "me", RefreshTokenID: tokenID},
{Role: sql.Text("user"), RefreshTokenID: tokenID},
{Role: sql.Text("me"), RefreshTokenID: tokenID},
}, nil)

return mock
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestPostToken(t *testing.T) { //nolint:maintidx
),
}),
).Return([]sql.RefreshTokenAndGetUserRolesRow{
{Role: "anonymous", RefreshTokenID: tokenID},
{Role: sql.Text("anonymous"), RefreshTokenID: tokenID},
}, nil)

return mock
Expand Down
14 changes: 10 additions & 4 deletions go/controller/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (wf *Workflows) GetUserByRefreshTokenHash(
return user, nil
}

func (wf *Workflows) UpdateSession(
func (wf *Workflows) UpdateSession( //nolint:funlen
ctx context.Context,
user sql.AuthUser,
refreshToken string,
Expand All @@ -314,9 +314,15 @@ func (wf *Workflows) UpdateSession(
return &api.Session{}, ErrInvalidRefreshToken //nolint:exhaustruct
}

allowedRoles := make([]string, len(userRoles))
for i, role := range userRoles {
allowedRoles[i] = role.Role
allowedRoles := make([]string, 0, len(userRoles))
for _, role := range userRoles {
if role.Role.Valid {
allowedRoles = append(allowedRoles, role.Role.String)
}
}

if !slices.Contains(allowedRoles, user.DefaultRole) {
allowedRoles = append(allowedRoles, user.DefaultRole)
}

accessToken, expiresIn, err := wf.jwtGetter.GetToken(
Expand Down
2 changes: 1 addition & 1 deletion go/sql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ updated_user AS (
WHERE auth.users.id = refreshed_token.user_id
)
SELECT refreshed_token.refresh_token_id, role FROM auth.user_roles
JOIN refreshed_token ON auth.user_roles.user_id = refreshed_token.user_id;
RIGHT JOIN refreshed_token ON auth.user_roles.user_id = refreshed_token.user_id;

-- name: UpdateUserLastSeen :one
UPDATE auth.users
Expand Down
4 changes: 2 additions & 2 deletions go/sql/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8bb4dcf

Please sign in to comment.