Giter VIP home page Giter VIP logo

csrf-token's Introduction

csrf-token

Generation and verification of CSRF prevention tokens.

Getting Started

Add the following dependency to Cargo.toml.

csrf-token = { git = "ssh://[email protected]/future-science-research/csrf-token.git", branch="v0.2.x" }

Running Example

Clone this repository and checkout the latest stable version.

$ git clone https://github.com/future-science-research/csrf-token.git
$ cd csrf-token
$ git checkout master

Then run the example.

$ cargo run --example main

Access http://localhost:8080/. The form to post a message is shown.

http://localhost:8080/

A CSRF prevention token is attached to the form as a hidden input. Check the source code of the web page.

<input type="hidden" name="csrf_token" value="486d8f4378397926413f24444972e93edb8975e04d3137912a6851aa2223894e156ad99d1af496bb95a25678ab3417a7a84b59fc2e31d7786333d65ae1a7733731ffcce3e55999ed">

Usage

Creating a Token Generator

To generate a token, first of all, create a CsrfTokenGenerator. See examples/main.rs.

let generator = Arc::new(CsrfTokenGenerator::new(secret, Duration::hours(1)));

The first argument is a secret value used for encryption of tokens. 32-byte secret length is recommended.

The second argument is the duration in which the token is valid. It must be sufficient length for human clients to fill the form and submit.

For using the token generator in Actix handlers, you have to immutably share the generator between threads. It can be achieved by using lazy_static macro or state of Actix App. In the example, app state is used.

server::new(move || {
    App::with_state(generator.clone())
    /* ... */
})

Generating a token

Just call generate method. You can get a token as a Vec<u8> value. In the form of the example, a hex-encoded is embedded.

let token = hex::encode(&generator.generate());
HttpResponse::Ok()
    .content_type("text/html")
    .body(FORM_TEMPLATE.replace("{{csrf-token}}", &token))

Verifying a token

Decode the token given by the client and call verify method.

generator.verify(&token)?;

It returns Result<(), CsrfTokenError>.

License

Licensed under the MIT License.

csrf-token's People

Contributors

kazuk avatar avtomat2023 avatar fsr-builder avatar chunhui2001 avatar

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.