khalyomede / filesess Goto Github PK
View Code? Open in Web Editor NEWFunctions to manipulate the session based on files for V.
License: MIT License
Functions to manipulate the session based on files for V.
License: MIT License
When compiling, the struct App
in the index.v file of this package collide with a user defined struct with the same name.
It should be commented unless testing locally.
Since #1 will more likely add libsodium, we could use it to generate random strings and pass them in a CSRF token key (modifable by the user), plus offer a way to check if the CSRF token is valid.
import khalyomede.filesess { FileSession }
import vweb
fn (mut app App) index() vweb.Result {
session := FileSession{
encryption_key: "securekey",
folders: "sessions",
context: &app.Context,
// csrf_token_name: "__token" // by default
}
csrf_token_name := session.csrf_token_name
csrf_token := session.get_csrf_token() // Also stores the token in session file at this moment
return app.html('<form action="/new" method="POST"><input type="hidden" name="$csrf_token_name" value="$csrf_token" /></form>')
}
["/new"; post]
fn (mut app App) new() vweb.Result {
session := FileSession{
encryption_key: "securekey",
folders: "sessions",
context: &app.Context,
// csrf_token_name: "__token" // by default
}
if !session.csrf_token_matches() {
// User tried to manipulate the token value, or something prevented the token to correctly be stored in the form
return app.redirect("/")
}
return app.redirect("/")
}
fn main() {
vweb.run(&App{}, 8000)
}
The CSRF token matching only check form POST form data.
We should also have an helper to get the full HTML code to avoid code repetition:
import khalyomede.filesess { FileSession }
import vweb
fn (mut app App) index() vweb.Result {
session := FileSession{
encryption_key: "securekey",
folders: "sessions",
context: &app.Context,
// csrf_token_name: "__token" // by default
}
csrf_token := session.get_html_csrf_token() // <input type="hidden" name="__token" value="BaAbWCX6W2s7U3NNst3E7RcXmsW7QvDB" />
return app.html('<form action="/new" method="POST">$csrf_token</form>')
}
fn main() {
vweb.run(&App{}, 8000)
}
vlang/libsodium It supports encrypting/decrypting strings.
We should have a new required encryption_key
key in the FileSession
struct:
import khalyomede.filesess { FileSession }
import vweb
struct App {
vweb.Context
}
fn (mut app App) index() vweb.Result {
session := FileSession{
encryption_key: "securekey",
folder: "sessions",
context: &app.Context
}
return app.html("<h1>Home page</h1>")
}
fn main() {
vweb.run(&App{}, 8000)
}
Currently the cookie will duplicate for different paths as the session is started on differents paths (/, /dashboard, /dashboard/products, ...).
The cookie should use by default "/" as the path. The domain should remain empty, so that the current one is used (but can be configured to enforce a specific domain).
import khalyomede.filesess { FileSession }
import vweb
struct App {
vweb.Context
}
fn (mut app App) index() vweb.Result {
session := FileSession{
folder: "sessions",
context: &app.Context,
domain: "localhost",
path: "/" // "/" will allow the cookie to be valid for all URLs on localhost, not only "/"
}
session.start() or {
panic(err)
}
return app.html("home")
}
fn main() {
vweb.run(&App{}, 8000)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.