1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/actix_identity/index.html

75 lines
11 KiB
HTML
Raw Normal View History

<!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/SourceSerif4-Bold-a2c9cd1067f8b328.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-ae1b282cda4c21ec.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.74.0-nightly (20999de3a 2023-09-15)" data-channel="nightly" data-search-js="search-5d3eaacf19ebf04f.js" data-settings-js="settings-74424d7eec62a23e.js" data-theme-light-css="light-f194925aa375ae96.css" data-theme-dark-css="dark-1dd4d1ce031e15de.css" data-theme-ayu-css="ayu-49e58d069f567085.css" ><script src="../static.files/storage-db41da1a38ea3cb8.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-cabe4b1bd7b9da95.js"></script><noscript><link rel="stylesheet" media="(prefers-color-scheme:light)" href="../static.files/light-f194925aa375ae96.css"><link rel="stylesheet" media="(prefers-color-scheme:dark)" href="../static.files/dark-1dd4d1ce031e15de.css"><link rel="stylesheet" href="../static.files/noscript-cffde32267a19fd6.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></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 class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2></h2></nav><nav class="sidebar"><a class="logo-container" href="../actix_identity/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2 class="location"><a href="#">Crate actix_identity</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.6.0</li><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"
<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>