Skip to content

oauth flow /authorize and /token endpoints#115

Open
Sanket-Singhvi wants to merge 4 commits intopatr-cloud:developfrom
Sanket-Singhvi:oauth-routes
Open

oauth flow /authorize and /token endpoints#115
Sanket-Singhvi wants to merge 4 commits intopatr-cloud:developfrom
Sanket-Singhvi:oauth-routes

Conversation

@Sanket-Singhvi
Copy link
Collaborator

No description provided.

ts-rs = { default-features = false, version = "11" }
typed-builder = { default-features = false, version = "0.23" }
url = { default-features = false, version = "2" }
urlencoding = { default-features = false, version = "2" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serde_qs is already there. Can't we use that?

Comment on lines +204 to +212
Self::InvalidResponseType => {
"The response type provided in the OAuth2.1 authorize request is not supported"
}
Self::InvalidGrantType => {
"The grant type provided in the OAuth2.1 token request is not supported"
}
Self::InvalidAuthorizationCode => {
"The authorization code provided in the OAuth2.1 token request is invalid"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • OAuthInvalidResponseType
  • OAuthInvalidGrantType
  • OAuthInvalidAuthorizationCode

Comment on lines +130 to +148
@@ -137,6 +144,8 @@ impl ErrorType {
Self::RoleInUse => StatusCode::CONFLICT,
Self::RunnerAlreadyConnected => StatusCode::CONFLICT,
Self::InvalidRunnerMode => StatusCode::FORBIDDEN,
Self::InvalidGrantType => StatusCode::BAD_REQUEST,
Self::InvalidAuthorizationCode => StatusCode::BAD_REQUEST,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these validated with the OAuth spec?

}
);

macros::declare_api_endpoint!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate file

.into_result()
}

pub async fn authorize_post(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate file

Comment on lines +129 to +146
let success = argon2::Argon2::new_with_secret(
config.password_pepper.as_ref(),
Algorithm::Argon2id,
Version::V0x13,
constants::HASHING_PARAMS,
)
.inspect_err(|err| {
error!("Error creating Argon2: `{}`", err);
})
.map_err(ErrorType::server_error)?
.verify_password(
password.as_bytes(),
&PasswordHash::new(&user_data.password).map_err(ErrorType::server_error)?,
)
.inspect_err(|err| {
info!("Error verifying password: `{}`", err);
})
.is_ok();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this? The whole reason of OAuth is to not give the password to the client


// Store the authorization code and its metadata in the redis with an expiration
// time.
let key = format!("auth_code:{}", authorization_code);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the redis::keys module

})
.map_err(ErrorType::server_error)?;

let redirect_uri_str = redirect_uri.as_deref().unwrap_or("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this should come from the DB ideally

}
} else {
if code_verifier != auth_code_data.code_challenge {
return Err(ErrorType::InvalidGrantType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk if this is the right error here

}
} else if grant_type == OAuthTokenGrantType::RefreshToken {
// Handle refresh token grant type
let key = format!("refresh_token:{}", refresh_token.unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keys in a separate module

"Generated authorization code `{}` for client_id `{}`",
authorization_code, client_id
);
let metadata = json!({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a struct out of this

use crate::prelude::*;

#[derive(Serialize)]
struct RedirectQueryParams {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document what this is

ErrorType::server_error(e)
})?;

let redirect_url = if let Some(uri) = redirect_uri {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be validated with the DB

use crate::prelude::*;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct AuthCodeData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have this na?

api/Cargo.toml Outdated
tracing-opentelemetry = { workspace = true, features = ["default"] }
tracing-subscriber = { workspace = true, features = ["default"] }
typed-builder = { workspace = true, features = [] }
urlencoding = { workspace = true, features = [] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

/// - PKCE challenge (if any)
///
/// Then backend generates an authorization code and redirects.
OAuthAuthorizePost,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better name

POST "/auth/oauth/authorize",
request = {
/// The client ID of the OAuth client
pub client_id: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client secret needs to be there

format!("registryBlobUploadPart:{}", session_id)
}
/// The prefix used for authorization_code for oauth
pub fn oauth_authorization_code_prefix(authorization_code: &str) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strong types internally

…e database

- Update `authorize` endpoint to validate client secret and redirect URI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants