mirror of
https://git.tesses.org/tesses50/crosslangextras.git
synced 2026-06-13 07:15:31 +00:00
Make CPKG more complete
This commit is contained in:
@@ -4,12 +4,37 @@ func DB.GetUserIdFromSession(session)
|
||||
var dbCon = DB.Open();
|
||||
var exec = Sqlite.Exec(dbCon,$"SELECT * FROM sessions WHERE key = {Sqlite.Escape(session)};");
|
||||
|
||||
Sqlite.Close(dbCon);
|
||||
DB.Unlock();
|
||||
|
||||
|
||||
if(TypeOf(exec) == "List" && exec.Length == 1) return ParseLong(exec[0].accountId);
|
||||
if(TypeOf(exec) == "List" && exec.Length == 1)
|
||||
{
|
||||
var expiry = 0;
|
||||
const expires = ParseLong(exec[0].expires);
|
||||
if(TypeIsLong(expires) && expires != 0)
|
||||
{
|
||||
const whenItExpires = ParseLong(expires);
|
||||
const currentTime = DateTime.NowEpoch ?? 0;
|
||||
if(whenItExpires != 0 && currentTime < whenItExpires && (whenItExpires - currentTime) < (DB.Expires-3600))
|
||||
{
|
||||
expiry = currentTime + DB.Expires;
|
||||
Sqlite.Exec(dbCon, $"UPDATE sessions SET expires = {expiry} WHERE key = {Sqlite.Escape(session)};");
|
||||
}
|
||||
else if(whenItExpires != 0 && currentTime >= whenItExpires)
|
||||
{
|
||||
Sqlite.Exec(dbCon, $"DELETE FROM sessions WHERE key = {Sqlite.Escape(session)};");
|
||||
Sqlite.Close(dbCon);
|
||||
DB.Unlock();
|
||||
return null;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
Sqlite.Close(dbCon);
|
||||
DB.Unlock();
|
||||
return { accountId = ParseLong(exec[0].accountId), expiry };
|
||||
}
|
||||
Sqlite.Close(dbCon);
|
||||
DB.Unlock();
|
||||
return null;
|
||||
}
|
||||
func DB.GetSessionFromBearer(ctx)
|
||||
{
|
||||
@@ -20,7 +45,7 @@ func DB.GetSessionFromBearer(ctx)
|
||||
if(auth.Length < 2) return null;
|
||||
if(auth[0] != "Bearer") return null;
|
||||
var uid = DB.GetUserIdFromSession(auth[1]);
|
||||
if(uid != -1) return auth[1];
|
||||
if(TypeIsDictionary(uid)) return auth[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -37,23 +62,34 @@ func DB.GetSession(ctx)
|
||||
if(cookieKV.Length == 2 && cookieKV[0] == "Session")
|
||||
{
|
||||
var session = cookieKV[1];
|
||||
var sessionId = DB.GetUserIdFromSession(session);
|
||||
var sessionObj = DB.GetUserIdFromSession(session);
|
||||
|
||||
if(sessionId != -1)
|
||||
if(TypeIsDictionary(sessionObj))
|
||||
{
|
||||
if(sessionObj.expiry > 0)
|
||||
{
|
||||
ctx.WithHeader("Set-Cookie",$"Session={session}; SameSite=Lax; Expires={new DateTime(sessionObj.expiry).ToHttpDate()}");
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
func DB.CreateSession(userId)
|
||||
func DB.CreateSession(userId, doesExpire, name)
|
||||
{
|
||||
const now = DateTime.NowEpoch;
|
||||
const expiryTime = doesExpire ? (DB.Expires + now) : 0;
|
||||
|
||||
|
||||
DB.Lock();
|
||||
var dbCon = DB.Open();
|
||||
|
||||
var rand = Net.Http.UrlEncode(Crypto.Base64Encode(Crypto.RandomBytes(32, "CPKG")));
|
||||
Sqlite.Exec(dbCon, $"INSERT INTO sessions (accountId,key) VALUES ({userId},{Sqlite.Escape(rand)});");
|
||||
Sqlite.Exec(dbCon,$"INSERT INTO sessions (accountId,key, expires, created, name) VALUES ({userId},{Sqlite.Escape(rand)},{Sqlite.Escape(expiryTime)},{Sqlite.Escape(now)}, {Sqlite.Escape(name)});");
|
||||
Sqlite.Close(dbCon);
|
||||
DB.Unlock();
|
||||
return rand;
|
||||
|
||||
Reference in New Issue
Block a user