@@ -14,6 +14,7 @@ use tauri_plugin_store::StoreExt;
1414const PUBLIC_KEY_HEX : & str = "c3b18e765fc5c74f9fb7f3a9869d14c6bdeda1f28ec85aa6182de78113930d26" ;
1515
1616const STORE_KEY_LICENSE : & str = "license_key" ;
17+ const STORE_KEY_SHORT_CODE : & str = "license_short_code" ;
1718
1819/// Information about the current license.
1920#[ derive( Debug , Clone , Serialize , Deserialize ) ]
@@ -23,53 +24,74 @@ pub struct LicenseInfo {
2324 pub transaction_id : String ,
2425 pub issued_at : String ,
2526 pub organization_name : Option < String > ,
27+ /// The short code used to activate (if available)
28+ pub short_code : Option < String > ,
2629}
2730
2831/// Activate a license key. Returns the license info if valid.
29- pub fn activate_license ( app : & tauri:: AppHandle , license_key : & str ) -> Result < LicenseInfo , String > {
32+ /// The `short_code` parameter is the original short code if this was activated via short code exchange.
33+ fn activate_license_internal (
34+ app : & tauri:: AppHandle ,
35+ license_key : & str ,
36+ short_code : Option < & str > ,
37+ ) -> Result < LicenseInfo , String > {
3038 // Validate the license key
3139 let data = validate_license_key ( license_key) ?;
3240
33- // Store the license key
41+ // Store the license key and optionally the short code
3442 let store = app
3543 . store ( "license.json" )
3644 . map_err ( |e| format ! ( "Failed to open store: {}" , e) ) ?;
3745
3846 store. set ( STORE_KEY_LICENSE , serde_json:: json!( license_key) ) ;
47+ if let Some ( code) = short_code {
48+ store. set ( STORE_KEY_SHORT_CODE , serde_json:: json!( code) ) ;
49+ }
3950
4051 Ok ( LicenseInfo {
4152 email : data. email ,
4253 transaction_id : data. transaction_id ,
4354 issued_at : data. issued_at ,
4455 organization_name : data. organization_name ,
56+ short_code : short_code. map ( |s| s. to_string ( ) ) ,
4557 } )
4658}
4759
60+ /// Activate a license key (full key, not short code). Returns the license info if valid.
61+ pub fn activate_license ( app : & tauri:: AppHandle , license_key : & str ) -> Result < LicenseInfo , String > {
62+ activate_license_internal ( app, license_key, None )
63+ }
64+
4865/// Activate a license key or short code (async version).
4966/// If the input is a short code (CMDR-XXXX-XXXX-XXXX), it first exchanges it for the full key.
5067pub async fn activate_license_async ( app : & tauri:: AppHandle , input : & str ) -> Result < LicenseInfo , String > {
51- let full_key = if is_short_code ( input) {
68+ let ( full_key, short_code ) = if is_short_code ( input) {
5269 // Exchange short code for full key via server
53- activate_short_code ( input) . await ?
70+ let key = activate_short_code ( input) . await ?;
71+ ( key, Some ( input) )
5472 } else {
5573 // Already a full key
56- input. to_string ( )
74+ ( input. to_string ( ) , None )
5775 } ;
5876
59- // Now activate with the full key
60- activate_license ( app, & full_key)
77+ // Now activate with the full key (and store the short code if we have one)
78+ activate_license_internal ( app, & full_key, short_code )
6179}
6280
6381/// Get stored license info, if any.
6482pub fn get_license_info ( app : & tauri:: AppHandle ) -> Option < LicenseInfo > {
6583 let store = app. store ( "license.json" ) . ok ( ) ?;
6684 let license_key = store. get ( STORE_KEY_LICENSE ) ?. as_str ( ) ?. to_string ( ) ;
85+ let short_code = store
86+ . get ( STORE_KEY_SHORT_CODE )
87+ . and_then ( |v| v. as_str ( ) . map ( |s| s. to_string ( ) ) ) ;
6788
6889 validate_license_key ( & license_key) . ok ( ) . map ( |data| LicenseInfo {
6990 email : data. email ,
7091 transaction_id : data. transaction_id ,
7192 issued_at : data. issued_at ,
7293 organization_name : data. organization_name ,
94+ short_code,
7395 } )
7496}
7597
@@ -114,8 +136,16 @@ fn validate_license_key_with_public_key(license_key: &str, public_key_hex: &str)
114136 . map_err ( |_| "Invalid license key: signature verification failed" ) ?;
115137
116138 // Parse payload
117- let data: LicenseData =
118- serde_json:: from_slice ( & payload_bytes) . map_err ( |_| "Invalid license key: bad payload data" ) ?;
139+ let data: LicenseData = serde_json:: from_slice ( & payload_bytes) . map_err ( |e| {
140+ log:: info!(
141+ "License payload parse error: {}. Raw payload: {}" ,
142+ e,
143+ String :: from_utf8_lossy( & payload_bytes)
144+ ) ;
145+ "Invalid license key: bad payload data"
146+ } ) ?;
147+
148+ log:: info!( "License validated successfully for: {}" , data. email) ;
119149
120150 Ok ( data)
121151}
0 commit comments