ide на основе vs code · VibeGit
ide на основе vs code Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
krutaya-ide-na-osnove-vs-code / aura-team-server / src / routes / auth.ts 131 строк · изменён 14 нояб., 04:52
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
import { hash, verify } from '@node-rs/argon2' ;
import type { FastifyInstance } from 'fastify' ;
import { database } from '../database.js' ;
import { config } from '../config.js' ;
import { userId } from '../access.js' ;
import { accessToken, digest, id, token } from '../security.js' ;
interface Credentials { email: string ; password: displayName
app FastifyInstance
app config rateLimit max timeWindow request reply
email request body email
email request body password length reply
existing database email id
existing reply
userId
passwordHash request body password
database userId email request body displayName email passwordHash
reply
ok
message
app request reply
tokenHash request query token
row database tokenHash user_id
row reply
now
database
database now row user_id
database now tokenHash
reply
app config rateLimit max timeWindow request reply
row database request body email id password_hash verified_at
row password_hash request body password reply
row reply
row verified_at reply
row id
app config rateLimit max timeWindow request reply
user request
request body currentPassword request body newPassword request body newPassword length
reply
row database user password_hash
row row password_hash request body currentPassword reply
passwordHash request body newPassword
database passwordHash user
database user
ok
app config rateLimit max timeWindow
deviceCode
userCode
database deviceCode userCode Date
deviceCode userCode verificationUri expiresIn interval
app _request reply reply
app _request reply reply
app config rateLimit max timeWindow request reply
row database request body email id password_hash verified_at
row password_hash request body password reply
row row verified_at reply
result database row id request body code
result changes reply
ok
app config rateLimit max timeWindow request reply
deviceId request body deviceCode
row database deviceId user_id
row user_id reply pending
claimed database deviceId
claimed changes reply
row user_id
app request reply
row database request body refreshToken id user_id
row reply
claimed database row id
claimed changes reply
row user_id
userId accessToken refreshToken expiresIn
refreshToken
database userId refreshToken Date
accessToken userId refreshToken expiresIn
title
string
;
?
:
string
;
}
const
MIN_PASSWORD_LENGTH
=
8
;
const
DUMMY_PASSWORD_HASH
=
'$argon2id$v=19$m=65536,t=3,p=1$c29tZXNhbHRzb21lc2FsdA$RdescudvJCsgt3ub+b+dWRWJTmaaJBi2VlKkZFHoyBY'
;
export
async
function
authRoutes
(
:
)
:
Promise
<
void
>
{
.
post < { Body: Credentials } >
(
'/v1/auth/register'
,
{
:
{
:
{
:
5
,
:
'15 minutes'
}
}
}
,
async
(
,
)
=>
{
const
=
.
.
?.
trim
(
)
.
toLowerCase
(
)
;
if
(
!
?.
includes
(
'@'
)
||
.
.
?.
<
MIN_PASSWORD_LENGTH
)
{
return
.
badRequest
(
` Use a valid email and a password of at least ${ MIN_PASSWORD_LENGTH } characters `
)
;
}
const
=
.
prepare
(
'SELECT id FROM users WHERE email=?'
)
.
get
(
)
as
{
:
string
}
|
undefined
;
if
(
)
{
return
.
conflict
(
'An account with this email already exists'
)
;
}
const
=
id
(
)
;
const
=
await
hash
(
.
.
)
;
.
prepare
(
'INSERT INTO users(id,email,display_name,password_hash,created_at,verified_at) VALUES(?,?,?,?,?,?)'
)
.
run
(
,
,
.
.
?.
trim
(
)
||
.
split
(
'@'
)
[
0
]
,
,
new
Date
(
)
.
toISOString
(
)
,
new
Date
(
)
.
toISOString
(
)
)
;
return
.
code
(
201
)
.
send
(
{
:
true
,
:
'Account created. You can sign in now.'
}
)
;
}
)
;
.
get < { Querystring: { token? : string } } >
(
'/v1/auth/verify'
,
async
(
,
)
=>
{
const
=
digest
(
.
.
??
''
)
;
const
=
.
prepare
(
'SELECT user_id FROM email_verifications WHERE token_hash=? AND expires_at>? AND consumed_at IS NULL'
)
.
get
(
,
new
Date
(
)
.
toISOString
(
)
)
as
{
:
string
}
|
undefined
;
if
(
!
)
{
return
.
badRequest
(
'Verification link is invalid or expired'
)
;
}
const
=
new
Date
(
)
.
toISOString
(
)
;
.
transaction
(
(
)
=>
{
.
prepare
(
'UPDATE users SET verified_at=? WHERE id=?'
)
.
run
(
,
.
)
;
.
prepare
(
'UPDATE email_verifications SET consumed_at=? WHERE token_hash=?'
)
.
run
(
,
)
;
}
)
(
)
;
return
.
type
(
'text/html'
)
.
send
(
'<!doctype html><meta charset="utf-8"><h1>Email verified</h1><p>You can return to Aura IDE.</p>'
)
;
}
)
;
.
post < { Body: Credentials } >
(
'/v1/auth/login'
,
{
:
{
:
{
:
10
,
:
'15 minutes'
}
}
}
,
async
(
,
)
=>
{
const
=
.
prepare
(
'SELECT id,password_hash,verified_at FROM users WHERE email=?'
)
.
get
(
.
.
?.
trim
(
)
.
toLowerCase
(
)
)
as
{
:
string
;
:
string
;
?
:
string
}
|
undefined
;
if
(
!
await
verify
(
?.
??
DUMMY_PASSWORD_HASH
,
.
.
??
''
)
)
{
return
.
unauthorized
(
'Invalid email or password'
)
;
}
if
(
!
)
{
return
.
unauthorized
(
'Invalid email or password'
)
;
}
if
(
!
.
)
{
return
.
forbidden
(
'Verify your email first'
)
;
}
return
issueTokens
(
.
)
;
}
)
;
.
post < { Body: { currentPassword? : string ; newPassword? : string } } >
(
'/v1/auth/password'
,
{
:
{
:
{
:
5
,
:
'15 minutes'
}
}
}
,
async
(
,
)
=>
{
const
=
await
userId
(
)
;
if
(
!
.
.
||
!
.
.
||
.
.
.
<
MIN_PASSWORD_LENGTH
)
{
return
.
badRequest
(
` The new password must contain at least ${ MIN_PASSWORD_LENGTH } characters `
)
;
}
const
=
.
prepare
(
'SELECT password_hash FROM users WHERE id=?'
)
.
get
(
)
as
{
:
string
}
|
undefined
;
if
(
!
||
!
await
verify
(
.
,
.
.
)
)
{
return
.
unauthorized
(
'Current password is incorrect'
)
;
}
const
=
await
hash
(
.
.
)
;
.
prepare
(
'UPDATE users SET password_hash=? WHERE id=?'
)
.
run
(
,
)
;
.
prepare
(
'UPDATE refresh_tokens SET revoked_at=? WHERE user_id=? AND revoked_at IS NULL'
)
.
run
(
new
Date
(
)
.
toISOString
(
)
,
)
;
return
{
:
true
}
;
}
)
;
.
post
(
'/v1/auth/device'
,
{
:
{
:
{
:
20
,
:
'15 minutes'
}
}
}
,
async
(
)
=>
{
const
=
token
(
)
;
const
=
token
(
6
)
.
slice
(
0
,
8
)
.
toUpperCase
(
)
;
.
prepare
(
'INSERT INTO device_codes(id,user_code_hash,expires_at) VALUES(?,?,?)'
)
.
run
(
digest
(
)
,
digest
(
)
,
new
Date
(
.
now
(
)
+
10
*
60_000
)
.
toISOString
(
)
)
;
return
{
,
,
:
` ${ config. publicUrl} /device `
,
:
600
,
:
5
}
;
}
)
;
.
get
(
'/device'
,
async
(
,
)
=>
.
type
(
'text/html'
)
.
send
(
devicePage
(
)
)
)
;
.
get
(
'/register'
,
async
(
,
)
=>
.
type
(
'text/html'
)
.
send
(
registerPage
(
)
)
)
;
.
post < { Body: Credentials & { code: string } } >
(
'/v1/auth/device/authorize'
,
{
:
{
:
{
:
10
,
:
'15 minutes'
}
}
}
,
async
(
,
)
=>
{
const
=
.
prepare
(
'SELECT id,password_hash,verified_at FROM users WHERE email=?'
)
.
get
(
.
.
?.
trim
(
)
.
toLowerCase
(
)
)
as
{
:
string
;
:
string
;
?
:
string
}
|
undefined
;
if
(
!
await
verify
(
?.
??
DUMMY_PASSWORD_HASH
,
.
.
??
''
)
)
{
return
.
unauthorized
(
'Invalid or unverified account'
)
;
}
if
(
!
||
!
.
)
{
return
.
unauthorized
(
'Invalid or unverified account'
)
;
}
const
=
.
prepare
(
'UPDATE device_codes SET user_id=? WHERE user_code_hash=? AND expires_at>? AND consumed_at IS NULL AND user_id IS NULL'
)
.
run
(
.
,
digest
(
.
.
?.
trim
(
)
.
toUpperCase
(
)
)
,
new
Date
(
)
.
toISOString
(
)
)
;
if
(
.
!==
1
)
{
return
.
badRequest
(
'Code is invalid or expired'
)
;
}
return
{
:
true
}
;
}
)
;
.
post < { Body: { deviceCode? : string } } >
(
'/v1/auth/device/token'
,
{
:
{
:
{
:
150
,
:
'15 minutes'
}
}
}
,
async
(
,
)
=>
{
const
=
digest
(
.
.
??
''
)
;
const
=
.
prepare
(
'SELECT user_id FROM device_codes WHERE id=? AND expires_at>? AND consumed_at IS NULL'
)
.
get
(
,
new
Date
(
)
.
toISOString
(
)
)
as
{
?
:
string
}
|
undefined
;
if
(
!
?.
)
{
return
.
code
(
202
)
.
send
(
{
:
true
}
)
;
}
const
=
.
prepare
(
'UPDATE device_codes SET consumed_at=? WHERE id=? AND consumed_at IS NULL'
)
.
run
(
new
Date
(
)
.
toISOString
(
)
,
)
;
if
(
.
!==
1
)
{
return
.
unauthorized
(
'Device code was already consumed'
)
;
}
return
issueTokens
(
.
)
;
}
)
;
.
post < { Body: { refreshToken? : string } } >
(
'/v1/auth/refresh'
,
async
(
,
)
=>
{
const
=
.
prepare
(
'SELECT id,user_id FROM refresh_tokens WHERE token_hash=? AND expires_at>? AND revoked_at IS NULL'
)
.
get
(
digest
(
.
.
??
''
)
,
new
Date
(
)
.
toISOString
(
)
)
as
{
:
string
;
:
string
}
|
undefined
;
if
(
!
)
{
return
.
unauthorized
(
)
;
}
const
=
.
prepare
(
'UPDATE refresh_tokens SET revoked_at=? WHERE id=? AND revoked_at IS NULL'
)
.
run
(
new
Date
(
)
.
toISOString
(
)
,
.
)
;
if
(
.
!==
1
)
{
return
.
unauthorized
(
)
;
}
return
issueTokens
(
.
)
;
}
)
;
}
async
function
issueTokens
(
:
string
)
:
Promise
<
{
:
string
;
:
string
;
:
number
}
>
{
const
=
token
(
)
;
.
prepare
(
'INSERT INTO refresh_tokens(id,user_id,token_hash,expires_at) VALUES(?,?,?,?)'
)
.
run
(
id
(
)
,
,
digest
(
)
,
new
Date
(
.
now
(
)
+
30
*
24
*
60
*
60_000
)
.
toISOString
(
)
)
;
return
{
:
await
accessToken
(
)
,
,
:
900
}
;
}
function
devicePage
(
)
:
string
{
return
` ${ pageHead ( 'Aura Team sign in' ) } <h1>Connect Aura IDE</h1><form><input name="code" placeholder="Code from Aura IDE" required><input name="email" type="email" placeholder="Email" required><input name="password" type="password" placeholder="Password" required><button>Connect</button></form><p id="result"></p><p><a href="/register">Create an account</a></p><script>document.querySelector("form").onsubmit=async event=>{event.preventDefault();const body=Object.fromEntries(new FormData(event.target));const response=await fetch("/v1/auth/device/authorize",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(body)});document.querySelector("#result").textContent=response.ok?"Connected. Return to Aura IDE.":await response.text()}</script> `
;
}
function
registerPage
(
)
:
string
{
return
` ${ pageHead ( 'Create Aura Team account' ) } <h1>Create Account</h1><form><input name="displayName" placeholder="Display name" required><input name="email" type="email" placeholder="Email" required><input name="password" type="password" minlength="8" placeholder="Password (8+ characters)" required><button>Create Account</button></form><p id="result"></p><script>document.querySelector("form").onsubmit=async event=>{event.preventDefault();const body=Object.fromEntries(new FormData(event.target));const response=await fetch("/v1/auth/register",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(body)});document.querySelector("#result").textContent=response.ok?"Account created. You can sign in now.":await response.text()}</script> `
;
}
function
pageHead
(
:
string
)
:
string
{
return
` <!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title> ${ title} </title><style>body{font:16px system-ui;max-width:420px;margin:10vh auto;padding:24px}input,button{box-sizing:border-box;width:100%;padding:12px;margin:6px 0}</style> `
;
}