Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { Router } from 'express'; |
| 19 | import asyncHandler from 'express-async-handler'; |
Misha Krieger-Raynauld | 99e5a70 | 2022-11-30 22:36:52 -0500 | [diff] [blame] | 20 | import { HttpStatusCode, RegisteredNameFoundState } from 'jami-web-common'; |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 21 | import { Container } from 'typedi'; |
| 22 | |
| 23 | import { Jamid } from '../jamid/jamid.js'; |
| 24 | import { authenticateOptionalToken } from '../middleware/auth.js'; |
| 25 | |
| 26 | const jamid = Container.get(Jamid); |
| 27 | |
| 28 | export const nameserverRouter = Router(); |
| 29 | |
| 30 | nameserverRouter.use(authenticateOptionalToken); |
| 31 | |
| 32 | nameserverRouter.get( |
| 33 | '/username/:username', |
| 34 | asyncHandler(async (req, res) => { |
| 35 | const result = await jamid.lookupUsername(req.params.username, res.locals.accountId); |
| 36 | switch (result.state) { |
Misha Krieger-Raynauld | 99e5a70 | 2022-11-30 22:36:52 -0500 | [diff] [blame] | 37 | case RegisteredNameFoundState.Found: |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 38 | res.status(HttpStatusCode.Ok).send(result); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 39 | break; |
Misha Krieger-Raynauld | 99e5a70 | 2022-11-30 22:36:52 -0500 | [diff] [blame] | 40 | case RegisteredNameFoundState.InvalidResponse: |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 41 | res.status(HttpStatusCode.BadRequest).send('Invalid username'); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 42 | break; |
| 43 | default: |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 44 | res.status(HttpStatusCode.NotFound).send('No such username found'); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 45 | break; |
| 46 | } |
| 47 | }) |
| 48 | ); |
| 49 | |
Ziwei Wang | 3c94119 | 2023-02-15 12:26:43 -0500 | [diff] [blame] | 50 | //Check if the username provided exists or not, if it doesn't exist, it is allowed for registration |
| 51 | nameserverRouter.get( |
| 52 | '/username/availability/:username', // |
| 53 | asyncHandler(async (req, res) => { |
| 54 | //This is using the same jamid service as the route /username/:username, except the responses are handled different |
| 55 | const result = await jamid.lookupUsername(req.params.username, res.locals.accountId); |
| 56 | switch (result.state) { |
| 57 | case RegisteredNameFoundState.Found: |
| 58 | res.status(HttpStatusCode.Ok).send('taken'); |
| 59 | break; |
| 60 | case RegisteredNameFoundState.InvalidResponse: |
| 61 | res.status(HttpStatusCode.BadRequest).send('Invalid username'); |
| 62 | break; |
| 63 | default: |
| 64 | res.status(HttpStatusCode.Ok).send('available'); |
| 65 | break; |
| 66 | } |
| 67 | }) |
| 68 | ); |
| 69 | |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 70 | nameserverRouter.get( |
| 71 | '/address/:address', |
| 72 | asyncHandler(async (req, res) => { |
| 73 | const result = await jamid.lookupAddress(req.params.address, res.locals.accountId); |
| 74 | switch (result.state) { |
Misha Krieger-Raynauld | 99e5a70 | 2022-11-30 22:36:52 -0500 | [diff] [blame] | 75 | case RegisteredNameFoundState.Found: |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 76 | res.send(result); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 77 | break; |
Misha Krieger-Raynauld | 99e5a70 | 2022-11-30 22:36:52 -0500 | [diff] [blame] | 78 | case RegisteredNameFoundState.InvalidResponse: |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 79 | res.status(HttpStatusCode.BadRequest).send('Invalid address'); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 80 | break; |
| 81 | default: |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 82 | res.status(HttpStatusCode.NotFound).send('No such address found'); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 83 | break; |
| 84 | } |
| 85 | }) |
| 86 | ); |