Skip to content

Commit

Permalink
make auth method a required field and support empty org token
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstephens committed Feb 26, 2023
1 parent 34fa5c5 commit 28f4595
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/github_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ async fn get_user_for_token(token: &String) -> Result<String, Error> {
}

async fn get_org_members_page(org: &String, token: &String, page: u32) -> Vec<GhOrgMember> {
let mut auth_header_value: String = "Bearer ".to_string();
auth_header_value.push_str(token);
let client = reqwest::Client::new();
let mut req_url = "https://api.github.com/orgs/".to_string();
req_url.push_str(&org);
req_url.push_str("/members?per_page=100&page=");
req_url.push_str(&page.to_string());
println!("URL: {}", req_url);
let resp = client.get(&req_url)
let mut req = client.get(&req_url)
.header("User-Agent", "Authenticating Reverse Proxy (https://github.com/richardstephens/authenticating-reverse-proxy)")
.header("Accept", "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28")
.header("Authorization", &auth_header_value)
.send().await;
.header("X-GitHub-Api-Version", "2022-11-28");
if token != "" {
let mut auth_header_value: String = "Bearer ".to_string();
auth_header_value.push_str(token);
req = req.header("Authorization", &auth_header_value);
}

let resp = req.send().await;

let resp_content = resp.unwrap().json::<Vec<GhOrgMember>>().await.unwrap();
return resp_content;
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ struct Args {
#[arg(env, long, default_value = "127.0.0.1:8000")]
bind: String,

/// Auth method to to use (currently only supported is GH_BASIC)
#[arg(env, long)]
auth_method: String,

/// Target URL of reverse proxy
#[arg(env, long)]
target: String,

/// Token to use to read organisation members
#[arg(env, long)]
#[arg(env, long, default_value = "")]
gh_org_token: String,

/// Organisation to check membership of
Expand All @@ -32,7 +36,13 @@ struct Args {
#[tokio::main]
async fn main() {
let args = Args::parse();
get_org_members(args.gh_org, args.gh_org_token).await;

start_reverse_proxy(args.target, args.bind).await
if args.auth_method == "GH_BASIC" {
get_org_members(args.gh_org, args.gh_org_token).await;

start_reverse_proxy(args.target, args.bind).await
} else {
eprintln!("Auth method must be set to GH_BASIC");
std::process::exit(1);
}
}

0 comments on commit 28f4595

Please sign in to comment.