Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00d76a4
commit 07954d7
Showing
8 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DATABASE_URL="postgres://postgres:password@localhost:5432/fakenews" | ||
ADMIN_EMAIL="admin@fakenewsdetector.org" | ||
ADMIN_PASSWORD="123" | ||
ROCKET_SECRET_KEY="secretKeyJustForDevelopmentB3VRq9qA2y4lc+Mg=" | ||
TWITTER_APP_KEY="you have to create a twitter app and fill this" | ||
TWITTER_APP_SECRET="you have to create a twitter app and fill this" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ target/ | |
target/ | ||
**/*.rs.bk | ||
node_modules* | ||
elm-stuff | ||
elm-stuff | ||
.env |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod votes; | |
pub mod categories; | ||
pub mod links; | ||
pub mod admin; | ||
pub mod twitter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
extern crate rocket; | ||
extern crate egg_mode; | ||
extern crate tokio_core; | ||
extern crate cookie; | ||
|
||
use rocket::response::{status, Redirect}; | ||
use rocket::http::Status; | ||
use tokio_core::reactor; | ||
use rocket::http::{Cookie, Cookies}; | ||
use cookie::SameSite; | ||
use std::env; | ||
|
||
#[get("/twitter/auth")] | ||
fn auth(mut cookies: Cookies) -> Result<Redirect, status::Custom<String>> { | ||
let (mut core, handle, con_token) = get_config(); | ||
let request_token = core.run(egg_mode::request_token( | ||
&con_token, | ||
"http://localhost:8000/twitter/callback", | ||
&handle, | ||
)).map_err(|err| internal_error(&*format!("{}", err)))?; | ||
|
||
let mut secret_cookie = Cookie::new("request_token_secret", request_token.secret.to_owned()); | ||
secret_cookie.set_same_site(SameSite::Lax); | ||
cookies.add_private(secret_cookie); | ||
|
||
Ok(Redirect::found( | ||
&*egg_mode::authenticate_url(&request_token), | ||
)) | ||
} | ||
|
||
#[derive(FromForm)] | ||
pub struct TwitterCallbackParams { | ||
oauth_token: String, | ||
oauth_verifier: String, | ||
} | ||
|
||
#[get("/twitter/callback?<params>")] | ||
fn callback( | ||
mut cookies: Cookies, | ||
params: TwitterCallbackParams, | ||
) -> Result<Redirect, status::Custom<String>> { | ||
let (mut core, handle, con_token) = get_config(); | ||
let request_token_secret = cookies | ||
.get_private("request_token_secret") | ||
.map(|cookie| cookie.value().to_owned()) | ||
.unwrap_or(String::from("")); | ||
|
||
if request_token_secret.is_empty() { | ||
return Ok(Redirect::found("/twitter/auth")); | ||
} | ||
|
||
let request_token = egg_mode::KeyPair::new(params.oauth_token, request_token_secret); | ||
|
||
let (_token, _user_id, screen_name) = core.run(egg_mode::access_token( | ||
con_token, | ||
&request_token, | ||
params.oauth_verifier, | ||
&handle, | ||
)).map_err(|err| internal_error(&*format!("{}", err)))?; | ||
|
||
Err(status::Custom(Status::Ok, String::from(screen_name))) | ||
} | ||
|
||
fn get_config() -> (tokio_core::reactor::Core, tokio_core::reactor::Handle, egg_mode::KeyPair) { | ||
let core = reactor::Core::new().unwrap(); | ||
let handle = core.handle(); | ||
let con_token = egg_mode::KeyPair::new( | ||
env::var("TWITTER_APP_KEY").unwrap(), | ||
env::var("TWITTER_APP_SECRET").unwrap(), | ||
); | ||
|
||
(core, handle, con_token) | ||
} | ||
|
||
fn internal_error(text: &str) -> status::Custom<String> { | ||
status::Custom(Status::InternalServerError, String::from(text)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters