This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
151 lines (122 loc) · 3.66 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Fastify from 'fastify';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcrypt';
import knex from 'knex';
import * as fs from 'fs';
import { ApolloServer, gql } from 'apollo-server-fastify';
const app = Fastify();
const gqlapp = Fastify();
const pg = knex({
client: 'pg',
connection: process.env.PG_CONNECTION_STRING,
});
const USER_TABLE = process.env.USER_TABLE;
const KEY = fs.readFileSync('/etc/jwtkeys/jwt.key');
const KEY_PUB = fs.readFileSync('/etc/jwtkeys/jwt.key.pub');
const typeDefs = gql`
type Query {
no_op: String
}
type Mutation {
update_Password(currentPassword: String!, newPassword: String!): Boolean
}
`
const resolvers = {
Query: {
no_op: () => {
return 'I do nothing';
}
},
Mutation: {
update_Password: async (_, { currentPassword, newPassword }, context) => {
const { id } = context.user;
const user = await pg(USER_TABLE).where({ id }).first();
if (!user) {
throw new Error(`User does not exist`);
}
const valid = await bcrypt.compare(currentPassword, user.password);
if (!valid) {
throw new Error('Invalid current password provided');
}
await pg(USER_TABLE)
.where({ id })
.update({ password: newPassword });
return true;
}
}
}
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req, connection }) => {
if (connection) {
return connection.context;
} else {
const id = req.headers['x-hasura-user-id'];
const roles = req.headers['x-hasura-allowed-roles'];
return { user: { id, roles } };
}
},
});
gqlapp.register(server.createHandler());
app.get('/', (req, res) => {
res.send('Service running');
});
app.post('/login', async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(500).send('Invalid request');
}
const testUser = await pg(USER_TABLE).where({ username }).first();
if (!testUser) {
return res.send('Invalid login');
}
const valid = await bcrypt.compare(password, testUser.password);
if (!valid) {
return res.send('Invalid login');
}
const claim = {
// TODO: find a better way to asign the default role
name: testUser.username,
'https://hasura.io/jwt/claims': {
'x-hasura-allowed-roles': testUser.roles,
'x-hasura-default-role': testUser.roles[0],
'x-hasura-user-id': testUser.id.toString()
}
};
jwt.sign(claim, KEY, { expiresIn: '1d', algorithm: 'RS256' }, (err, token) => {
if (err) {
res.status(500).send(JSON.stringify(err));
} else {
res.send({ token });
}
});
});
app.post('/verifyToken', async (req, res) => {
const { token } = req.body;
if (!token) {
return res.status(500).send('Invalid request');
}
jwt.verify(token, KEY_PUB, (err, decoded) => {
if (err) {
res.send({ valid: false });
} else {
// TODO: also ensure that the user still exists
res.send({ valid: true });
}
});
});
app.listen(3000, '0.0.0.0', (err, address) => {
if (err) {
app.log.error(err)
process.exit(1)
}
app.log.info(`server listening on ${address}`);
});
gqlapp.listen(80, '0.0.0.0', (err, address) => {
if (err) {
gqlapp.log.error(err)
process.exit(1)
}
gqlapp.log.info(`gql server listening on ${address}`);
});