Skip to content

Commit

Permalink
chore: Add user authentication check in handleUserPath middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya committed Aug 5, 2024
1 parent eda2690 commit 9395c3c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RUN curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.gpg | sudo apt-key add - \
&& curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.list | sudo tee /etc/apt/sources.list.d/tailscale.list \
&& sudo apt-get update -q \
&& sudo apt-get install -yq tailscale jq \
&& sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-nft
26 changes: 26 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: npm install && npm run build
command: npm run start
- name: tailscaled
command: |
if [ -n "${TAILSCALE_STATE_MYPROJECT}" ]; then
# restore the tailscale state from gitpod user's env vars
sudo mkdir -p /var/lib/tailscale
echo "${TAILSCALE_STATE_MYPROJECT}" | sudo tee /var/lib/tailscale/tailscaled.state > /dev/null
fi
sudo tailscaled
- name: tailscale
command: |
if [ -n "${TAILSCALE_STATE_MYPROJECT}" ]; then
sudo -E tailscale up
else
sudo -E tailscale up --hostname "gitpod-${GITPOD_GIT_USER_NAME// /-}-$(echo ${GITPOD_WORKSPACE_CONTEXT} | jq -r .repository.name)"
# store the tailscale state into gitpod user
gp env TAILSCALE_STATE_MYPROJECT="$(sudo cat /var/lib/tailscale/tailscaled.state)"
fi
File renamed without changes.
8 changes: 6 additions & 2 deletions src/middlewares/handlers/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ const errorResponse = (message: string, status: number) => {
};

const handleUserPath = (request: NextRequest, user: any) => {
if (!user) {
return errorResponse("User not authenticated", 401);
}

const { pathname, search } = request.nextUrl;
const userParam = pathname.split("/")[3];
if (typeof userParam !== "string" || userParam.length > 100) {
return errorResponse("Invalid userParam", 400);
}
logger.info(`userParam: ${userParam}`);

if (userParam === "@me" && user.providerAccountId) {
if (userParam === "@me" && user?.providerAccountId) {
const userId = user.providerAccountId as string;
const url = new URL(
pathname.replace("@me", encodeURIComponent(userId)) + search,
Expand All @@ -93,7 +97,7 @@ const handleUserPath = (request: NextRequest, user: any) => {
});
}
} else if (
user.providerAccountId &&
user?.providerAccountId &&
userParam &&
user.providerAccountId !== userParam
) {
Expand Down

0 comments on commit 9395c3c

Please sign in to comment.