Skip to content

Commit 6ad5bec

Browse files
committed
add unauthorised200 parameter to /user endpoint
1 parent 877765c commit 6ad5bec

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

backend/app/rest/api/rest.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,17 @@ func (s *Rest) routes() chi.Router {
291291
rauth.Use(middleware.Timeout(30 * time.Second))
292292
rauth.Use(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(10, nil)))
293293
rauth.Use(authMiddleware.Auth, matchSiteID, middleware.NoCache, logInfoWithBody)
294-
rauth.Get("/user", s.privRest.userInfoCtrl)
295294
rauth.Get("/userdata", s.privRest.userAllDataCtrl)
296295
})
297296

297+
// protected routes, user is set but checked inside the handlers
298+
rapi.Group(func(rauthOptional chi.Router) {
299+
rauthOptional.Use(middleware.Timeout(30 * time.Second))
300+
rauthOptional.Use(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(10, nil)))
301+
rauthOptional.Use(authMiddleware.Trace, middleware.NoCache, logInfoWithBody)
302+
rauthOptional.Get("/user", s.privRest.userInfoCtrl)
303+
})
304+
298305
// admin routes, require auth and admin users only
299306
rapi.Route("/admin", func(radmin chi.Router) {
300307
radmin.Use(middleware.Timeout(30 * time.Second))

backend/app/rest/api/rest_private.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,22 @@ func (s *private) updateCommentCtrl(w http.ResponseWriter, r *http.Request) {
236236
render.JSON(w, r, res)
237237
}
238238

239-
// GET /user?site=siteID - returns user info
239+
// GET /user?site=siteID&unauthorised200=false - returns user info, with unauthorised200=true returns 200 with error message
240240
func (s *private) userInfoCtrl(w http.ResponseWriter, r *http.Request) {
241-
user := rest.MustGetUserInfo(r)
241+
user, err := rest.GetUserInfo(r)
242+
if err != nil {
243+
log.Printf("[ERROR] unathorised200: %s (%v)", r.URL.Query().Get("unauthorised200"), r.URL.Query().Get("unauthorised200") == "true")
244+
if r.URL.Query().Get("unauthorised200") == "true" {
245+
render.JSON(w, r, R.JSON{"error": err.Error()})
246+
return
247+
}
248+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
249+
return
250+
}
251+
252+
// as user is set, call matchSiteID middleware to verify SiteID match
253+
matchSiteID(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})).ServeHTTP(w, r)
254+
242255
if siteID := r.URL.Query().Get("site"); siteID != "" {
243256
user.Verified = s.dataService.IsVerified(siteID, user.ID)
244257

frontend/apps/remark42/app/common/api.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ export const removeMyComment = (id: Comment['id']): Promise<void> =>
6060

6161
export const getPreview = (text: string): Promise<string> => apiFetcher.post('/preview', {}, { text });
6262

63-
export function getUser(): Promise<User | null> {
64-
return apiFetcher.get<User | null>('/user').catch(() => null);
63+
export function getUser(unauthorised200= true): Promise<User | null> {
64+
return apiFetcher
65+
.get<User | null>('/user', { unauthorised200: String(unauthorised200) })
66+
.then((response) => {
67+
if (unauthorised200 && response && 'error' in response) {
68+
return null;
69+
}
70+
return response;
71+
})
72+
.catch(() => null);
6573
}
6674

6775
export const uploadImage = (image: File): Promise<Image> => {

frontend/packages/api/clients/public.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,16 @@ export function createPublicClient({ siteId: site, baseUrl }: ClientParams) {
9393
/**
9494
* Get current authorized user
9595
*/
96-
async function getUser(): Promise<User | null> {
97-
return fetcher.get<User | null>('/user').catch(() => null)
96+
async function getUser(unauthorised200= true): Promise<User | null> {
97+
return fetcher
98+
.get<User | null>('/user', { unauthorised200: String(unauthorised200) })
99+
.then((response) => {
100+
if (unauthorised200 && response && 'error' in response) {
101+
return null;
102+
}
103+
return response;
104+
})
105+
.catch(() => null);
98106
}
99107

100108
/**

0 commit comments

Comments
 (0)