forked from unimatrix/web
Complete V2 sign-in, permission-aware navigation and retire the V1 shell
This commit is contained in:
parent
8881216e30
commit
81836613b4
2 changed files with 153 additions and 316 deletions
|
|
@ -10,7 +10,7 @@ export async function identity(request,db){
|
|||
const token=sessionToken(request);if(!token)return null;
|
||||
return db.prepare('SELECT a.id,a.username,a.display_name,a.role FROM site_sessions s JOIN site_accounts a ON a.id=s.user_id WHERE s.token_hash=? AND s.expires_at>?').bind(digest(token),Date.now()).first();
|
||||
}
|
||||
export function createAuth(db,origin){
|
||||
export function createAuth(db,origin,{trackFirstLogin=false}={}){
|
||||
const attempts=new Map();
|
||||
function allowed(key,max=10,window=900000){const now=Date.now();if(attempts.size>5000)for(const [k,v]of attempts)if(v.until<now)attempts.delete(k);if(attempts.size>5000)return false;let row=attempts.get(key);if(!row||row.until<now){row={count:0,until:now+window};attempts.set(key,row);}return ++row.count<=max;}
|
||||
async function issue(user){const token=randomBytes(32).toString('base64url');await db.prepare('INSERT INTO site_sessions(token_hash,user_id,expires_at) VALUES(?,?,?)').bind(digest(token),user.id,Date.now()+604800000).run();return json({ok:true,user:await publicUser(db,user)},200,{'Set-Cookie':`${cookieName}=${token}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=604800`});}
|
||||
|
|
@ -28,7 +28,7 @@ export function createAuth(db,origin){
|
|||
const actual=await scrypt(data.currentPassword,row.password_salt,64);
|
||||
if(!timingSafeEqual(actual,Buffer.from(row.password_hash,'hex')))return json({message:'Incorrect current password.'},403);
|
||||
const salt=randomBytes(16).toString('hex'),hash=(await scrypt(data.newPassword,salt,64)).toString('hex');
|
||||
const result=await db.batch([db.prepare('UPDATE site_accounts SET password_salt=?,password_hash=? WHERE id=? AND password_hash=?').bind(salt,hash,user.id,row.password_hash),db.prepare('DELETE FROM site_sessions WHERE user_id=?').bind(user.id)]);
|
||||
const result=await db.batch([db.prepare('UPDATE site_accounts SET password_salt=?,password_hash=? WHERE id=? AND password_hash=?').bind(salt,hash,user.id,row.password_hash),...(trackFirstLogin?[db.prepare('UPDATE site_first_login SET password_changed=1 WHERE user_id=?').bind(user.id)]:[]),db.prepare('DELETE FROM site_sessions WHERE user_id=?').bind(user.id)]);
|
||||
if(!result[0].meta.changes)return json({message:'Account changed. Sign in again.'},409);
|
||||
return issue(row);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,4 +13,7 @@ export async function firstLoginGate(request,db){
|
|||
const path=new URL(request.url).pathname;if(!path.startsWith('/api/')&&!path.startsWith('/sso/')&&!path.startsWith('/git/'))return null;
|
||||
if(['/api/auth/login','/api/auth/logout','/api/auth/session','/api/auth/password','/api/auth/terms','/api/account/email'].includes(path))return null;
|
||||
const state=await firstLoginState(db,await identity(request,db));return state?Response.json({message:'Complete your first sign-in: change your password and verify your own email address.',firstLogin:state,securityUrl:'https://cp.dunerealms.com/#security'},{status:428,headers:{'Cache-Control':'no-store'}}):null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function initializeFirstLogin(db){await db.prepare(FIRST_LOGIN_SCHEMA).run();await db.prepare('CREATE TABLE IF NOT EXISTS site_terms_acceptances(user_id CHAR(36) NOT NULL,version VARCHAR(64) NOT NULL,accepted_at BIGINT NOT NULL,PRIMARY KEY(user_id,version))').run();}
|
||||
export function createFirstLoginTerms(db,origin){return async request=>{const headers={'Cache-Control':'no-store'},reply=(data,status)=>Response.json(data,{status,headers});if(request.method!=='POST')return reply({message:'Method not allowed'},405);const user=await identity(request,db);if(!user)return reply({message:'Sign in first.'},401);if(request.headers.get('Origin')!==origin)return reply({message:'Request origin rejected.'},403);let body;try{body=await request.json();}catch{return reply({message:'Send valid JSON.'},400);}if(body.accepted!==true||body.version!==TERMS_VERSION)return reply({message:'Confirm the current Terms of Service.'},400);await db.prepare('INSERT INTO site_terms_acceptances(user_id,version,accepted_at) VALUES(?,?,?) ON CONFLICT(user_id,version) DO UPDATE SET accepted_at=excluded.accepted_at').bind(user.id,TERMS_VERSION,Date.now()).run();return reply({ok:true},200);};}
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff