-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.rs
67 lines (58 loc) · 2.12 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use rauthy_client::device_code::DeviceCode;
use rauthy_client::rauthy_error::RauthyError;
use rauthy_client::token_set::OidcTokenSet;
use rauthy_client::{DangerAcceptInvalidCerts, RauthyHttpsOnly};
use tracing::{subscriber, Level};
use tracing_subscriber::FmtSubscriber;
#[tokio::main]
async fn main() -> Result<(), RauthyError> {
// the rauthy-client emits some tracing output
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let client_id = "device".to_string();
let mut device_code = DeviceCode::request_with(
"http://localhost:8080",
client_id.clone(),
None,
None,
None,
RauthyHttpsOnly::No,
DangerAcceptInvalidCerts::Yes,
)
.await?;
println!("{}", device_code);
println!(
r#"
You will get a complete uri with the code included as well
for displaying in a QR code or something like that:
{}"#,
device_code.verification_uri_complete.as_ref().unwrap()
);
// with the `qrcode` feature endabled, we can render the
// verification_uri_complete into one
let qr = device_code.qr_string()?;
println!("\n{}", qr);
// we can get a QR as SVG as well, but this example can't display it.
// let qr = device_code.qr_svg()?;
let ts = device_code.wait_for_token().await?;
println!("\nTokenSet on accept:\n{:?}", ts);
let claims = ts.id_claims()?.unwrap();
println!("\nWe get the user claims as well:\n{:?}", claims);
// If the client is configured to receive a refresh token, we can
// spawn a refresh handler that does all the magic behind the scenes.
// This will consume the current OidcTokenSet.
ts.into_refresh_handler_with(
client_id,
None,
None,
RauthyHttpsOnly::No,
DangerAcceptInvalidCerts::Yes,
)
.await?;
// After spawning it, you can get the currently saved tokens with
let _access_token = OidcTokenSet::access_token().await?;
let _id_token = OidcTokenSet::id_token().await?;
Ok(())
}