forked from http-rs/tide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.rs
35 lines (29 loc) · 1.08 KB
/
simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#![feature(async_await, futures_api)]
use tide::{App, Seeded};
use tide_authorize::{Credentials, Realm, Unauthorized, User};
/// Does not enforce any authentication but accepts it.
async fn introspect(user: Result<User, Unauthorized>) -> String {
match user {
Ok(User(user)) => format!("Hello {}", user),
Err(_) => "No user logged authentication".into(),
}
}
async fn member(User(user): User) -> String {
format!("Only accessible to users. Like you, handsome {}", user)
}
async fn admin(User(_): User) -> &'static str {
"Highly secure admin page"
}
fn main() {
let mut app = App::new(());
let realm = Realm::new("WorldOfPureImagination").unwrap();
let credentials: Credentials = vec![
("irc".into(), "hunter2".into()),
("admin".into(), "admin".into()),
].into_iter().collect();
let accounts = credentials.freeze(realm);
app.at("/self").get(Seeded(introspect, accounts.clone()));
app.at("/").get(Seeded(member, accounts.clone()));
app.at("/admin").get(Seeded(admin, accounts.single_user("admin")));
app.serve();
}