Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29 implement refresh token into get auth token endpoint #36

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
merge
  • Loading branch information
Simon Bejer committed Nov 10, 2023
commit 499bdcdf90f890aba1f5166afc38ad5d744dbb75
96 changes: 47 additions & 49 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use std::env;
use std::sync::Arc;

use crate::api::ecdar_api::helpers::helpers::{setup_db_with_entities, AnyEntity};
use crate::api::server::server::get_auth_token_request::{AuthOption, user_credentials};
use regex::Regex;
use sea_orm::SqlErr;
use tonic::{Code, Request, Response, Status};
@@ -212,57 +213,54 @@ impl EcdarApiAuth for ConcreteEcdarApi {
) -> Result<Response<GetAuthTokenResponse>, Status> {

let message = request.get_ref().clone();
let uid: String;
let mut username = "".to_string();
let mut email = "".to_string();
let mut password = "".to_string();
// match message.auth_option {
// Some(auth_option) => match auth_option {
// AuthOption::RefreshToken(refresh_token) => {
// let refresh_token = refresh_token;
// println!("Refresh token: {}", refresh_token);
// }
// AuthOption::UserCredentials(user_credentials) => {
// match user_credentials.user {
// Some(user) => match user {
// user_credentials::User::Username(_username) => {
// username = _username;
// }
// user_credentials::User::Email(_email) => {
// email = _email;
// }
// },
// None => Err(Status::new(Code::Internal, "No user provided"))?,
// }
// password = user_credentials.password;
// }
// },
// None => Err(Status::new(Code::Internal, "No auth option provided"))?,
// }
// println!("Username: {}", username);
// println!("Email: {}", email);
// println!("Password: {}", password);

// uid = match self.user_context.get_user_by_credentials(email, username, password).await {
// Ok(user) => match user {
// Some(user) => user.id.to_string(),
// None => Err(Status::new(Code::Internal, "No user found"))?,
// },
// Err(error) => Err(Status::new(Code::Internal, error.to_string()))?,
// };

// let access_token = match auth::create_access_token(&uid) {
// Ok(token) => token,
// Err(e) => return Err(Status::new(Code::Internal, e.to_string())),
// };
// let refresh_token = match auth::create_refresh_token(&uid) {
// Ok(token) => token,
// Err(e) => return Err(Status::new(Code::Internal, e.to_string())),
// };
// Ok(Response::new(GetAuthTokenResponse {
// access_token,
// refresh_token,
// }))
let uid = match message.auth_option {
Some(auth_option) => match auth_option {
AuthOption::RefreshToken(refresh_token) => {
let refresh_token = refresh_token;
println!("Refresh token: {}", refresh_token);
let uid = get_uid_from_request(&request).unwrap().to_string();
uid
}
AuthOption::UserCredentials(user_credentials) => {
if let Some(user) = user_credentials.user {
match user {
user_credentials::User::Username(username) => {
match self.user_context.get_by_username(username).await {
Ok(Some(user)) => user.id.to_string(),
Ok(None) => Err(Status::new(Code::Internal, "No user found"))?,
Err(err) => Err(Status::new(Code::Internal, err.to_string()))?,
}
}
user_credentials::User::Email(email) => {
match self.user_context.get_by_email(email).await {
Ok(Some(user)) => user.id.to_string(),
Ok(None) => Err(Status::new(Code::Internal, "No user found"))?,
Err(err) => Err(Status::new(Code::Internal, err.to_string()))?,
}
}
}
} else {
Err(Status::new(Code::Internal, "No user provided"))?
}
}
},
None => Err(Status::new(Code::Internal, "No auth option provided"))?,
};

let access_token = match auth::create_access_token(&uid) {
Ok(token) => token,
Err(e) => return Err(Status::new(Code::Internal, e.to_string())),
};
let refresh_token = match auth::create_refresh_token(&uid) {
Ok(token) => token,
Err(e) => return Err(Status::new(Code::Internal, e.to_string())),
};
Ok(Response::new(GetAuthTokenResponse {
access_token,
refresh_token,
}))
}
async fn create_user(
&self,
7 changes: 7 additions & 0 deletions src/database/user_context.rs
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ pub trait UserContextTrait: EntityContextTrait<User> {
/// assert_eq!(model.id,1);
/// ```
async fn get_by_username(&self, username: String) -> Result<Option<User>, DbErr>;
async fn get_by_email(&self, email: String) -> Result<Option<User>, DbErr>;
}

impl Debug for dyn UserContextTrait + Send + Sync + 'static {
@@ -42,6 +43,12 @@ impl UserContextTrait for UserContext {
.one(&self.db_context.get_connection())
.await
}
async fn get_by_email(&self, email: String) -> Result<Option<User>, DbErr> {
UserEntity::find()
.filter(UserColumn::Email.eq(email))
.one(&self.db_context.get_connection())
.await
}
}

#[async_trait]