Merge pull request #161 from kpcyrd/oauth
Use new github oauth endpoint
This commit is contained in:
@@ -2,7 +2,7 @@ use diesel::pg::PgConnection;
|
||||
use oauth2::basic::BasicClient;
|
||||
use oauth2::prelude::*;
|
||||
use oauth2::{AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, RedirectUrl, TokenUrl, TokenResponse};
|
||||
use crate::github::GithubAuthenticator;
|
||||
use crate::github;
|
||||
use sn0int_registry::errors::*;
|
||||
use sn0int_registry::models::AuthToken;
|
||||
use url::Url;
|
||||
@@ -67,8 +67,7 @@ impl Authenticator {
|
||||
let access_token = response.access_token();
|
||||
let access_token = access_token.secret().to_string();
|
||||
|
||||
let client = GithubAuthenticator::from_env()?;
|
||||
let user = client.get_username(&access_token)?;
|
||||
let user = github::get_username(&access_token)?;
|
||||
|
||||
AuthToken::create(&AuthToken {
|
||||
id: state,
|
||||
|
||||
@@ -4,7 +4,7 @@ use sn0int_registry::db::Connection;
|
||||
use rocket::http::Status;
|
||||
use rocket::{Request, Outcome};
|
||||
use rocket::request::{self, FromRequest};
|
||||
use crate::github::GithubAuthenticator;
|
||||
use crate::github;
|
||||
|
||||
|
||||
pub struct AuthHeader(String);
|
||||
@@ -12,8 +12,7 @@ pub struct AuthHeader(String);
|
||||
impl AuthHeader {
|
||||
pub fn verify(&self, connection: &Connection) -> Result<String> {
|
||||
let session = AuthToken::read(&self.0, &connection)?;
|
||||
let client = GithubAuthenticator::from_env()?;
|
||||
client.get_username(&session.access_token)
|
||||
github::get_username(&session.access_token)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +1,23 @@
|
||||
use sn0int_registry::errors::*;
|
||||
use std::env;
|
||||
use reqwest;
|
||||
|
||||
pub fn get_username(oauth_token: &str) -> Result<String> {
|
||||
let client = reqwest::Client::new();
|
||||
let mut resp = client.get("https://api.github.com/user")
|
||||
.header("Authorization", format!("token {}", oauth_token))
|
||||
.send()
|
||||
.context("Failed to check access_token")?
|
||||
.error_for_status()
|
||||
.context("Github returned http error")?;
|
||||
|
||||
pub struct GithubAuthenticator {
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
}
|
||||
let data = resp.json::<GithubUser>()
|
||||
.context("Failed to deserialize github reply")?;
|
||||
|
||||
impl GithubAuthenticator {
|
||||
pub fn new(client_id: String, client_secret: String) -> GithubAuthenticator {
|
||||
GithubAuthenticator {
|
||||
client_id,
|
||||
client_secret,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_env() -> Result<GithubAuthenticator> {
|
||||
let client_id = env::var("GITHUB_CLIENT_ID")
|
||||
.context("GITHUB_CLIENT_ID is not set")?;
|
||||
let client_secret = env::var("GITHUB_CLIENT_SECRET")
|
||||
.context("GITHUB_CLIENT_SECRET is not set")?;
|
||||
Ok(GithubAuthenticator::new(client_id, client_secret))
|
||||
}
|
||||
|
||||
pub fn get_username(&self, oauth_token: &str) -> Result<String> {
|
||||
let url = format!("https://api.github.com/applications/{}/tokens/{}", self.client_id, oauth_token);
|
||||
let client = reqwest::Client::new();
|
||||
let mut resp = client.get(&url)
|
||||
.basic_auth(&self.client_id, Some(&self.client_secret))
|
||||
.send()?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
bail!("Github returned: {}", resp.status())
|
||||
}
|
||||
|
||||
let data = resp.json::<GithubReply>()
|
||||
.context("Failed to deserialize github reply")?;
|
||||
|
||||
Ok(data.user.login)
|
||||
}
|
||||
Ok(data.login)
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GithubReply {
|
||||
user: GithubUser,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GithubUser {
|
||||
struct GithubUser {
|
||||
login: String,
|
||||
#[serde(rename="type")]
|
||||
user_type: String,
|
||||
|
||||
Reference in New Issue
Block a user