Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
49ac4f6ca4 | |||
8e69511e3e | |||
7e305429b4 | |||
b71da7dcc9 | |||
a6dd112e4a | |||
ab94d1e656 | |||
8483cd4772 | |||
46745e1b7e |
@ -1 +1,7 @@
|
|||||||
{ "extends": ["@commitlint/config-conventional"] }
|
{
|
||||||
|
"extends": ["@commitlint/config-conventional"],
|
||||||
|
"rules": {
|
||||||
|
"body-max-length": [0, "always"],
|
||||||
|
"body-max-line-length": [0, "always"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@ services:
|
|||||||
- 'host.docker.internal:host-gateway'
|
- 'host.docker.internal:host-gateway'
|
||||||
|
|
||||||
thream-database:
|
thream-database:
|
||||||
image: 'postgres:14.2'
|
image: 'postgres:14.5'
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: 'user'
|
POSTGRES_USER: 'user'
|
||||||
POSTGRES_PASSWORD: 'password'
|
POSTGRES_PASSWORD: 'password'
|
||||||
|
12
.env.example
12
.env.example
@ -1,21 +1,21 @@
|
|||||||
API_URL=http://localhost:8080
|
|
||||||
COMPOSE_PROJECT_NAME=thream-api
|
COMPOSE_PROJECT_NAME=thream-api
|
||||||
|
NODE_ENV=development
|
||||||
|
API_URL=http://localhost:8080
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=8080
|
||||||
DATABASE_URL=postgresql://user:password@thream-database:5432/thream
|
DATABASE_URL=postgresql://user:password@thream-database:5432/thream
|
||||||
DISCORD_CLIENT_ID=
|
|
||||||
DISCORD_CLIENT_SECRET=
|
|
||||||
EMAIL_HOST=thream-maildev
|
EMAIL_HOST=thream-maildev
|
||||||
EMAIL_PASSWORD=password
|
EMAIL_PASSWORD=password
|
||||||
EMAIL_PORT=25
|
EMAIL_PORT=25
|
||||||
EMAIL_USER=no-reply@thream.fr
|
EMAIL_USER=no-reply@thream.fr
|
||||||
FILE_UPLOADS_API_KEY=apiKeySecret
|
FILE_UPLOADS_API_KEY=apiKeySecret
|
||||||
FILE_UPLOADS_API_URL=http://host.docker.internal:8000
|
FILE_UPLOADS_API_URL=http://host.docker.internal:8000
|
||||||
|
DISCORD_CLIENT_ID=
|
||||||
|
DISCORD_CLIENT_SECRET=
|
||||||
GITHUB_CLIENT_ID=
|
GITHUB_CLIENT_ID=
|
||||||
GITHUB_CLIENT_SECRET=
|
GITHUB_CLIENT_SECRET=
|
||||||
GOOGLE_CLIENT_ID=
|
GOOGLE_CLIENT_ID=
|
||||||
GOOGLE_CLIENT_SECRET=
|
GOOGLE_CLIENT_SECRET=
|
||||||
HOST=0.0.0.0
|
|
||||||
JWT_ACCESS_EXPIRES_IN=15 minutes
|
JWT_ACCESS_EXPIRES_IN=15 minutes
|
||||||
JWT_ACCESS_SECRET=accessTokenSecret
|
JWT_ACCESS_SECRET=accessTokenSecret
|
||||||
JWT_REFRESH_SECRET=refreshTokenSecret
|
JWT_REFRESH_SECRET=refreshTokenSecret
|
||||||
NODE_ENV=development
|
|
||||||
PORT=8080
|
|
||||||
|
3
.github/workflows/lint.yml
vendored
3
.github/workflows/lint.yml
vendored
@ -45,3 +45,6 @@ jobs:
|
|||||||
uses: 'hadolint/hadolint-action@v1.6.0'
|
uses: 'hadolint/hadolint-action@v1.6.0'
|
||||||
with:
|
with:
|
||||||
dockerfile: './Dockerfile'
|
dockerfile: './Dockerfile'
|
||||||
|
|
||||||
|
- name: 'prisma:validate'
|
||||||
|
run: 'cp .env.example .env && npm run prisma:validate'
|
||||||
|
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -8,7 +8,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: 'macos-latest'
|
runs-on: 'ubuntu-latest'
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.0.0'
|
- uses: 'actions/checkout@v3.0.0'
|
||||||
|
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -35,3 +35,4 @@ npm-debug.log*
|
|||||||
|
|
||||||
# misc
|
# misc
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
*.hbs
|
||||||
|
@ -2,5 +2,6 @@
|
|||||||
"*": ["editorconfig-checker"],
|
"*": ["editorconfig-checker"],
|
||||||
"*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"],
|
"*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"],
|
||||||
"*.{json,jsonc,yml,yaml}": ["prettier --write"],
|
"*.{json,jsonc,yml,yaml}": ["prettier --write"],
|
||||||
"*.md": ["prettier --write", "markdownlint --dot --fix"]
|
"*.{md,mdx}": ["prettier --write", "markdownlint-cli2 --fix"],
|
||||||
|
"prisma/schema.prisma": ["prisma validate"]
|
||||||
}
|
}
|
||||||
|
10
.markdownlint-cli2.jsonc
Normal file
10
.markdownlint-cli2.jsonc
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"config": {
|
||||||
|
"default": true,
|
||||||
|
"MD013": false,
|
||||||
|
"MD033": false,
|
||||||
|
"MD041": false
|
||||||
|
},
|
||||||
|
"globs": ["**/*.{md,mdx}"],
|
||||||
|
"ignores": ["**/node_modules"]
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"default": true,
|
|
||||||
"MD013": false,
|
|
||||||
"MD033": false,
|
|
||||||
"MD041": false
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
build
|
|
||||||
node_modules
|
|
||||||
coverage
|
|
||||||
package.json
|
|
||||||
package-lock.json
|
|
||||||
*.hbs
|
|
14
.swcrc
14
.swcrc
@ -2,21 +2,11 @@
|
|||||||
"jsc": {
|
"jsc": {
|
||||||
"parser": {
|
"parser": {
|
||||||
"syntax": "typescript",
|
"syntax": "typescript",
|
||||||
"decorators": true,
|
|
||||||
"dynamicImport": true
|
"dynamicImport": true
|
||||||
},
|
},
|
||||||
"transform": {
|
"target": "es2022"
|
||||||
"legacyDecorator": true,
|
|
||||||
"decoratorMetadata": true
|
|
||||||
},
|
|
||||||
"target": "es2022",
|
|
||||||
"loose": true
|
|
||||||
},
|
},
|
||||||
"module": {
|
"module": {
|
||||||
"type": "es6",
|
"type": "es6"
|
||||||
"strict": false,
|
|
||||||
"strictMode": true,
|
|
||||||
"lazy": false,
|
|
||||||
"noInterop": false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,6 @@ git commit -m "fix(services): should emit events to connected users"
|
|||||||
- `services` : all REST API endpoints
|
- `services` : all REST API endpoints
|
||||||
- `tools` : configs and utilities
|
- `tools` : configs and utilities
|
||||||
- `typings` : types gloablly used in the project
|
- `typings` : types gloablly used in the project
|
||||||
- `uploads` : uploaded files by users
|
|
||||||
|
|
||||||
### Services folder explained with an example
|
### Services folder explained with an example
|
||||||
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
FROM node:16.14.2 AS dependencies
|
FROM node:16.17.0 AS dependencies
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
COPY ./package*.json ./
|
COPY ./package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
|
||||||
FROM node:16.14.2 AS builder
|
FROM node:16.17.0 AS builder
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
COPY --from=dependencies /usr/src/app/node_modules ./node_modules
|
COPY --from=dependencies /usr/src/app/node_modules ./node_modules
|
||||||
COPY ./ ./
|
COPY ./ ./
|
||||||
RUN npm run prisma:generate && npm run build
|
RUN npm run prisma:generate && npm run build
|
||||||
|
|
||||||
FROM node:16.14.2 AS runner
|
FROM node:16.17.0 AS runner
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
COPY --from=builder /usr/src/app/node_modules ./node_modules
|
COPY --from=builder /usr/src/app/node_modules ./node_modules
|
||||||
|
@ -22,7 +22,7 @@ export const serviceGenerator = {
|
|||||||
type: 'list',
|
type: 'list',
|
||||||
name: 'tag',
|
name: 'tag',
|
||||||
message: 'tag',
|
message: 'tag',
|
||||||
choices: ['users', 'guilds', 'channels', 'messages', 'members', 'uploads']
|
choices: ['users', 'oauth2', 'guilds', 'channels', 'messages', 'members']
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
|
4656
package-lock.json
generated
4656
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
68
package.json
68
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@thream/api",
|
"name": "@thream/api",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"description": "Thream's application programming interface to stay close with your friends and communities.",
|
"description": "Thream's application programming interface to stay close with your friends and communities.",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
@ -20,11 +20,12 @@
|
|||||||
"generate": "plop",
|
"generate": "plop",
|
||||||
"lint:commit": "commitlint",
|
"lint:commit": "commitlint",
|
||||||
"lint:editorconfig": "editorconfig-checker",
|
"lint:editorconfig": "editorconfig-checker",
|
||||||
"lint:markdown": "markdownlint \"**/*.md\" --dot --ignore-path \".gitignore\"",
|
"lint:markdown": "markdownlint-cli2",
|
||||||
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
|
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
|
||||||
"lint:prettier": "prettier \".\" --check",
|
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"",
|
||||||
"lint:staged": "lint-staged",
|
"lint:staged": "lint-staged",
|
||||||
"test": "cross-env NODE_ENV=test c8 tap",
|
"test": "cross-env NODE_ENV=test c8 tap",
|
||||||
|
"prisma:validate": "prisma validate",
|
||||||
"prisma:generate": "prisma generate",
|
"prisma:generate": "prisma generate",
|
||||||
"prisma:studio": "prisma studio",
|
"prisma:studio": "prisma studio",
|
||||||
"prisma:migrate:dev": "prisma migrate dev",
|
"prisma:migrate:dev": "prisma migrate dev",
|
||||||
@ -33,69 +34,70 @@
|
|||||||
"postinstall": "husky install"
|
"postinstall": "husky install"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fastify/cors": "8.0.0",
|
"@fastify/cors": "8.1.0",
|
||||||
"@fastify/helmet": "9.1.0",
|
"@fastify/helmet": "9.1.0",
|
||||||
"@fastify/multipart": "7.1.0",
|
"@fastify/multipart": "7.1.1",
|
||||||
"@fastify/rate-limit": "7.0.0",
|
"@fastify/rate-limit": "7.3.0",
|
||||||
"@fastify/sensible": "5.1.0",
|
"@fastify/sensible": "5.1.1",
|
||||||
"@fastify/swagger": "7.4.0",
|
"@fastify/swagger": "7.4.1",
|
||||||
"@prisma/client": "3.12.0",
|
"@prisma/client": "4.2.1",
|
||||||
"@sinclair/typebox": "0.23.5",
|
"@sinclair/typebox": "0.24.28",
|
||||||
"@thream/socketio-jwt": "3.0.0",
|
"@thream/socketio-jwt": "3.0.0",
|
||||||
"axios": "0.26.1",
|
"axios": "0.26.1",
|
||||||
"bcryptjs": "2.4.3",
|
"bcryptjs": "2.4.3",
|
||||||
"dotenv": "16.0.1",
|
"dotenv": "16.0.1",
|
||||||
"ejs": "3.1.8",
|
"ejs": "3.1.8",
|
||||||
"fastify": "4.2.0",
|
"fastify": "4.5.3",
|
||||||
"fastify-plugin": "3.0.1",
|
"fastify-plugin": "4.2.1",
|
||||||
"form-data": "4.0.0",
|
"form-data": "4.0.0",
|
||||||
"http-errors": "2.0.0",
|
"http-errors": "2.0.0",
|
||||||
"jsonwebtoken": "8.5.1",
|
"jsonwebtoken": "8.5.1",
|
||||||
"ms": "2.1.3",
|
"ms": "2.1.3",
|
||||||
"nodemailer": "6.7.5",
|
"nodemailer": "6.7.8",
|
||||||
"read-pkg": "7.1.0",
|
"read-pkg": "7.1.0",
|
||||||
"socket.io": "4.5.1"
|
"socket.io": "4.5.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@commitlint/cli": "17.0.3",
|
"@commitlint/cli": "17.1.2",
|
||||||
"@commitlint/config-conventional": "17.0.3",
|
"@commitlint/config-conventional": "17.1.0",
|
||||||
"@saithodev/semantic-release-backmerge": "2.1.2",
|
"@saithodev/semantic-release-backmerge": "2.1.2",
|
||||||
"@semantic-release/git": "10.0.1",
|
"@semantic-release/git": "10.0.1",
|
||||||
"@swc/cli": "0.1.57",
|
"@swc/cli": "0.1.57",
|
||||||
"@swc/core": "1.2.207",
|
"@swc/core": "1.2.244",
|
||||||
"@types/bcryptjs": "2.4.2",
|
"@types/bcryptjs": "2.4.2",
|
||||||
"@types/busboy": "1.5.0",
|
"@types/busboy": "1.5.0",
|
||||||
"@types/ejs": "3.1.1",
|
"@types/ejs": "3.1.1",
|
||||||
"@types/http-errors": "1.8.2",
|
"@types/http-errors": "1.8.2",
|
||||||
"@types/jsonwebtoken": "8.5.8",
|
"@types/jsonwebtoken": "8.5.9",
|
||||||
"@types/ms": "0.7.31",
|
"@types/ms": "0.7.31",
|
||||||
"@types/node": "18.0.0",
|
"@types/node": "18.7.13",
|
||||||
"@types/nodemailer": "6.4.4",
|
"@types/nodemailer": "6.4.5",
|
||||||
"@types/sinon": "10.0.12",
|
"@types/sinon": "10.0.13",
|
||||||
"@types/tap": "15.0.7",
|
"@types/tap": "15.0.7",
|
||||||
"@typescript-eslint/eslint-plugin": "5.30.0",
|
"@typescript-eslint/eslint-plugin": "5.35.1",
|
||||||
"c8": "7.11.3",
|
"@typescript-eslint/parser": "5.35.1",
|
||||||
"concurrently": "7.2.2",
|
"c8": "7.12.0",
|
||||||
|
"concurrently": "7.3.0",
|
||||||
"cross-env": "7.0.3",
|
"cross-env": "7.0.3",
|
||||||
"editorconfig-checker": "4.0.2",
|
"editorconfig-checker": "4.0.2",
|
||||||
"eslint": "8.18.0",
|
"eslint": "8.23.0",
|
||||||
"eslint-config-conventions": "2.0.0",
|
"eslint-config-conventions": "3.0.0",
|
||||||
"eslint-config-prettier": "8.5.0",
|
"eslint-config-prettier": "8.5.0",
|
||||||
"eslint-plugin-import": "2.26.0",
|
"eslint-plugin-import": "2.26.0",
|
||||||
"eslint-plugin-prettier": "4.1.0",
|
"eslint-plugin-prettier": "4.2.1",
|
||||||
"eslint-plugin-promise": "6.0.0",
|
"eslint-plugin-promise": "6.0.1",
|
||||||
"eslint-plugin-unicorn": "42.0.0",
|
"eslint-plugin-unicorn": "43.0.2",
|
||||||
"husky": "8.0.1",
|
"husky": "8.0.1",
|
||||||
"lint-staged": "13.0.3",
|
"lint-staged": "13.0.3",
|
||||||
"markdownlint-cli": "0.31.1",
|
"markdownlint-cli2": "0.5.1",
|
||||||
"nodemon": "2.0.18",
|
"nodemon": "2.0.19",
|
||||||
"plop": "3.1.1",
|
"plop": "3.1.1",
|
||||||
"prettier": "2.7.1",
|
"prettier": "2.7.1",
|
||||||
"prisma": "3.12.0",
|
"prisma": "4.2.1",
|
||||||
"rimraf": "3.0.2",
|
"rimraf": "3.0.2",
|
||||||
"semantic-release": "19.0.3",
|
"semantic-release": "19.0.5",
|
||||||
"sinon": "14.0.0",
|
"sinon": "14.0.0",
|
||||||
"tap": "16.3.0",
|
"tap": "16.3.0",
|
||||||
"typescript": "4.7.4"
|
"typescript": "4.8.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,7 @@ export const messageSchema = {
|
|||||||
type: Type.Union(types, { default: 'text' }),
|
type: Type.Union(types, { default: 'text' }),
|
||||||
mimetype: Type.String({
|
mimetype: Type.String({
|
||||||
maxLength: 127,
|
maxLength: 127,
|
||||||
default: 'text/plain',
|
default: 'text/plain'
|
||||||
format: 'mimetype'
|
|
||||||
}),
|
}),
|
||||||
createdAt: date.createdAt,
|
createdAt: date.createdAt,
|
||||||
updatedAt: date.updatedAt,
|
updatedAt: date.updatedAt,
|
||||||
|
@ -6,7 +6,7 @@ import { date, id } from './utils.js'
|
|||||||
|
|
||||||
export const refreshTokensSchema = {
|
export const refreshTokensSchema = {
|
||||||
id,
|
id,
|
||||||
token: Type.String(),
|
token: Type.String({ format: 'uuid' }),
|
||||||
createdAt: date.createdAt,
|
createdAt: date.createdAt,
|
||||||
updatedAt: date.updatedAt,
|
updatedAt: date.updatedAt,
|
||||||
userId: id
|
userId: id
|
||||||
@ -15,7 +15,7 @@ export const refreshTokensSchema = {
|
|||||||
export const refreshTokenExample: RefreshToken = {
|
export const refreshTokenExample: RefreshToken = {
|
||||||
id: 1,
|
id: 1,
|
||||||
userId: userExample.id,
|
userId: userExample.id,
|
||||||
token: 'sometoken',
|
token: 'sometokenUUID',
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
updatedAt: new Date()
|
updatedAt: new Date()
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,10 @@ export interface UserJWT {
|
|||||||
currentStrategy: AuthenticationStrategy
|
currentStrategy: AuthenticationStrategy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UserRefreshJWT extends UserJWT {
|
||||||
|
tokenUUID: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface UserRequest {
|
export interface UserRequest {
|
||||||
current: User
|
current: User
|
||||||
currentStrategy: AuthenticationStrategy
|
currentStrategy: AuthenticationStrategy
|
||||||
|
@ -50,6 +50,7 @@ export const getUserById: FastifyPluginAsync = async (fastify) => {
|
|||||||
id: true,
|
id: true,
|
||||||
name: true,
|
name: true,
|
||||||
email: settings.isPublicEmail,
|
email: settings.isPublicEmail,
|
||||||
|
isConfirmed: true,
|
||||||
logo: true,
|
logo: true,
|
||||||
status: true,
|
status: true,
|
||||||
biography: true,
|
biography: true,
|
||||||
|
@ -14,7 +14,7 @@ type Parameters = Static<typeof parametersSchema>
|
|||||||
|
|
||||||
const deleteServiceSchema: FastifySchema = {
|
const deleteServiceSchema: FastifySchema = {
|
||||||
description: 'DELETE a provider to authenticate with for a user.',
|
description: 'DELETE a provider to authenticate with for a user.',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
security: [
|
security: [
|
||||||
{
|
{
|
||||||
bearerAuth: []
|
bearerAuth: []
|
||||||
|
@ -14,7 +14,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Discord OAuth2 - add-strategy',
|
description: 'Discord OAuth2 - add-strategy',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
security: [
|
security: [
|
||||||
{
|
{
|
||||||
bearerAuth: []
|
bearerAuth: []
|
||||||
|
@ -17,7 +17,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Discord OAuth2 - callback-add-strategy',
|
description: 'Discord OAuth2 - callback-add-strategy',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -15,7 +15,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Discord OAuth2 - callback',
|
description: 'Discord OAuth2 - callback',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -13,7 +13,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Discord OAuth2 - signin',
|
description: 'Discord OAuth2 - signin',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -14,7 +14,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'GitHub OAuth2 - add-strategy',
|
description: 'GitHub OAuth2 - add-strategy',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
security: [
|
security: [
|
||||||
{
|
{
|
||||||
bearerAuth: []
|
bearerAuth: []
|
||||||
|
@ -17,7 +17,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'GitHub OAuth2 - callback-add-strategy',
|
description: 'GitHub OAuth2 - callback-add-strategy',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -15,7 +15,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'GitHub OAuth2 - callback',
|
description: 'GitHub OAuth2 - callback',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -13,7 +13,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'GitHub OAuth2 - signin',
|
description: 'GitHub OAuth2 - signin',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -14,7 +14,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Google OAuth2 - add-strategy',
|
description: 'Google OAuth2 - add-strategy',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
security: [
|
security: [
|
||||||
{
|
{
|
||||||
bearerAuth: []
|
bearerAuth: []
|
||||||
|
@ -17,7 +17,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Google OAuth2 - callback-add-strategy',
|
description: 'Google OAuth2 - callback-add-strategy',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -15,7 +15,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Google OAuth2 - callback',
|
description: 'Google OAuth2 - callback',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -13,7 +13,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
|||||||
|
|
||||||
const getServiceSchema: FastifySchema = {
|
const getServiceSchema: FastifySchema = {
|
||||||
description: 'Google OAuth2 - signin',
|
description: 'Google OAuth2 - signin',
|
||||||
tags: ['users'] as string[],
|
tags: ['oauth2'] as string[],
|
||||||
querystring: querySchema,
|
querystring: querySchema,
|
||||||
response: {
|
response: {
|
||||||
200: Type.String(),
|
200: Type.String(),
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import tap from 'tap'
|
import tap from 'tap'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
|
import jwt from 'jsonwebtoken'
|
||||||
|
|
||||||
import { application } from '../../../../application.js'
|
import { application } from '../../../../application.js'
|
||||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||||
@ -13,8 +14,7 @@ await tap.test('POST /users/refresh-token', async (t) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
await t.test('succeeds', async (t) => {
|
await t.test('succeeds', async (t) => {
|
||||||
const { accessToken, refreshToken, refreshTokenStubValue } =
|
const { refreshToken, refreshTokenStubValue } = await authenticateUserTest()
|
||||||
await authenticateUserTest()
|
|
||||||
sinon.stub(prisma, 'refreshToken').value({
|
sinon.stub(prisma, 'refreshToken').value({
|
||||||
...refreshTokenStubValue,
|
...refreshTokenStubValue,
|
||||||
findFirst: async () => {
|
findFirst: async () => {
|
||||||
@ -28,9 +28,6 @@ await tap.test('POST /users/refresh-token', async (t) => {
|
|||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/users/refresh-token',
|
url: '/users/refresh-token',
|
||||||
headers: {
|
|
||||||
authorization: `Bearer ${accessToken}`
|
|
||||||
},
|
|
||||||
payload: { refreshToken }
|
payload: { refreshToken }
|
||||||
})
|
})
|
||||||
const responseJson = response.json()
|
const responseJson = response.json()
|
||||||
@ -62,6 +59,9 @@ await tap.test('POST /users/refresh-token', async (t) => {
|
|||||||
return refreshTokenExample
|
return refreshTokenExample
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
sinon.stub(jwt, 'verify').value(() => {
|
||||||
|
throw new Error('Invalid token')
|
||||||
|
})
|
||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/users/refresh-token',
|
url: '/users/refresh-token',
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
jwtSchema,
|
jwtSchema,
|
||||||
expiresIn
|
expiresIn
|
||||||
} from '../../../tools/utils/jwtToken.js'
|
} from '../../../tools/utils/jwtToken.js'
|
||||||
import { UserJWT } from '../../../models/User.js'
|
import { UserRefreshJWT } from '../../../models/User.js'
|
||||||
import { JWT_REFRESH_SECRET } from '../../../tools/configurations/index.js'
|
import { JWT_REFRESH_SECRET } from '../../../tools/configurations/index.js'
|
||||||
|
|
||||||
const bodyPostRefreshTokenSchema = Type.Object({
|
const bodyPostRefreshTokenSchema = Type.Object({
|
||||||
@ -43,20 +43,20 @@ export const postRefreshTokenUser: FastifyPluginAsync = async (fastify) => {
|
|||||||
schema: postRefreshTokenSchema,
|
schema: postRefreshTokenSchema,
|
||||||
handler: async (request, reply) => {
|
handler: async (request, reply) => {
|
||||||
const { refreshToken } = request.body
|
const { refreshToken } = request.body
|
||||||
|
try {
|
||||||
|
const userRefreshJWT = jwt.verify(
|
||||||
|
refreshToken,
|
||||||
|
JWT_REFRESH_SECRET
|
||||||
|
) as UserRefreshJWT
|
||||||
const foundRefreshToken = await prisma.refreshToken.findFirst({
|
const foundRefreshToken = await prisma.refreshToken.findFirst({
|
||||||
where: { token: refreshToken }
|
where: { token: userRefreshJWT.tokenUUID }
|
||||||
})
|
})
|
||||||
if (foundRefreshToken == null) {
|
if (foundRefreshToken == null) {
|
||||||
throw fastify.httpErrors.forbidden()
|
throw fastify.httpErrors.forbidden()
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
const userJWT = jwt.verify(
|
|
||||||
foundRefreshToken.token,
|
|
||||||
JWT_REFRESH_SECRET
|
|
||||||
) as UserJWT
|
|
||||||
const accessToken = generateAccessToken({
|
const accessToken = generateAccessToken({
|
||||||
id: userJWT.id,
|
id: userRefreshJWT.id,
|
||||||
currentStrategy: userJWT.currentStrategy
|
currentStrategy: userRefreshJWT.currentStrategy
|
||||||
})
|
})
|
||||||
reply.statusCode = 200
|
reply.statusCode = 200
|
||||||
return {
|
return {
|
||||||
|
@ -25,6 +25,11 @@ await tap.test('PUT /users/reset-password', async (t) => {
|
|||||||
return userExample
|
return userExample
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
sinon.stub(prisma, 'refreshToken').value({
|
||||||
|
deleteMany: async () => {
|
||||||
|
return { count: 1 }
|
||||||
|
}
|
||||||
|
})
|
||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
url: '/users/reset-password',
|
url: '/users/reset-password',
|
||||||
|
@ -39,7 +39,7 @@ export const putResetPasswordUser: FastifyPluginAsync = async (fastify) => {
|
|||||||
user?.temporaryExpirationToken != null &&
|
user?.temporaryExpirationToken != null &&
|
||||||
user.temporaryExpirationToken.getTime() > Date.now()
|
user.temporaryExpirationToken.getTime() > Date.now()
|
||||||
if (user == null || !isValidTemporaryToken) {
|
if (user == null || !isValidTemporaryToken) {
|
||||||
throw fastify.httpErrors.badRequest('"tempToken" is invalid')
|
throw fastify.httpErrors.badRequest('`temporaryToken` is invalid.')
|
||||||
}
|
}
|
||||||
const hashedPassword = await bcrypt.hash(password, 12)
|
const hashedPassword = await bcrypt.hash(password, 12)
|
||||||
await prisma.user.update({
|
await prisma.user.update({
|
||||||
@ -52,6 +52,11 @@ export const putResetPasswordUser: FastifyPluginAsync = async (fastify) => {
|
|||||||
temporaryExpirationToken: null
|
temporaryExpirationToken: null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
await prisma.refreshToken.deleteMany({
|
||||||
|
where: {
|
||||||
|
userId: user.id
|
||||||
|
}
|
||||||
|
})
|
||||||
reply.statusCode = 200
|
reply.statusCode = 200
|
||||||
return 'The new password has been saved!'
|
return 'The new password has been saved!'
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import tap from 'tap'
|
import tap from 'tap'
|
||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
|
import jwt from 'jsonwebtoken'
|
||||||
|
|
||||||
import { application } from '../../../../application.js'
|
import { application } from '../../../../application.js'
|
||||||
import prisma from '../../../../tools/database/prisma.js'
|
import prisma from '../../../../tools/database/prisma.js'
|
||||||
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
|
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
|
||||||
|
import { UserRefreshJWT } from '../../../../models/User.js'
|
||||||
|
|
||||||
await tap.test('POST /users/signout', async (t) => {
|
await tap.test('POST /users/signout', async (t) => {
|
||||||
t.afterEach(() => {
|
t.afterEach(() => {
|
||||||
@ -17,10 +19,18 @@ await tap.test('POST /users/signout', async (t) => {
|
|||||||
},
|
},
|
||||||
delete: async () => {}
|
delete: async () => {}
|
||||||
})
|
})
|
||||||
|
sinon.stub(jwt, 'verify').value(() => {
|
||||||
|
const value: UserRefreshJWT = {
|
||||||
|
id: 1,
|
||||||
|
tokenUUID: refreshTokenExample.token,
|
||||||
|
currentStrategy: 'Local'
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
})
|
||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/users/signout',
|
url: '/users/signout',
|
||||||
payload: { refreshToken: refreshTokenExample.token }
|
payload: { refreshToken: 'jwt token' }
|
||||||
})
|
})
|
||||||
t.equal(response.statusCode, 200)
|
t.equal(response.statusCode, 200)
|
||||||
})
|
})
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import { Static, Type } from '@sinclair/typebox'
|
import { Static, Type } from '@sinclair/typebox'
|
||||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||||
|
import jwt from 'jsonwebtoken'
|
||||||
|
|
||||||
import prisma from '../../../tools/database/prisma.js'
|
import prisma from '../../../tools/database/prisma.js'
|
||||||
import { fastifyErrors } from '../../../models/utils.js'
|
import { fastifyErrors } from '../../../models/utils.js'
|
||||||
import { refreshTokensSchema } from '../../../models/RefreshToken.js'
|
import { JWT_REFRESH_SECRET } from '../../../tools/configurations/index.js'
|
||||||
|
import { UserRefreshJWT } from '../../../models/User.js'
|
||||||
|
import { jwtSchema } from '../../../tools/utils/jwtToken.js'
|
||||||
|
|
||||||
const bodyPostSignoutSchema = Type.Object({
|
const bodyPostSignoutSchema = Type.Object({
|
||||||
refreshToken: refreshTokensSchema.token
|
refreshToken: jwtSchema.refreshToken
|
||||||
})
|
})
|
||||||
|
|
||||||
type BodyPostSignoutSchemaType = Static<typeof bodyPostSignoutSchema>
|
type BodyPostSignoutSchemaType = Static<typeof bodyPostSignoutSchema>
|
||||||
@ -32,21 +35,27 @@ export const postSignoutUser: FastifyPluginAsync = async (fastify) => {
|
|||||||
schema: postSignoutSchema,
|
schema: postSignoutSchema,
|
||||||
handler: async (request, reply) => {
|
handler: async (request, reply) => {
|
||||||
const { refreshToken } = request.body
|
const { refreshToken } = request.body
|
||||||
const token = await prisma.refreshToken.findFirst({
|
try {
|
||||||
where: {
|
const userRefreshJWT = jwt.verify(
|
||||||
token: refreshToken
|
refreshToken,
|
||||||
}
|
JWT_REFRESH_SECRET
|
||||||
|
) as UserRefreshJWT
|
||||||
|
const foundRefreshToken = await prisma.refreshToken.findFirst({
|
||||||
|
where: { token: userRefreshJWT.tokenUUID }
|
||||||
})
|
})
|
||||||
if (token == null) {
|
if (foundRefreshToken == null) {
|
||||||
throw fastify.httpErrors.notFound()
|
throw fastify.httpErrors.notFound()
|
||||||
}
|
}
|
||||||
await prisma.refreshToken.delete({
|
await prisma.refreshToken.delete({
|
||||||
where: {
|
where: {
|
||||||
id: token.id
|
id: foundRefreshToken.id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
reply.statusCode = 200
|
reply.statusCode = 200
|
||||||
return {}
|
return {}
|
||||||
|
} catch {
|
||||||
|
throw fastify.httpErrors.notFound()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ export const swaggerOptions: FastifyDynamicSwaggerOptions = {
|
|||||||
},
|
},
|
||||||
tags: [
|
tags: [
|
||||||
{ name: 'users' },
|
{ name: 'users' },
|
||||||
|
{ name: 'oauth2' },
|
||||||
{ name: 'guilds' },
|
{ name: 'guilds' },
|
||||||
{ name: 'channels' },
|
{ name: 'channels' },
|
||||||
{ name: 'messages' },
|
{ name: 'messages' },
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import nodemailer from 'nodemailer'
|
import nodemailer from 'nodemailer'
|
||||||
import type SMTPTransport from 'nodemailer/lib/smtp-transport.js'
|
import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js'
|
||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
const EMAIL_PORT = parseInt(process.env.EMAIL_PORT ?? '465', 10)
|
const EMAIL_PORT = parseInt(process.env.EMAIL_PORT ?? '465', 10)
|
||||||
|
@ -8,7 +8,9 @@ await tap.test('tools/plugins/socket-io', async (t) => {
|
|||||||
const PORT = 3030
|
const PORT = 3030
|
||||||
const application = fastify()
|
const application = fastify()
|
||||||
await application.register(fastifySocketIo)
|
await application.register(fastifySocketIo)
|
||||||
await application.listen(PORT)
|
await application.listen({
|
||||||
|
port: PORT
|
||||||
|
})
|
||||||
t.not(application.io, null)
|
t.not(application.io, null)
|
||||||
await application.close()
|
await application.close()
|
||||||
})
|
})
|
||||||
|
@ -80,8 +80,8 @@ export default fastifyPlugin(
|
|||||||
emitToAuthorizedUsers,
|
emitToAuthorizedUsers,
|
||||||
emitToMembers
|
emitToMembers
|
||||||
}
|
}
|
||||||
await fastify.decorate('io', io)
|
fastify.decorate('io', io)
|
||||||
await fastify.addHook('onClose', (fastify) => {
|
fastify.addHook('onClose', (fastify) => {
|
||||||
fastify.io.instance.close()
|
fastify.io.instance.close()
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { randomUUID } from 'node:crypto'
|
||||||
|
|
||||||
import { Type } from '@sinclair/typebox'
|
import { Type } from '@sinclair/typebox'
|
||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import ms from 'ms'
|
import ms from 'ms'
|
||||||
@ -22,7 +24,7 @@ export const jwtSchema = {
|
|||||||
refreshToken: Type.String(),
|
refreshToken: Type.String(),
|
||||||
expiresIn: Type.Integer({
|
expiresIn: Type.Integer({
|
||||||
description:
|
description:
|
||||||
'expiresIn is how long, in milliseconds, until the returned accessToken expires'
|
'expiresIn is how long, in milliseconds, until the accessToken expires'
|
||||||
}),
|
}),
|
||||||
type: Type.Literal('Bearer')
|
type: Type.Literal('Bearer')
|
||||||
}
|
}
|
||||||
@ -30,15 +32,20 @@ export const jwtSchema = {
|
|||||||
export const expiresIn = ms(JWT_ACCESS_EXPIRES_IN)
|
export const expiresIn = ms(JWT_ACCESS_EXPIRES_IN)
|
||||||
|
|
||||||
export const generateAccessToken = (user: UserJWT): string => {
|
export const generateAccessToken = (user: UserJWT): string => {
|
||||||
return jwt.sign(user, JWT_ACCESS_SECRET, {
|
return jwt.sign(user, JWT_ACCESS_SECRET, { expiresIn })
|
||||||
expiresIn: JWT_ACCESS_EXPIRES_IN
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const generateRefreshToken = async (user: UserJWT): Promise<string> => {
|
export const generateRefreshToken = async (user: UserJWT): Promise<string> => {
|
||||||
const refreshToken = jwt.sign(user, JWT_REFRESH_SECRET)
|
const tokenUUID = randomUUID()
|
||||||
|
const refreshToken = jwt.sign(
|
||||||
|
{
|
||||||
|
...user,
|
||||||
|
tokenUUID
|
||||||
|
},
|
||||||
|
JWT_REFRESH_SECRET
|
||||||
|
)
|
||||||
await prisma.refreshToken.create({
|
await prisma.refreshToken.create({
|
||||||
data: { token: refreshToken, userId: user.id }
|
data: { token: tokenUUID, userId: user.id }
|
||||||
})
|
})
|
||||||
return refreshToken
|
return refreshToken
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@
|
|||||||
"target": "ESNext",
|
"target": "ESNext",
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"lib": ["ESNext"],
|
"lib": ["ESNext"],
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "Node",
|
||||||
"outDir": "./build",
|
"outDir": "./build",
|
||||||
"rootDir": "./src",
|
"rootDir": "./src",
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
"esModuleInterop": true
|
"esModuleInterop": true
|
||||||
},
|
}
|
||||||
"exclude": ["node_modules", "generators"]
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user