-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on auth. moving REST /auth to /graphql, still need to setup csrf…
… for new endpoint
- Loading branch information
Showing
24 changed files
with
536 additions
and
135 deletions.
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
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
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,137 @@ | ||
use crate::graphql::Context; | ||
use crate::{auth, model}; | ||
use rand::{distributions::Alphanumeric, Rng}; | ||
use thiserror::Error; | ||
use juniper::FieldResult; | ||
use chrono::{Duration, Utc}; | ||
// use std::net::SocketAddr; | ||
use uuid::Uuid; | ||
|
||
#[derive(juniper::GraphQLInputObject, Debug)] | ||
pub struct AuthInput { | ||
email: String, | ||
password: String, | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Error, Debug)] | ||
pub enum AuthError { | ||
#[error("invalid credentials")] | ||
InvalidCredentials, | ||
#[error("could not hash password")] | ||
ArgonError, | ||
} | ||
|
||
|
||
pub struct AuthMutation; | ||
|
||
#[juniper::graphql_object(Context = Context)] | ||
impl AuthMutation { | ||
async fn register(ctx: &Context, input: AuthInput) -> FieldResult<model::Auth> { | ||
let argon = ctx.argon(); | ||
|
||
let AuthInput { email, password } = input; | ||
let password = argon.hasher().with_password(password).hash()?; | ||
let id = Uuid::new_v4(); | ||
|
||
crate::sql::account::create_account(ctx.database(), id, &email, &password).await?; | ||
|
||
let account = crate::sql::account::get_account(ctx.database(), &email).await?; | ||
|
||
let identity = model::session::Identity { | ||
fingerprint: None, | ||
// TODO actually get remote IP | ||
ip: None | ||
}; | ||
|
||
let claims = auth::Claims { | ||
session: rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(64) | ||
.collect(), | ||
csrf: rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(64) | ||
.collect(), | ||
}; | ||
|
||
let csrf = claims.csrf.clone(); | ||
// TODO make request lifetime a custom field | ||
let expiry = Utc::now() + Duration::seconds(ctx.session_lifetime(Some(1000000))); | ||
|
||
crate::sql::account::create_session( | ||
ctx.database(), | ||
&claims.session, | ||
&claims.csrf, | ||
account.id, | ||
identity, | ||
expiry, | ||
) | ||
.await?; | ||
|
||
let jwt = ctx.jwt().encode(claims, expiry)?; | ||
|
||
Ok(model::Auth { | ||
jwt, | ||
csrf | ||
}) | ||
} | ||
async fn login(ctx: &Context, input: AuthInput) -> FieldResult<model::Auth> { | ||
let AuthInput { email, password } = input; | ||
|
||
let account = crate::sql::account::get_account_id_password_by_email(ctx.database(), &email) | ||
.await? | ||
.ok_or(AuthError::InvalidCredentials)?; | ||
|
||
let is_valid = ctx | ||
.argon() | ||
.verifier() | ||
.with_hash(&account.password) | ||
.with_password(&password) | ||
.verify() | ||
.or(Err(AuthError::ArgonError))?; | ||
|
||
if !is_valid { | ||
return Err(AuthError::InvalidCredentials.into()); | ||
} | ||
|
||
let identity = model::session::Identity { | ||
fingerprint: None, | ||
// TODO actually get remote IP | ||
ip: None | ||
}; | ||
|
||
let claims = auth::Claims { | ||
session: rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(64) | ||
.collect(), | ||
csrf: rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(64) | ||
.collect(), | ||
}; | ||
|
||
let csrf = claims.csrf.clone(); | ||
// TODO make request lifetime a custom field | ||
let expiry = Utc::now() + Duration::seconds(ctx.session_lifetime(Some(1000000))); | ||
|
||
crate::sql::account::create_session( | ||
ctx.database(), | ||
&claims.session, | ||
&claims.csrf, | ||
account.id, | ||
identity, | ||
expiry, | ||
) | ||
.await?; | ||
|
||
let jwt = ctx.jwt().encode(claims, expiry)?; | ||
|
||
Ok(model::Auth { | ||
jwt, | ||
csrf | ||
}) | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,3 +1,21 @@ | ||
use crate::graphql::Context; | ||
|
||
mod account; | ||
mod auth; | ||
|
||
use account::AccountMutation; | ||
use auth::AuthMutation; | ||
|
||
pub struct Mutation; | ||
|
||
#[juniper::graphql_object(Context = Context)] | ||
impl Mutation { | ||
fn auth() -> AuthMutation { | ||
AuthMutation | ||
} | ||
fn account() -> AccountMutation { | ||
AccountMutation | ||
} | ||
} | ||
|
||
|
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,8 @@ | ||
use juniper::GraphQLObject; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Serialize, Deserialize, GraphQLObject, Debug)] | ||
pub struct Auth { | ||
pub csrf: String, | ||
pub jwt: String | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
pub mod account; | ||
mod redacted; | ||
pub mod session; | ||
pub mod auth; | ||
|
||
pub use account::Account; | ||
pub use session::Session; | ||
pub use auth::Auth; |
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApolloClient, InMemoryCache } from '@apollo/client'; | ||
|
||
const client = new ApolloClient({ | ||
uri: 'http://localhost:3535/graphql/query', | ||
cache: new InMemoryCache() | ||
}); | ||
|
||
export default client; |
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
Oops, something went wrong.