@@ -122,7 +122,7 @@ pub struct CredentialProfile {
122122}
123123
124124/// Origin-aware protobuf duration presence retained across profile conversion.
125- #[ derive( Debug , Clone , Copy , Default ) ]
125+ #[ derive( Debug , Clone , Copy , Default , PartialEq , Eq ) ]
126126#[ doc( hidden) ]
127127pub enum ProfileDurationWkt {
128128 /// The profile came from YAML and uses the compatibility seconds field.
@@ -138,6 +138,46 @@ impl ProfileDurationWkt {
138138 fn from_proto ( value : Option < prost_types:: Duration > ) -> Self {
139139 value. map_or ( Self :: Absent , Self :: Present )
140140 }
141+
142+ pub fn to_proto ( self , legacy_seconds : i64 ) -> Option < prost_types:: Duration > {
143+ match self {
144+ Self :: FromYaml => ( legacy_seconds != 0 ) . then_some ( prost_types:: Duration {
145+ seconds : legacy_seconds,
146+ nanos : 0 ,
147+ } ) ,
148+ Self :: Absent => None ,
149+ Self :: Present ( value) => Some ( value) ,
150+ }
151+ }
152+ }
153+
154+ impl Serialize for ProfileDurationWkt {
155+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
156+ where
157+ S : Serializer ,
158+ {
159+ match self {
160+ Self :: Present ( value) => serializer. serialize_str ( & value. to_string ( ) ) ,
161+ Self :: FromYaml | Self :: Absent => serializer. serialize_none ( ) ,
162+ }
163+ }
164+ }
165+
166+ impl < ' de > Deserialize < ' de > for ProfileDurationWkt {
167+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
168+ where
169+ D : Deserializer < ' de > ,
170+ {
171+ let value = String :: deserialize ( deserializer) ?;
172+ value
173+ . parse :: < prost_types:: Duration > ( )
174+ . map ( Self :: Present )
175+ . map_err ( de:: Error :: custom)
176+ }
177+ }
178+
179+ fn profile_duration_is_legacy_or_absent ( value : & ProfileDurationWkt ) -> bool {
180+ !matches ! ( value, ProfileDurationWkt :: Present ( _) )
141181}
142182
143183#[ derive( Debug , Clone , Deserialize , Serialize ) ]
@@ -161,7 +201,11 @@ pub struct TokenGrantProfile {
161201 #[ serde( default , skip_serializing_if = "is_zero_i64" ) ]
162202 pub cache_ttl_seconds : i64 ,
163203 /// Exact protobuf value retained for lossless gRPC import/export.
164- #[ serde( skip) ]
204+ #[ serde(
205+ default ,
206+ rename = "cache_ttl" ,
207+ skip_serializing_if = "profile_duration_is_legacy_or_absent"
208+ ) ]
165209 #[ doc( hidden) ]
166210 pub cache_ttl_wkt : ProfileDurationWkt ,
167211 #[ serde( default , skip_serializing_if = "Vec::is_empty" ) ]
@@ -225,13 +269,21 @@ pub struct CredentialRefreshProfile {
225269 #[ serde( default , skip_serializing_if = "is_zero_i64" ) ]
226270 pub refresh_before_seconds : i64 ,
227271 /// Exact protobuf value retained for lossless gRPC import/export.
228- #[ serde( skip) ]
272+ #[ serde(
273+ default ,
274+ rename = "refresh_before" ,
275+ skip_serializing_if = "profile_duration_is_legacy_or_absent"
276+ ) ]
229277 #[ doc( hidden) ]
230278 pub refresh_before_wkt : ProfileDurationWkt ,
231279 #[ serde( default , skip_serializing_if = "is_zero_i64" ) ]
232280 pub max_lifetime_seconds : i64 ,
233281 /// Exact protobuf value retained for lossless gRPC import/export.
234- #[ serde( skip) ]
282+ #[ serde(
283+ default ,
284+ rename = "max_lifetime" ,
285+ skip_serializing_if = "profile_duration_is_legacy_or_absent"
286+ ) ]
235287 #[ doc( hidden) ]
236288 pub max_lifetime_wkt : ProfileDurationWkt ,
237289 #[ serde( default , skip_serializing_if = "Vec::is_empty" ) ]
@@ -1323,9 +1375,9 @@ fn credential_refresh_from_proto(refresh: &ProviderCredentialRefresh) -> Credent
13231375 . unwrap_or ( ProviderCredentialRefreshStrategy :: Unspecified ) ,
13241376 token_url : refresh. token_url . clone ( ) ,
13251377 scopes : refresh. scopes . clone ( ) ,
1326- refresh_before_seconds : duration_seconds_for_runtime ( refresh . refresh_before . as_ref ( ) ) ,
1378+ refresh_before_seconds : 0 ,
13271379 refresh_before_wkt : ProfileDurationWkt :: from_proto ( refresh. refresh_before ) ,
1328- max_lifetime_seconds : duration_seconds_for_runtime ( refresh . max_lifetime . as_ref ( ) ) ,
1380+ max_lifetime_seconds : 0 ,
13291381 max_lifetime_wkt : ProfileDurationWkt :: from_proto ( refresh. max_lifetime ) ,
13301382 material : refresh
13311383 . material
@@ -1348,26 +1400,11 @@ fn credential_refresh_from_proto(refresh: &ProviderCredentialRefresh) -> Credent
13481400 }
13491401}
13501402
1351- fn duration_seconds_for_runtime ( value : Option < & prost_types:: Duration > ) -> i64 {
1352- let Some ( value) = value else {
1353- return 0 ;
1354- } ;
1355- openshell_core:: time:: duration_to_std ( value) . map_or ( 0 , |duration| {
1356- i64:: try_from ( duration. as_secs ( ) ) . unwrap_or_default ( )
1357- } )
1358- }
1359-
13601403fn profile_duration_to_proto (
13611404 exact : ProfileDurationWkt ,
13621405 seconds : i64 ,
13631406) -> Option < prost_types:: Duration > {
1364- match exact {
1365- ProfileDurationWkt :: FromYaml => {
1366- ( seconds != 0 ) . then_some ( prost_types:: Duration { seconds, nanos : 0 } )
1367- }
1368- ProfileDurationWkt :: Absent => None ,
1369- ProfileDurationWkt :: Present ( value) => Some ( value) ,
1370- }
1407+ exact. to_proto ( seconds)
13711408}
13721409
13731410fn validate_profile_duration ( value : & prost_types:: Duration ) -> Result < ( ) , String > {
@@ -1423,7 +1460,7 @@ fn token_grant_from_proto(
14231460 jwt_svid_audience : token_grant. jwt_svid_audience . clone ( ) ,
14241461 client_assertion_type : token_grant. client_assertion_type . clone ( ) ,
14251462 scopes : token_grant. scopes . clone ( ) ,
1426- cache_ttl_seconds : duration_seconds_for_runtime ( token_grant . cache_ttl . as_ref ( ) ) ,
1463+ cache_ttl_seconds : 0 ,
14271464 cache_ttl_wkt : ProfileDurationWkt :: from_proto ( token_grant. cache_ttl ) ,
14281465 audience_overrides : token_grant
14291466 . audience_overrides
@@ -2170,6 +2207,16 @@ pub fn validate_profile_set(
21702207 "refresh strategy is required" ,
21712208 ) ) ;
21722209 }
2210+ if matches ! ( refresh. refresh_before_wkt, ProfileDurationWkt :: Present ( _) )
2211+ && refresh. refresh_before_seconds != 0
2212+ {
2213+ diagnostics. push ( ProfileValidationDiagnostic :: error (
2214+ source,
2215+ profile_id,
2216+ "credentials.refresh.refresh_before" ,
2217+ "refresh_before and refresh_before_seconds cannot both be set" ,
2218+ ) ) ;
2219+ }
21732220 if let ProfileDurationWkt :: Present ( value) = refresh. refresh_before_wkt
21742221 && let Err ( error) = validate_profile_duration ( & value)
21752222 {
@@ -2208,6 +2255,30 @@ pub fn validate_profile_set(
22082255 "max_lifetime_seconds must be greater than or equal to 0" ,
22092256 ) ) ;
22102257 }
2258+ if matches ! ( refresh. max_lifetime_wkt, ProfileDurationWkt :: Present ( _) )
2259+ && refresh. max_lifetime_seconds != 0
2260+ {
2261+ diagnostics. push ( ProfileValidationDiagnostic :: error (
2262+ source,
2263+ profile_id,
2264+ "credentials.refresh.max_lifetime" ,
2265+ "max_lifetime and max_lifetime_seconds cannot both be set" ,
2266+ ) ) ;
2267+ }
2268+ if matches ! (
2269+ refresh. max_lifetime_wkt,
2270+ ProfileDurationWkt :: Present ( prost_types:: Duration {
2271+ seconds: 0 ,
2272+ nanos: 0
2273+ } )
2274+ ) {
2275+ diagnostics. push ( ProfileValidationDiagnostic :: error (
2276+ source,
2277+ profile_id,
2278+ "credentials.refresh.max_lifetime" ,
2279+ "max_lifetime must be greater than zero when present" ,
2280+ ) ) ;
2281+ }
22112282 let mut material_names = HashSet :: new ( ) ;
22122283 for material in & refresh. material {
22132284 let name = material. name . trim ( ) ;
@@ -2379,6 +2450,16 @@ pub fn validate_profile_set(
23792450 "cache_ttl_seconds must be greater than or equal to 0" ,
23802451 ) ) ;
23812452 }
2453+ if matches ! ( token_grant. cache_ttl_wkt, ProfileDurationWkt :: Present ( _) )
2454+ && token_grant. cache_ttl_seconds != 0
2455+ {
2456+ diagnostics. push ( ProfileValidationDiagnostic :: error (
2457+ source,
2458+ profile_id,
2459+ "credentials.token_grant.cache_ttl" ,
2460+ "cache_ttl and cache_ttl_seconds cannot both be set" ,
2461+ ) ) ;
2462+ }
23822463 if let Err ( message) = validate_token_grant_endpoint ( & token_grant. token_endpoint ) {
23832464 diagnostics. push ( ProfileValidationDiagnostic :: error (
23842465 source,
@@ -3470,6 +3551,80 @@ mod tests {
34703551 ) ;
34713552 }
34723553
3554+ #[ test]
3555+ fn protobuf_profile_durations_round_trip_through_yaml_exactly ( ) {
3556+ let profile = parse_profile_yaml (
3557+ r"
3558+ id: exact-durations
3559+ display_name: Exact Durations
3560+ credentials:
3561+ - name: access_token
3562+ refresh:
3563+ strategy: static
3564+ token_grant:
3565+ token_endpoint: https://auth.example.com/token
3566+ " ,
3567+ )
3568+ . unwrap ( ) ;
3569+ let mut proto = profile. to_proto ( ) ;
3570+ let credential = & mut proto. credentials [ 0 ] ;
3571+ let refresh = credential. refresh . as_mut ( ) . unwrap ( ) ;
3572+ refresh. refresh_before = Some ( prost_types:: Duration {
3573+ seconds : 0 ,
3574+ nanos : 0 ,
3575+ } ) ;
3576+ refresh. max_lifetime = Some ( prost_types:: Duration {
3577+ seconds : 1 ,
3578+ nanos : 500_000_000 ,
3579+ } ) ;
3580+ credential. token_grant . as_mut ( ) . unwrap ( ) . cache_ttl = Some ( prost_types:: Duration {
3581+ seconds : 0 ,
3582+ nanos : 500_000_000 ,
3583+ } ) ;
3584+
3585+ let exact = ProviderTypeProfile :: from_proto ( & proto) ;
3586+ let yaml = profile_to_yaml ( & exact) . unwrap ( ) ;
3587+ assert ! ( yaml. contains( "refresh_before: \" 0s\" " ) , "{yaml}" ) ;
3588+ assert ! ( yaml. contains( "max_lifetime: \" 1.500s\" " ) , "{yaml}" ) ;
3589+ assert ! ( yaml. contains( "cache_ttl: \" 0.500s\" " ) , "{yaml}" ) ;
3590+ assert ! ( !yaml. contains( "refresh_before_seconds" ) ) ;
3591+ assert ! ( !yaml. contains( "max_lifetime_seconds" ) ) ;
3592+ assert ! ( !yaml. contains( "cache_ttl_seconds" ) ) ;
3593+
3594+ let reparsed = parse_profile_yaml ( & yaml) . unwrap ( ) . to_proto ( ) ;
3595+ assert_eq ! (
3596+ reparsed. credentials[ 0 ] . refresh,
3597+ proto. credentials[ 0 ] . refresh
3598+ ) ;
3599+ assert_eq ! (
3600+ reparsed. credentials[ 0 ] . token_grant,
3601+ proto. credentials[ 0 ] . token_grant
3602+ ) ;
3603+ }
3604+
3605+ #[ test]
3606+ fn canonical_zero_max_lifetime_is_rejected ( ) {
3607+ let profile = parse_profile_yaml (
3608+ r#"
3609+ id: zero-max-lifetime
3610+ display_name: Zero Max Lifetime
3611+ credentials:
3612+ - name: access_token
3613+ refresh:
3614+ strategy: oauth2_client_credentials
3615+ token_url: https://auth.example.com/token
3616+ max_lifetime: "0s"
3617+ "# ,
3618+ )
3619+ . expect ( "profile should parse before semantic validation" ) ;
3620+
3621+ let diagnostics = validate_profile_set ( & [ ( "zero.yaml" . to_string ( ) , profile) ] ) ;
3622+ assert ! ( diagnostics. iter( ) . any( |diagnostic| {
3623+ diagnostic. field == "credentials.refresh.max_lifetime"
3624+ && diagnostic. message. contains( "greater than zero" )
3625+ } ) ) ;
3626+ }
3627+
34733628 #[ test]
34743629 fn builtin_agent_conversation_defaults_preserve_own_and_foreign_body_text ( ) {
34753630 use openshell_core:: proto:: { StaticCredentialBinding , StaticCredentialEndpointBinding } ;
@@ -4679,15 +4834,15 @@ credentials:
46794834 ) ;
46804835 assert_eq ! ( refresh. material. len( ) , 2 ) ;
46814836
4682- let from_proto = ProviderTypeProfile :: from_proto ( & profile. to_proto ( ) ) ;
4683- assert_eq ! (
4684- from_proto. credentials[ 0 ] . refresh,
4685- profile. credentials[ 0 ] . refresh
4686- ) ;
4837+ let proto = profile. to_proto ( ) ;
4838+ let from_proto = ProviderTypeProfile :: from_proto ( & proto) ;
4839+ assert_eq ! ( from_proto. to_proto( ) , proto) ;
46874840
46884841 let exported = profile_to_yaml ( & from_proto) . expect ( "yaml" ) ;
46894842 assert ! ( exported. contains( "oauth2_client_credentials" ) ) ;
46904843 assert ! ( exported. contains( "client_secret" ) ) ;
4844+ let reparsed = parse_profile_yaml ( & exported) . expect ( "exported profile should parse" ) ;
4845+ assert_eq ! ( reparsed. to_proto( ) , proto) ;
46914846 }
46924847
46934848 #[ test]
0 commit comments