Skip to content

Actix jwt #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7a42c78
initial commit
fbucek May 2, 2020
5384562
adding SQLite setup
fbucek May 2, 2020
14bad72
ci: adding sqlite3 dev libraries
fbucek May 2, 2020
1f16a02
working users database
fbucek May 2, 2020
3ed4bdf
reorganization ( divide db / api calls )
fbucek May 6, 2020
ee8f7e0
cargo fmt
fbucek May 6, 2020
a979fc7
fix .gitignore -> missing '/' omitted 'src/db' folder
fbucek May 6, 2020
8522f30
using nafta: adding integrations tests
fbucek May 7, 2020
4a8a0d4
cargo fmt + removed unused imports
fbucek May 7, 2020
061587b
cargo fmt
fbucek May 7, 2020
86602a4
nafta path change
fbucek May 7, 2020
63b8d4d
moved naft to dependencies ( from dev .. )
fbucek May 7, 2020
8dc46d1
nafta crate used as git not relative path
fbucek May 7, 2020
8b0ad60
nafta moved to dev-dependencies
fbucek May 7, 2020
d854f4f
removed tempdir ( included in nafta )
fbucek May 7, 2020
68d9c0a
call_service building -> simpler tests
fbucek May 7, 2020
947b08e
added test method for GET
fbucek May 7, 2020
dc98538
removed unnecessary crate: bytes
fbucek May 7, 2020
a27f994
api::users has config method
fbucek May 7, 2020
37d8656
using crate testax
fbucek May 10, 2020
9408fae
test for add_user and delete user
fbucek May 12, 2020
6d9596e
cargo fmt
fbucek May 12, 2020
bbaaecd
GitHub Actions caching (#9)
fbucek May 12, 2020
8c7689f
InputUser moved into db::users again
fbucek May 12, 2020
8ed57e9
renamed integrations test files ( add test_ as prefix )
fbucek May 12, 2020
188cf49
basic error
fbucek May 12, 2020
314fe52
basic error
fbucek May 12, 2020
3753c48
new user api test
fbucek May 12, 2020
cc4253a
updated db, added jsonwebtoken
fbucek May 13, 2020
5496f8e
Using select for specified columns
fbucek May 13, 2020
84b455f
removed unused + comments
fbucek May 13, 2020
9ae5187
adding login/signup hash reorganizing
fbucek May 14, 2020
083169e
adding login and logout
fbucek May 14, 2020
89847ca
UserInfo as public User private
fbucek May 14, 2020
184b39e
login_session
fbucek May 14, 2020
5fb5a7d
cargo fmt
fbucek May 14, 2020
36e2f23
WIP: have to solve authorization
fbucek May 15, 2020
2b54cbf
logout api works
fbucek May 16, 2020
24376be
api logout works
fbucek May 16, 2020
a6eddd4
removed warnings
fbucek May 16, 2020
621dfb7
reorganized structure
fbucek May 17, 2020
15db0a5
uri path changed + better errors
fbucek May 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ jobs:
build-linux:
runs-on: ubuntu-latest
steps:
- name: Install sqlite
run: sudo apt-get install libsqlite3-dev
- uses: actions/checkout@v1
# Caching
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
# Build & Test
- name: Build
run: cargo build
- name: Run tests
Expand All @@ -16,7 +35,24 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
# Caching
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
# Build & Test
- name: Build
run: cargo build
run: cargo build
- name: Run tests
run: cargo test
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "AAA 'actix_jwt' test",
"cargo": {
"args": [
"cargo test",
"--package=actixjwt",
"--no-fail-fast",
"--",
"--nocapture"
],
"filter": {
"name": "actix_async",
"kind": "bin"
}
},
"args": [],
"env": { "RUST_LOG":"actix_jwt=trace" },
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"actixasync",
"actixcomplex/backend",
"actixfileupload",
"actix-jwt",
"actixssl",
"actixsslproxy",
"async-std",
Expand All @@ -16,7 +17,7 @@ members = [
"tokio-async",
]

exclude = [
exclude = [
"actixcomplex/frontendyew"
]

Expand Down
2 changes: 2 additions & 0 deletions actix-jwt/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_URL=db/actix-jwd.sqlite
# TODO: add JWT_SECRET
1 change: 1 addition & 0 deletions actix-jwt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/db/
51 changes: 51 additions & 0 deletions actix-jwt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
name = "actixjwt"
version = "0.1.0"
authors = ["Filip Bucek <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Common
log = "0.4"
env_logger = "0.7"
dotenv = "0.15"
anyhow = "1.0"
derive_more = "0.99"

# Web
actix-web = "2.0"
actix-web-httpauth = "0.4"
actix-service = "1.0"
alcoholic_jwt = "1.0"
reqwest = "0.10"
actix-rt = "1.1"
actix-http = "1.0"
jsonwebtoken = "7.1"

# Crypto / Generators
rust-argon2 = "0.8"
uuid = { version = "0.8", features = ["v4"] }


chrono = { version = "0.4", features = ["serde"] }
# derive_more = "0.99"

# Database
diesel = { version = "1.4", features = ["sqlite", "uuidv07", "r2d2", "chrono"] }
diesel_migrations = "1.4"
r2d2 = "0.8"

# Async
futures = "0.3"

# Serialization
serde = "1"
serde_derive = "1"
serde_json = "1"


[dev-dependencies]
nafta = "0.1"
testax = "0.2"
17 changes: 17 additions & 0 deletions actix-jwt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Actix JWT with SQLite

Source: [Build an API in Rust with JWT Authentication](https://auth0.com/blog/build-an-api-in-rust-with-jwt-authentication-using-actix-web/)

Sqlite choosed because of easier setup.

## How to run

### Devel

- `cd actix-jwt`
- `cargo watch -x "run --bin actixjwt"` start server
- `cargo watch -s "./check.sh"` -> will perform checking waiting for server ( run in second terminal )

### Secret key

`secret.key` generated using `head -c16 /dev/urandom > secret.key`
79 changes: 79 additions & 0 deletions actix-jwt/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash

# src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1
set -e # error -> trap -> exit
function info() { echo -e "[\033[0;34m $@ \033[0m]"; } # blue: [ info message ]
function pass() { echo -e "[\033[0;32mPASS\033[0m] $@"; } # green: [PASS]
function fail() { FAIL="true"; echo -e "[\033[0;31mFAIL\033[0m] $@"; } # red: [FAIL]
trap 'LASTRES=$?; LAST=$BASH_COMMAND; if [[ LASTRES -ne 0 ]]; then fail "Command: \"$LAST\" exited with exit code: $LASTRES"; elif [ "$FAIL" == "true" ]; then fail finished with error; else echo -e "[\033[0;32m Finished $@ \033[0m]";fi' EXIT
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # this source dir

info "Run integrations tests"
# Backtrace: RUST_BACKTRACE=1
# RUST_BACKTRACE=1 RUST_LOG=actix_jwt=trace cargo test --package actixjwt --no-fail-fast -- --nocapture
RUST_LOG=actixjwt=trace cargo test --package actixjwt --no-fail-fast -- --nocapture

info "Sleep for until port is open"
while ! nc -z localhost 8080; do
sleep 0.1 # wait for 1/10 of the second before check again
done

info "Signup user"
curl -i -X POST 'http://127.0.0.1:8080/api/users/signup' \
-H "Content-Type: application/json" \
--data '{
"username": "johndoe",
"password": "Strong password",
"email": "[email protected]"
}'

echo ""
info "Signup user - expecting error"
curl -i -X POST 'http://127.0.0.1:8080/api/users/signup' \
-H "Content-Type: application/json" \
--data '{
"username": "johndoe",
"password": "Strong password",
"email": "[email protected]"
}'

echo ""
info "Login user"
curl -i -X POST 'http://127.0.0.1:8080/api/users/login' \
-H "Content-Type: application/json" \
--data '{
"username": "johndoe",
"password": "Strong password"
}'

# TODO: remove in favour of intergration tests
echo ""
info "Get user"
curl -X GET 'http://127.0.0.1:8080/users/1'

info "Add user"
curl -i -X POST 'http://127.0.0.1:8080/users' \
-H "Content-Type: application/json" \
--data '{
"username": "johndoe",
"password": "Strong password",
"email": "[email protected]"
}'

info "Get user"
curl -X GET 'http://127.0.0.1:8080/users'

info "Delete user"
curl -X DELETE 'http://127.0.0.1:8080/users/1'


info "Sign up"
curl -X POST 'http://127.0.0.1:8080/api/auth/signup' \
-H "Content-Type: application/json" \
--data '{
"username": "johndoe",
"email": "[email protected]",
"password": "4S3cr3tPa55w0rd"
}'


7 changes: 7 additions & 0 deletions actix-jwt/diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# For documentation on how to configure this file,
# see diesel.rs/guides/configuring-diesel-cli

# automatically created using `diesel setup`

[print_schema]
file = "src/db/schema.rs"
Empty file added actix-jwt/migrations/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions actix-jwt/migrations/2020-05-02-121007_add_users/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE users;
12 changes: 12 additions & 0 deletions actix-jwt/migrations/2020-05-02-121007_add_users/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Your SQL goes here

CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY,
username TEXT NOT NULL UNIQUE, -- Must be unique
password TEXT NOT NULL,
-- first_name TEXT NOT NULL,
-- last_name TEXT NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMP NOT NULL,
login_session TEXT NOT NULL
);
1 change: 1 addition & 0 deletions actix-jwt/salt.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
34iv;ln3s
1 change: 1 addition & 0 deletions actix-jwt/secret.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
?�1*�F%�9��
7 changes: 7 additions & 0 deletions actix-jwt/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct TokenBodyResponse {
pub token: String,
pub token_type: String,
}
11 changes: 11 additions & 0 deletions actix-jwt/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use diesel::prelude::SqliteConnection;
use diesel::r2d2::{self, ConnectionManager};

// Reexport
pub use diesel::prelude::*;

pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
pub type Conn = r2d2::PooledConnection<ConnectionManager<SqliteConnection>>;

pub mod schema;
pub mod users;
10 changes: 10 additions & 0 deletions actix-jwt/src/db/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
table! {
users (id) {
id -> Integer,
username -> Text,
password -> Text,
email -> Text,
created_at -> Timestamp,
login_session -> Text,
}
}
Loading