Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
feat: google login
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSoZRious committed Aug 4, 2023
1 parent 337ab32 commit 60d4269
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use utoipa::{
crate::handler::auth::validate,
crate::handler::auth::verify_ticket,
crate::handler::auth::refresh_token,
crate::handler::auth::get_google_oauth_redirect_uri,
crate::handler::auth::get_token_from_google_oauth_code,
crate::handler::file::upload,
crate::handler::user::update,
crate::handler::user::update_personality,
Expand Down Expand Up @@ -60,7 +62,8 @@ use utoipa::{
crate::dto::CheckinResponse,
crate::dto::RedeemItemResponse,
crate::dto::HasRedeemItemResponse,
crate::dto::UpdatePersonality
crate::dto::UpdatePersonality,
crate::dto::GoogleAuthorizationCodeQuery,
)),
info(
title = "RPKM66",
Expand Down
5 changes: 5 additions & 0 deletions src/dto/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub struct Validate {
pub token: String,
}

#[derive(serde::Deserialize, ToSchema)]
pub struct GoogleAuthorizationCodeQuery {
pub code: String,
}

#[derive(serde::Deserialize, serde::Serialize, ToSchema)]
pub struct Credential {
#[schema(example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3BiZX...")]
Expand Down
45 changes: 44 additions & 1 deletion src/handler/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::dto::RedeemNewToken;
use crate::dto::{self, IntoDto};
use crate::dto::{GoogleAuthorizationCodeQuery, RedeemNewToken};
use crate::error::Error;
use crate::middleware::auth::Cred;
use crate::service::auth::Service;
use axum::extract::Query;
use axum::response::Redirect;
use axum::{
extract::{Json, State},
response::IntoResponse,
Expand Down Expand Up @@ -83,3 +86,43 @@ pub async fn refresh_token(
.await
.map(IntoDto::into_response)
}

#[utoipa::path(
get,
path = "/auth/google",
tag = "Auth",
responses(
(status = 200, description = "Success"),
),
)]
pub async fn get_google_oauth_redirect_uri(State(handler): State<Handler>) -> impl IntoResponse {
let url = match handler.service.get_google_oauth_redirect_uri().await {
Ok(x) => x,
Err(_) => return Err(Error::InternalServer),
};

Ok(Redirect::temporary(&url))
}

#[utoipa::path(
post,
path = "/auth/google",
tag = "Auth",
responses(
(status = 200, description = "Success", body = Credential),
(status = 400, description = "Bad request"),
(status = 401, description = "Unauthorized"),
),
)]
pub async fn get_token_from_google_oauth_code(
State(handler): State<Handler>,
Query(code): Query<GoogleAuthorizationCodeQuery>,
) -> impl IntoResponse {
let code = code.code;

handler
.service
.get_token_from_google_oauth_code(code)
.await
.map(IntoDto::into_response)
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ async fn main() {
.route("/", get(health_check))
.route("/auth/verify", post(handler::auth::verify_ticket))
.route("/auth/me", get(handler::auth::validate))
.route("/auth/google", get(handler::auth::get_google_oauth_redirect_uri))
.route("/auth/google", post(handler::auth::get_token_from_google_oauth_code))
.route("/auth/refreshToken", post(handler::auth::refresh_token))
.route("/file/upload", post(handler::file::upload))
.route("/user", patch(handler::user::update))
Expand Down
24 changes: 22 additions & 2 deletions src/service/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rpkm66_rust_proto::rpkm66::auth::auth::v1::{
auth_service_client::AuthServiceClient, Credential, RefreshTokenRequest, ValidateRequest,
VerifyTicketRequest,
auth_service_client::AuthServiceClient, Credential, GetGoogleLoginUrlRequest,
RefreshTokenRequest, ValidateRequest, VerifyGoogleLoginRequest, VerifyTicketRequest,
};
use tonic::transport::Channel;

Expand Down Expand Up @@ -58,4 +58,24 @@ impl Service {
.credential
.ok_or(Error::InternalServer)
}

pub async fn get_google_oauth_redirect_uri(&self) -> Result<String> {
Ok(self
.client
.clone()
.get_google_login_url(GetGoogleLoginUrlRequest {})
.await?
.into_inner()
.url)
}

pub async fn get_token_from_google_oauth_code(&self, code: String) -> Result<Credential> {
self.client
.clone()
.verify_google_login(VerifyGoogleLoginRequest { code: code })
.await?
.into_inner()
.credential
.ok_or(Error::InternalServer)
}
}

0 comments on commit 60d4269

Please sign in to comment.