1- use crate :: authentication_manager:: ServiceAccount ;
2- use crate :: error:: Error :: AplicationProfileMissing ;
3- use crate :: prelude:: * ;
1+ use std:: collections:: HashMap ;
2+ use std:: path:: Path ;
43use std:: sync:: RwLock ;
4+
5+ use async_trait:: async_trait;
6+ use serde:: { Deserialize , Serialize } ;
57use tokio:: fs;
68
9+ use crate :: authentication_manager:: ServiceAccount ;
10+ use crate :: error:: Error ;
11+ use crate :: types:: { HyperClient , Token } ;
12+ use crate :: util:: HyperExt ;
13+
714#[ derive( Debug ) ]
8- pub struct CustomServiceAccount {
15+ pub ( crate ) struct CustomServiceAccount {
916 tokens : RwLock < HashMap < Vec < String > , Token > > ,
1017 credentials : ApplicationCredentials ,
1118}
1219
1320impl CustomServiceAccount {
14- pub async fn from_file ( path : & str ) -> Result < Self , Error > {
15- let credentials = ApplicationCredentials :: from_file ( path) . await ?;
21+ pub ( crate ) async fn from_file ( path : & Path ) -> Result < Self , Error > {
1622 Ok ( Self {
17- credentials,
23+ credentials : ApplicationCredentials :: from_file ( path ) . await ? ,
1824 tokens : RwLock :: new ( HashMap :: new ( ) ) ,
1925 } )
2026 }
21-
22- pub async fn from_env ( ) -> Result < Self , Error > {
23- const GOOGLE_APPLICATION_CREDENTIALS : & str = "GOOGLE_APPLICATION_CREDENTIALS" ;
24- let path =
25- std:: env:: var ( GOOGLE_APPLICATION_CREDENTIALS ) . map_err ( |_| AplicationProfileMissing ) ?;
26- CustomServiceAccount :: from_file ( & path) . await
27- }
2827}
2928
3029#[ async_trait]
@@ -43,12 +42,12 @@ impl ServiceAccount for CustomServiceAccount {
4342
4443 async fn refresh_token ( & self , client : & HyperClient , scopes : & [ & str ] ) -> Result < Token , Error > {
4544 use crate :: jwt:: Claims ;
46- use crate :: jwt:: JWTSigner ;
45+ use crate :: jwt:: JwtSigner ;
4746 use crate :: jwt:: GRANT_TYPE ;
4847 use hyper:: header;
4948 use url:: form_urlencoded;
5049
51- let signer = JWTSigner :: new ( & self . credentials . private_key ) ?;
50+ let signer = JwtSigner :: new ( & self . credentials . private_key ) ?;
5251
5352 let claims = Claims :: new ( & self . credentials , scopes, None ) ;
5453 let signed = signer. sign_claims ( & claims) . map_err ( Error :: TLSError ) ?;
@@ -73,33 +72,33 @@ impl ServiceAccount for CustomServiceAccount {
7372}
7473
7574#[ derive( Serialize , Deserialize , Debug , Clone ) ]
76- pub struct ApplicationCredentials {
77- pub r#type : Option < String > ,
75+ pub ( crate ) struct ApplicationCredentials {
76+ pub ( crate ) r#type : Option < String > ,
7877 /// project_id
79- pub project_id : Option < String > ,
78+ pub ( crate ) project_id : Option < String > ,
8079 /// private_key_id
81- pub private_key_id : Option < String > ,
80+ pub ( crate ) private_key_id : Option < String > ,
8281 /// private_key
83- pub private_key : String ,
82+ pub ( crate ) private_key : String ,
8483 /// client_email
85- pub client_email : String ,
84+ pub ( crate ) client_email : String ,
8685 /// client_id
87- pub client_id : Option < String > ,
86+ pub ( crate ) client_id : Option < String > ,
8887 /// auth_uri
89- pub auth_uri : Option < String > ,
88+ pub ( crate ) auth_uri : Option < String > ,
9089 /// token_uri
91- pub token_uri : String ,
90+ pub ( crate ) token_uri : String ,
9291 /// auth_provider_x509_cert_url
93- pub auth_provider_x509_cert_url : Option < String > ,
92+ pub ( crate ) auth_provider_x509_cert_url : Option < String > ,
9493 /// client_x509_cert_url
95- pub client_x509_cert_url : Option < String > ,
94+ pub ( crate ) client_x509_cert_url : Option < String > ,
9695}
9796
9897impl ApplicationCredentials {
9998 async fn from_file < T : AsRef < Path > > ( path : T ) -> Result < ApplicationCredentials , Error > {
10099 let content = fs:: read_to_string ( path)
101100 . await
102- . map_err ( Error :: AplicationProfilePath ) ?;
103- Ok ( serde_json:: from_str ( & content) . map_err ( Error :: AplicationProfileFormat ) ?)
101+ . map_err ( Error :: ApplicationProfilePath ) ?;
102+ Ok ( serde_json:: from_str ( & content) . map_err ( Error :: ApplicationProfileFormat ) ?)
104103 }
105104}
0 commit comments