99
1010use crate :: paths:: gateways_dir;
1111use miette:: { IntoDiagnostic , Result , WrapErr } ;
12+ use openshell_core:: paths:: { ensure_parent_dir_restricted, set_file_owner_only} ;
1213use std:: path:: PathBuf ;
1314
1415/// Path to the stored edge auth token for a gateway.
@@ -24,38 +25,47 @@ fn legacy_token_path(gateway_name: &str) -> Result<PathBuf> {
2425/// Store an edge authentication token for a gateway.
2526pub fn store_edge_token ( gateway_name : & str , token : & str ) -> Result < ( ) > {
2627 let path = edge_token_path ( gateway_name) ?;
27- if let Some ( parent) = path. parent ( ) {
28- std:: fs:: create_dir_all ( parent)
29- . into_diagnostic ( )
30- . wrap_err_with ( || format ! ( "failed to create {}" , parent. display( ) ) ) ?;
31- }
28+ ensure_parent_dir_restricted ( & path) ?;
3229 std:: fs:: write ( & path, token)
3330 . into_diagnostic ( )
3431 . wrap_err_with ( || format ! ( "failed to write edge token to {}" , path. display( ) ) ) ?;
3532 // Restrict permissions to owner-only (0600).
36- #[ cfg( unix) ]
37- {
38- use std:: os:: unix:: fs:: PermissionsExt ;
39- std:: fs:: set_permissions ( & path, std:: fs:: Permissions :: from_mode ( 0o600 ) )
40- . into_diagnostic ( )
41- . wrap_err ( "failed to set token file permissions" ) ?;
42- }
33+ set_file_owner_only ( & path) ?;
4334 Ok ( ( ) )
4435}
4536
4637/// Load a stored edge authentication token for a gateway.
4738///
4839/// Returns `None` if no token file exists or the file is empty.
4940/// Falls back to the legacy `cf_token` path for backwards compatibility.
41+ /// When loading from the legacy path, migrates the token to the new path
42+ /// with proper permissions.
5043pub fn load_edge_token ( gateway_name : & str ) -> Option < String > {
51- // Try the new path first, then fall back to legacy.
52- let path = edge_token_path ( gateway_name)
44+ // Try the new path first.
45+ if let Some ( path) = edge_token_path ( gateway_name) . ok ( ) . filter ( |p| p. exists ( ) ) {
46+ let contents = std:: fs:: read_to_string ( & path) . ok ( ) ?;
47+ let token = contents. trim ( ) . to_string ( ) ;
48+ if !token. is_empty ( ) {
49+ return Some ( token) ;
50+ }
51+ }
52+
53+ // Fall back to the legacy cf_token path.
54+ let legacy_path = legacy_token_path ( gateway_name)
5355 . ok ( )
54- . filter ( |p| p. exists ( ) )
55- . or_else ( || legacy_token_path ( gateway_name) . ok ( ) . filter ( |p| p. exists ( ) ) ) ?;
56- let contents = std:: fs:: read_to_string ( & path) . ok ( ) ?;
56+ . filter ( |p| p. exists ( ) ) ?;
57+ let contents = std:: fs:: read_to_string ( & legacy_path) . ok ( ) ?;
5758 let token = contents. trim ( ) . to_string ( ) ;
58- if token. is_empty ( ) { None } else { Some ( token) }
59+ if token. is_empty ( ) {
60+ return None ;
61+ }
62+
63+ // Migrate: write to new path with proper permissions, then remove legacy.
64+ if store_edge_token ( gateway_name, & token) . is_ok ( ) {
65+ let _ = std:: fs:: remove_file ( & legacy_path) ;
66+ }
67+
68+ Some ( token)
5969}
6070
6171/// Remove a stored edge authentication token.
0 commit comments