Skip to content

Commit

Permalink
e2e working Notion
Browse files Browse the repository at this point in the history
  • Loading branch information
spolu committed Jul 15, 2024
1 parent 01b2e40 commit 54f30cc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/src/oauth/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl FromStr for ConnectionProvider {

#[derive(Debug, Clone, Serialize, PartialEq, Deserialize)]
pub struct FinalizeResult {
pub redirect_uri: String,
pub code: String,
pub access_token: String,
pub access_token_expiry: Option<u64>,
Expand Down Expand Up @@ -415,7 +416,7 @@ impl Connection {
self.status = ConnectionStatus::Finalized;
store.update_connection_status(self).await?;

self.redirect_uri = Some(redirect_uri.to_string());
self.redirect_uri = Some(finalize.redirect_uri);
self.encrypted_authorization_code = Some(Connection::seal_str(&finalize.code)?);
self.access_token_expiry = finalize.access_token_expiry;
self.encrypted_access_token = Some(Connection::seal_str(&finalize.access_token)?);
Expand Down
3 changes: 2 additions & 1 deletion core/src/oauth/providers/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ impl Provider for GithubConnectionProvider {
&self,
_connection: &Connection,
code: &str,
_redirect_uri: &str,
redirect_uri: &str,
) -> Result<FinalizeResult> {
// `code` is the installation_id returned by Github.
let (token, expiry, raw_json) = self.refresh_token(code).await?;

// We store the installation_id as `code` which will be used to refresh tokens.
Ok(FinalizeResult {
redirect_uri: redirect_uri.to_string(),
code: code.to_string(),
access_token: token.to_string(),
access_token_expiry: Some(expiry),
Expand Down
9 changes: 6 additions & 3 deletions core/src/oauth/providers/notion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl NotionConnectionProvider {
NotionConnectionProvider {}
}

fn bearer(&self) -> String {
fn basic_auth(&self) -> String {
general_purpose::STANDARD.encode(&format!(
"{}:{}",
*OAUTH_NOTION_CLIENT_ID, *OAUTH_NOTION_CLIENT_SECRET
Expand All @@ -54,11 +54,13 @@ impl Provider for NotionConnectionProvider {
"redirect_uri": redirect_uri,
});

println!("Notion finalize body: {}", body);

let req = reqwest::Client::new()
.post("https://api.notion.com/v1/oauth/token")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", self.bearer()))
.header("Authorization", format!("Basic {}", self.basic_auth()))
.json(&body);

let now = utils::now_secs();
Expand All @@ -72,7 +74,7 @@ impl Provider for NotionConnectionProvider {
if !res.status().is_success() {
Err(anyhow!(
"Error generating access token with Notion: status={}",
res.status().as_u16()
res.status().as_u16(),
))?;
}

Expand All @@ -99,6 +101,7 @@ impl Provider for NotionConnectionProvider {
};

Ok(FinalizeResult {
redirect_uri: redirect_uri.to_string(),
code: code.to_string(),
access_token: access_token.to_string(),
access_token_expiry: None,
Expand Down
2 changes: 1 addition & 1 deletion types/src/core/oauth_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class OAuthAPI {
body: JSON.stringify({
provider,
code,
redirectUri,
redirect_uri: redirectUri,
}),
}
);
Expand Down

0 comments on commit 54f30cc

Please sign in to comment.