|
| 1 | +const express = require('express'); |
| 2 | +const bodyParser = require('body-parser'); |
| 3 | +const jwt = require('jsonwebtoken'); |
| 4 | +const cors = require('cors'); |
| 5 | + |
| 6 | +const app = express(); |
| 7 | +const port = process.env.PORT || 3000; |
| 8 | + |
| 9 | +app.use(bodyParser.json()); |
| 10 | +app.use(cors({ origin: '*' })); |
| 11 | + |
| 12 | +const JWT_SECRET = 'test-jwt-secret'; |
| 13 | +const TOKEN_EXPIRY = '1h'; |
| 14 | + |
| 15 | +const users = { |
| 16 | + |
| 17 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 18 | + |
| 19 | + password: 'password123', |
| 20 | + name: 'Test User' |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +const sessions = {}; |
| 25 | + |
| 26 | +app.get('/health', (req, res) => { |
| 27 | + res.status(200).json({ status: 'ok' }); |
| 28 | +}); |
| 29 | + |
| 30 | + |
| 31 | +app.post('/auth/v1/token', (req, res) => { |
| 32 | + const { email, password } = req.body; |
| 33 | + |
| 34 | + if (req.body.grant_type === 'refresh_token') { |
| 35 | + const { refresh_token } = req.body; |
| 36 | + |
| 37 | + if (!refresh_token || !sessions[refresh_token]) { |
| 38 | + return res.status(401).json({ error: 'Invalid refresh token' }); |
| 39 | + } |
| 40 | + |
| 41 | + const user = sessions[refresh_token].user; |
| 42 | + const newTokens = generateTokens(user); |
| 43 | + |
| 44 | + delete sessions[refresh_token]; |
| 45 | + sessions[newTokens.refresh_token] = { |
| 46 | + user, |
| 47 | + access_token: newTokens.access_token |
| 48 | + }; |
| 49 | + |
| 50 | + return res.json({ |
| 51 | + access_token: newTokens.access_token, |
| 52 | + refresh_token: newTokens.refresh_token, |
| 53 | + user: user, |
| 54 | + expires_in: 3600 |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + if (!email || !password) { |
| 59 | + return res.status(400).json({ error: 'Email and password are required' }); |
| 60 | + } |
| 61 | + |
| 62 | + if (!users[email] || users[email].password !== password) { |
| 63 | + return res.status(401).json({ error: 'Invalid login credentials' }); |
| 64 | + } |
| 65 | + |
| 66 | + const user = { ...users[email] }; |
| 67 | + delete user.password; |
| 68 | + |
| 69 | + const tokens = generateTokens(user); |
| 70 | + |
| 71 | + sessions[tokens.refresh_token] = { |
| 72 | + user, |
| 73 | + access_token: tokens.access_token |
| 74 | + }; |
| 75 | + |
| 76 | + res.json({ |
| 77 | + access_token: tokens.access_token, |
| 78 | + refresh_token: tokens.refresh_token, |
| 79 | + user: user, |
| 80 | + expires_in: 3600 |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +app.post('/auth/v1/signup', (req, res) => { |
| 85 | + const { email, password, name } = req.body; |
| 86 | + |
| 87 | + if (!email || !password) { |
| 88 | + return res.status(400).json({ error: 'Email and password are required' }); |
| 89 | + } |
| 90 | + |
| 91 | + if (users[email]) { |
| 92 | + return res.status(400).json({ error: 'User already exists' }); |
| 93 | + } |
| 94 | + |
| 95 | + const userId = `user-${Date.now()}`; |
| 96 | + users[email] = { |
| 97 | + id: userId, |
| 98 | + email, |
| 99 | + password, |
| 100 | + name: name || email.split('@')[0] |
| 101 | + }; |
| 102 | + |
| 103 | + const user = { ...users[email] }; |
| 104 | + delete user.password; |
| 105 | + |
| 106 | + const tokens = generateTokens(user); |
| 107 | + |
| 108 | + sessions[tokens.refresh_token] = { |
| 109 | + user, |
| 110 | + access_token: tokens.access_token |
| 111 | + }; |
| 112 | + |
| 113 | + res.json({ |
| 114 | + access_token: tokens.access_token, |
| 115 | + refresh_token: tokens.refresh_token, |
| 116 | + user: user, |
| 117 | + expires_in: 3600 |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +app.get('/auth/v1/user', (req, res) => { |
| 122 | + const authHeader = req.headers.authorization; |
| 123 | + |
| 124 | + if (!authHeader || !authHeader.startsWith('Bearer ')) { |
| 125 | + return res.status(401).json({ error: 'Missing or invalid token' }); |
| 126 | + } |
| 127 | + |
| 128 | + const token = authHeader.split(' ')[1]; |
| 129 | + |
| 130 | + try { |
| 131 | + const decoded = jwt.verify(token, JWT_SECRET); |
| 132 | + const userId = decoded.sub; |
| 133 | + |
| 134 | + let user = null; |
| 135 | + for (const email in users) { |
| 136 | + if (users[email].id === userId) { |
| 137 | + user = { ...users[email] }; |
| 138 | + delete user.password; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (!user) { |
| 144 | + return res.status(404).json({ error: 'User not found' }); |
| 145 | + } |
| 146 | + |
| 147 | + res.json({ user }); |
| 148 | + } catch (error) { |
| 149 | + console.error('Token verification error:', error); |
| 150 | + res.status(401).json({ error: 'Invalid token' }); |
| 151 | + } |
| 152 | +}); |
| 153 | + |
| 154 | +app.post('/auth/v1/logout', (req, res) => { |
| 155 | + const authHeader = req.headers.authorization; |
| 156 | + |
| 157 | + if (!authHeader || !authHeader.startsWith('Bearer ')) { |
| 158 | + return res.status(401).json({ error: 'Missing or invalid token' }); |
| 159 | + } |
| 160 | + |
| 161 | + const token = authHeader.split(' ')[1]; |
| 162 | + |
| 163 | + for (const refreshToken in sessions) { |
| 164 | + if (sessions[refreshToken].access_token === token) { |
| 165 | + delete sessions[refreshToken]; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + res.json({ message: 'Signed out successfully' }); |
| 170 | +}); |
| 171 | + |
| 172 | +function generateTokens(user) { |
| 173 | + const accessToken = jwt.sign({ sub: user.id, email: user.email }, JWT_SECRET, { expiresIn: TOKEN_EXPIRY }); |
| 174 | + const refreshToken = `${user.id}-${Date.now()}`; |
| 175 | + |
| 176 | + return { |
| 177 | + access_token: accessToken, |
| 178 | + refresh_token: refreshToken |
| 179 | + }; |
| 180 | +} |
| 181 | + |
| 182 | +app.post('/reset', (req, res) => { |
| 183 | + Object.keys(users).forEach(key => { |
| 184 | + if (key !== '[email protected]') { |
| 185 | + delete users[key]; |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + Object.keys(sessions).forEach(key => { |
| 190 | + delete sessions[key]; |
| 191 | + }); |
| 192 | + |
| 193 | + res.json({ status: 'ok', message: 'Data reset successful' }); |
| 194 | +}); |
| 195 | + |
| 196 | +app.listen(port, () => { |
| 197 | + console.log(`Mock Auth API server running on port ${port}`); |
| 198 | +}); |
0 commit comments