Skip to content

Commit dade499

Browse files
authored
Merge pull request #3 from adisve/feat/populate-priv-key-sync
Feat/populate priv key sync
2 parents f17f2a4 + 512208f commit dade499

File tree

3 files changed

+19
-53
lines changed

3 files changed

+19
-53
lines changed

crates/cli/src/sync.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,17 @@ pub async fn config_sync(
189189
let encrypted_client_secret =
190190
if let Some(client_secret) = provider.client_secret.as_deref() {
191191
Some(encrypter.encrypt_to_string(client_secret.as_bytes())?)
192-
} else if let Some(siwa) = provider.sign_in_with_apple.as_ref() {
193-
// For SIWA, we JSON-encode the config and encrypt it, reusing the client_secret
194-
// field in the database
195-
let encoded = serde_json::to_vec(siwa)?;
192+
} else if let Some(mut siwa) = provider.sign_in_with_apple.clone() {
193+
// if private key file is defined and not private key (raw), we populate the private key
194+
// to hold the content of the private key file. private key (raw) takes precedence so
195+
// both can be defined without issues
196+
if siwa.private_key.is_none() {
197+
if let Some(private_key_file) = siwa.private_key_file.take() {
198+
let key = tokio::fs::read_to_string(private_key_file).await?;
199+
siwa.private_key = Some(key);
200+
}
201+
}
202+
let encoded = serde_json::to_vec(&siwa)?;
196203
Some(encrypter.encrypt_to_string(&encoded)?)
197204
} else {
198205
None

crates/handlers/src/upstream_oauth2/callback.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ pub(crate) async fn handler(
294294
lazy_metadata.token_endpoint().await?,
295295
&keystore,
296296
&encrypter,
297-
)
298-
.await?;
297+
)?;
299298

300299
let redirect_uri = url_builder.upstream_oauth_callback(provider.id);
301300

crates/handlers/src/upstream_oauth2/mod.rs

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
use std::string::FromUtf8Error;
88

9-
use camino::Utf8PathBuf;
109
use mas_data_model::{UpstreamOAuthProvider, UpstreamOAuthProviderTokenAuthMethod};
1110
use mas_iana::jose::JsonWebSignatureAlg;
1211
use mas_keystore::{DecryptError, Encrypter, Keystore};
1312
use mas_oidc_client::types::client_credentials::ClientCredentials;
1413
use pkcs8::DecodePrivateKey;
15-
use schemars::JsonSchema;
1614
use serde::Deserialize;
1715
use thiserror::Error;
1816
use url::Url;
@@ -32,12 +30,6 @@ enum ProviderCredentialsError {
3230
#[error("Provider doesn't have a client secret")]
3331
MissingClientSecret,
3432

35-
#[error("Duplicate private key and private key file for Sign in with Apple")]
36-
DuplicatePrivateKey,
37-
38-
#[error("Missing private key for signing the id_token")]
39-
MissingPrivateKey,
40-
4133
#[error("Could not decrypt client secret")]
4234
DecryptClientSecret {
4335
#[from]
@@ -63,32 +55,22 @@ enum ProviderCredentialsError {
6355
},
6456
}
6557

66-
#[derive(Debug, Deserialize, JsonSchema)]
58+
#[derive(Debug, Deserialize)]
6759
pub struct SignInWithApple {
68-
/// The private key file used to sign the `id_token`
69-
#[serde(skip_serializing_if = "Option::is_none")]
70-
#[schemars(with = "Option<String>")]
71-
pub private_key_file: Option<Utf8PathBuf>,
72-
73-
/// The private key used to sign the `id_token`
74-
#[serde(skip_serializing_if = "Option::is_none")]
75-
pub private_key: Option<String>,
76-
77-
/// The Team ID of the Apple Developer Portal
60+
pub private_key: String,
7861
pub team_id: String,
79-
80-
/// The key ID of the Apple Developer Portal
8162
pub key_id: String,
8263
}
8364

84-
async fn client_credentials_for_provider(
65+
fn client_credentials_for_provider(
8566
provider: &UpstreamOAuthProvider,
8667
token_endpoint: &Url,
8768
keystore: &Keystore,
8869
encrypter: &Encrypter,
8970
) -> Result<ClientCredentials, ProviderCredentialsError> {
9071
let client_id = provider.client_id.clone();
9172

73+
// Decrypt the client secret
9274
let client_secret = provider
9375
.encrypted_client_secret
9476
.as_deref()
@@ -142,32 +124,10 @@ async fn client_credentials_for_provider(
142124
},
143125

144126
UpstreamOAuthProviderTokenAuthMethod::SignInWithApple => {
145-
let client_secret =
146-
client_secret.ok_or(ProviderCredentialsError::MissingClientSecret)?;
147-
148-
let params: SignInWithApple = serde_json::from_str(&client_secret)
149-
.map_err(|inner| ProviderCredentialsError::InvalidClientSecretJson { inner })?;
150-
151-
if params.private_key.is_none() && params.private_key_file.is_none() {
152-
return Err(ProviderCredentialsError::MissingPrivateKey);
153-
}
154-
155-
if params.private_key.is_some() && params.private_key_file.is_some() {
156-
return Err(ProviderCredentialsError::DuplicatePrivateKey);
157-
}
127+
let params = client_secret.ok_or(ProviderCredentialsError::MissingClientSecret)?;
128+
let params: SignInWithApple = serde_json::from_str(&params)?;
158129

159-
let private_key_pem = if let Some(private_key) = params.private_key {
160-
private_key
161-
} else if let Some(private_key_file) = params.private_key_file {
162-
tokio::fs::read_to_string(private_key_file)
163-
.await
164-
.map_err(|_| ProviderCredentialsError::MissingPrivateKey)?
165-
} else {
166-
unreachable!("already validated above")
167-
};
168-
169-
let key = elliptic_curve::SecretKey::from_pkcs8_pem(&private_key_pem)
170-
.map_err(|inner| ProviderCredentialsError::InvalidPrivateKey { inner })?;
130+
let key = elliptic_curve::SecretKey::from_pkcs8_pem(&params.private_key)?;
171131

172132
ClientCredentials::SignInWithApple {
173133
client_id,

0 commit comments

Comments
 (0)