66
77use std:: string:: FromUtf8Error ;
88
9- use camino:: Utf8PathBuf ;
109use mas_data_model:: { UpstreamOAuthProvider , UpstreamOAuthProviderTokenAuthMethod } ;
1110use mas_iana:: jose:: JsonWebSignatureAlg ;
1211use mas_keystore:: { DecryptError , Encrypter , Keystore } ;
1312use mas_oidc_client:: types:: client_credentials:: ClientCredentials ;
1413use pkcs8:: DecodePrivateKey ;
15- use schemars:: JsonSchema ;
1614use serde:: Deserialize ;
1715use thiserror:: Error ;
1816use 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 ) ]
6759pub 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