blob: 037d2797f2dbf745fbfa755ad31f17731d695129 [file] [log] [blame]
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -04001/*
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 */
18import { Router } from 'express';
19import asyncHandler from 'express-async-handler';
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050020import { HttpStatusCode, RegisteredNameFoundState } from 'jami-web-common';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040021import { Container } from 'typedi';
22
23import { Jamid } from '../jamid/jamid.js';
24import { authenticateOptionalToken } from '../middleware/auth.js';
25
26const jamid = Container.get(Jamid);
27
28export const nameserverRouter = Router();
29
30nameserverRouter.use(authenticateOptionalToken);
31
32nameserverRouter.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-Raynauld99e5a702022-11-30 22:36:52 -050037 case RegisteredNameFoundState.Found:
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050038 res.status(HttpStatusCode.Ok).send(result);
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040039 break;
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050040 case RegisteredNameFoundState.InvalidResponse:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050041 res.status(HttpStatusCode.BadRequest).send('Invalid username');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040042 break;
43 default:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050044 res.status(HttpStatusCode.NotFound).send('No such username found');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040045 break;
46 }
47 })
48);
49
Ziwei Wang3c941192023-02-15 12:26:43 -050050//Check if the username provided exists or not, if it doesn't exist, it is allowed for registration
51nameserverRouter.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-Raynauld6f9c7ae2022-10-28 11:41:45 -040070nameserverRouter.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-Raynauld99e5a702022-11-30 22:36:52 -050075 case RegisteredNameFoundState.Found:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050076 res.send(result);
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040077 break;
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050078 case RegisteredNameFoundState.InvalidResponse:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050079 res.status(HttpStatusCode.BadRequest).send('Invalid address');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040080 break;
81 default:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050082 res.status(HttpStatusCode.NotFound).send('No such address found');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040083 break;
84 }
85 })
86);