Skip to content

Commit

Permalink
✨ QRCode API (#148)
Browse files Browse the repository at this point in the history
* ✨ feat: added QRCode service

* ✨ feat: added QRCode API controller

* ✨ feat: added QRCode API Router

* 🏷️ chore: add QRCode API types

* ✨ feat: added QRCode API

* ➕ chore(deps): added qrcode deps

* ✅ chore: add unit tests

* 🏷️ chore: added qr_code_url to the config

* ✨ feat: use a QRCode url config

* ✅ chore: update unit tests

* 📝 chore: update swagger docs

* 🔧 chore: add qr_code_url to default config

* 🧪 chore: update test config

* ✨ feat: added qrcode token service

* ✨ feat: added qrcode service

* 🎨 chore: expose services

* 🎨 feat: use exposes services

* 🏷️ chore: update qrcode API related types

* ✅ chore: add service tests

* 🧪 chore: update controller tests

* 🔇 chore: remove console log

* 🏷️ chore: add oidc flow related types

* ✨ feat: fetch access_token using OIDC

* 🎨 feat: use cookies to start the oidc flow

* 🐛 fix: Dockerfile QR CODE env variable init

* ✨ feat: use OIDC flow to request a new access_token

* 🧪 chore: update token service unit tests

* 🏷️ chore: add oidc flow related types

* 🧪 chore: update controllers unit test
  • Loading branch information
rezk2ll authored Oct 10, 2024
1 parent 30c9591 commit 3ca01a9
Show file tree
Hide file tree
Showing 21 changed files with 1,262 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ ENV BASE_URL= \
SMS_API_KEY= \
RATE_LIMITING_WINDOW= \
RATE_LIMITING_NB_REQUESTS= \
TRUSTED_PROXIES=
TRUSTED_PROXIES= \
QRCODE_URL=

COPY --from=1 /usr/src/app /usr/src/app/

Expand Down
2 changes: 1 addition & 1 deletion docs/openapi.json

Large diffs are not rendered by default.

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.

3 changes: 2 additions & 1 deletion packages/matrix-client-server/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@
}
],
"sms_folder": "./src/__testData__/sms",
"is_registration_enabled": true
"is_registration_enabled": true,
"qr_code_url": "twake.chat://login"
}
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: 2 additions & 1 deletion packages/tom-server/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@
"sms_api_url": "",
"sms_api_login": "",
"sms_api_key": "",
"trust_x_forwarded_for": false
"trust_x_forwarded_for": false,
"qr_code_url": "twake.chat://login"
}
3 changes: 2 additions & 1 deletion packages/tom-server/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('Tom-server', () => {
userdb_host: userDb,
sms_api_key: '',
sms_api_login: '',
sms_api_url: ''
sms_api_url: '',
qr_code_url: 'http://example.com/'
}
if (process.env.TEST_PG === 'yes') {
conf.database_engine = 'pg'
Expand Down
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
58 changes: 58 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,58 @@
import { type TwakeLogger } from '@twake/logger'
import {
type IQRCodeTokenService,
type IQRCodeApiController,
type IQRCodeService
} from '../types'
import { type Response, type NextFunction } from 'express'
import type { Config, AuthRequest } from '../../types'
import { QRCodeService, QRCodeTokenService } from '../services'

class QRCodeApiController implements IQRCodeApiController {
private readonly qrCodeService: IQRCodeService
private readonly qrCodeTokenService: IQRCodeTokenService

constructor(private readonly logger: TwakeLogger, config: Config) {
this.qrCodeService = new QRCodeService(config, logger)
this.qrCodeTokenService = new QRCodeTokenService(config, 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 cookies = req.headers.cookie

if (cookies === undefined) {
res.status(400).json({ error: 'Cookies are missing' })
return
}

const accessToken = await this.qrCodeTokenService.getAccessToken(cookies)

if (accessToken === null) {
res.status(400).json({ error: 'Invalid access token' })
return
}

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

res.setHeader('Content-Type', 'image/svg+xml')
res.send(qrcode)
} catch (error) {
this.logger.error('Failed to generate QR Code', { 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'
Loading

0 comments on commit 3ca01a9

Please sign in to comment.