Skip to content

Commit 4c62f85

Browse files
committed
Initial commit
0 parents  commit 4c62f85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+16228
-0
lines changed

.gitignore

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
/build
5+
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
pnpm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
lerna-debug.log*
14+
15+
# OS
16+
.DS_Store
17+
18+
# Tests
19+
/coverage
20+
/.nyc_output
21+
22+
# IDEs and editors
23+
/.idea
24+
.project
25+
.classpath
26+
.c9/
27+
*.launch
28+
.settings/
29+
*.sublime-workspace
30+
31+
# IDE - VSCode
32+
.vscode/*
33+
!.vscode/settings.json
34+
!.vscode/tasks.json
35+
!.vscode/launch.json
36+
!.vscode/extensions.json
37+
38+
# dotenv environment variable files
39+
.env
40+
.env.development.local
41+
.env.test.local
42+
.env.production.local
43+
.env.local
44+
45+
# temp directory
46+
.temp
47+
.tmp
48+
49+
# Runtime data
50+
pids
51+
*.pid
52+
*.seed
53+
*.pid.lock
54+
55+
# Diagnostic reports (https://nodejs.org/api/report.html)
56+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v22.13.1

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM node:22.13.1-alpine
4+
5+
RUN apk add --no-cache bash
6+
RUN apk update
7+
8+
ARG RESET_DB_ARG=false
9+
ENV RESET_DB=$RESET_DB_ARG
10+
ARG SEED_DATA_ARG=""
11+
ENV SEED_DATA=$SEED_DATA_ARG
12+
ENV PRISMA_CLI_BINARY_TARGETS=linux-musl-openssl-3.0.x
13+
14+
WORKDIR /app
15+
COPY . .
16+
RUN npm install pnpm -g
17+
RUN pnpm install
18+
RUN pnpm run build
19+
RUN chmod +x appStartUp.sh
20+
CMD ./appStartUp.sh

M2M_TOKEN_GUIDE.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Machine-to-Machine (M2M) Token Guide for Topcoder Review API
2+
3+
## Overview
4+
5+
The Topcoder Review API supports machine-to-machine (M2M) authentication using Auth0 for service-to-service integrations.
6+
This guide explains how to use M2M tokens with the API.
7+
8+
## What are M2M Tokens?
9+
10+
Machine-to-Machine tokens are designed for service-to-service authentication where a human user is not directly involved.
11+
Instead of using user roles for authorization, M2M tokens use scopes, which are permissions attached to the token.
12+
13+
## M2M Token Structure
14+
15+
M2M tokens from Auth0 contain the following important claims:
16+
- `iss` (issuer): The Auth0 domain that issued the token
17+
- `sub` (subject): The client ID of the application
18+
- `aud` (audience): The API identifier (audience)
19+
- `exp` (expiration time): When the token expires
20+
- `iat` (issued at time): When the token was issued
21+
- `scope`: Space-separated list of authorized scopes
22+
23+
## Available Scopes
24+
25+
The Topcoder Review API supports the following scopes:
26+
27+
### Group Scopes
28+
- `read:groups` - Do read action of groups
29+
- `write:groups` - Do write action of groups
30+
- `all:groups` - All groups-related operations
31+
32+
## Getting an M2M Token
33+
34+
To get an M2M token, you need to have a client registered in Auth0 with the appropriate permissions.
35+
36+
### Example Request
37+
38+
```bash
39+
curl --request POST \
40+
--url https://topcoder-dev.auth0.com/oauth/token \
41+
--header 'content-type: application/json' \
42+
--data '{
43+
"client_id": "YOUR_CLIENT_ID",
44+
"client_secret": "YOUR_CLIENT_SECRET",
45+
"audience": "https://m2m.topcoder-dev.com/",
46+
"grant_type": "client_credentials"
47+
}'
48+
```
49+
50+
### Example Response
51+
52+
```json
53+
{
54+
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6...",
55+
"scope": "read:appeal create:review all:scorecard",
56+
"expires_in": 86400,
57+
"token_type": "Bearer"
58+
}
59+
```
60+
61+
## Using the Token
62+
63+
Include the token in your API requests in the Authorization header:
64+
65+
```
66+
Authorization: Bearer YOUR_ACCESS_TOKEN
67+
```
68+
69+
## Scope Validation
70+
71+
When a request is made to the API, the token's scopes are validated against the required scopes for the endpoint.
72+
If the token has at least one of the required scopes, access is granted.
73+
74+
### Notes on "all:" Scopes
75+
76+
Scopes that start with "all:" automatically grant access to all the specific operations in that category.
77+
For example, `all:groups` includes `read:groups`, `write:groups`, etc.
78+
79+
## Testing M2M Tokens
80+
81+
For testing locally, you can use the following predefined test tokens:
82+
83+
- `m2m-token-all` - Has all available scopes
84+
- `m2m-token-groups` - Has all groups scopes
85+
86+
Example usage with curl:
87+
88+
```bash
89+
curl -X GET http://localhost:3000/v6/groups \
90+
-H "Authorization: Bearer m2m-token-review"
91+
```
92+
93+
## Troubleshooting
94+
95+
If you receive a 403 Forbidden response, check that:
96+
1. Your token is valid and not expired
97+
2. The token has the required scope for the endpoint
98+
3. You're using the correct audience and issuer

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Topcoder Group API
2+
3+
Group API built on modern frameworks for managing all group-related Topcoder needs.
4+
5+
## Project setup
6+
7+
```bash
8+
$ pnpm install
9+
```
10+
11+
## Compile and run the project
12+
13+
```bash
14+
# development
15+
$ pnpm run start
16+
17+
# watch mode
18+
$ pnpm run start:dev
19+
20+
# production mode
21+
$ pnpm run start:prod
22+
```
23+
24+
## Database
25+
26+
```bash
27+
# run postgres in docker, or other approach
28+
docker run -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres:14
29+
30+
export DATABASE_URL="postgresql://postgres:mysecretpassword@localhost:5432/group-api?schema=groups"
31+
32+
# run migration
33+
npx prisma migrate dev
34+
35+
# seed data
36+
npx prisma db seed
37+
or
38+
npx prisma migrate reset
39+
40+
# if you modify prisma schema, run migration again
41+
# and it'll ask
42+
# Enter a name for the new migration:
43+
# just provide a good migration name, such as
44+
#- `add_user_table`
45+
#- `update_user_fields`
46+
#- `create_posts_table`
47+
#- `add_email_to_users`
48+
#- `update_foreign_keys`
49+
```
50+
51+
## Data import
52+
53+
- create a .env file `mv .env.sample .env`
54+
- update the postgres database url in .env file —
55+
`DATABASE_URL="postgresql://postgres:mysecretpassword@localhost:5432/group-api?schema=groups"`
56+
- install dependencies `pnpm install`
57+
- run the prisma migration `npx prisma migrate dev`
58+
- run the prisma seed `npx prisma db seed`
59+
- run the project `pnpm run start`
60+
61+
62+
## Test API
63+
- import Postman collection. The files are `doc/Group API.postman_collection.json` and `doc/Group.postman_environment.json`
64+
- set env to suit your environment
65+
- run mock api `node mock/mock-api.js`
66+
- reset seed data `pnpm run seed-data`
67+
- start app `pnpm run start:dev`
68+
- generate the token
69+
70+
```bash
71+
curl --request POST \
72+
--url https://auth0proxy.topcoder-dev.com/token \
73+
--header 'content-type: application/json' \
74+
--data '{"auth0_url": "https://topcoder-dev.auth0.com/oauth/token", "client_id":"8uHVTW2WHp8BbBPX7J0YTAwgYbYTfjsM","client_secret":"hQXGeKO-cSZ15CLBNRDGZAGRcHWay6PAR_zTQQz4YjdX7QNU7NwGoaa3YuuUYvUv","audience":"https://m2m.topcoder-dev.com/","grant_type":"client_credentials"}'
75+
```
76+
77+
For a read-only scope M2M token, use:
78+
79+
```
80+
"client_id":"dgrh9mkk8N6sWsVPFf6JqdVmjjuibowf"
81+
"client_secret":"_6jFqIppsA3u87Tm7mRDXhX_yf8-DC2ooc5f0affjuWoRiLG1ZriHtUsMQgo61qp"
82+
```
83+
84+
- then you can use Postman to test all apis
85+
- Swagger docs are accessible at `http://localhost:3000/api-docs`

appStartUp.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
4+
export DATABASE_URL=$(echo -e ${DATABASE_URL})
5+
6+
echo "Database - running migrations."
7+
if $RESET_DB; then
8+
echo "Resetting DB"
9+
npx prisma migrate reset --force
10+
else
11+
echo "Running migrations"
12+
npx prisma migrate deploy
13+
fi
14+
15+
# Start the app
16+
pnpm start:prod

build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
docker buildx build --no-cache=true --build-arg RESET_DB_ARG=<<pipeline.parameters.reset-db>> --build-arg SEED_DATA_ARG=${DEPLOYMENT_ENVIRONMENT} -t ${APPNAME}}:latest .

0 commit comments

Comments
 (0)