From 3605650a90c0710aeee93930ba2622bf00a88b70 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Wed, 23 Mar 2022 08:22:38 -0700 Subject: Add support for token extensions --- examples/demo/main.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 examples/demo/main.rs (limited to 'examples/demo/main.rs') diff --git a/examples/demo/main.rs b/examples/demo/main.rs new file mode 100644 index 0000000..7fddcb2 --- /dev/null +++ b/examples/demo/main.rs @@ -0,0 +1,53 @@ +use actix_middleware_rfc7662::indieauth::IndieAuthToken; +use actix_middleware_rfc7662::{ + AnyScope, RequireAuthorization, RequireAuthorizationConfig, RequireScope, +}; +use actix_web::{get, HttpResponse, HttpServer, Responder}; + +#[get("/read")] +async fn handle_read(auth: RequireAuthorization) -> impl Responder { + HttpResponse::Ok().body(format!( + "Hello {}!\n", + auth.introspection().extra_fields().me() + )) +} + +struct WriteScope; +impl RequireScope for WriteScope { + fn scope() -> &'static str { + "write" + } +} + +#[get("/write")] +async fn handle_write(_auth: RequireAuthorization) -> impl Responder { + HttpResponse::Ok().body("Success!\n") +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + let bind = std::env::var("BIND").unwrap_or_else(|_| "127.0.0.1:8182".to_string()); + + let oauth_config = RequireAuthorizationConfig::::new( + std::env::var("CLIENT_ID").expect("Missing CLIENT_ID"), + std::env::var("CLIENT_SECRET").ok(), + std::env::var("AUTH_ENDPOINT") + .expect("Missing AUTH_ENDPOINT") + .parse() + .expect("AUTH_ENDPOINT: invalid url"), + std::env::var("INTROSPECT_ENDPOINT") + .expect("Missing INTROSPECT_ENDPOINT") + .parse() + .expect("INTROSPECT_ENDPOINT: invalid url"), + ); + + HttpServer::new(move || { + actix_web::App::new() + .app_data(oauth_config.clone()) + .service(handle_read) + .service(handle_write) + }) + .bind(bind)? + .run() + .await +} -- cgit v1.2.3