Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [DRAFT] QRCode API #148

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 148 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/tom-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@twake/matrix-identity-server": "*",
"@twake/utils": "*",
"lodash": "^4.17.21",
"qrcode": "^1.5.4",
"redis": "^4.6.6",
"validator": "^13.11.0"
},
Expand All @@ -53,5 +54,8 @@
"ldapjs": "^2.3.3",
"pg": "^8.10.0",
"sqlite3": "^5.1.6"
},
"devDependencies": {
"@types/qrcode": "^1.5.5"
}
}
3 changes: 3 additions & 0 deletions packages/tom-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import userInfoAPIRouter from './user-info-api'
import VaultServer from './vault-api'
import WellKnown from './wellKnown'
import ActiveContacts from './active-contacts-api'
import QRCode from './qrcode-api'

export default class TwakeServer {
conf: Config
Expand Down Expand Up @@ -145,6 +146,7 @@ export default class TwakeServer {
this.idServer.authenticate,
this.logger
)
const qrCodeApi = QRCode(this.idServer, this.conf, this.logger)

this.endpoints.use(privateNoteApi)
this.endpoints.use(mutualRoolsApi)
Expand All @@ -153,6 +155,7 @@ export default class TwakeServer {
this.endpoints.use(userInfoApi)
this.endpoints.use(smsApi)
this.endpoints.use(activeContactsApi)
this.endpoints.use(qrCodeApi)

if (
this.conf.opensearch_is_activated != null &&
Expand Down
44 changes: 44 additions & 0 deletions packages/tom-server/src/qrcode-api/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { type TwakeLogger } from '@twake/logger'
import { type IQRCodeApiController, type IQRCodeService } from '../types'
import QRCodeService from '../services'
import { type Response, type NextFunction } from 'express'
import { type AuthRequest } from '../../types'

class QRCodeApiController implements IQRCodeApiController {
private readonly qrCodeService: IQRCodeService

constructor(private readonly logger: TwakeLogger) {
this.qrCodeService = new QRCodeService(logger)
}

/**
* Get the QR code for the connected user.
*
* @param {AuthRequest} req - The request object.
* @param {Response} res - The response object.
* @param {NextFunction} next - The next function.
*/
get = async (
req: AuthRequest,
res: Response,
next: NextFunction
): Promise<void> => {
try {
const { accessToken } = req

if (accessToken === undefined || accessToken.length === 0) {
res.status(400).json({ error: 'Access token is missing' })
return
}

const qrcode = await this.qrCodeService.get(accessToken)

res.setHeader('Content-Type', 'image/svg+xml')
res.send(qrcode)
} catch (error) {
next(error)
}
}
}

export default QRCodeApiController
1 change: 1 addition & 0 deletions packages/tom-server/src/qrcode-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './routes'
49 changes: 49 additions & 0 deletions packages/tom-server/src/qrcode-api/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import {
getLogger,
type TwakeLogger,
type Config as LoggerConfig
} from '@twake/logger'
import type IdServer from '../../identity-server'
import { type Config } from '../../types'
import { Router } from 'express'
import authMiddleware from '../../utils/middlewares/auth.middleware'
import QRCodeApiController from '../controllers'

export const PATH = '/_twake/v1/qrcode'

export default (
idServer: IdServer,
config: Config,
defaultLogger?: TwakeLogger
): Router => {
const logger = defaultLogger ?? getLogger(config as unknown as LoggerConfig)
const router = Router()
const authenticator = authMiddleware(idServer.authenticate, logger)
const qrCodeController = new QRCodeApiController(logger)

/**
* @openapi
* /_twake/v1/qrcode:
* get:
* tags:
* - QR Code
* description: Get access QR Code
* responses:
* 200:
* description: QR code generated
* content:
* application/json:
* schema:
* type: string
* 400:
* description: Access token is missing
* 500:
* description: Internal server error
* 401:
* description: Unauthorized
*/
router.get(PATH, authenticator, qrCodeController.get)
Dismissed Show dismissed Hide dismissed

return router
}
24 changes: 24 additions & 0 deletions packages/tom-server/src/qrcode-api/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import QRCode from 'qrcode'
import type { IQRCodeService } from '../types'
import type { TwakeLogger } from '@twake/logger'

class QRCodeService implements IQRCodeService {
constructor(private readonly logger: TwakeLogger) {}
/**
* Generates a QR code as a string in SVG format.
*
* @param {string} text - The text to be encoded in the QR code.
* @returns {Promise<string | null>} - The QR code as an SVG string or null if an error occurs.
*/
get = async (text: string): Promise<string | null> => {
try {
return await QRCode.toString(text, { type: 'svg' })
} catch (error) {
this.logger.error('Failed to generate QRCode', { error })

return null
}
}
}

export default QRCodeService
Loading
Loading