summaryrefslogtreecommitdiff
path: root/src/demo/main.rs
blob: f598b8423f3a130ff8f185c6b9409dae3cecabbf (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
use actix_middleware_rfc7662::{
    AnyScope, RequireAuthorization, RequireAuthorizationConfig, RequireScope,
};
use actix_web::{get, HttpResponse, HttpServer, Responder};

#[get("/read")]
async fn handle_read(_auth: RequireAuthorization<AnyScope>) -> impl Responder {
    HttpResponse::Ok().body("Success!\n")
}

struct WriteScope;
impl RequireScope for WriteScope {
    fn scope() -> &'static str {
        "write"
    }
}

#[get("/write")]
async fn handle_write(_auth: RequireAuthorization<WriteScope>) -> 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(
        "cid1".to_string(),
        Some("cs1".to_string()),
        "https://cadmium.jesterpm.net/oauth/authorize"
            .parse()
            .expect("invalid url"),
        "https://cadmium.jesterpm.net/oauth/introspect"
            .parse()
            .expect("invalid url"),
    );

    HttpServer::new(move || {
        actix_web::App::new()
            .app_data(oauth_config.clone())
            .service(handle_read)
            .service(handle_write)
    })
    .bind(bind)?
    .run()
    .await
}