Giter Club home page Giter Club logo

rust-jwt-auth-mysql's Introduction

Rocket.rs + diesel.rs + MySQL - restful JWT auth API boilerplate

An example how to set up Rust server restful API with JWT authentication and ORM under the hood.

  • User passwords hashed (argon2)

Requirements

  1. Configure Rust to satisfy rocket.rs dependencies (Rust nightly build needed)
  2. Install Diesel ORM CLI and dependencies
    cargo install diesel_cli
  3. Install and create Mysql database. More info here
  4. Configure project environment variable in .env file taking username and passwork from Posgresql step
    DATABASE_URL=mysql://username:password@localhost/database_name
    

Installation

  1. First run the migration to create tables and schema
    diesel migration run
  2. Compile the code and run
    cargo run
If everything was installed right and compiles without errors you should see Rocekt server listening at http://localhost:8001

API rout JWT protection

Routs can be protected through JWT check in the message Header

#[get("/sensitive")]
fn sensitive(key: ApiKey) -> String {
    format!("Hello, you have been identified as {}", key.0)
}

API

JWT token life is set to 2 weeks. Can be changed

/user/register

curl -X POST \
  http://localhost:8001/user/register \
  -H 'content-type: application/json' \
  -d '{ "id": "test",
        "password": "12345"
}'

/auth/login

Get a jwt token for the user

curl -X POST \
  http://localhost:8001/auth/login \
  -H 'content-type: application/json' \
  -d '{ "id": "test",
        "password": "12345"
}'

/user

Call a protected route with a JWT in the HEADER authentication (use the token returned from the /auth/login API)

curl -X GET \
  http://localhost:8001/user/sensitive \
  -H 'authentication: eyJ0eXAiOiJKV1QiLCJraWQiOm51bGwsImFsZyI6IkhTMjU2In0.eyJpc3MiOm51bGwsInN1YiI6InRlc3QiLCJhdWQiOm51bGwsImV4cCI6MTU3MzAyNzg5MSwibmJmIjpudWxsLCJpYXQiOm51bGwsImp0aSI6bnVsbH0.DJ5tb/ic91oULyMjZMeam9kMU31sxGSxSnTmTppUhdA'

Based on previous work:

  1. https://github.com/marcocastignoli/rust_rocket_api_authentication
  2. https://github.com/sean3z/rocket-diesel-rest-api-example
  3. https://medium.com/sean3z/building-a-restful-crud-api-with-rust-1867308352d8
  4. https://dzone.com/articles/creating-a-rest-api-in-rust-using-rocket-and-diese

rust-jwt-auth-mysql's People

Contributors

pavelkrolevets avatar

Watchers

James Cloos avatar  avatar  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.