licentivo-sdk 0.11.7-x86_64-linux-gnu → 0.11.9-x86_64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60ac73b8e23cc4e8ec2eada32ed8b5e0e77b8ee61c5277a46aedda1c17d28c92
4
- data.tar.gz: b9b41c8674509a72b6fb5891bfac27abb00c946a92d6c8ef7250f35716bbe217
3
+ metadata.gz: 3fb14dcc2eca3069c607c0975644bc523cf180bb65d3109c761b8f23c9ff78ac
4
+ data.tar.gz: af3329bd8ba3724957e01821a99c9667ff1448572eba52f1f4fefa6b9eabec76
5
5
  SHA512:
6
- metadata.gz: 0dffa9219da6a1ecaaefbb172df4ae427a03d1cda20ee769db1c354e7fd19758efcd06e1fd951687c32bf5a96f71d12e7cc41201176fd97a7ebd7f6cb6ed5d4b
7
- data.tar.gz: ea532e96f71acf90e0897d974ccea4ed08be8b3fd83643e6068752fb5989d78fee27e09478f7cd29f3d580e1b6d8b37f75a64a9f2a50a0a39c109a31537e199a
6
+ metadata.gz: cba5bda7b115e87f96a62ffdaa8886d9d620f35e1798d8b5bb56ec02b71ce57ce9ec39539f630541eaebc6154ade265fe2c6e23b76c7f69901adc05ef56537f8
7
+ data.tar.gz: 33aacf0f7b3501533614570dbf7563a6d847015287dc1b1f320ba53e9c214817143ce92d97812bfbaa66dc8148bf1af21e25a0ca45f5f2ee948b388ea4da1254
data/README.md CHANGED
@@ -13,10 +13,10 @@ Requirements: Ruby 3.1+, Fiddle, C11 compiler and libcurl development files. Sou
13
13
  Install the public SDK package in your existing application project:
14
14
 
15
15
  ```sh
16
- gem install licentivo-sdk --version 0.11.7
16
+ gem install licentivo-sdk --version 0.11.9
17
17
  ```
18
18
 
19
- For Bundler, use `gem "licentivo-sdk", "= 0.11.7"` in your Gemfile.
19
+ For Bundler, use `gem "licentivo-sdk", "= 0.11.9"` in your Gemfile.
20
20
 
21
21
  No SDK source download, extraction or local path dependency is required.
22
22
 
data/lib/licentivo.rb CHANGED
@@ -4,7 +4,7 @@ require 'json'
4
4
  require 'monitor'
5
5
 
6
6
  module Licentivo
7
- VERSION = '0.11.7'
7
+ VERSION = '0.11.9'
8
8
 
9
9
  class Error < StandardError
10
10
  attr_reader :code, :http_status, :request_id, :retryable, :retry_after_seconds
@@ -44,12 +44,35 @@ static void task_leave(ln_client*c){view_lock(c);c->task_busy=0;view_unlock(c);}
44
44
  static char*task_path(ln_client*c,const char*id){
45
45
  if(!id||!id[0]||strlen(id)>80)return NULL;
46
46
  for(const unsigned char*p=(const unsigned char*)id;*p;p++)if(*p<33||*p>126)return NULL;
47
- char source[65],digest[65],input[256];lock(c);
48
- const char*key=text(c->cfg,"license_key");int ok=key&&key[0]?hash(key,source):text(c->auth,"source_hash")!=NULL;
49
- if(ok&&(!key||!key[0]))snprintf(source,sizeof(source),"%s",text(c->auth,"source_hash"));unlock(c);
47
+ char source[65],digest[65],legacy_digest[65],input[256];lock(c);
48
+ int ok=source_hash(c,source);const char*connection=text(c->cfg,"launch_connection_id");
49
+ if(!ok){unlock(c);return NULL;}
50
+ if(ok){snprintf(input,sizeof(input),"%s\n%s",source,id);ok=hash(input,legacy_digest);}
51
+ if(connection&&connection[0]){
52
+ cJSON*claims=cache_valid(c)?claims_from(c->cached):NULL;
53
+ const char*origin=text(c->cfg,"base_url"),*product=text(claims,"product_id"),*license=text(claims,"license_id"),*dev=text(claims,"device_id");
54
+ ok=claims&&origin&&product&&license&&dev;
55
+ if(ok){size_t size=strlen(origin)+strlen(product)+strlen(license)+strlen(dev)+32;char*scope=malloc(size);ok=scope!=NULL;if(ok){snprintf(scope,size,"LicentivoTask/v2\n%s\n%s\n%s\n%s",origin,product,license,dev);ok=hash(scope,source);free(scope);}}
56
+ cJSON_Delete(claims);
57
+ }
58
+ unlock(c);
50
59
  if(!ok)return NULL;snprintf(input,sizeof(input),"%s\n%s",source,id);if(!hash(input,digest))return NULL;
51
60
  const char*cache=text(c->cfg,"cache_path");if(!cache)return NULL;
52
- char*path=malloc(strlen(cache)+78);if(path)sprintf(path,"%s.task-%s.json",cache,digest);return path;
61
+ char*path=malloc(strlen(cache)+78);if(!path)return NULL;sprintf(path,"%s.task-%s.json",cache,digest);
62
+ if(strcmp(digest,legacy_digest)){
63
+ struct stat info;if(stat(path,&info)!=0&&errno==ENOENT){
64
+ char*legacy=malloc(strlen(cache)+78);if(!legacy){free(path);return NULL;}sprintf(legacy,"%s.task-%s.json",cache,legacy_digest);
65
+ if(stat(legacy,&info)==0){
66
+ #ifdef _WIN32
67
+ ok=state_windows_move(legacy,path);
68
+ #else
69
+ ok=rename(legacy,path)==0;
70
+ #endif
71
+ }else ok=errno==ENOENT;
72
+ free(legacy);if(!ok){free(path);return NULL;}
73
+ }
74
+ }
75
+ return path;
53
76
  }
54
77
  static cJSON*task_read(const char*path,const char*id){
55
78
  struct stat info;if(stat(path,&info)==0){if(info.st_size>4096)return NULL;char*raw=readfile(path,4096);cJSON*task=raw?ln_json(raw):NULL;free(raw);int64_t version;
@@ -68,10 +91,66 @@ static int task_finish(ln_client*c,const char*path,cJSON*task,int succeeded){
68
91
  if(!ok)return task_error(c,"RESERVATION_CLOSED");task_state(task,expected);
69
92
  return write_state(path,task)?1:task_error(c,"TASK_JOURNAL_ERROR");
70
93
  }
94
+ /* Renew only the original reservation. The worker owns no business callback
95
+ * and never changes the task journal or decides the business outcome. */
96
+ typedef struct {
97
+ ln_client*client;const char*feature,*operation,*reservation;int64_t quantity;double deadline;int started;
98
+ #ifdef _WIN32
99
+ HANDLE thread,event;
100
+ #else
101
+ pthread_t thread;pthread_mutex_t mutex;pthread_cond_t condition;int stop;
102
+ #endif
103
+ } task_renewal;
104
+ static int task_renewal_wait(task_renewal*r,double seconds){
105
+ #ifdef _WIN32
106
+ return WaitForSingleObject(r->event,(DWORD)(seconds*1000))==WAIT_TIMEOUT;
107
+ #else
108
+ struct timespec until;clock_gettime(CLOCK_REALTIME,&until);int64_t nanos=(int64_t)(seconds*1e9)+until.tv_nsec;until.tv_sec+=(time_t)(nanos/1000000000);until.tv_nsec=(long)(nanos%1000000000);
109
+ pthread_mutex_lock(&r->mutex);int result=0;while(!r->stop&&result!=ETIMEDOUT)result=pthread_cond_timedwait(&r->condition,&r->mutex,&until);int active=!r->stop;pthread_mutex_unlock(&r->mutex);return active;
110
+ #endif
111
+ }
112
+ #ifdef _WIN32
113
+ static DWORD WINAPI task_renewal_run(LPVOID argument){
114
+ #else
115
+ static void*task_renewal_run(void*argument){
116
+ #endif
117
+ task_renewal*r=argument;double until=r->deadline,interval=60;
118
+ for(;;){
119
+ double remaining=until-task_wall_time();if(remaining<=0)break;double delay=remaining/3;if(delay<.01)delay=.01;if(delay>interval)delay=interval;
120
+ if(!task_renewal_wait(r,delay))break;
121
+ char*raw=ln_reserve(r->client,r->feature,r->quantity,r->operation);cJSON*hold=raw?ln_json(raw):NULL;ln_free_string(raw);
122
+ if(!hold){interval=5;continue;}
123
+ const char*status=text(hold,"status"),*id=text(hold,"reservation_id"),*operation=text(hold,"operation_id");double renewed;
124
+ int valid=status&&!strcmp(status,"pending")&&id&&!strcmp(id,r->reservation)&&operation&&!strcmp(operation,r->operation)&&task_deadline(text(hold,"expires_at"),&renewed);
125
+ cJSON_Delete(hold);if(!valid)break;until=renewed;interval=60;
126
+ }
127
+ return 0;
128
+ }
129
+ static int task_renewal_start(task_renewal*r,ln_client*c,cJSON*task,double deadline){
130
+ r->client=c;r->feature=text(task,"feature");r->operation=text(task,"operation_id");r->reservation=text(task,"reservation_id");num(task,"quantity",&r->quantity);r->deadline=deadline;
131
+ #ifdef _WIN32
132
+ r->event=CreateEventW(NULL,TRUE,FALSE,NULL);if(!r->event)return 0;
133
+ r->thread=CreateThread(NULL,0,task_renewal_run,r,0,NULL);if(!r->thread){CloseHandle(r->event);return 0;}
134
+ #else
135
+ if(pthread_mutex_init(&r->mutex,NULL))return 0;
136
+ if(pthread_cond_init(&r->condition,NULL)){pthread_mutex_destroy(&r->mutex);return 0;}
137
+ if(pthread_create(&r->thread,NULL,task_renewal_run,r)){pthread_cond_destroy(&r->condition);pthread_mutex_destroy(&r->mutex);return 0;}
138
+ #endif
139
+ r->started=1;return 1;
140
+ }
141
+ static void task_renewal_stop(task_renewal*r){
142
+ if(!r->started)return;
143
+ #ifdef _WIN32
144
+ SetEvent(r->event);WaitForSingleObject(r->thread,INFINITE);CloseHandle(r->thread);CloseHandle(r->event);
145
+ #else
146
+ pthread_mutex_lock(&r->mutex);r->stop=1;pthread_cond_signal(&r->condition);pthread_mutex_unlock(&r->mutex);pthread_join(r->thread,NULL);pthread_cond_destroy(&r->condition);pthread_mutex_destroy(&r->mutex);
147
+ #endif
148
+ r->started=0;
149
+ }
71
150
  int ln_run_metered_feature(ln_client*c,const char*feature,int64_t quantity,const char*id,ln_feature_action action,void*context){
72
151
  if(!c||!feature||!action||quantity<=0)return c?task_error(c,"TASK_INPUT_INVALID"):0;
73
152
  if(!task_enter(c))return task_error(c,"TASK_BUSY");
74
- int ok=0;char*path=task_path(c,id);cJSON*task=path?task_read(path,id):NULL;
153
+ task_renewal renewal={0};int ok=0;char*path=task_path(c,id);cJSON*task=path?task_read(path,id):NULL;
75
154
  if(!task){task_error(c,"TASK_JOURNAL_ERROR");goto done;}
76
155
  const char*state=text(task,"state");
77
156
  if(state[0]){int64_t old_quantity;if(!text(task,"feature")||strcmp(text(task,"feature"),feature)||!num(task,"quantity",&old_quantity)||old_quantity!=quantity){task_error(c,"OPERATION_ID_CONFLICT");goto done;}}
@@ -92,14 +171,16 @@ int ln_run_metered_feature(ln_client*c,const char*feature,int64_t quantity,const
92
171
  cJSON_ReplaceItemInObjectCaseSensitive(task,"reservation_id",cJSON_CreateString(text(hold,"reservation_id")));cJSON_Delete(hold);task_state(task,"business_running");
93
172
  if(!write_state(path,task)){raw=ln_cancel(c,text(task,"reservation_id"),id);ln_free_string(raw);task_error(c,"TASK_JOURNAL_ERROR");goto done;}
94
173
  int allowed=ln_check_feature(c,feature),unexpired=valid_deadline&&deadline>task_wall_time();
95
- if(!allowed||!unexpired||!action(context)){
96
- const char*failure=!allowed?"FEATURE_NOT_ENTITLED":!unexpired?"RESERVATION_EXPIRED":"BUSINESS_ACTION_FAILED";
97
- task_state(task,"cancel_pending");if(write_state(path,task))task_finish(c,path,task,0);task_error(c,failure);goto done;
174
+ int renewing=allowed&&unexpired&&task_renewal_start(&renewal,c,task,deadline);
175
+ if(!renewing||!action(context)){
176
+ const char*failure=!allowed?"FEATURE_NOT_ENTITLED":!unexpired?"RESERVATION_EXPIRED":!renewing?"RESERVATION_RENEWAL_UNAVAILABLE":"BUSINESS_ACTION_FAILED";
177
+ task_state(task,"cancel_pending");int saved=write_state(path,task);task_renewal_stop(&renewal);if(saved)task_finish(c,path,task,0);task_error(c,failure);goto done;
98
178
  }
99
179
  task_state(task,"commit_pending");if(!write_state(path,task)){task_error(c,"BUSINESS_COMPLETED_JOURNAL_ERROR");goto done;}
180
+ task_renewal_stop(&renewal);
100
181
  ok=task_finish(c,path,task,1);
101
182
  done:
102
- cJSON_Delete(task);free(path);task_leave(c);return ok;
183
+ task_renewal_stop(&renewal);cJSON_Delete(task);free(path);task_leave(c);return ok;
103
184
  }
104
185
  int ln_resolve_metered_feature(ln_client*c,const char*id,int succeeded){
105
186
  if(!c)return 0;if(!task_enter(c))return task_error(c,"TASK_BUSY");
data/native/licentivo.c CHANGED
@@ -166,7 +166,7 @@ static int advanced_claims(const cJSON*c){
166
166
  if(get(x,"app_version")&&!app_version(text(x,"app_version")))return 0;
167
167
  if(!num(c,"issued_at",&issued)||!num(c,"expires_at",&expires))return 0;
168
168
  const cJSON*policy=get(c,"runtime_policy");int64_t heartbeat=0;if(!strcmp(model,"floating")&&(!uuid(text(x,"session_id"))||session<=issued||expires>session||!num(policy,"heartbeat_seconds",&heartbeat)||heartbeat==0||!strcmp(text(policy,"offline_mode")?text(policy,"offline_mode"):"","permanent")))return 0;
169
- if(get(x,"launch_connection_id")||get(x,"environment")||get(x,"binding_version")){int64_t binding=0,h=0,o=0;const char*env=text(x,"environment"),*mode=text(policy,"offline_mode");if(!uuid(text(x,"launch_connection_id"))||!env||(strcmp(env,"live")&&strcmp(env,"sandbox"))||!num(x,"binding_version",&binding)||binding<1||strcmp(model,"node_locked")||!uuid(text(x,"session_id"))||session<=issued||expires>session||!num(policy,"heartbeat_seconds",&h)||h<600||!mode||!strcmp(mode,"permanent")||!num(policy,"offline_seconds",&o)||o>86400)return 0;}
169
+ if(get(x,"launch_connection_id")||get(x,"environment")||get(x,"binding_version")){int64_t binding=0,h=0,o=0;const char*env=text(x,"environment"),*mode=text(policy,"offline_mode");if(!uuid(text(x,"launch_connection_id"))||!env||(strcmp(env,"live")&&strcmp(env,"sandbox"))||!num(x,"binding_version",&binding)||binding<1||!uuid(text(x,"session_id"))||session<=issued||expires>session||!num(policy,"heartbeat_seconds",&h)||h<600||!mode||!strcmp(mode,"permanent")||!num(policy,"offline_seconds",&o)||o>86400)return 0;}
170
170
  if(released>issued||(maintenance>0&&(!text(x,"app_version")||released==0||released>maintenance)))return 0;
171
171
  return 1;
172
172
  }
@@ -419,7 +419,7 @@ static int prepare_credential(ln_client*c){
419
419
  static int accept_credential(ln_client*c,const cJSON*result){
420
420
  if(logout_binding_blocked(c,result))return 0;
421
421
  if(cJSON_IsTrue(get(c->logout_state,"signed_out"))){cJSON_ReplaceItemInObjectCaseSensitive(c->logout_state,"signed_out",cJSON_CreateBool(0));if(!save_logout_state(c))return 0;}
422
- if(get(result,"logout_ticket")){if(!prepare_credential(c))return 0;cJSON_DeleteItemFromObjectCaseSensitive(c->auth,"logout_ticket");cJSON_AddItemToObject(c->auth,"logout_ticket",cJSON_Duplicate(get(result,"logout_ticket"),1));c->auth_dirty=1;if(!save_credential(c))return 0;}if(!temporary(c)||(text(c->auth,"device_key")&&text(c->auth,"device_key")[0]))return 1;
422
+ if(get(result,"logout_ticket")){if(!prepare_credential(c))return 0;cJSON_DeleteItemFromObjectCaseSensitive(c->auth,"logout_ticket");cJSON_AddItemToObject(c->auth,"logout_ticket",cJSON_Duplicate(get(result,"logout_ticket"),1));c->auth_dirty=1;if(!save_credential(c))return 0;}if(!temporary(c)||(text(c->auth,"device_key")&&text(c->auth,"device_key")[0]&&(!text(result,"device_key")||!text(result,"device_key")[0])))return 1;
423
423
  const char*k=text(result,"device_key");if(!credential_secret(k,"lv_dev_")){if(strncmp(text(c->cfg,"license_key"),"lv_tmp_",7)&&!(text(c->cfg,"identity_token")&&text(c->cfg,"identity_token")[0]))return 1;strcpy(c->error,"DEVICE_CREDENTIAL_MISSING");return 0;}if(!prepare_credential(c))return 0;cJSON_ReplaceItemInObjectCaseSensitive(c->auth,"device_key",cJSON_CreateString(k));c->auth_dirty=1;return save_credential(c);
424
424
  }
425
425
  struct buffer{char data[LIMIT+1];size_t size;int oversized;};
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: licentivo-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.7
4
+ version: 0.11.9
5
5
  platform: x86_64-linux-gnu
6
6
  authors:
7
7
  - Licentivo