|
14 | 14 | //!
|
15 | 15 |
|
16 | 16 | use oauth2::basic::BasicClient;
|
17 |
| - |
18 | 17 | // Alternatively, this can be `oauth2::curl::http_client` or a custom client.
|
19 | 18 | use oauth2::reqwest::http_client;
|
20 | 19 | use oauth2::{
|
21 | 20 | AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, RedirectUrl, Scope,
|
22 | 21 | TokenResponse, TokenUrl,
|
23 | 22 | };
|
| 23 | +use url::Url; |
| 24 | + |
24 | 25 | use std::env;
|
25 | 26 | use std::io::{BufRead, BufReader, Write};
|
26 | 27 | use std::net::TcpListener;
|
27 |
| -use url::Url; |
28 | 28 |
|
29 | 29 | fn main() {
|
30 | 30 | let github_client_id = ClientId::new(
|
@@ -60,88 +60,73 @@ fn main() {
|
60 | 60 | .add_scope(Scope::new("user:email".to_string()))
|
61 | 61 | .url();
|
62 | 62 |
|
| 63 | + println!("Open this URL in your browser:\n{authorize_url}\n"); |
| 64 | + |
| 65 | + let (code, state) = { |
| 66 | + // A very naive implementation of the redirect server. |
| 67 | + let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); |
| 68 | + |
| 69 | + // The server will terminate itself after collecting the first code. |
| 70 | + let Some(mut stream) = listener.incoming().flatten().next() else { |
| 71 | + panic!("listener terminated without accepting a connection"); |
| 72 | + }; |
| 73 | + |
| 74 | + let mut reader = BufReader::new(&stream); |
| 75 | + |
| 76 | + let mut request_line = String::new(); |
| 77 | + reader.read_line(&mut request_line).unwrap(); |
| 78 | + |
| 79 | + let redirect_url = request_line.split_whitespace().nth(1).unwrap(); |
| 80 | + let url = Url::parse(&("http://localhost".to_string() + redirect_url)).unwrap(); |
| 81 | + |
| 82 | + let code = url |
| 83 | + .query_pairs() |
| 84 | + .find(|(key, _)| key == "code") |
| 85 | + .map(|(_, code)| AuthorizationCode::new(code.into_owned())) |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + let state = url |
| 89 | + .query_pairs() |
| 90 | + .find(|(key, _)| key == "state") |
| 91 | + .map(|(_, state)| CsrfToken::new(state.into_owned())) |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + let message = "Go back to your terminal :)"; |
| 95 | + let response = format!( |
| 96 | + "HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}", |
| 97 | + message.len(), |
| 98 | + message |
| 99 | + ); |
| 100 | + stream.write_all(response.as_bytes()).unwrap(); |
| 101 | + |
| 102 | + (code, state) |
| 103 | + }; |
| 104 | + |
| 105 | + println!("Github returned the following code:\n{}\n", code.secret()); |
63 | 106 | println!(
|
64 |
| - "Open this URL in your browser:\n{}\n", |
65 |
| - authorize_url.to_string() |
| 107 | + "Github returned the following state:\n{} (expected `{}`)\n", |
| 108 | + state.secret(), |
| 109 | + csrf_state.secret() |
66 | 110 | );
|
67 | 111 |
|
68 |
| - // A very naive implementation of the redirect server. |
69 |
| - let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); |
70 |
| - for stream in listener.incoming() { |
71 |
| - if let Ok(mut stream) = stream { |
72 |
| - let code; |
73 |
| - let state; |
74 |
| - { |
75 |
| - let mut reader = BufReader::new(&stream); |
76 |
| - |
77 |
| - let mut request_line = String::new(); |
78 |
| - reader.read_line(&mut request_line).unwrap(); |
79 |
| - |
80 |
| - let redirect_url = request_line.split_whitespace().nth(1).unwrap(); |
81 |
| - let url = Url::parse(&("http://localhost".to_string() + redirect_url)).unwrap(); |
82 |
| - |
83 |
| - let code_pair = url |
84 |
| - .query_pairs() |
85 |
| - .find(|pair| { |
86 |
| - let &(ref key, _) = pair; |
87 |
| - key == "code" |
88 |
| - }) |
89 |
| - .unwrap(); |
90 |
| - |
91 |
| - let (_, value) = code_pair; |
92 |
| - code = AuthorizationCode::new(value.into_owned()); |
93 |
| - |
94 |
| - let state_pair = url |
95 |
| - .query_pairs() |
96 |
| - .find(|pair| { |
97 |
| - let &(ref key, _) = pair; |
98 |
| - key == "state" |
99 |
| - }) |
100 |
| - .unwrap(); |
101 |
| - |
102 |
| - let (_, value) = state_pair; |
103 |
| - state = CsrfToken::new(value.into_owned()); |
104 |
| - } |
105 |
| - |
106 |
| - let message = "Go back to your terminal :)"; |
107 |
| - let response = format!( |
108 |
| - "HTTP/1.1 200 OK\r\ncontent-length: {}\r\n\r\n{}", |
109 |
| - message.len(), |
110 |
| - message |
111 |
| - ); |
112 |
| - stream.write_all(response.as_bytes()).unwrap(); |
113 |
| - |
114 |
| - println!("Github returned the following code:\n{}\n", code.secret()); |
115 |
| - println!( |
116 |
| - "Github returned the following state:\n{} (expected `{}`)\n", |
117 |
| - state.secret(), |
118 |
| - csrf_state.secret() |
119 |
| - ); |
120 |
| - |
121 |
| - // Exchange the code with a token. |
122 |
| - let token_res = client.exchange_code(code).request(http_client); |
123 |
| - |
124 |
| - println!("Github returned the following token:\n{:?}\n", token_res); |
125 |
| - |
126 |
| - if let Ok(token) = token_res { |
127 |
| - // NB: Github returns a single comma-separated "scope" parameter instead of multiple |
128 |
| - // space-separated scopes. Github-specific clients can parse this scope into |
129 |
| - // multiple scopes by splitting at the commas. Note that it's not safe for the |
130 |
| - // library to do this by default because RFC 6749 allows scopes to contain commas. |
131 |
| - let scopes = if let Some(scopes_vec) = token.scopes() { |
132 |
| - scopes_vec |
133 |
| - .iter() |
134 |
| - .map(|comma_separated| comma_separated.split(',')) |
135 |
| - .flatten() |
136 |
| - .collect::<Vec<_>>() |
137 |
| - } else { |
138 |
| - Vec::new() |
139 |
| - }; |
140 |
| - println!("Github returned the following scopes:\n{:?}\n", scopes); |
141 |
| - } |
142 |
| - |
143 |
| - // The server will terminate itself after collecting the first code. |
144 |
| - break; |
145 |
| - } |
| 112 | + // Exchange the code with a token. |
| 113 | + let token_res = client.exchange_code(code).request(http_client); |
| 114 | + |
| 115 | + println!("Github returned the following token:\n{:?}\n", token_res); |
| 116 | + |
| 117 | + if let Ok(token) = token_res { |
| 118 | + // NB: Github returns a single comma-separated "scope" parameter instead of multiple |
| 119 | + // space-separated scopes. Github-specific clients can parse this scope into |
| 120 | + // multiple scopes by splitting at the commas. Note that it's not safe for the |
| 121 | + // library to do this by default because RFC 6749 allows scopes to contain commas. |
| 122 | + let scopes = if let Some(scopes_vec) = token.scopes() { |
| 123 | + scopes_vec |
| 124 | + .iter() |
| 125 | + .flat_map(|comma_separated| comma_separated.split(',')) |
| 126 | + .collect::<Vec<_>>() |
| 127 | + } else { |
| 128 | + Vec::new() |
| 129 | + }; |
| 130 | + println!("Github returned the following scopes:\n{:?}\n", scopes); |
146 | 131 | }
|
147 | 132 | }
|
0 commit comments