Giter VIP home page Giter VIP logo

filesess's People

Contributors

khalyomede avatar

Watchers

 avatar  avatar

filesess's Issues

Add CSRF token support

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)
}

Use vlang/libsodium to encrypt session file content

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)
}

add optionals path and domain to FileSession struct

Currently the cookie will duplicate for different paths as the session is started on differents paths (/, /dashboard, /dashboard/products, ...).

multiple-sessions

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)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.