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

✨ QRCode API #148

Merged
merged 29 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f94312a
✨ feat: added QRCode service
rezk2ll Sep 27, 2024
2df39a3
✨ feat: added QRCode API controller
rezk2ll Sep 27, 2024
e9a587f
✨ feat: added QRCode API Router
rezk2ll Sep 27, 2024
d057440
🏷️ chore: add QRCode API types
rezk2ll Sep 27, 2024
1be9ba4
✨ feat: added QRCode API
rezk2ll Sep 27, 2024
0d9c7fe
➕ chore(deps): added qrcode deps
rezk2ll Sep 27, 2024
4d0e0fe
✅ chore: add unit tests
rezk2ll Sep 27, 2024
93305e4
🏷️ chore: added qr_code_url to the config
rezk2ll Sep 27, 2024
5a91fa5
✨ feat: use a QRCode url config
rezk2ll Sep 27, 2024
69db856
✅ chore: update unit tests
rezk2ll Sep 27, 2024
fcf2e06
📝 chore: update swagger docs
rezk2ll Sep 27, 2024
595a883
🔧 chore: add qr_code_url to default config
rezk2ll Sep 27, 2024
d710b84
🧪 chore: update test config
rezk2ll Sep 27, 2024
069e693
✨ feat: added qrcode token service
rezk2ll Oct 4, 2024
1694a38
✨ feat: added qrcode service
rezk2ll Oct 4, 2024
a1904f2
🎨 chore: expose services
rezk2ll Oct 4, 2024
4965abd
🎨 feat: use exposes services
rezk2ll Oct 4, 2024
5e78d55
🏷️ chore: update qrcode API related types
rezk2ll Oct 4, 2024
2f14fdb
✅ chore: add service tests
rezk2ll Oct 4, 2024
e7a270a
🧪 chore: update controller tests
rezk2ll Oct 7, 2024
ea6fef1
🔇 chore: remove console log
rezk2ll Oct 7, 2024
9598bd5
🏷️ chore: add oidc flow related types
rezk2ll Oct 7, 2024
cedd269
✨ feat: fetch access_token using OIDC
rezk2ll Oct 7, 2024
9808023
🎨 feat: use cookies to start the oidc flow
rezk2ll Oct 7, 2024
feeaee9
🐛 fix: Dockerfile QR CODE env variable init
rezk2ll Oct 7, 2024
c198590
✨ feat: use OIDC flow to request a new access_token
rezk2ll Oct 8, 2024
8f3901b
🧪 chore: update token service unit tests
rezk2ll Oct 8, 2024
f06dada
🏷️ chore: add oidc flow related types
rezk2ll Oct 8, 2024
aebcb4d
🧪 chore: update controllers unit test
rezk2ll Oct 8, 2024
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