Skip to content

Commit

Permalink
Add draft tests for simple email notifications
Browse files Browse the repository at this point in the history
and implement new initialization and email verification
  • Loading branch information
mrkvon committed Jan 13, 2024
1 parent ec53a8c commit 18f6980
Show file tree
Hide file tree
Showing 14 changed files with 705 additions and 435 deletions.
10 changes: 10 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ MAILER_IDENTITY_EMAIL=bot@example
MAILER_IDENTITY_PASSWORD=password
MAILER_IDENTITY_PROVIDER=http://localhost:3456

# link to group of users who are allowed to use the service
ALLOWED_GROUPS=

# SMTP Transport for sending emails
# string or undefined
SMTP_TRANSPORT_HOST=
Expand All @@ -39,3 +42,10 @@ DB_USERNAME=
DB_PASSWORD=
DB_HOST=
DB_PORT=

# JWT
# path to JWT (private) key
# you can use the command `openssl ecparam -name prime256v1 -genkey -noout -out ecdsa-p256-private.pem` to generate one
JWT_KEY=./ecdsa-p256-private.pem
# jwt algorithm
JWT_ALG=ES256
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ output
node_modules

database.sqlite

*.pem
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"@types/bcryptjs": "^2.4.2",
"@types/chai": "^4.3.5",
"@types/co-body": "^6.1.0",
"@types/fs-extra": "^11.0.4",
"@types/jsonwebtoken": "^9.0.5",
"@types/koa": "^2.13.7",
"@types/koa-static": "^4.0.3",
"@types/koa__cors": "^4.0.1",
Expand All @@ -37,6 +39,7 @@
"eslint-plugin-prettier": "^5.0.0",
"maildev": "^2.1.0",
"mocha": "^10.2.0",
"msw": "^2.0.14",
"prettier": "^3.0.0",
"rdf-namespaces": "^1.11.0",
"sinon": "^15.2.0",
Expand All @@ -57,6 +60,8 @@
"cross-fetch": "^4.0.0",
"css-authn": "^0.0.11",
"dotenv": "^16.0.0",
"fs-extra": "^11.2.0",
"jsonwebtoken": "^9.0.2",
"koa": "^2.14.2",
"koa-helmet": "^7.0.2",
"koa-static": "^5.0.0",
Expand Down
96 changes: 62 additions & 34 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import Router from '@koa/router'
import Koa from 'koa'
import helmet from 'koa-helmet'
import serve from 'koa-static'
import { isBehindProxy } from './config'
import { allowedGroups, isBehindProxy } from './config'
import {
checkVerificationLink,
finishIntegration,
initializeIntegration,
} from './controllers/integration'
import { getStatus } from './controllers/status'
import { webhookReceiver } from './controllers/webhookReceiver'
import { authorizeGroups } from './middlewares/authorizeGroup'
import { solidAuth } from './middlewares/solidAuth'
import { validateBody } from './middlewares/validate'

Expand All @@ -20,42 +21,69 @@ app.proxy = isBehindProxy
const router = new Router()

router
// .post(
// '/inbox',
// solidAuth,
// /*
// #swagger.requestBody = {
// required: true,
// content: {
// 'application/json': {
// schema: {
// type: 'object',
// properties: {
// '@context': { const: 'https://www.w3.org/ns/activitystreams' },
// '@id': { type: 'string' },
// '@type': { const: 'Add' },
// actor: { type: 'string', format: 'uri' },
// object: { type: 'string', format: 'uri' },
// target: { type: 'string', format: 'email' },
// },
// required: ['@context', '@type', 'actor', 'object', 'target'],
// additionalProperties: false,
// },
// },
// },
// }
// */
// validateBody({
// type: 'object',
// properties: {
// '@context': { const: 'https://www.w3.org/ns/activitystreams' },
// '@id': { type: 'string' },
// '@type': { const: 'Add' },
// actor: { type: 'string', format: 'uri' },
// object: { type: 'string', format: 'uri' },
// target: { type: 'string', format: 'email' },
// },
// required: ['@context', '@type', 'actor', 'object', 'target'],
// additionalProperties: false,
// }),
// initializeIntegration,
// )
.post(
'/inbox',
'/init',
solidAuth,
/*
#swagger.requestBody = {
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
'@context': { const: 'https://www.w3.org/ns/activitystreams' },
'@id': { type: 'string' },
'@type': { const: 'Add' },
actor: { type: 'string', format: 'uri' },
object: { type: 'string', format: 'uri' },
target: { type: 'string', format: 'email' },
},
required: ['@context', '@type', 'actor', 'object', 'target'],
additionalProperties: false,
},
},
},
}
*/
authorizeGroups(allowedGroups),
// #swagger.requestBody = {
// required: true,
// content: {
// 'application/json': {
// schema: {
// type: 'object',
// properties: {
// email: { type: 'string', format: 'email' },
// },
// required: ['email'],
// additionalProperties: false,
// },
// },
// },
// }
validateBody({
type: 'object',
properties: {
'@context': { const: 'https://www.w3.org/ns/activitystreams' },
'@id': { type: 'string' },
'@type': { const: 'Add' },
actor: { type: 'string', format: 'uri' },
object: { type: 'string', format: 'uri' },
target: { type: 'string', format: 'email' },
},
required: ['@context', '@type', 'actor', 'object', 'target'],
properties: { email: { type: 'string', format: 'email' } },
required: ['email'],
additionalProperties: false,
}),
initializeIntegration,
Expand All @@ -71,7 +99,7 @@ app
bodyParser({
enableTypes: ['text', 'json'],
extendTypes: {
json: ['application/ld+json'],
json: ['application/ld+json', 'application/json'],
text: ['text/turtle'],
},
}),
Expand Down
18 changes: 16 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const emailSender = process.env.EMAIL_SENDER

export const port: number = +(process.env.PORT ?? 3005)

// email verification expiration in milliseconds (1 hour)
export const emailVerificationExpiration = 3600 * 1000
// email verification expiration in seconds (1 hour)
export const emailVerificationExpiration = 3600

// configuration of database in form of sequelize options
export const database: Options = {
Expand All @@ -54,3 +54,17 @@ export const database: Options = {
}

export const isBehindProxy = stringToBoolean(process.env.BEHIND_PROXY)

const stringToArray = (value: string | undefined) => {
if (!value) return []
return value.split(/\s*,\s*/)
}

export const allowedGroups = stringToArray(
process.env.ALLOWED_GROUPS ?? 'https://example.com#us',
)

export const jwt = {
key: process.env.JWT_KEY ?? './ecdsa-p256-private.pem',
alg: process.env.JWT_ALG ?? 'ES256',
}
Loading

0 comments on commit 18f6980

Please sign in to comment.