Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(github): support redirect flow for github link (#2291)

Will Hutchinsonffe768b84fa6
diff --git a/js/app/packages/service-clients/service-auth/generated/client.ts b/js/app/packages/service-clients/service-auth/generated/client.ts
index e89e19326d..d3bc3f37ba 100644
--- a/js/app/packages/service-clients/service-auth/generated/client.ts
+++ b/js/app/packages/service-clients/service-auth/generated/client.ts
@@ -21,6 +21,7 @@ import type {
GetProfilePicturesRequestBody,
GetUserInfo,
GetUserLinkExistsParams,
+ InitGithubLinkParams,
InitGithubLinkResponse,
InviteToTeamRequest,
MacroApiTokenParams,
@@ -547,14 +548,27 @@ export type initGithubLinkResponse =
| initGithubLinkResponseSuccess
| initGithubLinkResponseError;
-export const getInitGithubLinkUrl = () => {
- return `/link/github`;
+export const getInitGithubLinkUrl = (params: InitGithubLinkParams) => {
+ const normalizedParams = new URLSearchParams();
+
+ Object.entries(params || {}).forEach(([key, value]) => {
+ if (value !== undefined) {
+ normalizedParams.append(key, value === null ? 'null' : value.toString());
+ }
+ });
+
+ const stringifiedParams = normalizedParams.toString();
+
+ return stringifiedParams.length > 0
+ ? `/link/github?${stringifiedParams}`
+ : `/link/github`;
};
export const initGithubLink = async (
+ params: InitGithubLinkParams,
options?: RequestInit
): Promise<initGithubLinkResponse> => {
- const res = await fetch(getInitGithubLinkUrl(), {
+ const res = await fetch(getInitGithubLinkUrl(params), {
...options,
method: 'POST',
});
diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/index.ts b/js/app/packages/service-clients/service-auth/generated/schemas/index.ts
index e571a0b6ce..ae60be3116 100644
--- a/js/app/packages/service-clients/service-auth/generated/schemas/index.ts
+++ b/js/app/packages/service-clients/service-auth/generated/schemas/index.ts
@@ -27,6 +27,7 @@ export * from './getProfilePicturesRequestBody';
export * from './getUserInfo';
export * from './getUserInfoOrganizationId';
export * from './getUserLinkExistsParams';
+export * from './initGithubLinkParams';
export * from './initGithubLinkResponse';
export * from './inviteToTeamRequest';
export * from './macroApiTokenParams';
diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/initGithubLinkParams.ts b/js/app/packages/service-clients/service-auth/generated/schemas/initGithubLinkParams.ts
new file mode 100644
index 0000000000..e95d3b58bc
--- /dev/null
+++ b/js/app/packages/service-clients/service-auth/generated/schemas/initGithubLinkParams.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.13.0 🍺
+ * Do not edit manually.
+ * authentication_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+export type InitGithubLinkParams = {
+ /**
+ * **OPTIONAL**. The original url to redirect to.
+ */
+ original_url: string;
+};
diff --git a/js/app/packages/service-clients/service-auth/openapi.json b/js/app/packages/service-clients/service-auth/openapi.json
index 618d50d826..fa56de575c 100644
--- a/js/app/packages/service-clients/service-auth/openapi.json
+++ b/js/app/packages/service-clients/service-auth/openapi.json
@@ -370,6 +370,17 @@
"tags": ["link::github"],
"summary": "Initiates a link for a user",
"operationId": "init_github_link",
+ "parameters": [
+ {
+ "name": "original_url",
+ "in": "query",
+ "description": "**OPTIONAL**. The original url to redirect to.",
+ "required": true,
+ "schema": {
+ "type": "string"
+ }
+ }
+ ],
"responses": {
"200": {
"description": "",
diff --git a/rust/cloud-storage/authentication_service/src/api/link/github.rs b/rust/cloud-storage/authentication_service/src/api/link/github.rs
index 9db0a15618..4fc238723a 100644
--- a/rust/cloud-storage/authentication_service/src/api/link/github.rs
+++ b/rust/cloud-storage/authentication_service/src/api/link/github.rs
@@ -1,12 +1,14 @@
use axum::{
Json,
- extract::State,
+ extract::{Query, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use github::domain::{models::GithubError, ports::GithubLinkService};
use macro_middleware::tracking::ClientIp;
use model_user::axum_extractor::MacroUserExtractor;
+use serde_utils::urlencode::UrlEncoded;
+use url::Url;
use crate::api::{context::ApiContext, oauth2::OAuthState};
@@ -61,11 +63,21 @@ impl IntoResponse for InitGithubLinkError {
}
}
+#[derive(Debug, serde::Deserialize)]
+pub(crate) struct InitGithubLinkQueryParams {
+ /// Once the frontend is update to NOT 2x urlencode this then this should be changed to
+ /// `Option<Url>`
+ original_url: Option<UrlEncoded<Url>>,
+}
+
/// Initiates a link for a user
#[utoipa::path(
post,
operation_id = "init_github_link",
path = "/link/github",
+ params(
+ ("original_url" = String, Query, description = "**OPTIONAL**. The original url to redirect to.")
+ ),
responses(
(status = 200, body=InitGithubLinkResponse),
(status = 400, body=ErrorResponse),
@@ -77,9 +89,11 @@ impl IntoResponse for InitGithubLinkError {
#[tracing::instrument(skip(ctx, ip_context, user_context), fields(client_ip=%ip_context, user_id=%user_context.user_context.user_id, fusion_user_id=%user_context.user_context.fusion_user_id), err)]
pub async fn init_github_link_handler(
State(ctx): State<ApiContext>,
+ query: Query<InitGithubLinkQueryParams>,
ip_context: ClientIp,
user_context: MacroUserExtractor,
) -> Result<Json<InitGithubLinkResponse>, InitGithubLinkError> {
+ let Query(InitGithubLinkQueryParams { original_url }) = query;
// TODO: this should probably be a middleware or extractor
// Check count of in-progress links
let count =
@@ -111,9 +125,7 @@ pub async fn init_github_link_handler(
let state = OAuthState {
identity_provider_id: github_idp_id.clone(),
link_id: Some(link_id),
- // TODO: support
- original_url: None,
- // TODO: support
+ original_url: original_url.map(|x| x.0.to_string()),
is_mobile: None,
};
diff --git a/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs b/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs
index 8da3df3257..d9fb3bf7d7 100644
--- a/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs
+++ b/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs
@@ -1,7 +1,7 @@
use anyhow::Context;
use axum::{
Json,
- response::{Html, IntoResponse, Response},
+ response::{Html, IntoResponse, Redirect, Response},
};
use github::domain::{models::GithubError, ports::GithubLinkService};
use macro_user_id::{cowlike::CowLike, user_id::MacroUserId};
@@ -102,6 +102,12 @@ pub(in crate::api::oauth2) async fn handler(
if let Some(link_id) = state.link_id.as_ref() {
link_user(ctx, link_id, code).await?;
+ if let Some(original_url) = state.original_url.as_ref() {
+ return Ok(GithubOAuthSuccess::Login(
+ Redirect::temporary(original_url).into_response(),
+ ));
+ }
+
// Return HTML that notifies the opener window and closes the popup
let html = Html(
r#"
@@ -128,6 +134,7 @@ pub(in crate::api::oauth2) async fn handler(
</html>
"#,
);
+
return Ok(GithubOAuthSuccess::Html(html));
}