|
1 | 1 | use conduit::{Request, Response};
|
2 | 2 | use conduit_middleware::Middleware;
|
3 | 3 |
|
4 |
| -use curl; |
5 |
| -use curl::easy::{Easy, List}; |
6 |
| - |
7 |
| -use oauth2::*; |
8 |
| - |
9 |
| -use serde_json; |
10 |
| -use serde::Deserialize; |
11 |
| - |
12 |
| -use std::str; |
13 | 4 | use std::error::Error;
|
14 | 5 | use std::collections::HashMap;
|
15 | 6 |
|
16 |
| -use app::App; |
17 |
| -use util::{human, internal, CargoResult, ChainError}; |
18 | 7 | use Uploader;
|
19 | 8 |
|
20 |
| -/// Does all the nonsense for sending a GET to Github. Doesn't handle parsing |
21 |
| -/// because custom error-code handling may be desirable. Use |
22 |
| -/// `parse_github_response` to handle the "common" processing of responses. |
23 |
| -pub fn github(app: &App, url: &str, auth: &Token) -> Result<(Easy, Vec<u8>), curl::Error> { |
24 |
| - let url = format!("{}://api.github.com{}", app.config.api_protocol, url); |
25 |
| - info!("GITHUB HTTP: {}", url); |
26 |
| - |
27 |
| - let mut headers = List::new(); |
28 |
| - headers |
29 |
| - .append("Accept: application/vnd.github.v3+json") |
30 |
| - .unwrap(); |
31 |
| - headers.append("User-Agent: hello!").unwrap(); |
32 |
| - headers |
33 |
| - .append(&format!("Authorization: token {}", auth.access_token)) |
34 |
| - .unwrap(); |
35 |
| - |
36 |
| - let mut handle = app.handle(); |
37 |
| - handle.url(&url).unwrap(); |
38 |
| - handle.get(true).unwrap(); |
39 |
| - handle.http_headers(headers).unwrap(); |
40 |
| - |
41 |
| - let mut data = Vec::new(); |
42 |
| - { |
43 |
| - let mut transfer = handle.transfer(); |
44 |
| - transfer |
45 |
| - .write_function(|buf| { |
46 |
| - data.extend_from_slice(buf); |
47 |
| - Ok(buf.len()) |
48 |
| - }) |
49 |
| - .unwrap(); |
50 |
| - transfer.perform()?; |
51 |
| - } |
52 |
| - Ok((handle, data)) |
53 |
| -} |
54 |
| - |
55 |
| -/// Checks for normal responses |
56 |
| -pub fn parse_github_response<'de, 'a: 'de, T: Deserialize<'de>>( |
57 |
| - mut resp: Easy, |
58 |
| - data: &'a [u8], |
59 |
| -) -> CargoResult<T> { |
60 |
| - match resp.response_code().unwrap() { |
61 |
| - 200 => {} |
62 |
| - // Ok! |
63 |
| - 403 => { |
64 |
| - return Err(human( |
65 |
| - "It looks like you don't have permission \ |
66 |
| - to query a necessary property from Github \ |
67 |
| - to complete this request. \ |
68 |
| - You may need to re-authenticate on \ |
69 |
| - crates.io to grant permission to read \ |
70 |
| - github org memberships. Just go to \ |
71 |
| - https://crates.io/login", |
72 |
| - )); |
73 |
| - } |
74 |
| - n => { |
75 |
| - let resp = String::from_utf8_lossy(data); |
76 |
| - return Err(internal(&format_args!( |
77 |
| - "didn't get a 200 result from \ |
78 |
| - github, got {} with: {}", |
79 |
| - n, |
80 |
| - resp |
81 |
| - ))); |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - let json = str::from_utf8(data) |
86 |
| - .ok() |
87 |
| - .chain_error(|| internal("github didn't send a utf8-response"))?; |
88 |
| - |
89 |
| - serde_json::from_str(json).chain_error(|| internal("github didn't send a valid json response")) |
90 |
| -} |
91 |
| - |
92 |
| -/// Gets a token with the given string as the access token, but all |
93 |
| -/// other info null'd out. Generally, just to be fed to the `github` fn. |
94 |
| -pub fn token(token: String) -> Token { |
95 |
| - Token { |
96 |
| - access_token: token, |
97 |
| - scopes: Vec::new(), |
98 |
| - token_type: String::new(), |
99 |
| - } |
100 |
| -} |
101 |
| - |
102 | 9 | #[derive(Clone, Debug)]
|
103 | 10 | pub struct SecurityHeadersMiddleware {
|
104 | 11 | headers: HashMap<String, Vec<String>>,
|
|
0 commit comments