mirror of
https://github.com/actix/actix-extras.git
synced 2025-04-21 17:46:49 +02:00
Compare commits
59 Commits
session-v0
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
c04cc19e73 | ||
|
6a13b3b182 | ||
|
d994912ac2 | ||
|
5f6f20cf37 | ||
|
5145924410 | ||
|
b20dec36ac | ||
|
f6e45d487b | ||
|
c53e198ea7 | ||
|
4d9984ee76 | ||
|
9a08090709 | ||
|
7d3348bb29 | ||
|
c0fa63af39 | ||
|
0b5e2b3647 | ||
|
b95595b9cd | ||
|
4b3f87e915 | ||
|
144c7f92b9 | ||
|
c71b9dd443 | ||
|
282d56e96b | ||
|
d514ad3af5 | ||
|
109e6a4793 | ||
|
bb0c7f21d9 | ||
|
3f7a479a76 | ||
|
fc4b656c3b | ||
|
0f35de7da1 | ||
|
8294fcc645 | ||
|
3de6b03711 | ||
|
64931189c7 | ||
|
265b213123 | ||
|
695369f02f | ||
|
87d9e51112 | ||
|
8c11d37dda | ||
|
d97b36652a | ||
|
98847b9279 | ||
|
cd1b77134e | ||
|
105932706d | ||
|
18f94fa8b5 | ||
|
66b82f0f30 | ||
|
d67abde5f3 | ||
|
3eafe7f5ce | ||
|
3b5f7ae68c | ||
|
036af488fd | ||
|
77406cbb71 | ||
|
2ede588693 | ||
|
21680e0ebe | ||
|
370f9d3033 | ||
|
8f4fb348b3 | ||
|
ff4b173716 | ||
|
49aacfce9f | ||
|
dd20ebb6cb | ||
|
a3211b73d3 | ||
|
a89d3a58bc | ||
|
3c640ec120 | ||
|
26ccf8b200 | ||
|
dd1421f1a0 | ||
|
4eb779be77 | ||
|
48646d1bd3 | ||
|
275675e1c2 | ||
|
50d2fee4e2 | ||
|
0c0d13be12 |
8
.github/workflows/ci-post-merge.yml
vendored
8
.github/workflows/ci-post-merge.yml
vendored
@ -31,12 +31,12 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust (nightly)
|
- name: Install Rust (nightly)
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: nightly
|
toolchain: nightly
|
||||||
|
|
||||||
- name: Install cargo-hack, cargo-ci-cache-clean
|
- name: Install cargo-hack, cargo-ci-cache-clean
|
||||||
uses: taiki-e/install-action@v2.42.14
|
uses: taiki-e/install-action@v2.49.42
|
||||||
with:
|
with:
|
||||||
tool: cargo-hack,cargo-ci-cache-clean
|
tool: cargo-hack,cargo-ci-cache-clean
|
||||||
|
|
||||||
@ -81,12 +81,12 @@ jobs:
|
|||||||
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Install Rust (nightly)
|
- name: Install Rust (nightly)
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: nightly
|
toolchain: nightly
|
||||||
|
|
||||||
- name: Install cargo-hack and cargo-ci-cache-clean
|
- name: Install cargo-hack and cargo-ci-cache-clean
|
||||||
uses: taiki-e/install-action@v2.42.14
|
uses: taiki-e/install-action@v2.49.42
|
||||||
with:
|
with:
|
||||||
tool: cargo-hack,cargo-ci-cache-clean
|
tool: cargo-hack,cargo-ci-cache-clean
|
||||||
|
|
||||||
|
34
.github/workflows/ci.yml
vendored
34
.github/workflows/ci.yml
vendored
@ -44,19 +44,18 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust (${{ matrix.version.name }})
|
- name: Install Rust (${{ matrix.version.name }})
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: ${{ matrix.version.version }}
|
toolchain: ${{ matrix.version.version }}
|
||||||
|
|
||||||
- name: Install cargo-hack and cargo-ci-cache-clean
|
- name: Install cargo-hack and cargo-ci-cache-clean, just
|
||||||
uses: taiki-e/install-action@v2.42.14
|
uses: taiki-e/install-action@v2.49.42
|
||||||
with:
|
with:
|
||||||
tool: cargo-hack,cargo-ci-cache-clean
|
tool: cargo-hack,cargo-ci-cache-clean,just
|
||||||
|
|
||||||
# - name: workaround MSRV issues
|
- name: workaround MSRV issues
|
||||||
# if: matrix.version.name == 'msrv'
|
if: matrix.version.name == 'msrv'
|
||||||
# run: |
|
run: just downgrade-for-msrv
|
||||||
# cargo update -p=time:0.3.20 --precise=0.3.16
|
|
||||||
|
|
||||||
- name: check minimal
|
- name: check minimal
|
||||||
run: cargo ci-min
|
run: cargo ci-min
|
||||||
@ -102,19 +101,18 @@ jobs:
|
|||||||
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Install Rust (${{ matrix.version.name }})
|
- name: Install Rust (${{ matrix.version.name }})
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: ${{ matrix.version.version }}
|
toolchain: ${{ matrix.version.version }}
|
||||||
|
|
||||||
- name: Install cargo-hack, cargo-ci-cache-clean
|
- name: Install cargo-hack, cargo-ci-cache-clean, just
|
||||||
uses: taiki-e/install-action@v2.42.14
|
uses: taiki-e/install-action@v2.49.42
|
||||||
with:
|
with:
|
||||||
tool: cargo-hack,cargo-ci-cache-clean
|
tool: cargo-hack,cargo-ci-cache-clean,just
|
||||||
|
|
||||||
# - name: workaround MSRV issues
|
- name: workaround MSRV issues
|
||||||
# if: matrix.version.name == 'msrv'
|
if: matrix.version.name == 'msrv'
|
||||||
# run: |
|
run: just downgrade-for-msrv
|
||||||
# cargo update -p=time:0.3.20 --precise=0.3.16
|
|
||||||
|
|
||||||
- name: check minimal
|
- name: check minimal
|
||||||
run: cargo ci-min
|
run: cargo ci-min
|
||||||
@ -139,12 +137,12 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust (nightly)
|
- name: Install Rust (nightly)
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: nightly
|
toolchain: nightly
|
||||||
|
|
||||||
- name: Install just
|
- name: Install just
|
||||||
uses: taiki-e/install-action@v2.42.14
|
uses: taiki-e/install-action@v2.49.42
|
||||||
with:
|
with:
|
||||||
tool: just
|
tool: just
|
||||||
|
|
||||||
|
6
.github/workflows/coverage.yml
vendored
6
.github/workflows/coverage.yml
vendored
@ -25,13 +25,13 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: nightly
|
toolchain: nightly
|
||||||
components: llvm-tools-preview
|
components: llvm-tools-preview
|
||||||
|
|
||||||
- name: Install just, cargo-llvm-cov, cargo-nextest
|
- name: Install just, cargo-llvm-cov, cargo-nextest
|
||||||
uses: taiki-e/install-action@v2.42.14
|
uses: taiki-e/install-action@v2.49.42
|
||||||
with:
|
with:
|
||||||
tool: just,cargo-llvm-cov,cargo-nextest
|
tool: just,cargo-llvm-cov,cargo-nextest
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ jobs:
|
|||||||
run: just test-coverage-codecov
|
run: just test-coverage-codecov
|
||||||
|
|
||||||
- name: Upload to Codecov
|
- name: Upload to Codecov
|
||||||
uses: codecov/codecov-action@v4.5.0
|
uses: codecov/codecov-action@v5.4.0
|
||||||
with:
|
with:
|
||||||
files: codecov.json
|
files: codecov.json
|
||||||
fail_ci_if_error: true
|
fail_ci_if_error: true
|
||||||
|
33
.github/workflows/lint.yml
vendored
33
.github/workflows/lint.yml
vendored
@ -16,7 +16,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust (nightly)
|
- name: Install Rust (nightly)
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
toolchain: nightly
|
toolchain: nightly
|
||||||
components: rustfmt
|
components: rustfmt
|
||||||
@ -34,7 +34,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
with:
|
with:
|
||||||
components: clippy
|
components: clippy
|
||||||
|
|
||||||
@ -46,32 +46,3 @@ jobs:
|
|||||||
clippy_flags: >-
|
clippy_flags: >-
|
||||||
--workspace --all-features --tests --examples --bins --
|
--workspace --all-features --tests --examples --bins --
|
||||||
-A unknown_lints -D clippy::todo -D clippy::dbg_macro
|
-A unknown_lints -D clippy::todo -D clippy::dbg_macro
|
||||||
|
|
||||||
public-api-diff:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout ${{ github.base_ref }}
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: ${{ github.base_ref }}
|
|
||||||
|
|
||||||
- name: checkout ${{ github.head_ref }}
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install Rust (nightly)
|
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
|
||||||
with:
|
|
||||||
toolchain: nightly
|
|
||||||
|
|
||||||
- name: Install cargo-public-api
|
|
||||||
uses: taiki-e/cache-cargo-install-action@v2.0.1
|
|
||||||
with:
|
|
||||||
tool: cargo-public-api
|
|
||||||
|
|
||||||
- name: generate API diff
|
|
||||||
run: |
|
|
||||||
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do
|
|
||||||
|
|
||||||
cargo public-api --manifest-path "$f" --all-features diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }} >> /tmp/diff.txt
|
|
||||||
done
|
|
||||||
cat /tmp/diff.txt
|
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
|
||||||
|
|
||||||
guide/build/
|
guide/build/
|
||||||
/gh-pages
|
/gh-pages
|
||||||
|
3292
Cargo.lock
generated
Normal file
3292
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
README.md
20
README.md
@ -33,8 +33,7 @@ These crates are provided by the community.
|
|||||||
| Crate | | |
|
| Crate | | |
|
||||||
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
|
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
|
||||||
| [actix-web-lab] | [][actix-web-lab] [](https://deps.rs/crate/actix-web-lab) | Experimental extractors, middleware, and other extras for possible inclusion in Actix Web. |
|
| [actix-web-lab] | [][actix-web-lab] [](https://deps.rs/crate/actix-web-lab) | Experimental extractors, middleware, and other extras for possible inclusion in Actix Web. |
|
||||||
| [actix-multipart-extract] | [][actix-multipart-extract] [](https://deps.rs/crate/actix-multipart-extract) | Better multipart form support for Actix Web. |
|
| [actix-form-data] | [][actix-form-data] [](https://deps.rs/crate/actix-form-data) | Multipart form data from actix multipart streams. |
|
||||||
| [actix-form-data] | [][actix-form-data] [](https://deps.rs/crate/actix-form-data) | Multipart form data from actix multipart streams |
|
|
||||||
| [actix-governor] | [][actix-governor] [](https://deps.rs/crate/actix-governor) | Rate-limiting backed by governor. |
|
| [actix-governor] | [][actix-governor] [](https://deps.rs/crate/actix-governor) | Rate-limiting backed by governor. |
|
||||||
| [actix-casbin] | [][actix-casbin] [](https://deps.rs/crate/actix-casbin) | Authorization library that supports access control models like ACL, RBAC & ABAC. |
|
| [actix-casbin] | [][actix-casbin] [](https://deps.rs/crate/actix-casbin) | Authorization library that supports access control models like ACL, RBAC & ABAC. |
|
||||||
| [actix-ip-filter] | [][actix-ip-filter] [](https://deps.rs/crate/actix-ip-filter) | IP address filter. Supports glob patterns. |
|
| [actix-ip-filter] | [][actix-ip-filter] [](https://deps.rs/crate/actix-ip-filter) | IP address filter. Supports glob patterns. |
|
||||||
@ -45,10 +44,13 @@ These crates are provided by the community.
|
|||||||
| [awmp] | [][awmp] [](https://deps.rs/crate/awmp) | An easy to use wrapper around multipart fields for Actix Web. |
|
| [awmp] | [][awmp] [](https://deps.rs/crate/awmp) | An easy to use wrapper around multipart fields for Actix Web. |
|
||||||
| [tracing-actix-web] | [][tracing-actix-web] [](https://deps.rs/crate/tracing-actix-web) | A middleware to collect telemetry data from applications built on top of the Actix Web framework. |
|
| [tracing-actix-web] | [][tracing-actix-web] [](https://deps.rs/crate/tracing-actix-web) | A middleware to collect telemetry data from applications built on top of the Actix Web framework. |
|
||||||
| [actix-hash] | [][actix-hash] [](https://deps.rs/crate/actix-hash) | Hashing utilities for Actix Web. |
|
| [actix-hash] | [][actix-hash] [](https://deps.rs/crate/actix-hash) | Hashing utilities for Actix Web. |
|
||||||
| [actix-bincode] |  [](https://deps.rs/crate/actix-bincode) | Bincode payload extractor for Actix Web |
|
| [actix-bincode] |  [](https://deps.rs/crate/actix-bincode) | Bincode payload extractor for Actix Web. |
|
||||||
| [sentinel-actix] |  [](https://deps.rs/crate/sentinel-actix) | General and flexible protection for Actix Web |
|
| [sentinel-actix] |  [](https://deps.rs/crate/sentinel-actix) | General and flexible protection for Actix Web. |
|
||||||
| [actix-telepathy] |  [](https://deps.rs/crate/actix-telepathy) | Build distributed applications with `RemoteActors` and `RemoteMessages`. |
|
| [actix-telepathy] |  [](https://deps.rs/crate/actix-telepathy) | Build distributed applications with `RemoteActors` and `RemoteMessages`. |
|
||||||
| [apistos] |  [](https://deps.rs/crate/apistos) | Automatic OpenAPI v3 documentation for Actix Web |
|
| [apistos] |  [](https://deps.rs/crate/apistos) | Automatic OpenAPI v3 documentation for Actix Web. |
|
||||||
|
| [actix-web-validation] |  [](https://deps.rs/crate/actix-web-validation) | Request validation for Actix Web. |
|
||||||
|
| [actix-jwt-cookies] |  [](https://deps.rs/repo/github/Necoo33/actix-jwt-cookies?path=%2F) | Store your data in encrypted cookies and get it elegantly. |
|
||||||
|
| [actix-ws-broadcaster] |  [](https://deps.rs/repo/github/Necoo33/actix-ws-broadcaster?path=%2F) | A broadcaster library for actix-ws that includes grouping and conditional broadcasting. |
|
||||||
|
|
||||||
To add a crate to this list, submit a pull request.
|
To add a crate to this list, submit a pull request.
|
||||||
|
|
||||||
@ -80,5 +82,9 @@ To add a crate to this list, submit a pull request.
|
|||||||
[actix-hash]: https://crates.io/crates/actix-hash
|
[actix-hash]: https://crates.io/crates/actix-hash
|
||||||
[actix-bincode]: https://crates.io/crates/actix-bincode
|
[actix-bincode]: https://crates.io/crates/actix-bincode
|
||||||
[sentinel-actix]: https://crates.io/crates/sentinel-actix
|
[sentinel-actix]: https://crates.io/crates/sentinel-actix
|
||||||
[actix-telepathy]: https://github.com/wenig/actix-telepathy
|
[actix-telepathy]: https://crates.io/crates/actix-telepathy
|
||||||
[apistos]: https://github.com/netwo-io/apistos
|
[actix-web-validation]: https://crates.io/crates/actix-web-validation
|
||||||
|
[actix-telepathy]: https://crates.io/crates/actix-telepathy
|
||||||
|
[apistos]: https://crates.io/crates/apistos
|
||||||
|
[actix-jwt-cookies]: https://crates.io/crates/actix-jwt-cookies
|
||||||
|
[actix-ws-broadcaster]: https://crates.io/crates/actix-ws-broadcaster
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
## 0.7.1
|
||||||
|
|
||||||
|
- Implement `PartialEq` for `Cors` allowing for better testing.
|
||||||
|
|
||||||
## 0.7.0
|
## 0.7.0
|
||||||
|
|
||||||
- `Cors` is now marked `#[must_use]`.
|
- `Cors` is now marked `#[must_use]`.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-cors"
|
name = "actix-cors"
|
||||||
version = "0.7.0"
|
version = "0.7.1"
|
||||||
authors = [
|
authors = [
|
||||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||||
"Rob Ede <robjtede@icloud.com>",
|
"Rob Ede <robjtede@icloud.com>",
|
||||||
@ -24,7 +24,7 @@ draft-private-network-access = []
|
|||||||
actix-utils = "3"
|
actix-utils = "3"
|
||||||
actix-web = { version = "4", default-features = false }
|
actix-web = { version = "4", default-features = false }
|
||||||
|
|
||||||
derive_more = "0.99.7"
|
derive_more = { version = "2", features = ["display", "error"] }
|
||||||
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
once_cell = "1"
|
once_cell = "1"
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
<!-- prettier-ignore-start -->
|
<!-- prettier-ignore-start -->
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-cors)
|
[](https://crates.io/crates/actix-cors)
|
||||||
[](https://docs.rs/actix-cors/0.7.0)
|
[](https://docs.rs/actix-cors/0.7.1)
|
||||||

|

|
||||||

|

|
||||||
<br />
|
<br />
|
||||||
[](https://deps.rs/crate/actix-cors/0.7.0)
|
[](https://deps.rs/crate/actix-cors/0.7.1)
|
||||||
[](https://crates.io/crates/actix-cors)
|
[](https://crates.io/crates/actix-cors)
|
||||||
[](https://discord.gg/NWpN5mmg3x)
|
[](https://discord.gg/NWpN5mmg3x)
|
||||||
|
|
||||||
|
@ -608,6 +608,19 @@ where
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Cors {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.inner == other.inner
|
||||||
|
// Because of the cors-function, checking if the content is equal implies that the errors are equal
|
||||||
|
//
|
||||||
|
// Proof by contradiction:
|
||||||
|
// Lets assume that the inner values are equal, but the error values are not.
|
||||||
|
// This means there had been an error, which has been fixed.
|
||||||
|
// This cannot happen as the first call to set the invalid value means that further usages of the cors-function will reject other input.
|
||||||
|
// => inner has to be in a different state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
@ -679,4 +692,11 @@ mod test {
|
|||||||
|
|
||||||
Cors::default().new_transform(srv).await.unwrap();
|
Cors::default().new_transform(srv).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn impl_eq() {
|
||||||
|
assert_eq!(Cors::default(), Cors::default());
|
||||||
|
assert_ne!(Cors::default().send_wildcard(), Cors::default());
|
||||||
|
assert_ne!(Cors::default(), Cors::permissive());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,40 @@
|
|||||||
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
||||||
use derive_more::{Display, Error};
|
use derive_more::derive::{Display, Error};
|
||||||
|
|
||||||
/// Errors that can occur when processing CORS guarded requests.
|
/// Errors that can occur when processing CORS guarded requests.
|
||||||
#[derive(Debug, Clone, Display, Error)]
|
#[derive(Debug, Clone, Display, Error)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum CorsError {
|
pub enum CorsError {
|
||||||
/// Allowed origin argument must not be wildcard (`*`).
|
/// Allowed origin argument must not be wildcard (`*`).
|
||||||
#[display(fmt = "`allowed_origin` argument must not be wildcard (`*`)")]
|
#[display("`allowed_origin` argument must not be wildcard (`*`)")]
|
||||||
WildcardOrigin,
|
WildcardOrigin,
|
||||||
|
|
||||||
/// Request header `Origin` is required but was not provided.
|
/// Request header `Origin` is required but was not provided.
|
||||||
#[display(fmt = "Request header `Origin` is required but was not provided")]
|
#[display("Request header `Origin` is required but was not provided")]
|
||||||
MissingOrigin,
|
MissingOrigin,
|
||||||
|
|
||||||
/// Request header `Access-Control-Request-Method` is required but is missing.
|
/// Request header `Access-Control-Request-Method` is required but is missing.
|
||||||
#[display(fmt = "Request header `Access-Control-Request-Method` is required but is missing")]
|
#[display("Request header `Access-Control-Request-Method` is required but is missing")]
|
||||||
MissingRequestMethod,
|
MissingRequestMethod,
|
||||||
|
|
||||||
/// Request header `Access-Control-Request-Method` has an invalid value.
|
/// Request header `Access-Control-Request-Method` has an invalid value.
|
||||||
#[display(fmt = "Request header `Access-Control-Request-Method` has an invalid value")]
|
#[display("Request header `Access-Control-Request-Method` has an invalid value")]
|
||||||
BadRequestMethod,
|
BadRequestMethod,
|
||||||
|
|
||||||
/// Request header `Access-Control-Request-Headers` has an invalid value.
|
/// Request header `Access-Control-Request-Headers` has an invalid value.
|
||||||
#[display(fmt = "Request header `Access-Control-Request-Headers` has an invalid value")]
|
#[display("Request header `Access-Control-Request-Headers` has an invalid value")]
|
||||||
BadRequestHeaders,
|
BadRequestHeaders,
|
||||||
|
|
||||||
/// Origin is not allowed to make this request.
|
/// Origin is not allowed to make this request.
|
||||||
#[display(fmt = "Origin is not allowed to make this request")]
|
#[display("Origin is not allowed to make this request")]
|
||||||
OriginNotAllowed,
|
OriginNotAllowed,
|
||||||
|
|
||||||
/// Request method is not allowed.
|
/// Request method is not allowed.
|
||||||
#[display(fmt = "Requested method is not allowed")]
|
#[display("Requested method is not allowed")]
|
||||||
MethodNotAllowed,
|
MethodNotAllowed,
|
||||||
|
|
||||||
/// One or more request headers are not allowed.
|
/// One or more request headers are not allowed.
|
||||||
#[display(fmt = "One or more request headers are not allowed")]
|
#[display("One or more request headers are not allowed")]
|
||||||
HeadersNotAllowed,
|
HeadersNotAllowed,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +27,12 @@ impl Default for OriginFn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for OriginFn {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
Rc::ptr_eq(&self.boxed_fn, &other.boxed_fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Debug for OriginFn {
|
impl fmt::Debug for OriginFn {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.write_str("origin_fn")
|
f.write_str("origin_fn")
|
||||||
@ -40,7 +46,7 @@ pub(crate) fn header_value_try_into_method(hdr: &HeaderValue) -> Option<Method>
|
|||||||
.and_then(|meth| Method::try_from(meth).ok())
|
.and_then(|meth| Method::try_from(meth).ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub(crate) struct Inner {
|
pub(crate) struct Inner {
|
||||||
pub(crate) allowed_origins: AllOrSome<HashSet<HeaderValue>>,
|
pub(crate) allowed_origins: AllOrSome<HashSet<HeaderValue>>,
|
||||||
pub(crate) allowed_origins_fns: SmallVec<[OriginFn; 4]>,
|
pub(crate) allowed_origins_fns: SmallVec<[OriginFn; 4]>,
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
## 0.8.0
|
||||||
|
|
||||||
|
- Update `actix-session` dependency to `0.10`.
|
||||||
|
|
||||||
## 0.7.1
|
## 0.7.1
|
||||||
|
|
||||||
- Add `IdentityMiddlewareBuilder::{id_key, last_visit_unix_timestamp_key, login_unix_timestamp_key}()` methods for customizing keys used in session. Defaults remain the same as before.
|
- Add `IdentityMiddlewareBuilder::{id_key, last_visit_unix_timestamp_key, login_unix_timestamp_key}()` methods for customizing keys used in session. Defaults remain the same as before.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-identity"
|
name = "actix-identity"
|
||||||
version = "0.7.1"
|
version = "0.8.0"
|
||||||
authors = [
|
authors = [
|
||||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||||
"Luca Palmieri <rust@lpalmieri.com>",
|
"Luca Palmieri <rust@lpalmieri.com>",
|
||||||
@ -23,7 +23,7 @@ actix-session = "0.10"
|
|||||||
actix-utils = "3"
|
actix-utils = "3"
|
||||||
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }
|
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }
|
||||||
|
|
||||||
derive_more = "0.99.7"
|
derive_more = { version = "2", features = ["display", "error", "from"] }
|
||||||
futures-core = "0.3.17"
|
futures-core = "0.3.17"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
<!-- prettier-ignore-start -->
|
<!-- prettier-ignore-start -->
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-identity)
|
[](https://crates.io/crates/actix-identity)
|
||||||
[](https://docs.rs/actix-identity/0.7.1)
|
[](https://docs.rs/actix-identity/0.8.0)
|
||||||

|

|
||||||
[](https://deps.rs/crate/actix-identity/0.7.1)
|
[](https://deps.rs/crate/actix-identity/0.8.0)
|
||||||
|
|
||||||
<!-- prettier-ignore-end -->
|
<!-- prettier-ignore-end -->
|
||||||
|
|
||||||
@ -83,8 +83,10 @@ async fn login(request: HttpRequest) -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/logout")]
|
#[post("/logout")]
|
||||||
async fn logout(user: Identity) -> impl Responder {
|
async fn logout(user: Option<Identity>) -> impl Responder {
|
||||||
|
if let Some(user) = user {
|
||||||
user.logout();
|
user.logout();
|
||||||
|
}
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -13,10 +13,10 @@
|
|||||||
//! http -v --session=identity GET localhost:8080/
|
//! http -v --session=identity GET localhost:8080/
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use std::io;
|
use std::{io, time::Duration};
|
||||||
|
|
||||||
use actix_identity::{Identity, IdentityMiddleware};
|
use actix_identity::{Identity, IdentityMiddleware};
|
||||||
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
|
use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
cookie::Key, get, middleware::Logger, post, App, HttpMessage, HttpRequest, HttpResponse,
|
cookie::Key, get, middleware::Logger, post, App, HttpMessage, HttpRequest, HttpResponse,
|
||||||
HttpServer, Responder,
|
HttpServer, Responder,
|
||||||
@ -28,16 +28,25 @@ async fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
let secret_key = Key::generate();
|
let secret_key = Key::generate();
|
||||||
|
|
||||||
|
let expiration = Duration::from_secs(24 * 60 * 60);
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let session_mw =
|
let session_mw =
|
||||||
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
||||||
// disable secure cookie for local testing
|
// disable secure cookie for local testing
|
||||||
.cookie_secure(false)
|
.cookie_secure(false)
|
||||||
|
// Set a ttl for the cookie if the identity should live longer than the user session
|
||||||
|
.session_lifecycle(
|
||||||
|
PersistentSession::default().session_ttl(expiration.try_into().unwrap()),
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
let identity_mw = IdentityMiddleware::builder()
|
||||||
|
.visit_deadline(Some(expiration))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
// Install the identity framework first.
|
// Install the identity framework first.
|
||||||
.wrap(IdentityMiddleware::default())
|
.wrap(identity_mw)
|
||||||
// The identity system is built on top of sessions. You must install the session
|
// The identity system is built on top of sessions. You must install the session
|
||||||
// middleware to leverage `actix-identity`. The session middleware must be mounted
|
// middleware to leverage `actix-identity`. The session middleware must be mounted
|
||||||
// AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
|
// AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
use actix_session::{SessionGetError, SessionInsertError};
|
use actix_session::{SessionGetError, SessionInsertError};
|
||||||
use actix_web::{cookie::time::error::ComponentRange, http::StatusCode, ResponseError};
|
use actix_web::{cookie::time::error::ComponentRange, http::StatusCode, ResponseError};
|
||||||
use derive_more::{Display, Error, From};
|
use derive_more::derive::{Display, Error, From};
|
||||||
|
|
||||||
/// Error that can occur during login attempts.
|
/// Error that can occur during login attempts.
|
||||||
#[derive(Debug, Display, Error, From)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
pub struct LoginError(SessionInsertError);
|
pub struct LoginError(SessionInsertError);
|
||||||
|
|
||||||
impl ResponseError for LoginError {
|
impl ResponseError for LoginError {
|
||||||
@ -17,7 +17,7 @@ impl ResponseError for LoginError {
|
|||||||
|
|
||||||
/// Error encountered when working with a session that has expired.
|
/// Error encountered when working with a session that has expired.
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
#[display(fmt = "The given session has expired and is no longer valid")]
|
#[display("The given session has expired and is no longer valid")]
|
||||||
pub struct SessionExpiryError(#[error(not(source))] pub(crate) ComponentRange);
|
pub struct SessionExpiryError(#[error(not(source))] pub(crate) ComponentRange);
|
||||||
|
|
||||||
/// The identity information has been lost.
|
/// The identity information has been lost.
|
||||||
@ -25,7 +25,7 @@ pub struct SessionExpiryError(#[error(not(source))] pub(crate) ComponentRange);
|
|||||||
/// Seeing this error in user code indicates a bug in actix-identity.
|
/// Seeing this error in user code indicates a bug in actix-identity.
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
#[display(
|
#[display(
|
||||||
fmt = "The identity information in the current session has disappeared after having been \
|
"The identity information in the current session has disappeared after having been \
|
||||||
successfully validated. This is likely to be a bug."
|
successfully validated. This is likely to be a bug."
|
||||||
)]
|
)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
@ -33,7 +33,7 @@ pub struct LostIdentityError;
|
|||||||
|
|
||||||
/// There is no identity information attached to the current session.
|
/// There is no identity information attached to the current session.
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
#[display(fmt = "There is no identity information attached to the current session")]
|
#[display("There is no identity information attached to the current session")]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct MissingIdentityError;
|
pub struct MissingIdentityError;
|
||||||
|
|
||||||
@ -42,21 +42,21 @@ pub struct MissingIdentityError;
|
|||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum GetIdentityError {
|
pub enum GetIdentityError {
|
||||||
/// The session has expired.
|
/// The session has expired.
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
SessionExpiryError(SessionExpiryError),
|
SessionExpiryError(SessionExpiryError),
|
||||||
|
|
||||||
/// No identity is found in a session.
|
/// No identity is found in a session.
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
MissingIdentityError(MissingIdentityError),
|
MissingIdentityError(MissingIdentityError),
|
||||||
|
|
||||||
/// Failed to accessing the session store.
|
/// Failed to accessing the session store.
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
SessionGetError(SessionGetError),
|
SessionGetError(SessionGetError),
|
||||||
|
|
||||||
/// Identity info was lost after being validated.
|
/// Identity info was lost after being validated.
|
||||||
///
|
///
|
||||||
/// Seeing this error indicates a bug in actix-identity.
|
/// Seeing this error indicates a bug in actix-identity.
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
LostIdentityError(LostIdentityError),
|
LostIdentityError(LostIdentityError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ impl IdentityExt for ServiceRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IdentityExt for GuardContext<'a> {
|
impl IdentityExt for GuardContext<'_> {
|
||||||
fn get_identity(&self) -> Result<Identity, GetIdentityError> {
|
fn get_identity(&self) -> Result<Identity, GetIdentityError> {
|
||||||
Identity::extract(&self.req_data())
|
Identity::extract(&self.req_data())
|
||||||
}
|
}
|
||||||
|
@ -74,8 +74,10 @@ async fn login(request: HttpRequest) -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/logout")]
|
#[post("/logout")]
|
||||||
async fn logout(user: Identity) -> impl Responder {
|
async fn logout(user: Option<Identity>) -> impl Responder {
|
||||||
|
if let Some(user) = user {
|
||||||
user.logout();
|
user.logout();
|
||||||
|
}
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
- Update `redis` dependency to `0.26`.
|
- Update `redis` dependency to `0.29`.
|
||||||
|
- Update `actix-session` dependency to `0.9`.
|
||||||
|
|
||||||
## 0.5.1
|
## 0.5.1
|
||||||
|
|
||||||
|
@ -26,9 +26,9 @@ actix-utils = "3"
|
|||||||
actix-web = { version = "4", default-features = false, features = ["cookies"] }
|
actix-web = { version = "4", default-features = false, features = ["cookies"] }
|
||||||
|
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
derive_more = "0.99.7"
|
derive_more = { version = "2", features = ["display", "error", "from"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
redis = { version = "0.26", default-features = false, features = ["tokio-comp"] }
|
redis = { version = "0.29", default-features = false, features = ["tokio-comp"] }
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
|
|
||||||
# session
|
# session
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use derive_more::{Display, Error, From};
|
use derive_more::derive::{Display, Error, From};
|
||||||
|
|
||||||
use crate::status::Status;
|
use crate::status::Status;
|
||||||
|
|
||||||
@ -6,20 +6,20 @@ use crate::status::Status;
|
|||||||
#[derive(Debug, Display, Error, From)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Redis client failed to connect or run a query.
|
/// Redis client failed to connect or run a query.
|
||||||
#[display(fmt = "Redis client failed to connect or run a query")]
|
#[display("Redis client failed to connect or run a query")]
|
||||||
Client(redis::RedisError),
|
Client(redis::RedisError),
|
||||||
|
|
||||||
/// Limit is exceeded for a key.
|
/// Limit is exceeded for a key.
|
||||||
#[display(fmt = "Limit is exceeded for a key")]
|
#[display("Limit is exceeded for a key")]
|
||||||
#[from(ignore)]
|
#[from(ignore)]
|
||||||
LimitExceeded(#[error(not(source))] Status),
|
LimitExceeded(#[error(not(source))] Status),
|
||||||
|
|
||||||
/// Time conversion failed.
|
/// Time conversion failed.
|
||||||
#[display(fmt = "Time conversion failed")]
|
#[display("Time conversion failed")]
|
||||||
Time(time::error::ComponentRange),
|
Time(time::error::ComponentRange),
|
||||||
|
|
||||||
/// Generic error.
|
/// Generic error.
|
||||||
#[display(fmt = "Generic error")]
|
#[display("Generic error")]
|
||||||
#[from(ignore)]
|
#[from(ignore)]
|
||||||
Other(#[error(not(source))] String),
|
Other(#[error(not(source))] String),
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ impl Status {
|
|||||||
/// Constructs status limit status from parts.
|
/// Constructs status limit status from parts.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub(crate) fn new(count: usize, limit: usize, reset_epoch_utc: usize) -> Self {
|
pub(crate) fn new(count: usize, limit: usize, reset_epoch_utc: usize) -> Self {
|
||||||
let remaining = if count >= limit { 0 } else { limit - count };
|
let remaining = limit.saturating_sub(count);
|
||||||
|
|
||||||
Status {
|
Status {
|
||||||
limit,
|
limit,
|
||||||
|
@ -19,7 +19,7 @@ all-features = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4", default-features = false }
|
actix-web = { version = "4", default-features = false }
|
||||||
derive_more = "0.99.7"
|
derive_more = { version = "2", features = ["display"] }
|
||||||
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
||||||
prost = { version = "0.13", default-features = false }
|
prost = { version = "0.13", default-features = false }
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ use actix_web::{
|
|||||||
Error, FromRequest, HttpMessage, HttpRequest, HttpResponse, HttpResponseBuilder, Responder,
|
Error, FromRequest, HttpMessage, HttpRequest, HttpResponse, HttpResponseBuilder, Responder,
|
||||||
ResponseError,
|
ResponseError,
|
||||||
};
|
};
|
||||||
use derive_more::Display;
|
use derive_more::derive::Display;
|
||||||
use futures_util::{
|
use futures_util::{
|
||||||
future::{FutureExt as _, LocalBoxFuture},
|
future::{FutureExt as _, LocalBoxFuture},
|
||||||
stream::StreamExt as _,
|
stream::StreamExt as _,
|
||||||
@ -32,26 +32,28 @@ use prost::{DecodeError as ProtoBufDecodeError, EncodeError as ProtoBufEncodeErr
|
|||||||
#[derive(Debug, Display)]
|
#[derive(Debug, Display)]
|
||||||
pub enum ProtoBufPayloadError {
|
pub enum ProtoBufPayloadError {
|
||||||
/// Payload size is bigger than 256k
|
/// Payload size is bigger than 256k
|
||||||
#[display(fmt = "Payload size is bigger than 256k")]
|
#[display("Payload size is bigger than 256k")]
|
||||||
Overflow,
|
Overflow,
|
||||||
|
|
||||||
/// Content type error
|
/// Content type error
|
||||||
#[display(fmt = "Content type error")]
|
#[display("Content type error")]
|
||||||
ContentType,
|
ContentType,
|
||||||
|
|
||||||
/// Serialize error
|
/// Serialize error
|
||||||
#[display(fmt = "ProtoBuf serialize error: {_0}")]
|
#[display("ProtoBuf serialize error: {_0}")]
|
||||||
Serialize(ProtoBufEncodeError),
|
Serialize(ProtoBufEncodeError),
|
||||||
|
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "ProtoBuf deserialize error: {_0}")]
|
#[display("ProtoBuf deserialize error: {_0}")]
|
||||||
Deserialize(ProtoBufDecodeError),
|
Deserialize(ProtoBufDecodeError),
|
||||||
|
|
||||||
/// Payload error
|
/// Payload error
|
||||||
#[display(fmt = "Error that occur during reading payload: {_0}")]
|
#[display("Error that occur during reading payload: {_0}")]
|
||||||
Payload(PayloadError),
|
Payload(PayloadError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: impl error for ProtoBufPayloadError
|
||||||
|
|
||||||
impl ResponseError for ProtoBufPayloadError {
|
impl ResponseError for ProtoBufPayloadError {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -2,6 +2,14 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Add `Session::contains_key` method.
|
||||||
|
- Add `Session::update[_or]()` methods.
|
||||||
|
- Update `redis` dependency to `0.29`.
|
||||||
|
|
||||||
|
## 0.10.1
|
||||||
|
|
||||||
|
- Expose `storage::generate_session_key()` without needing to enable a crate feature.
|
||||||
|
|
||||||
## 0.10.0
|
## 0.10.0
|
||||||
|
|
||||||
- Add `redis-session-rustls` crate feature that enables `rustls`-secured Redis sessions.
|
- Add `redis-session-rustls` crate feature that enables `rustls`-secured Redis sessions.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-session"
|
name = "actix-session"
|
||||||
version = "0.10.0"
|
version = "0.10.1"
|
||||||
authors = [
|
authors = [
|
||||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||||
"Luca Palmieri <rust@lpalmieri.com>",
|
"Luca Palmieri <rust@lpalmieri.com>",
|
||||||
@ -20,7 +20,7 @@ all-features = true
|
|||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
cookie-session = []
|
cookie-session = []
|
||||||
redis-session = ["dep:redis", "dep:rand"]
|
redis-session = ["dep:redis"]
|
||||||
redis-session-native-tls = ["redis-session", "redis/tokio-native-tls-comp"]
|
redis-session-native-tls = ["redis-session", "redis/tokio-native-tls-comp"]
|
||||||
redis-session-rustls = ["redis-session", "redis/tokio-rustls-comp"]
|
redis-session-rustls = ["redis-session", "redis/tokio-rustls-comp"]
|
||||||
redis-pool = ["dep:deadpool-redis"]
|
redis-pool = ["dep:deadpool-redis"]
|
||||||
@ -31,19 +31,19 @@ actix-utils = "3"
|
|||||||
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }
|
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }
|
||||||
|
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
derive_more = "0.99.7"
|
derive_more = { version = "2", features = ["display", "error", "from"] }
|
||||||
rand = { version = "0.8", optional = true }
|
rand = "0.9"
|
||||||
serde = { version = "1" }
|
serde = { version = "1" }
|
||||||
serde_json = { version = "1" }
|
serde_json = { version = "1" }
|
||||||
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
||||||
|
|
||||||
# redis-session
|
# redis-session
|
||||||
redis = { version = "0.26", default-features = false, features = ["tokio-comp", "connection-manager"], optional = true }
|
redis = { version = "0.29", default-features = false, features = ["tokio-comp", "connection-manager"], optional = true }
|
||||||
deadpool-redis = { version = "0.16", optional = true }
|
deadpool-redis = { version = "0.20", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-session = { path = ".", features = ["cookie-session", "redis-session"] }
|
actix-session = { path = ".", features = ["cookie-session", "redis-session"] }
|
||||||
actix-test = "0.1.0-beta.10"
|
actix-test = "0.1"
|
||||||
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies", "macros"] }
|
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies", "macros"] }
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
tracing = "0.1.30"
|
tracing = "0.1.30"
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
<!-- prettier-ignore-start -->
|
<!-- prettier-ignore-start -->
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-session)
|
[](https://crates.io/crates/actix-session)
|
||||||
[](https://docs.rs/actix-session/0.10.0)
|
[](https://docs.rs/actix-session/0.10.1)
|
||||||

|

|
||||||
[](https://deps.rs/crate/actix-session/0.10.0)
|
[](https://deps.rs/crate/actix-session/0.10.1)
|
||||||
|
|
||||||
<!-- prettier-ignore-end -->
|
<!-- prettier-ignore-end -->
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Configuration options to tune the behaviour of [`SessionMiddleware`].
|
//! Configuration options to tune the behaviour of [`SessionMiddleware`].
|
||||||
|
|
||||||
use actix_web::cookie::{time::Duration, Key, SameSite};
|
use actix_web::cookie::{time::Duration, Key, SameSite};
|
||||||
use derive_more::From;
|
use derive_more::derive::From;
|
||||||
|
|
||||||
use crate::{storage::SessionStore, SessionMiddleware};
|
use crate::{storage::SessionStore, SessionMiddleware};
|
||||||
|
|
||||||
|
@ -148,6 +148,7 @@ pub use self::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
#[allow(missing_docs)]
|
||||||
pub mod test_helpers {
|
pub mod test_helpers {
|
||||||
use actix_web::cookie::Key;
|
use actix_web::cookie::Key;
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ use actix_web::{
|
|||||||
FromRequest, HttpMessage, HttpRequest, HttpResponse, ResponseError,
|
FromRequest, HttpMessage, HttpRequest, HttpResponse, ResponseError,
|
||||||
};
|
};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use derive_more::{Display, From};
|
use derive_more::derive::{Display, From};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
/// The primary interface to access and modify session state.
|
/// The primary interface to access and modify session state.
|
||||||
@ -33,6 +33,9 @@ use serde::{de::DeserializeOwned, Serialize};
|
|||||||
/// session.insert("counter", 1)?;
|
/// session.insert("counter", 1)?;
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
/// // or use the shorthand
|
||||||
|
/// session.update_or("counter", 1, |count: i32| count + 1);
|
||||||
|
///
|
||||||
/// Ok("Welcome!")
|
/// Ok("Welcome!")
|
||||||
/// }
|
/// }
|
||||||
/// # actix_web::web::to(index);
|
/// # actix_web::web::to(index);
|
||||||
@ -97,6 +100,11 @@ impl Session {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the session contains a value for the specified `key`.
|
||||||
|
pub fn contains_key(&self, key: &str) -> bool {
|
||||||
|
self.0.borrow().state.contains_key(key)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get all raw key-value data from the session.
|
/// Get all raw key-value data from the session.
|
||||||
///
|
///
|
||||||
/// Note that values are JSON encoded.
|
/// Note that values are JSON encoded.
|
||||||
@ -114,7 +122,9 @@ impl Session {
|
|||||||
/// Any serializable value can be used and will be encoded as JSON in session data, hence why
|
/// Any serializable value can be used and will be encoded as JSON in session data, hence why
|
||||||
/// only a reference to the value is taken.
|
/// only a reference to the value is taken.
|
||||||
///
|
///
|
||||||
/// It returns an error if it fails to serialize `value` to JSON.
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if JSON serialization of `value` fails.
|
||||||
pub fn insert<T: Serialize>(
|
pub fn insert<T: Serialize>(
|
||||||
&self,
|
&self,
|
||||||
key: impl Into<String>,
|
key: impl Into<String>,
|
||||||
@ -132,9 +142,8 @@ impl Session {
|
|||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!(
|
format!(
|
||||||
"Failed to serialize the provided `{}` type instance as JSON in order to \
|
"Failed to serialize the provided `{}` type instance as JSON in order to \
|
||||||
attach as session data to the `{}` key",
|
attach as session data to the `{key}` key",
|
||||||
std::any::type_name::<T>(),
|
std::any::type_name::<T>(),
|
||||||
&key
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.map_err(SessionInsertError)?;
|
.map_err(SessionInsertError)?;
|
||||||
@ -145,6 +154,83 @@ impl Session {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates a key-value pair into the session.
|
||||||
|
///
|
||||||
|
/// If the key exists then update it to the new value and place it back in. If the key does not
|
||||||
|
/// exist it will not be updated.
|
||||||
|
///
|
||||||
|
/// Any serializable value can be used and will be encoded as JSON in the session data, hence
|
||||||
|
/// why only a reference to the value is taken.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if JSON serialization of the value fails.
|
||||||
|
pub fn update<T: Serialize + DeserializeOwned, F>(
|
||||||
|
&self,
|
||||||
|
key: impl Into<String>,
|
||||||
|
updater: F,
|
||||||
|
) -> Result<(), SessionUpdateError>
|
||||||
|
where
|
||||||
|
F: FnOnce(T) -> T,
|
||||||
|
{
|
||||||
|
let mut inner = self.0.borrow_mut();
|
||||||
|
let key_str = key.into();
|
||||||
|
|
||||||
|
if let Some(val_str) = inner.state.get(&key_str) {
|
||||||
|
let value = serde_json::from_str(val_str)
|
||||||
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Failed to deserialize the JSON-encoded session data attached to key \
|
||||||
|
`{key_str}` as a `{}` type",
|
||||||
|
std::any::type_name::<T>()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map_err(SessionUpdateError)?;
|
||||||
|
|
||||||
|
let val = serde_json::to_string(&updater(value))
|
||||||
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Failed to serialize the provided `{}` type instance as JSON in order to \
|
||||||
|
attach as session data to the `{key_str}` key",
|
||||||
|
std::any::type_name::<T>(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map_err(SessionUpdateError)?;
|
||||||
|
|
||||||
|
inner.state.insert(key_str, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates a key-value pair into the session, or inserts a default value.
|
||||||
|
///
|
||||||
|
/// If the key exists then update it to the new value and place it back in. If the key does not
|
||||||
|
/// exist the default value will be inserted instead.
|
||||||
|
///
|
||||||
|
/// Any serializable value can be used and will be encoded as JSON in session data, hence why
|
||||||
|
/// only a reference to the value is taken.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns error if JSON serialization of a value fails.
|
||||||
|
pub fn update_or<T: Serialize + DeserializeOwned, F>(
|
||||||
|
&self,
|
||||||
|
key: &str,
|
||||||
|
default_value: T,
|
||||||
|
updater: F,
|
||||||
|
) -> Result<(), SessionUpdateError>
|
||||||
|
where
|
||||||
|
F: FnOnce(T) -> T,
|
||||||
|
{
|
||||||
|
if self.contains_key(key) {
|
||||||
|
self.update(key, updater)
|
||||||
|
} else {
|
||||||
|
self.insert(key, default_value)
|
||||||
|
.map_err(|err| SessionUpdateError(err.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Remove value from the session.
|
/// Remove value from the session.
|
||||||
///
|
///
|
||||||
/// If present, the JSON encoded value is returned.
|
/// If present, the JSON encoded value is returned.
|
||||||
@ -288,7 +374,7 @@ impl FromRequest for Session {
|
|||||||
|
|
||||||
/// Error returned by [`Session::get`].
|
/// Error returned by [`Session::get`].
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
pub struct SessionGetError(anyhow::Error);
|
pub struct SessionGetError(anyhow::Error);
|
||||||
|
|
||||||
impl StdError for SessionGetError {
|
impl StdError for SessionGetError {
|
||||||
@ -305,7 +391,7 @@ impl ResponseError for SessionGetError {
|
|||||||
|
|
||||||
/// Error returned by [`Session::insert`].
|
/// Error returned by [`Session::insert`].
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
#[display(fmt = "{_0}")]
|
#[display("{_0}")]
|
||||||
pub struct SessionInsertError(anyhow::Error);
|
pub struct SessionInsertError(anyhow::Error);
|
||||||
|
|
||||||
impl StdError for SessionInsertError {
|
impl StdError for SessionInsertError {
|
||||||
@ -319,3 +405,20 @@ impl ResponseError for SessionInsertError {
|
|||||||
HttpResponse::new(self.status_code())
|
HttpResponse::new(self.status_code())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Error returned by [`Session::update`].
|
||||||
|
#[derive(Debug, Display, From)]
|
||||||
|
#[display("{_0}")]
|
||||||
|
pub struct SessionUpdateError(anyhow::Error);
|
||||||
|
|
||||||
|
impl StdError for SessionUpdateError {
|
||||||
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||||
|
Some(self.0.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResponseError for SessionUpdateError {
|
||||||
|
fn error_response(&self) -> HttpResponse<BoxBody> {
|
||||||
|
HttpResponse::new(self.status_code())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -31,7 +31,7 @@ impl SessionExt for ServiceResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SessionExt for GuardContext<'a> {
|
impl SessionExt for GuardContext<'_> {
|
||||||
fn get_session(&self) -> Session {
|
fn get_session(&self) -> Session {
|
||||||
Session::get_session(&mut self.req_data_mut())
|
Session::get_session(&mut self.req_data_mut())
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{collections::HashMap, future::Future};
|
use std::{collections::HashMap, future::Future};
|
||||||
|
|
||||||
use actix_web::cookie::time::Duration;
|
use actix_web::cookie::time::Duration;
|
||||||
use derive_more::Display;
|
use derive_more::derive::Display;
|
||||||
|
|
||||||
use super::SessionKey;
|
use super::SessionKey;
|
||||||
|
|
||||||
@ -53,11 +53,11 @@ pub trait SessionStore {
|
|||||||
#[derive(Debug, Display)]
|
#[derive(Debug, Display)]
|
||||||
pub enum LoadError {
|
pub enum LoadError {
|
||||||
/// Failed to deserialize session state.
|
/// Failed to deserialize session state.
|
||||||
#[display(fmt = "Failed to deserialize session state")]
|
#[display("Failed to deserialize session state")]
|
||||||
Deserialization(anyhow::Error),
|
Deserialization(anyhow::Error),
|
||||||
|
|
||||||
/// Something went wrong when retrieving the session state.
|
/// Something went wrong when retrieving the session state.
|
||||||
#[display(fmt = "Something went wrong when retrieving the session state")]
|
#[display("Something went wrong when retrieving the session state")]
|
||||||
Other(anyhow::Error),
|
Other(anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,11 +74,11 @@ impl std::error::Error for LoadError {
|
|||||||
#[derive(Debug, Display)]
|
#[derive(Debug, Display)]
|
||||||
pub enum SaveError {
|
pub enum SaveError {
|
||||||
/// Failed to serialize session state.
|
/// Failed to serialize session state.
|
||||||
#[display(fmt = "Failed to serialize session state")]
|
#[display("Failed to serialize session state")]
|
||||||
Serialization(anyhow::Error),
|
Serialization(anyhow::Error),
|
||||||
|
|
||||||
/// Something went wrong when persisting the session state.
|
/// Something went wrong when persisting the session state.
|
||||||
#[display(fmt = "Something went wrong when persisting the session state")]
|
#[display("Something went wrong when persisting the session state")]
|
||||||
Other(anyhow::Error),
|
Other(anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,11 +95,11 @@ impl std::error::Error for SaveError {
|
|||||||
/// Possible failures modes for [`SessionStore::update`].
|
/// Possible failures modes for [`SessionStore::update`].
|
||||||
pub enum UpdateError {
|
pub enum UpdateError {
|
||||||
/// Failed to serialize session state.
|
/// Failed to serialize session state.
|
||||||
#[display(fmt = "Failed to serialize session state")]
|
#[display("Failed to serialize session state")]
|
||||||
Serialization(anyhow::Error),
|
Serialization(anyhow::Error),
|
||||||
|
|
||||||
/// Something went wrong when updating the session state.
|
/// Something went wrong when updating the session state.
|
||||||
#[display(fmt = "Something went wrong when updating the session state.")]
|
#[display("Something went wrong when updating the session state.")]
|
||||||
Other(anyhow::Error),
|
Other(anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,17 +6,14 @@ mod interface;
|
|||||||
#[cfg(feature = "redis-session")]
|
#[cfg(feature = "redis-session")]
|
||||||
mod redis_rs;
|
mod redis_rs;
|
||||||
mod session_key;
|
mod session_key;
|
||||||
#[cfg(feature = "redis-session")]
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
#[cfg(feature = "cookie-session")]
|
#[cfg(feature = "cookie-session")]
|
||||||
pub use self::cookie::CookieSessionStore;
|
pub use self::cookie::CookieSessionStore;
|
||||||
|
#[cfg(feature = "redis-session")]
|
||||||
|
pub use self::redis_rs::{RedisSessionStore, RedisSessionStoreBuilder};
|
||||||
pub use self::{
|
pub use self::{
|
||||||
interface::{LoadError, SaveError, SessionStore, UpdateError},
|
interface::{LoadError, SaveError, SessionStore, UpdateError},
|
||||||
session_key::SessionKey,
|
session_key::SessionKey,
|
||||||
};
|
|
||||||
#[cfg(feature = "redis-session")]
|
|
||||||
pub use self::{
|
|
||||||
redis_rs::{RedisSessionStore, RedisSessionStoreBuilder},
|
|
||||||
utils::generate_session_key,
|
utils::generate_session_key,
|
||||||
};
|
};
|
||||||
|
@ -44,7 +44,7 @@ use crate::storage::{
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # TLS support
|
/// # TLS support
|
||||||
/// Add the `redis-rs-tls-session` or `redis-rs-tls-session-rustls` feature flag to enable TLS support. You can then establish a TLS
|
/// Add the `redis-session-native-tls` or `redis-session-rustls` feature flag to enable TLS support. You can then establish a TLS
|
||||||
/// connection to Redis using the `rediss://` URL scheme:
|
/// connection to Redis using the `rediss://` URL scheme:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
@ -205,7 +205,6 @@ impl SessionStore for RedisSessionStore {
|
|||||||
let value: Option<String> = self
|
let value: Option<String> = self
|
||||||
.execute_command(redis::cmd("GET").arg(&[&cache_key]))
|
.execute_command(redis::cmd("GET").arg(&[&cache_key]))
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
|
||||||
.map_err(LoadError::Other)?;
|
.map_err(LoadError::Other)?;
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
@ -240,7 +239,6 @@ impl SessionStore for RedisSessionStore {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
|
||||||
.map_err(SaveError::Other)?;
|
.map_err(SaveError::Other)?;
|
||||||
|
|
||||||
Ok(session_key)
|
Ok(session_key)
|
||||||
@ -267,7 +265,6 @@ impl SessionStore for RedisSessionStore {
|
|||||||
&format!("{}", ttl.whole_seconds()),
|
&format!("{}", ttl.whole_seconds()),
|
||||||
]))
|
]))
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
|
||||||
.map_err(UpdateError::Other)?;
|
.map_err(UpdateError::Other)?;
|
||||||
|
|
||||||
match v {
|
match v {
|
||||||
@ -318,7 +315,6 @@ impl SessionStore for RedisSessionStore {
|
|||||||
|
|
||||||
self.execute_command::<()>(redis::cmd("DEL").arg(&[&cache_key]))
|
self.execute_command::<()>(redis::cmd("DEL").arg(&[&cache_key]))
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
|
||||||
.map_err(UpdateError::Other)?;
|
.map_err(UpdateError::Other)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use derive_more::{Display, From};
|
use derive_more::derive::{Display, From};
|
||||||
|
|
||||||
/// A session key, the string stored in a client-side cookie to associate a user with its session
|
/// A session key, the string stored in a client-side cookie to associate a user with its session
|
||||||
/// state on the backend.
|
/// state on the backend.
|
||||||
@ -45,7 +45,7 @@ impl From<SessionKey> for String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
#[display(fmt = "The provided string is not a valid session key")]
|
#[display("The provided string is not a valid session key")]
|
||||||
pub struct InvalidSessionKeyError(anyhow::Error);
|
pub struct InvalidSessionKeyError(anyhow::Error);
|
||||||
|
|
||||||
impl std::error::Error for InvalidSessionKeyError {
|
impl std::error::Error for InvalidSessionKeyError {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use rand::distributions::{Alphanumeric, DistString as _};
|
use rand::distr::{Alphanumeric, SampleString as _};
|
||||||
|
|
||||||
use crate::storage::SessionKey;
|
use crate::storage::SessionKey;
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ use crate::storage::SessionKey;
|
|||||||
/// [OWASP recommendations]: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy
|
/// [OWASP recommendations]: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy
|
||||||
pub fn generate_session_key() -> SessionKey {
|
pub fn generate_session_key() -> SessionKey {
|
||||||
Alphanumeric
|
Alphanumeric
|
||||||
.sample_string(&mut rand::thread_rng(), 64)
|
.sample_string(&mut rand::rng(), 64)
|
||||||
.try_into()
|
.try_into()
|
||||||
.expect("generated string should be within size range for a session key")
|
.expect("generated string should be within size range for a session key")
|
||||||
}
|
}
|
||||||
|
@ -69,6 +69,16 @@ async fn session_entries() {
|
|||||||
map.contains_key("test_num");
|
map.contains_key("test_num");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn session_contains_key() {
|
||||||
|
let req = test::TestRequest::default().to_srv_request();
|
||||||
|
let session = req.get_session();
|
||||||
|
session.insert("test_str", "val").unwrap();
|
||||||
|
session.insert("test_str", 1).unwrap();
|
||||||
|
assert!(session.contains_key("test_str"));
|
||||||
|
assert!(!session.contains_key("test_num"));
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::test]
|
#[actix_web::test]
|
||||||
async fn insert_session_after_renew() {
|
async fn insert_session_after_renew() {
|
||||||
let session = test::TestRequest::default().to_srv_request().get_session();
|
let session = test::TestRequest::default().to_srv_request().get_session();
|
||||||
@ -83,6 +93,35 @@ async fn insert_session_after_renew() {
|
|||||||
assert_eq!(session.status(), SessionStatus::Renewed);
|
assert_eq!(session.status(), SessionStatus::Renewed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn update_session() {
|
||||||
|
let session = test::TestRequest::default().to_srv_request().get_session();
|
||||||
|
|
||||||
|
session.update("test_val", |c: u32| c + 1).unwrap();
|
||||||
|
assert_eq!(session.status(), SessionStatus::Unchanged);
|
||||||
|
|
||||||
|
session.insert("test_val", 0).unwrap();
|
||||||
|
assert_eq!(session.status(), SessionStatus::Changed);
|
||||||
|
|
||||||
|
session.update("test_val", |c: u32| c + 1).unwrap();
|
||||||
|
assert_eq!(session.get("test_val").unwrap(), Some(1));
|
||||||
|
|
||||||
|
session.update("test_val", |c: u32| c + 1).unwrap();
|
||||||
|
assert_eq!(session.get("test_val").unwrap(), Some(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn update_or_session() {
|
||||||
|
let session = test::TestRequest::default().to_srv_request().get_session();
|
||||||
|
|
||||||
|
session.update_or("test_val", 1, |c: u32| c + 1).unwrap();
|
||||||
|
assert_eq!(session.status(), SessionStatus::Changed);
|
||||||
|
assert_eq!(session.get("test_val").unwrap(), Some(1));
|
||||||
|
|
||||||
|
session.update_or("test_val", 1, |c: u32| c + 1).unwrap();
|
||||||
|
assert_eq!(session.get("test_val").unwrap(), Some(2));
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::test]
|
#[actix_web::test]
|
||||||
async fn remove_session_after_renew() {
|
async fn remove_session_after_renew() {
|
||||||
let session = test::TestRequest::default().to_srv_request().get_session();
|
let session = test::TestRequest::default().to_srv_request().get_session();
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
- Add new feature named `openssl` for TLS settings using `OpenSSL` dependency. [#380]
|
## 0.8.0
|
||||||
- Add new function `settings::tls::Tls::get_ssl_acceptor_builder()` to build `openssl::ssl::SslAcceptorBuilder`. [#380]
|
|
||||||
- Implement TLS logic for `ApplySettings<S>::try_apply_settings()`. [#380]
|
|
||||||
- Add `openssl` dependency;
|
|
||||||
- Minimum supported Rust version (MSRV) is now 1.75.
|
|
||||||
- `ApplySettings<S>::apply_settings()` is deprecated; `ApplySettings<S>::try_apply_settings()` should be preferred. [#380]
|
|
||||||
|
|
||||||
[#380]: https://github.com/actix/actix-extras/pull/380
|
- Add `openssl` crate feature for TLS settings using OpenSSL.
|
||||||
|
- Add `ApplySettings::try_apply_settings()`.
|
||||||
|
- Implement TLS logic for `ApplySettings::try_apply_settings()`.
|
||||||
|
- Add `Tls::get_ssl_acceptor_builder()` function to build `openssl::ssl::SslAcceptorBuilder`.
|
||||||
|
- Deprecate `ApplySettings::apply_settings()`.
|
||||||
|
- Minimum supported Rust version (MSRV) is now 1.75.
|
||||||
|
|
||||||
## 0.7.1
|
## 0.7.1
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-settings"
|
name = "actix-settings"
|
||||||
version = "0.7.1"
|
version = "0.8.0"
|
||||||
authors = [
|
authors = [
|
||||||
"Joey Ezechiels <joey.ezechiels@gmail.com>",
|
"Joey Ezechiels <joey.ezechiels@gmail.com>",
|
||||||
"Rob Ede <robjtede@icloud.com>",
|
"Rob Ede <robjtede@icloud.com>",
|
||||||
@ -22,8 +22,8 @@ openssl = ["dep:openssl", "actix-web/openssl"]
|
|||||||
actix-http = "3"
|
actix-http = "3"
|
||||||
actix-service = "2"
|
actix-service = "2"
|
||||||
actix-web = { version = "4", default-features = false }
|
actix-web = { version = "4", default-features = false }
|
||||||
derive_more = "0.99.7"
|
derive_more = { version = "2", features = ["display", "error"] }
|
||||||
once_cell = "1.13"
|
once_cell = "1.21"
|
||||||
openssl = { version = "0.10", features = ["v110"], optional = true }
|
openssl = { version = "0.10", features = ["v110"], optional = true }
|
||||||
regex = "1.5"
|
regex = "1.5"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
<!-- prettier-ignore-start -->
|
<!-- prettier-ignore-start -->
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-settings)
|
[](https://crates.io/crates/actix-settings)
|
||||||
[](https://docs.rs/actix-settings/0.7.1)
|
[](https://docs.rs/actix-settings/0.8.0)
|
||||||

|

|
||||||
[](https://deps.rs/crate/actix-settings/0.7.1)
|
[](https://deps.rs/crate/actix-settings/0.8.0)
|
||||||
|
|
||||||
<!-- prettier-ignore-end -->
|
<!-- prettier-ignore-end -->
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError};
|
use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError};
|
||||||
|
|
||||||
use derive_more::{Display, Error};
|
use derive_more::derive::{Display, Error};
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
use openssl::error::ErrorStack as OpenSSLError;
|
use openssl::error::ErrorStack as OpenSSLError;
|
||||||
use toml::de::Error as TomlError;
|
use toml::de::Error as TomlError;
|
||||||
@ -9,16 +9,16 @@ use toml::de::Error as TomlError;
|
|||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Environment variable does not exists or is invalid.
|
/// Environment variable does not exists or is invalid.
|
||||||
#[display(fmt = "Env var error: {_0}")]
|
#[display("Env var error: {_0}")]
|
||||||
EnvVarError(VarError),
|
EnvVarError(VarError),
|
||||||
|
|
||||||
/// File already exists on disk.
|
/// File already exists on disk.
|
||||||
#[display(fmt = "File exists: {}", "_0.display()")]
|
#[display("File exists: {}", _0.display())]
|
||||||
FileExists(#[error(not(source))] PathBuf),
|
FileExists(#[error(not(source))] PathBuf),
|
||||||
|
|
||||||
/// Invalid value.
|
/// Invalid value.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[display(fmt = "Expected {expected}, got {got} (@ {file}:{line}:{column})")]
|
#[display("Expected {expected}, got {got} (@ {file}:{line}:{column})")]
|
||||||
InvalidValue {
|
InvalidValue {
|
||||||
expected: &'static str,
|
expected: &'static str,
|
||||||
got: String,
|
got: String,
|
||||||
@ -28,28 +28,28 @@ pub enum Error {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/// I/O error.
|
/// I/O error.
|
||||||
#[display(fmt = "")]
|
#[display("I/O error: {_0}")]
|
||||||
IoError(io::Error),
|
IoError(io::Error),
|
||||||
|
|
||||||
/// OpenSSL Error.
|
/// OpenSSL Error.
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
#[display(fmt = "OpenSSL error: {_0}")]
|
#[display("OpenSSL error: {_0}")]
|
||||||
OpenSSLError(OpenSSLError),
|
OpenSSLError(OpenSSLError),
|
||||||
|
|
||||||
/// Value is not a boolean.
|
/// Value is not a boolean.
|
||||||
#[display(fmt = "Failed to parse boolean: {_0}")]
|
#[display("Failed to parse boolean: {_0}")]
|
||||||
ParseBoolError(ParseBoolError),
|
ParseBoolError(ParseBoolError),
|
||||||
|
|
||||||
/// Value is not an integer.
|
/// Value is not an integer.
|
||||||
#[display(fmt = "Failed to parse integer: {_0}")]
|
#[display("Failed to parse integer: {_0}")]
|
||||||
ParseIntError(ParseIntError),
|
ParseIntError(ParseIntError),
|
||||||
|
|
||||||
/// Value is not an address.
|
/// Value is not an address.
|
||||||
#[display(fmt = "Failed to parse address: {_0}")]
|
#[display("Failed to parse address: {_0}")]
|
||||||
ParseAddressError(#[error(not(source))] String),
|
ParseAddressError(#[error(not(source))] String),
|
||||||
|
|
||||||
/// Error deserializing as TOML.
|
/// Error deserializing as TOML.
|
||||||
#[display(fmt = "TOML error: {_0}")]
|
#[display("TOML error: {_0}")]
|
||||||
TomlError(TomlError),
|
TomlError(TomlError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,11 +242,26 @@ where
|
|||||||
|
|
||||||
/// Extension trait for applying parsed settings to the server object.
|
/// Extension trait for applying parsed settings to the server object.
|
||||||
pub trait ApplySettings<S>: Sized {
|
pub trait ApplySettings<S>: Sized {
|
||||||
/// Apply some settings object value to `self`.
|
/// Applies some settings object value to `self`.
|
||||||
#[deprecated = "Prefer `try_apply_settings`."]
|
///
|
||||||
fn apply_settings(self, settings: &S) -> Self;
|
/// The default implementation calls [`try_apply_settings()`].
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// May panic if settings are invalid or cannot be applied.
|
||||||
|
///
|
||||||
|
/// [`try_apply_settings()`]: ApplySettings::try_apply_settings().
|
||||||
|
#[deprecated = "Prefer `try_apply_settings()`."]
|
||||||
|
fn apply_settings(self, settings: &S) -> Self {
|
||||||
|
self.try_apply_settings(settings)
|
||||||
|
.expect("Could not apply settings")
|
||||||
|
}
|
||||||
|
|
||||||
/// Apply some settings object value to `self`.
|
/// Applies some settings object value to `self`.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// May return error if settings are invalid or cannot be applied.
|
||||||
fn try_apply_settings(self, settings: &S) -> AsResult<Self>;
|
fn try_apply_settings(self, settings: &S) -> AsResult<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ impl<'de> de::Deserialize<'de> for Backlog {
|
|||||||
{
|
{
|
||||||
struct BacklogVisitor;
|
struct BacklogVisitor;
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for BacklogVisitor {
|
impl de::Visitor<'_> for BacklogVisitor {
|
||||||
type Value = Backlog;
|
type Value = Backlog;
|
||||||
|
|
||||||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -68,7 +68,7 @@ impl<'de> de::Deserialize<'de> for KeepAlive {
|
|||||||
{
|
{
|
||||||
struct KeepAliveVisitor;
|
struct KeepAliveVisitor;
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for KeepAliveVisitor {
|
impl de::Visitor<'_> for KeepAliveVisitor {
|
||||||
type Value = KeepAlive;
|
type Value = KeepAlive;
|
||||||
|
|
||||||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -40,7 +40,7 @@ impl<'de> de::Deserialize<'de> for MaxConnectionRate {
|
|||||||
{
|
{
|
||||||
struct MaxConnectionRateVisitor;
|
struct MaxConnectionRateVisitor;
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for MaxConnectionRateVisitor {
|
impl de::Visitor<'_> for MaxConnectionRateVisitor {
|
||||||
type Value = MaxConnectionRate;
|
type Value = MaxConnectionRate;
|
||||||
|
|
||||||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -40,7 +40,7 @@ impl<'de> de::Deserialize<'de> for MaxConnections {
|
|||||||
{
|
{
|
||||||
struct MaxConnectionsVisitor;
|
struct MaxConnectionsVisitor;
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for MaxConnectionsVisitor {
|
impl de::Visitor<'_> for MaxConnectionsVisitor {
|
||||||
type Value = MaxConnections;
|
type Value = MaxConnections;
|
||||||
|
|
||||||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -39,7 +39,7 @@ impl<'de> de::Deserialize<'de> for NumWorkers {
|
|||||||
{
|
{
|
||||||
struct NumWorkersVisitor;
|
struct NumWorkersVisitor;
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for NumWorkersVisitor {
|
impl de::Visitor<'_> for NumWorkersVisitor {
|
||||||
type Value = NumWorkers;
|
type Value = NumWorkers;
|
||||||
|
|
||||||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -71,7 +71,7 @@ impl<'de> de::Deserialize<'de> for Timeout {
|
|||||||
{
|
{
|
||||||
struct TimeoutVisitor;
|
struct TimeoutVisitor;
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for TimeoutVisitor {
|
impl de::Visitor<'_> for TimeoutVisitor {
|
||||||
type Value = Timeout;
|
type Value = Timeout;
|
||||||
|
|
||||||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -21,35 +21,31 @@ pub struct Tls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Tls {
|
impl Tls {
|
||||||
/// Generates an [`SslAcceptorBuilder`] with its settings. It is often used for the following method
|
/// Returns an [`SslAcceptorBuilder`] with the configured settings.
|
||||||
/// [`actix_web::server::HttpServer::bind_openssl`].
|
///
|
||||||
|
/// The result is often used with [`actix_web::HttpServer::bind_openssl()`].
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```no_run
|
|
||||||
/// use actix_settings::{ApplySettings, Settings};
|
|
||||||
/// use actix_web::{get, App, HttpServer, Responder};
|
|
||||||
///
|
///
|
||||||
/// #[get("/")]
|
/// ```no_run
|
||||||
/// async fn index() -> impl Responder {
|
/// use std::io;
|
||||||
/// "Hello."
|
/// use actix_settings::{ApplySettings as _, Settings};
|
||||||
/// }
|
/// use actix_web::{get, web, App, HttpServer, Responder};
|
||||||
///
|
///
|
||||||
/// #[actix_web::main]
|
/// #[actix_web::main]
|
||||||
/// async fn main() -> std::io::Result<()> {
|
/// async fn main() -> io::Result<()> {
|
||||||
/// let settings = Settings::from_default_template();
|
/// let settings = Settings::from_default_template();
|
||||||
///
|
///
|
||||||
/// HttpServer::new(|| {
|
/// HttpServer::new(|| {
|
||||||
/// App::new()
|
/// App::new().route("/", web::to(|| async { "Hello, World!" }))
|
||||||
/// .service(index)
|
|
||||||
/// })
|
/// })
|
||||||
/// .try_apply_settings(&settings)?
|
/// .try_apply_settings(&settings)?
|
||||||
/// .bind(("127.0.0.1", 8080))?
|
/// .bind(("127.0.0.1", 8080))?
|
||||||
/// .bind_openssl(("127.0.0.1", 8081), settings.actix.tls.get_ssl_acceptor_builder()?)?
|
/// .bind_openssl(("127.0.0.1", 8443), settings.actix.tls.get_ssl_acceptor_builder()?)?
|
||||||
/// .run()
|
/// .run()
|
||||||
/// .await
|
/// .await
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[allow(rustdoc::broken_intra_doc_links)]
|
|
||||||
pub fn get_ssl_acceptor_builder(&self) -> AsResult<SslAcceptorBuilder> {
|
pub fn get_ssl_acceptor_builder(&self) -> AsResult<SslAcceptorBuilder> {
|
||||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;
|
||||||
builder.set_certificate_chain_file(&self.certificate)?;
|
builder.set_certificate_chain_file(&self.certificate)?;
|
||||||
|
@ -12,8 +12,8 @@ struct Quoted<'a> {
|
|||||||
state: State,
|
state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Quoted<'a> {
|
impl Quoted<'_> {
|
||||||
pub fn new(s: &'a str) -> Quoted<'_> {
|
pub fn new(s: &str) -> Quoted<'_> {
|
||||||
Quoted {
|
Quoted {
|
||||||
inner: s.split('"').peekable(),
|
inner: s.split('"').peekable(),
|
||||||
state: State::YieldStr,
|
state: State::YieldStr,
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Ensure TCP connection is properly shut down when session is dropped.
|
||||||
|
|
||||||
## 0.3.0
|
## 0.3.0
|
||||||
|
|
||||||
- Add `AggregatedMessage[Stream]` types.
|
- Add `AggregatedMessage[Stream]` types.
|
||||||
|
@ -25,7 +25,7 @@ async fn ws(req: HttpRequest, body: web::Payload) -> actix_web::Result<impl Resp
|
|||||||
let (response, mut session, mut msg_stream) = actix_ws::handle(&req, body)?;
|
let (response, mut session, mut msg_stream) = actix_ws::handle(&req, body)?;
|
||||||
|
|
||||||
actix_web::rt::spawn(async move {
|
actix_web::rt::spawn(async move {
|
||||||
while let Some(Ok(msg)) = msg_stream.next().await {
|
while let Some(Ok(msg)) = msg_stream.recv().await {
|
||||||
match msg {
|
match msg {
|
||||||
Message::Ping(bytes) => {
|
Message::Ping(bytes) => {
|
||||||
if session.pong(&bytes).await.is_err() {
|
if session.pong(&bytes).await.is_err() {
|
||||||
|
@ -145,6 +145,10 @@ impl Stream for StreamingBody {
|
|||||||
return Poll::Ready(Some(Ok(mem::take(&mut this.buf).freeze())));
|
return Poll::Ready(Some(Ok(mem::take(&mut this.buf).freeze())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if this.closing {
|
||||||
|
return Poll::Ready(None);
|
||||||
|
}
|
||||||
|
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
justfile
15
justfile
@ -43,6 +43,20 @@ update-readmes:
|
|||||||
[group("test")]
|
[group("test")]
|
||||||
test:
|
test:
|
||||||
cargo {{ toolchain }} nextest run --workspace --all-features
|
cargo {{ toolchain }} nextest run --workspace --all-features
|
||||||
|
cargo {{ toolchain }} test --doc --workspace --all-features
|
||||||
|
|
||||||
|
# Downgrade dev-dependencies necessary to run MSRV checks/tests.
|
||||||
|
[private]
|
||||||
|
downgrade-for-msrv:
|
||||||
|
cargo update -p=native-tls --precise=0.2.13
|
||||||
|
cargo update -p=litemap --precise=0.7.4
|
||||||
|
cargo update -p=zerofrom --precise=0.1.5
|
||||||
|
|
||||||
|
# Test workspace using MSRV.
|
||||||
|
[group("test")]
|
||||||
|
test-msrv:
|
||||||
|
@just downgrade-for-msrv
|
||||||
|
@just toolchain={{ msrv_rustup }} test
|
||||||
|
|
||||||
# Test workspace code and docs.
|
# Test workspace code and docs.
|
||||||
[group("test")]
|
[group("test")]
|
||||||
@ -71,6 +85,7 @@ test-docs:
|
|||||||
# Document crates in workspace.
|
# Document crates in workspace.
|
||||||
[group("docs")]
|
[group("docs")]
|
||||||
doc *args: && doc-set-workspace-crates
|
doc *args: && doc-set-workspace-crates
|
||||||
|
rm -f "$(cargo metadata --format-version=1 | jq -r '.target_directory')/doc/crates.js"
|
||||||
RUSTDOCFLAGS="--cfg=docsrs -Dwarnings" cargo +nightly doc --workspace --all-features {{ args }}
|
RUSTDOCFLAGS="--cfg=docsrs -Dwarnings" cargo +nightly doc --workspace --all-features {{ args }}
|
||||||
|
|
||||||
[group("docs")]
|
[group("docs")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user