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
2022-09-11 20:14:58 +00:00

79 lines
11 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."><meta name="keywords" content="rust, rustlang, rust-lang, actix_identity"><title>actix_identity - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.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="sidebar-logo" href="../actix_identity/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../actix_identity/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate actix_identity</a></h2><div class="sidebar-elems"><div class="block"><ul><li class="version">Version 0.5.2</li><li><a id="all-types" href="all.html">All Items</a></li></ul></div><section><div class="block"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../actix_identity/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">actix_identity</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/actix_identity/lib.rs.html#1-100">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-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="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="attribute">#[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="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="attribute">#[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="attribute">#[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="attribute">#[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="IdentityMiddleware"><code>IdentityMiddleware</code></a> via <a href="struct.IdentityMiddleware.html#method.builder" title="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"><code>IdentityMiddlewareBuilder::visit_deadline</code></a>;</li>
<li>logged in too long ago (see <a href="config/struct.IdentityMiddlewareBuilder.html#method.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><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="config/index.html" title="actix_identity::config mod">config</a></div><div class="item-right docblock-short"><p>Configuration options to tune the behaviour of <a href="struct.IdentityMiddleware.html" title="IdentityMiddleware"><code>IdentityMiddleware</code></a>.</p>
</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Identity.html" title="actix_identity::Identity struct">Identity</a></div><div class="item-right docblock-short"><p>A verified user identity. It can be used as a request extractor.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.IdentityMiddleware.html" title="actix_identity::IdentityMiddleware struct">IdentityMiddleware</a></div><div class="item-right docblock-short"><p>Identity management middleware.</p>
</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.IdentityExt.html" title="actix_identity::IdentityExt trait">IdentityExt</a></div><div class="item-right docblock-short"><p>Helper trait to retrieve an <a href="struct.Identity.html" title="Identity"><code>Identity</code></a> instance from various <code>actix-web</code>s types.</p>
</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="actix_identity" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0-nightly (228710758 2022-09-10)" ></div></body></html>