mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Allow setting of custom session cookie name.
This commit is contained in:
parent
b32d24ad80
commit
94cf122847
@ -2,12 +2,12 @@ const std = @import("std");
|
|||||||
|
|
||||||
const jetzig = @import("../../jetzig.zig");
|
const jetzig = @import("../../jetzig.zig");
|
||||||
|
|
||||||
pub const cookie_name = "_jetzig-session";
|
|
||||||
pub const Cipher = std.crypto.aead.chacha_poly.XChaCha20Poly1305;
|
pub const Cipher = std.crypto.aead.chacha_poly.XChaCha20Poly1305;
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
encryption_key: []const u8,
|
encryption_key: []const u8,
|
||||||
cookies: *jetzig.http.Cookies,
|
cookies: *jetzig.http.Cookies,
|
||||||
|
cookie_name: []const u8,
|
||||||
|
|
||||||
initialized: bool = false,
|
initialized: bool = false,
|
||||||
data: jetzig.data.Data,
|
data: jetzig.data.Data,
|
||||||
@ -20,17 +20,21 @@ pub fn init(
|
|||||||
cookies: *jetzig.http.Cookies,
|
cookies: *jetzig.http.Cookies,
|
||||||
encryption_key: []const u8,
|
encryption_key: []const u8,
|
||||||
) Self {
|
) Self {
|
||||||
|
const env_cookie_name = std.process.getEnvVarOwned(allocator, "JETZIG_SESSION_COOKIE") catch null;
|
||||||
|
const cookie_name = env_cookie_name orelse "_jetzig-session";
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.data = jetzig.data.Data.init(allocator),
|
.data = jetzig.data.Data.init(allocator),
|
||||||
.cookies = cookies,
|
.cookies = cookies,
|
||||||
|
.cookie_name = cookie_name,
|
||||||
.encryption_key = encryption_key,
|
.encryption_key = encryption_key,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse session cookie.
|
/// Parse session cookie.
|
||||||
pub fn parse(self: *Self) !void {
|
pub fn parse(self: *Self) !void {
|
||||||
if (self.cookies.get(cookie_name)) |cookie| {
|
if (self.cookies.get(self.cookie_name)) |cookie| {
|
||||||
try self.parseSessionCookie(cookie.value);
|
try self.parseSessionCookie(cookie.value);
|
||||||
} else {
|
} else {
|
||||||
try self.reset();
|
try self.reset();
|
||||||
@ -111,7 +115,7 @@ fn save(self: *Self) !void {
|
|||||||
defer self.allocator.free(encrypted);
|
defer self.allocator.free(encrypted);
|
||||||
const encoded = try jetzig.util.base64Encode(self.allocator, encrypted);
|
const encoded = try jetzig.util.base64Encode(self.allocator, encrypted);
|
||||||
defer self.allocator.free(encoded);
|
defer self.allocator.free(encoded);
|
||||||
try self.cookies.put(.{ .name = cookie_name, .value = encoded });
|
try self.cookies.put(.{ .name = self.cookie_name, .value = encoded });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseSessionCookie(self: *Self, cookie_value: []const u8) !void {
|
fn parseSessionCookie(self: *Self, cookie_value: []const u8) !void {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user