Agent Harness · VibeGit
Agent Harness Публичная копия обвязки агентной разработки: гейты, journeys, скиллы и шаблоны. Без ключей и личных данных.
agent-harness / templates / backend / auth-postgres / server.mjs 135 строк · изменён 6 нояб., 03:33
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
import http from 'node:http' ;
import crypto from 'node:crypto' ;
import pg from 'pg' ;
const pool = new pg. Pool ( { connectionString : process. env . DATABASE_URL } ) ;
const PORT = Number ( process. env . PORT || 8141 ) ;
const SESSION_DAYS = 30 ;
const
salt crypto
hash crypto pass salt
saltHex hashHex stored
hash crypto pass saltHex
crypto hash hashHex
res code
res obj
data
req
data c
data
req data
v v
maxAge expiresAt
res
token crypto
tokenHash crypto token
expires
pool
tokenHash userId expires
res token expires
cookie req
sid cookie s s
sid
tokenHash crypto sid
rows pool
tokenHash
rows
attempts
now
rec attempts ip now t
rec now
attempts ip rec
rec
server http
url req
req url
req res
email name password req
email res
password password
res
mail email
rows pool
mail name password
res rows
res
e
e res
e
req url
req res
email password req
mail email
rows pool mail
res
rows
password password rows
res rows
res
req url
cookie req
sid cookie s s
sid
tokenHash crypto sid
pool tokenHash
res
res
req url
user req
user res
res user
res
e
e
res
server
hashPass
=
(
pass
)
=>
{
const
=
.
randomBytes
(
16
)
;
const
=
.
scryptSync
(
,
,
64
)
;
return
` ${ salt. toString ( 'hex' ) } : ${ hash. toString ( 'hex' ) } `
;
}
;
const
checkPass
=
(
pass, stored
)
=>
{
const
[
,
]
=
.
split
(
':'
)
;
const
=
.
scryptSync
(
,
Buffer
.
from
(
,
'hex'
)
,
64
)
;
return
.
timingSafeEqual
(
,
Buffer
.
from
(
,
'hex'
)
)
;
}
;
const
json
=
(
res, code, obj
)
=>
{
.
writeHead
(
,
{
'Content-Type'
:
'application/json'
}
)
;
.
end
(
JSON
.
stringify
(
)
)
;
}
;
const
readBody
=
(
req
)
=>
new
Promise
(
(
resolve, reject
)
=>
{
let
=
''
;
.
on
(
'data'
,
(
c
)
=>
{
+=
;
if
(
.
length
>
16_384
)
reject
(
new
Error
(
'body too large'
)
)
;
}
)
;
.
on
(
'end'
,
(
)
=>
{
try
{
resolve
(
JSON
.
parse
(
||
'{}'
)
)
;
}
catch
{
reject
(
new
Error
(
'bad json'
)
)
;
}
}
)
;
}
)
;
const
isEmail
=
(
v
)
=>
typeof
===
'string'
&&
/ ^ [ ^ \s @] + @[ ^ \s @] + \. [ ^ \s @] + $ /
.
test
(
.
trim
(
)
)
;
function
setSessionCookie
(
res, token, expiresAt
)
{
const
=
Math
.
floor
(
(
.
getTime
(
)
-
Date
.
now
(
)
)
/
1000
)
;
.
setHeader
(
'Set-Cookie'
,
` sid= ${ token} ; HttpOnly; SameSite=Lax; Path=/; Max-Age= ${ maxAge} `
)
;
}
async
function
createSession
(
res, userId
)
{
const
=
.
randomBytes
(
32
)
.
toString
(
'base64url'
)
;
const
=
.
createHash
(
'sha256'
)
.
update
(
)
.
digest
(
'hex'
)
;
const
=
new
Date
(
Date
.
now
(
)
+
SESSION_DAYS
*
86400_000
)
;
await
.
query
(
'insert into sessions(token_hash, user_id, expires_at) values ($1,$2,$3)'
,
[
,
,
]
)
;
setSessionCookie
(
,
,
)
;
}
async
function
userBySession
(
req
)
{
const
=
.
headers
.
cookie
||
''
;
const
=
.
split
(
';'
)
.
map
(
(
s
)
=>
.
trim
(
)
)
.
find
(
(
s
)
=>
.
startsWith
(
'sid='
)
)
;
if
(
!
)
return
null
;
const
=
.
createHash
(
'sha256'
)
.
update
(
.
slice
(
4
)
)
.
digest
(
'hex'
)
;
const
{
}
=
await
.
query
(
` select u.id, u.email, u.name from sessions s join users u on u.id = s.user_id
where s.token_hash = $1 and s.expires_at > now() `
,
[
]
)
;
return
[
0
]
||
null
;
}
const
=
new
Map
(
)
;
function
rateLimited
(
ip
)
{
const
=
Date
.
now
(
)
;
const
=
(
.
get
(
)
||
[
]
)
.
filter
(
(
t
)
=>
-
<
300_000
)
;
.
push
(
)
;
.
set
(
,
)
;
return
.
length
>
10
;
}
const
=
.
createServer
(
async
(
req, res
)
=>
{
try
{
const
=
new
URL
(
.
url
,
'http://x'
)
;
if
(
.
method
===
'POST'
&&
.
pathname
===
'/api/register'
)
{
if
(
rateLimited
(
.
socket
.
remoteAddress
)
)
return
json
(
,
429
,
{
error
:
'Слишком много попыток'
}
)
;
const
{
,
,
}
=
await
readBody
(
)
;
if
(
!
isEmail
(
)
)
return
json
(
,
400
,
{
error
:
'Некорректный email'
}
)
;
if
(
typeof
!==
'string'
||
.
length
<
8
)
return
json
(
,
400
,
{
error
:
'Пароль — минимум 8 символов'
}
)
;
const
=
.
trim
(
)
.
toLowerCase
(
)
;
try
{
const
{
}
=
await
.
query
(
'insert into users(email, name, pass_hash) values ($1,$2,$3) returning id'
,
[
,
(
||
''
)
.
trim
(
)
.
slice
(
0
,
80
)
,
hashPass
(
)
]
)
;
await
createSession
(
,
[
0
]
.
id
)
;
return
json
(
,
201
,
{
ok
:
true
}
)
;
}
catch
(
)
{
if
(
.
code
===
'23505'
)
return
json
(
,
409
,
{
error
:
'Такой email уже зарегистрирован'
}
)
;
throw
;
}
}
if
(
.
method
===
'POST'
&&
.
pathname
===
'/api/login'
)
{
if
(
rateLimited
(
.
socket
.
remoteAddress
)
)
return
json
(
,
429
,
{
error
:
'Слишком много попыток'
}
)
;
const
{
,
}
=
await
readBody
(
)
;
const
=
(
||
''
)
.
trim
(
)
.
toLowerCase
(
)
;
const
{
}
=
await
.
query
(
'select id, pass_hash from users where email = $1'
,
[
]
)
;
const
fail
=
(
)
=>
json
(
,
401
,
{
error
:
'Неверная пара email/пароль'
}
)
;
if
(
!
[
0
]
)
return
fail
(
)
;
if
(
typeof
!==
'string'
||
!
checkPass
(
,
[
0
]
.
pass_hash
)
)
return
fail
(
)
;
await
createSession
(
,
[
0
]
.
id
)
;
return
json
(
,
200
,
{
ok
:
true
}
)
;
}
if
(
.
method
===
'POST'
&&
.
pathname
===
'/api/logout'
)
{
const
=
.
headers
.
cookie
||
''
;
const
=
.
split
(
';'
)
.
map
(
(
s
)
=>
.
trim
(
)
)
.
find
(
(
s
)
=>
.
startsWith
(
'sid='
)
)
;
if
(
)
{
const
=
.
createHash
(
'sha256'
)
.
update
(
.
slice
(
4
)
)
.
digest
(
'hex'
)
;
await
.
query
(
'delete from sessions where token_hash = $1'
,
[
]
)
;
}
.
setHeader
(
'Set-Cookie'
,
'sid=; HttpOnly; SameSite=Lax; Path=/; Max-Age=0'
)
;
return
json
(
,
200
,
{
ok
:
true
}
)
;
}
if
(
.
method
===
'GET'
&&
.
pathname
===
'/api/me'
)
{
const
=
await
userBySession
(
)
;
if
(
!
)
return
json
(
,
401
,
{
error
:
'unauthorized'
}
)
;
return
json
(
,
200
,
{
}
)
;
}
json
(
,
404
,
{
error
:
'not found'
}
)
;
}
catch
(
)
{
console
.
error
(
)
;
json
(
,
500
,
{
error
:
'internal'
}
)
;
}
}
)
;
.
listen
(
PORT
,
(
)
=>
console
.
log
(
` auth api on : ${ PORT } `
)
)
;