1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-27 17:22:57 +01:00
actix-extras/actix_identity/index.html
2023-11-27 15:47:33 +00:00

76 lines
10 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-9ee3a5e31a2afa3e.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.76.0-nightly (6cf088810 2023-11-26)" data-channel="nightly" data-search-js="search-56a624991a76cfad.js" data-settings-js="settings-fe03fdc259827cd2.js" ><script src="../static.files/storage-fec3eaa3851e447d.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-e1c03aa593074e71.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-5d8b3c7633ad77ba.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">&#9776;</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.6.0</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><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><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"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/actix_identity/lib.rs.html#1-102">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</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 href="#getting-started">Getting started</a></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="kw">let </span>secret_key = Key::generate();
<span class="kw">let </span>redis_store = RedisSessionStore::new(<span class="string">&quot;redis://127.0.0.1:6379&quot;</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">&quot;/&quot;</span>)]
</span><span class="kw">async fn </span>index(user: <span class="prelude-ty">Option</span>&lt;Identity&gt;) -&gt; <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">&quot;Welcome! {}&quot;</span>, user.id().unwrap())
} <span class="kw">else </span>{
<span class="string">&quot;Welcome Anonymous!&quot;</span>.to_owned()
}
}
<span class="attr">#[post(<span class="string">&quot;/login&quot;</span>)]
</span><span class="kw">async fn </span>login(request: HttpRequest) -&gt; <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">&amp;</span>request.extensions(), <span class="string">&quot;User1&quot;</span>.into()).unwrap();
HttpResponse::Ok()
}
<span class="attr">#[post(<span class="string">&quot;/logout&quot;</span>)]
</span><span class="kw">async fn </span>logout(user: Identity) -&gt; <span class="kw">impl </span>Responder {
user.logout();
HttpResponse::Ok()
}</code></pre></div>
<h2 id="advanced-configuration"><a href="#advanced-configuration">Advanced configuration</a></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="small-section-header"><a href="#modules">Modules</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="small-section-header"><a href="#structs">Structs</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="small-section-header"><a href="#traits">Traits</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>