Skip to content

Commit

Permalink
fix: make leaders endpoint publicly accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Dec 2, 2023
1 parent 99f2b3c commit f82ab82
Show file tree
Hide file tree
Showing 8 changed files with 516 additions and 470 deletions.
964 changes: 502 additions & 462 deletions coverage/coverage.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion middlewares/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewAuthenticateMiddleware(userService services.IUserService) *AuthenticateM
}
}

func (m *AuthenticateMiddleware) WithOptionalFor(paths []string) *AuthenticateMiddleware {
func (m *AuthenticateMiddleware) WithOptionalFor(paths ...string) *AuthenticateMiddleware {
m.optionalForPaths = paths
return m
}
Expand Down
2 changes: 1 addition & 1 deletion routes/api/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewActivityApiHandler(userService services.IUserService, activityService se
func (h *ActivityApiHandler) RegisterRoutes(router chi.Router) {
r := chi.NewRouter()
r.Use(
middlewares.NewAuthenticateMiddleware(h.userService).WithOptionalFor([]string{"/api/activity/chart/"}).Handler,
middlewares.NewAuthenticateMiddleware(h.userService).WithOptionalFor("/api/activity/chart/").Handler,
middleware.Compress(9, "image/svg+xml"),
)
r.Get("/chart/{user}.svg", h.GetActivityChart)
Expand Down
2 changes: 1 addition & 1 deletion routes/api/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewBadgeHandler(userService services.IUserService, summaryService services.

func (h *BadgeHandler) RegisterRoutes(router chi.Router) {
r := chi.NewRouter()
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).WithOptionalFor([]string{"/api/badge/"}).Handler)
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).WithOptionalFor("/api/badge/").Handler)
r.Get("/{user}/*", h.Get)
router.Mount("/badge", r)
}
Expand Down
10 changes: 8 additions & 2 deletions routes/compat/wakatime/v1/leaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewLeadersHandler(userService services.IUserService, leaderboardService ser

func (h *LeadersHandler) RegisterRoutes(router chi.Router) {
router.Group(func(r chi.Router) {
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).WithOptionalFor("/").Handler)
r.Get("/compat/wakatime/v1/leaders", h.Get)
})
}
Expand Down Expand Up @@ -65,6 +65,9 @@ func (h *LeadersHandler) Get(w http.ResponseWriter, r *http.Request) {
}

loadPrimaryUserLeaderboard := func() (models.Leaderboard, error) {
if user == nil {
return []*models.LeaderboardItemRanked{}, nil
}
if languageParam == "" {
return h.leaderboardSrvc.GetByIntervalAndUser(h.leaderboardSrvc.GetDefaultScope(), user.ID, true)
} else {
Expand Down Expand Up @@ -109,7 +112,10 @@ func (h *LeadersHandler) Get(w http.ResponseWriter, r *http.Request) {
}

func (h *LeadersHandler) buildViewModel(globalLeaderboard, languageLeaderboard models.Leaderboard, user *models.User, interval *models.IntervalKey, pageParams *utils.PageParams) *v1.LeadersViewModel {
currentUserGlobal := *globalLeaderboard.GetByUser(user.ID)
var currentUserGlobal []*models.LeaderboardItemRanked
if user != nil {
currentUserGlobal = *globalLeaderboard.GetByUser(user.ID)
}

totalUsers, _ := h.leaderboardSrvc.CountUsers(true)
totalPages := int(totalUsers/int64(pageParams.PageSize) + 1)
Expand Down
2 changes: 1 addition & 1 deletion routes/compat/wakatime/v1/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewStatsHandler(userService services.IUserService, summaryService services.
func (h *StatsHandler) RegisterRoutes(router chi.Router) {
router.Group(func(r chi.Router) {
r.Use(
middlewares.NewAuthenticateMiddleware(h.userSrvc).WithOptionalFor([]string{"/"}).Handler,
middlewares.NewAuthenticateMiddleware(h.userSrvc).WithOptionalFor("/").Handler,
)
r.Get("/v1/users/{user}/stats/{range}", h.Get)
r.Get("/compat/wakatime/v1/users/{user}/stats/{range}", h.Get)
Expand Down
2 changes: 1 addition & 1 deletion routes/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h *LeaderboardHandler) RegisterRoutes(router chi.Router) {
middlewares.NewAuthenticateMiddleware(h.userService).
WithRedirectTarget(defaultErrorRedirectTarget()).
WithRedirectErrorMessage("unauthorized").
WithOptionalFor([]string{"/"}).Handler,
WithOptionalFor("/").Handler,
)
r.Get("/", h.GetIndex)

Expand Down
2 changes: 1 addition & 1 deletion routes/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *LoginHandler) RegisterRoutes(router chi.Router) {
authMiddleware := middlewares.NewAuthenticateMiddleware(h.userSrvc).
WithRedirectTarget(defaultErrorRedirectTarget()).
WithRedirectErrorMessage("unauthorized").
WithOptionalFor([]string{"/logout"})
WithOptionalFor("/logout")

logoutRouter := chi.NewRouter()
logoutRouter.Use(authMiddleware.Handler)
Expand Down

0 comments on commit f82ab82

Please sign in to comment.