]> wagnertech.de Git - mfinanz.git/blob - SL/DB/ValidityToken.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.git] / SL / DB / ValidityToken.pm
1 package SL::DB::ValidityToken;
2
3 use strict;
4
5 use Carp;
6 use Digest::SHA qw(sha256_hex);
7 use Time::HiRes qw(gettimeofday);
8
9 use SL::DB::MetaSetup::ValidityToken;
10 use SL::DB::Manager::ValidityToken;
11
12 __PACKAGE__->meta->initialize;
13
14 use constant SCOPE_SALES_INVOICE_POST    => 'SalesInvoice::Post';
15 use constant SCOPE_PURCHASE_INVOICE_POST => 'PurchaseInvoice::Post';
16 use constant SCOPE_DELIVERY_ORDER_SAVE   => 'DeliveryOrder::Save';
17 use constant SCOPE_ORDER_SAVE            => 'Order::Save';
18 use constant SCOPE_RECLAMATION_SAVE      => 'Reclamation::Save';
19 use constant SCOPE_GL_TRANSACTION_POST   => 'GLTransaction::Post';
20
21 sub create {
22   my ($class, %params) = @_;
23
24   croak "missing required parameter 'scope'" if !$params{scope};
25
26   my $token_obj = $class->new(
27     scope       => $params{scope},
28     valid_until => $params{valid_until} // DateTime->now_local->add(hours => 24),
29   );
30
31   while (1) {
32     my $token_value = join('-', gettimeofday(), $$, int(rand(1 << 63)));
33
34     $token_obj->token(sha256_hex($token_value));
35
36     last if eval {
37       $token_obj->save;
38       1;
39     };
40   }
41
42   return $token_obj;
43 }
44
45
46 1;