mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 23:51:06 +01:00
79 lines
10 KiB
HTML
79 lines
10 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Identity management for Actix Web."><title>actix_identity - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_identity" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (b5b13568f 2024-06-10)" data-channel="nightly" data-search-js="search-0fe7219eb170c82e.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../actix_identity/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../actix_identity/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_identity/index.html">actix_identity</a><span class="version">0.7.1</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">actix_identity</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/actix_identity/lib.rs.html#1-108">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Identity management for Actix Web.</p>
|
||
<p><code>actix-identity</code> can be used to track identity of a user across multiple requests. It is built
|
||
on top of HTTP sessions, via <a href="https://docs.rs/actix-session"><code>actix-session</code></a>.</p>
|
||
<h2 id="getting-started"><a class="doc-anchor" href="#getting-started">§</a>Getting started</h2>
|
||
<p>To start using identity management in your Actix Web application you must register
|
||
<a href="struct.IdentityMiddleware.html" title="struct actix_identity::IdentityMiddleware"><code>IdentityMiddleware</code></a> and <code>SessionMiddleware</code> as middleware on your <code>App</code>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{cookie::Key, App, HttpServer, HttpResponse};
|
||
<span class="kw">use </span>actix_identity::IdentityMiddleware;
|
||
<span class="kw">use </span>actix_session::{storage::RedisSessionStore, SessionMiddleware};
|
||
|
||
<span class="attr">#[actix_web::main]
|
||
</span><span class="kw">async fn </span>main() {
|
||
<span class="comment">// When using `Key::generate()` it is important to initialize outside of the
|
||
// `HttpServer::new` closure. When deployed the secret key should be read from a
|
||
// configuration file or environment variables.
|
||
</span><span class="kw">let </span>secret_key = Key::generate();
|
||
|
||
<span class="kw">let </span>redis_store = RedisSessionStore::new(<span class="string">"redis://127.0.0.1:6379"</span>)
|
||
.<span class="kw">await
|
||
</span>.unwrap();
|
||
|
||
HttpServer::new(<span class="kw">move </span>|| {
|
||
App::new()
|
||
<span class="comment">// Install the identity framework first.
|
||
</span>.wrap(IdentityMiddleware::default())
|
||
<span class="comment">// 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
|
||
// AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
|
||
// order of registration when it receives an incoming request.
|
||
</span>.wrap(SessionMiddleware::new(
|
||
redis_store.clone(),
|
||
secret_key.clone(),
|
||
))
|
||
<span class="comment">// Your request handlers [...]
|
||
</span>})
|
||
}</code></pre></div>
|
||
<p>User identities can be created, accessed and destroyed using the <a href="struct.Identity.html" title="struct actix_identity::Identity"><code>Identity</code></a> extractor in your
|
||
request handlers:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{get, post, HttpResponse, Responder, HttpRequest, HttpMessage};
|
||
<span class="kw">use </span>actix_identity::Identity;
|
||
<span class="kw">use </span>actix_session::storage::RedisSessionStore;
|
||
|
||
<span class="attr">#[get(<span class="string">"/"</span>)]
|
||
</span><span class="kw">async fn </span>index(user: <span class="prelude-ty">Option</span><Identity>) -> <span class="kw">impl </span>Responder {
|
||
<span class="kw">if let </span><span class="prelude-val">Some</span>(user) = user {
|
||
<span class="macro">format!</span>(<span class="string">"Welcome! {}"</span>, user.id().unwrap())
|
||
} <span class="kw">else </span>{
|
||
<span class="string">"Welcome Anonymous!"</span>.to_owned()
|
||
}
|
||
}
|
||
|
||
<span class="attr">#[post(<span class="string">"/login"</span>)]
|
||
</span><span class="kw">async fn </span>login(request: HttpRequest) -> <span class="kw">impl </span>Responder {
|
||
<span class="comment">// Some kind of authentication should happen here
|
||
// e.g. password-based, biometric, etc.
|
||
// [...]
|
||
|
||
// attach a verified user identity to the active session
|
||
</span>Identity::login(<span class="kw-2">&</span>request.extensions(), <span class="string">"User1"</span>.into()).unwrap();
|
||
|
||
HttpResponse::Ok()
|
||
}
|
||
|
||
<span class="attr">#[post(<span class="string">"/logout"</span>)]
|
||
</span><span class="kw">async fn </span>logout(user: Identity) -> <span class="kw">impl </span>Responder {
|
||
user.logout();
|
||
HttpResponse::Ok()
|
||
}</code></pre></div>
|
||
<h2 id="advanced-configuration"><a class="doc-anchor" href="#advanced-configuration">§</a>Advanced configuration</h2>
|
||
<p>By default, <code>actix-identity</code> does not automatically log out users. You can change this behaviour
|
||
by customising the configuration for <a href="struct.IdentityMiddleware.html" title="struct actix_identity::IdentityMiddleware"><code>IdentityMiddleware</code></a> via <a href="struct.IdentityMiddleware.html#method.builder" title="associated function actix_identity::IdentityMiddleware::builder"><code>IdentityMiddleware::builder</code></a>.</p>
|
||
<p>In particular, you can automatically log out users who:</p>
|
||
<ul>
|
||
<li>have been inactive for a while (see <a href="config/struct.IdentityMiddlewareBuilder.html#method.visit_deadline" title="method actix_identity::config::IdentityMiddlewareBuilder::visit_deadline"><code>IdentityMiddlewareBuilder::visit_deadline</code></a>);</li>
|
||
<li>logged in too long ago (see <a href="config/struct.IdentityMiddlewareBuilder.html#method.login_deadline" title="method actix_identity::config::IdentityMiddlewareBuilder::login_deadline"><code>IdentityMiddlewareBuilder::login_deadline</code></a>).</li>
|
||
</ul>
|
||
</div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="config/index.html" title="mod actix_identity::config">config</a></div><div class="desc docblock-short">Configuration options to tune the behaviour of <a href="struct.IdentityMiddleware.html" title="struct actix_identity::IdentityMiddleware"><code>IdentityMiddleware</code></a>.</div></li><li><div class="item-name"><a class="mod" href="error/index.html" title="mod actix_identity::error">error</a></div><div class="desc docblock-short">Failure modes of identity operations.</div></li></ul><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Identity.html" title="struct actix_identity::Identity">Identity</a></div><div class="desc docblock-short">A verified user identity. It can be used as a request extractor.</div></li><li><div class="item-name"><a class="struct" href="struct.IdentityMiddleware.html" title="struct actix_identity::IdentityMiddleware">IdentityMiddleware</a></div><div class="desc docblock-short">Identity management middleware.</div></li></ul><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.IdentityExt.html" title="trait actix_identity::IdentityExt">IdentityExt</a></div><div class="desc docblock-short">Helper trait to retrieve an <a href="struct.Identity.html" title="struct actix_identity::Identity"><code>Identity</code></a> instance from various <code>actix-web</code>’s types.</div></li></ul></section></div></main></body></html> |