unipdf/common/license/license.go
2020-08-31 21:12:07 +00:00

16 lines
14 KiB
Go

//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
// Package license helps manage commercial licenses and check if they are valid for the version of unidoc used.
package license ;import (_b "crypto";_beg "crypto/rand";_c "crypto/rsa";_ea "crypto/sha512";_fb "crypto/x509";_feg "encoding/base64";_ef "encoding/json";_be "encoding/pem";_fe "fmt";_ff "github.com/unidoc/unipdf/v3/common";_gg "io/ioutil";_g "os";_e "strings";_f "time";);var _gcf =_f .Date (2020,1,1,0,0,0,0,_f .UTC );
// SetLicenseKey sets and validates the license key.
func SetLicenseKey (content string ,customerName string )error {_fea ,_da :=_gdd (content );if _da !=nil {return _da ;};if _e .ToLower (_fea .CustomerName )!=_e .ToLower (customerName ){return _fe .Errorf ("\u0063\u0075\u0073\u0074\u006fm\u0065\u0072\u0020\u006e\u0061\u006d\u0065\u0020\u006d\u0069\u0073\u006d\u0061t\u0063\u0068\u002c\u0020\u0065\u0078\u0070\u0065\u0063\u0074\u0065\u0064\u0020\u0027\u0025\u0073\u0027\u002c\u0020\u0062\u0075\u0074\u0020\u0067\u006f\u0074\u0020\u0027\u0025\u0073'",customerName ,_fea .CustomerName );};_da =_fea .Validate ();if _da !=nil {return _da ;};_ab =&_fea ;return nil ;};func _gd (_fgf string ,_dg string ,_feb string )(string ,error ){_dgg :=_e .Index (_feb ,_fgf );if _dgg ==-1{return "",_fe .Errorf ("\u0068\u0065a\u0064\u0065\u0072 \u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_ga :=_e .Index (_feb ,_dg );if _ga ==-1{return "",_fe .Errorf ("\u0066\u006fo\u0074\u0065\u0072 \u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_bg :=_dgg +len (_fgf )+1;return _feb [_bg :_ga -1],nil ;};func _dd (_bb string ,_ee []byte )(string ,error ){_a ,_ :=_be .Decode ([]byte (_bb ));if _a ==nil {return "",_fe .Errorf ("\u0050\u0072\u0069\u0076\u004b\u0065\u0079\u0020\u0066a\u0069\u006c\u0065\u0064");};_ge ,_ag :=_fb .ParsePKCS1PrivateKey (_a .Bytes );if _ag !=nil {return "",_ag ;};_ac :=_ea .New ();_ac .Write (_ee );_gb :=_ac .Sum (nil );_agc ,_ag :=_c .SignPKCS1v15 (_beg .Reader ,_ge ,_b .SHA512 ,_gb );if _ag !=nil {return "",_ag ;};_ec :=_feg .StdEncoding .EncodeToString (_ee );_ec +="\u000a\u002b\u000a";_ec +=_feg .StdEncoding .EncodeToString (_agc );return _ec ,nil ;};var _ab =MakeUnlicensedKey ();func (_dc *LicenseKey )ToString ()string {_fae :=_fe .Sprintf ("\u004ci\u0063e\u006e\u0073\u0065\u0020\u0049\u0064\u003a\u0020\u0025\u0073\u000a",_dc .LicenseId );_fae +=_fe .Sprintf ("\u0043\u0075s\u0074\u006f\u006de\u0072\u0020\u0049\u0064\u003a\u0020\u0025\u0073\u000a",_dc .CustomerId );_fae +=_fe .Sprintf ("\u0043u\u0073t\u006f\u006d\u0065\u0072\u0020N\u0061\u006de\u003a\u0020\u0025\u0073\u000a",_dc .CustomerName );_fae +=_fe .Sprintf ("\u0054i\u0065\u0072\u003a\u0020\u0025\u0073\n",_dc .Tier );_fae +=_fe .Sprintf ("\u0043r\u0065a\u0074\u0065\u0064\u0020\u0041\u0074\u003a\u0020\u0025\u0073\u000a",_ff .UtcTimeFormat (_dc .CreatedAt ));if _dc .ExpiresAt ==nil {_fae +=_fe .Sprintf ("\u0045x\u0070i\u0072\u0065\u0073\u0020\u0041t\u003a\u0020N\u0065\u0076\u0065\u0072\u000a");}else {_fae +=_fe .Sprintf ("\u0045x\u0070i\u0072\u0065\u0073\u0020\u0041\u0074\u003a\u0020\u0025\u0073\u000a",_ff .UtcTimeFormat (*_dc .ExpiresAt ));};_fae +=_fe .Sprintf ("\u0043\u0072\u0065\u0061\u0074\u006f\u0072\u003a\u0020\u0025\u0073\u0020<\u0025\u0073\u003e\u000a",_dc .CreatorName ,_dc .CreatorEmail );return _fae ;};const (LicenseTierUnlicensed ="\u0075\u006e\u006c\u0069\u0063\u0065\u006e\u0073\u0065\u0064";LicenseTierCommunity ="\u0063o\u006d\u006d\u0075\u006e\u0069\u0074y";LicenseTierIndividual ="\u0069\u006e\u0064\u0069\u0076\u0069\u0064\u0075\u0061\u006c";LicenseTierBusiness ="\u0062\u0075\u0073\u0069\u006e\u0065\u0073\u0073";);const _de ="\u0055\u004e\u0049\u0050DF\u005f\u004c\u0049\u0043\u0045\u004e\u0053\u0045\u005f\u0050\u0041\u0054\u0048";func GetLicenseKey ()*LicenseKey {if _ab ==nil {return nil ;};_ce :=*_ab ;return &_ce ;};func (_ba *LicenseKey )isExpired ()bool {return _ba .getExpiryDateToCompare ().After (*_ba .ExpiresAt )};func (_eee *LicenseKey )IsLicensed ()bool {return _eee .Tier !=LicenseTierUnlicensed };type LicenseKey struct{LicenseId string `json:"license_id"`;CustomerId string `json:"customer_id"`;CustomerName string `json:"customer_name"`;Tier string `json:"tier"`;CreatedAt _f .Time `json:"-"`;CreatedAtInt int64 `json:"created_at"`;ExpiresAt *_f .Time `json:"-"`;ExpiresAtInt int64 `json:"expires_at"`;CreatedBy string `json:"created_by"`;CreatorName string `json:"creator_name"`;CreatorEmail string `json:"creator_email"`;UniPDF bool `json:"unipdf"`;UniOffice bool `json:"unioffice"`;Trial bool `json:"trial"`;};var _eef =_f .Date (2010,1,1,0,0,0,0,_f .UTC );const _fddd ="U\u004eI\u0050\u0044\u0046\u005f\u0043\u0055\u0053\u0054O\u004d\u0045\u0052\u005fNA\u004d\u0045";func (_fcf *LicenseKey )TypeToString ()string {if _fcf .Tier ==LicenseTierUnlicensed {return "\u0055\u006e\u006c\u0069\u0063\u0065\u006e\u0073\u0065\u0064";};if _fcf .Tier ==LicenseTierCommunity {return "\u0041\u0047PL\u0076\u0033\u0020O\u0070\u0065\u006e\u0020Sou\u0072ce\u0020\u0043\u006f\u006d\u006d\u0075\u006eit\u0079\u0020\u004c\u0069\u0063\u0065\u006es\u0065";};if _fcf .Tier ==LicenseTierIndividual ||_fcf .Tier =="\u0069\u006e\u0064i\u0065"{return "\u0043\u006f\u006dm\u0065\u0072\u0063\u0069a\u006c\u0020\u004c\u0069\u0063\u0065\u006es\u0065\u0020\u002d\u0020\u0049\u006e\u0064\u0069\u0076\u0069\u0064\u0075\u0061\u006c";};return "\u0043\u006fm\u006d\u0065\u0072\u0063\u0069\u0061\u006c\u0020\u004c\u0069\u0063\u0065\u006e\u0073\u0065\u0020\u002d\u0020\u0042\u0075\u0073\u0069ne\u0073\u0073";};func (_bbd *LicenseKey )Validate ()error {if len (_bbd .LicenseId )< 10{return _fe .Errorf ("i\u006e\u0076\u0061\u006c\u0069\u0064 \u006c\u0069\u0063\u0065\u006e\u0073\u0065\u003a\u0020L\u0069\u0063\u0065n\u0073e\u0020\u0049\u0064");};if len (_bbd .CustomerId )< 10{return _fe .Errorf ("\u0069\u006e\u0076\u0061l\u0069\u0064\u0020\u006c\u0069\u0063\u0065\u006e\u0073\u0065:\u0020C\u0075\u0073\u0074\u006f\u006d\u0065\u0072 \u0049\u0064");};if len (_bbd .CustomerName )< 1{return _fe .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006c\u0069c\u0065\u006e\u0073\u0065\u003a\u0020\u0043u\u0073\u0074\u006f\u006d\u0065\u0072\u0020\u004e\u0061\u006d\u0065");};if _eef .After (_bbd .CreatedAt ){return _fe .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006c\u0069\u0063\u0065\u006e\u0073\u0065\u003a\u0020\u0043\u0072\u0065\u0061\u0074\u0065\u0064 \u0041\u0074\u0020\u0069\u0073 \u0069\u006ev\u0061\u006c\u0069\u0064");};if _bbd .ExpiresAt ==nil {_gdde :=_bbd .CreatedAt .AddDate (1,0,0);if _gcf .After (_gdde ){_gdde =_gcf ;};_bbd .ExpiresAt =&_gdde ;};if _bbd .CreatedAt .After (*_bbd .ExpiresAt ){return _fe .Errorf ("i\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006c\u0069\u0063\u0065\u006e\u0073\u0065\u003a\u0020\u0043\u0072\u0065\u0061\u0074\u0065\u0064\u0020\u0041\u0074 \u0063a\u006e\u006e\u006f\u0074 \u0062\u0065 \u0047\u0072\u0065\u0061\u0074\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0045\u0078\u0070\u0069\u0072\u0065\u0073\u0020\u0041\u0074");};if _bbd .isExpired (){return _fe .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020l\u0069\u0063\u0065ns\u0065\u003a\u0020\u0054\u0068\u0065 \u006c\u0069\u0063\u0065\u006e\u0073\u0065\u0020\u0068\u0061\u0073\u0020\u0061\u006c\u0072e\u0061\u0064\u0079\u0020\u0065\u0078\u0070\u0069r\u0065\u0064");};if len (_bbd .CreatorName )< 1{return _fe .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u006c\u0069\u0063\u0065\u006e\u0073\u0065\u003a\u0020\u0043\u0072\u0065\u0061\u0074\u006f\u0072\u0020na\u006d\u0065");};if len (_bbd .CreatorEmail )< 1{return _fe .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006c\u0069c\u0065\u006e\u0073\u0065\u003a\u0020\u0043r\u0065\u0061\u0074\u006f\u0072\u0020\u0065\u006d\u0061\u0069\u006c");};if _bbd .CreatedAt .After (_ddc ){if !_bbd .UniPDF {return _fe .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064 \u006c\u0069\u0063e\u006e\u0073\u0065:\u0020\u0054h\u0069\u0073\u0020\u0055\u006e\u0069D\u006fc \u006b\u0065\u0079\u0020\u0069\u0073\u0020\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0066\u006f\u0072\u0020\u0055\u006e\u0069\u0050\u0044\u0046\u002e");};};return nil ;};func MakeUnlicensedKey ()*LicenseKey {_bc :=LicenseKey {};_bc .CustomerName ="\u0055\u006e\u006c\u0069\u0063\u0065\u006e\u0073\u0065\u0064";_bc .Tier =LicenseTierUnlicensed ;_bc .CreatedAt =_f .Now ().UTC ();_bc .CreatedAtInt =_bc .CreatedAt .Unix ();return &_bc ;};func (_cg *LicenseKey )getExpiryDateToCompare ()_f .Time {if _cg .Trial {return _f .Now ().UTC ();};return _ff .ReleasedAt ;};func _fc (_fg string ,_fd string )([]byte ,error ){_bd :="\u000a\u002b\u000a";_eg :="\u000d\u000a\u002b\r\u000a";_cd :=_e .Index (_fd ,_bd );if _cd ==-1{_cd =_e .Index (_fd ,_eg );if _cd ==-1{return nil ,_fe .Errorf ("\u0069\u006e\u0076al\u0069\u0064\u0020\u0069\u006e\u0070\u0075\u0074\u002c \u0073i\u0067n\u0061t\u0075\u0072\u0065\u0020\u0073\u0065\u0070\u0061\u0072\u0061\u0074\u006f\u0072");};};_bf :=_fd [:_cd ];_fdd :=_cd +len (_bd );_efe :=_fd [_fdd :];if _bf ==""||_efe ==""{return nil ,_fe .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020\u0069n\u0070\u0075\u0074,\u0020\u006d\u0069\u0073\u0073\u0069\u006e\u0067\u0020or\u0069\u0067\u0069n\u0061\u006c \u006f\u0072\u0020\u0073\u0069\u0067n\u0061\u0074u\u0072\u0065");};_fa ,_eec :=_feg .StdEncoding .DecodeString (_bf );if _eec !=nil {return nil ,_fe .Errorf ("\u0069\u006e\u0076\u0061li\u0064\u0020\u0069\u006e\u0070\u0075\u0074\u0020\u006f\u0072\u0069\u0067\u0069\u006ea\u006c");};_gc ,_eec :=_feg .StdEncoding .DecodeString (_efe );if _eec !=nil {return nil ,_fe .Errorf ("\u0069\u006e\u0076al\u0069\u0064\u0020\u0069\u006e\u0070\u0075\u0074\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065");};_gee ,_ :=_be .Decode ([]byte (_fg ));if _gee ==nil {return nil ,_fe .Errorf ("\u0050\u0075\u0062\u004b\u0065\u0079\u0020\u0066\u0061\u0069\u006c\u0065\u0064");};_eda ,_eec :=_fb .ParsePKIXPublicKey (_gee .Bytes );if _eec !=nil {return nil ,_eec ;};_fdf :=_eda .(*_c .PublicKey );if _fdf ==nil {return nil ,_fe .Errorf ("\u0050u\u0062\u004b\u0065\u0079\u0020\u0063\u006f\u006e\u0076\u0065\u0072s\u0069\u006f\u006e\u0020\u0066\u0061\u0069\u006c\u0065\u0064");};_ega :=_ea .New ();_ega .Write (_fa );_ca :=_ega .Sum (nil );_eec =_c .VerifyPKCS1v15 (_fdf ,_b .SHA512 ,_ca ,_gc );if _eec !=nil {return nil ,_eec ;};return _fa ,nil ;};const (_ed ="\u002d\u002d\u002d--\u0042\u0045\u0047\u0049\u004e\u0020\u0055\u004e\u0049D\u004fC\u0020L\u0049C\u0045\u004e\u0053\u0045\u0020\u004b\u0045\u0059\u002d\u002d\u002d\u002d\u002d";_efc ="\u002d\u002d\u002d\u002d\u002d\u0045\u004e\u0044\u0020\u0055\u004e\u0049\u0044\u004f\u0043 \u004cI\u0043\u0045\u004e\u0053\u0045\u0020\u004b\u0045\u0059\u002d\u002d\u002d\u002d\u002d";);const _fed ="\u000a\u002d\u002d\u002d\u002d\u002d\u0042\u0045\u0047\u0049\u004e \u0050\u0055\u0042\u004c\u0049\u0043\u0020\u004b\u0045Y\u002d\u002d\u002d\u002d\u002d\u000a\u004d\u0049I\u0042\u0049\u006a\u0041NB\u0067\u006b\u0071\u0068\u006b\u0069G\u0039\u0077\u0030\u0042\u0041\u0051\u0045\u0046A\u0041\u004f\u0043\u0041\u0051\u0038\u0041\u004d\u0049\u0049\u0042\u0043\u0067\u004b\u0043\u0041\u0051\u0045A\u006dF\u0055\u0069\u0079\u0064\u0037\u0062\u0035\u0058\u006a\u0070\u006b\u0050\u0035\u0052\u0061\u0070\u0034\u0077\u000a\u0044\u0063\u0031d\u0079\u007a\u0049\u0051\u0034\u004c\u0065\u006b\u0078\u0072\u0076\u0079\u0074\u006e\u0045\u004d\u0070\u004e\u0055\u0062\u006f\u0036i\u0041\u0037\u0034\u0056\u0038\u0072\u0075\u005a\u004f\u0076\u0072\u0053\u0063\u0073\u0066\u0032\u0051\u0065\u004e9\u002f\u0071r\u0055\u0047\u0038\u0071\u0045\u0062\u0055\u0057\u0064\u006f\u0045\u0059\u0071+\u000a\u006f\u0074\u0046\u004e\u0041\u0046N\u0078\u006c\u0047\u0062\u0078\u0062\u0044\u0048\u0063\u0064\u0047\u0056\u0061\u004d\u0030\u004f\u0058\u0064\u0058g\u0044y\u004c5\u0061\u0049\u0045\u0061\u0067\u004c\u0030\u0063\u0035\u0070\u0077\u006a\u0049\u0064\u0050G\u0049\u006e\u0034\u0036\u0066\u0037\u0038\u0065\u004d\u004a\u002b\u004a\u006b\u0064\u0063\u0070\u0044\n\u0044\u004a\u0061\u0071\u0059\u0058d\u0072\u007a5\u004b\u0065\u0073\u0068\u006aS\u0069\u0049\u0061\u0061\u0037\u006d\u0065\u006e\u0042\u0049\u0041\u0058\u0053\u0034\u0055\u0046\u0078N\u0066H\u0068\u004e\u0030\u0048\u0043\u0059\u005a\u0059\u0071\u0051\u0047\u0037\u0062K+\u0073\u0035\u0072R\u0048\u006f\u006e\u0079\u0064\u004eW\u0045\u0047\u000a\u0048\u0038M\u0079\u0076\u00722\u0070\u0079\u0061\u0032K\u0072\u004d\u0075m\u0066\u006d\u0041\u0078\u0055\u0042\u0036\u0066\u0065\u006e\u0043\u002f4\u004f\u0030\u0057\u00728\u0067\u0066\u0050\u004f\u0055\u0038R\u0069\u0074\u006d\u0062\u0044\u0076\u0051\u0050\u0049\u0052\u0058\u004fL\u0034\u0076\u0054B\u0072\u0042\u0064\u0062a\u0041\u000a9\u006e\u0077\u004e\u0050\u002b\u0069\u002f\u002f\u0032\u0030\u004d\u00542\u0062\u0078\u006d\u0065\u0057\u0042\u002b\u0067\u0070\u0063\u0045\u0068G\u0070\u0058\u005a7\u0033\u0033\u0061\u007a\u0051\u0078\u0072\u0043\u0033\u004a\u0034\u0076\u0033C\u005a\u006d\u0045\u004eS\u0074\u0044\u004b\u002f\u004b\u0044\u0053\u0050\u004b\u0055\u0047\u0066\u00756\u000a\u0066\u0077I\u0044\u0041\u0051\u0041\u0042\u000a\u002d\u002d\u002d\u002d\u002dE\u004e\u0044\u0020\u0050\u0055\u0042\u004c\u0049\u0043 \u004b\u0045Y\u002d\u002d\u002d\u002d\u002d\n";var _ddc =_f .Date (2019,6,6,0,0,0,0,_f .UTC );func init (){_geg :=_g .Getenv (_de );_bbc :=_g .Getenv (_fddd );if len (_geg )==0||len (_bbc )==0{return ;};_bgc ,_cgf :=_gg .ReadFile (_geg );if _cgf !=nil {_ff .Log .Debug ("\u0055\u006e\u0061b\u006c\u0065\u0020\u0074o\u0020\u0072\u0065\u0061\u0064\u0020\u006ci\u0063\u0065\u006e\u0073\u0065\u0020\u0066\u0069\u006c\u0065\u003a\u0020\u0025\u0076",_cgf );return ;};_cgf =SetLicenseKey (string (_bgc ),_bbc );if _cgf !=nil {_ff .Log .Debug ("\u0055\u006e\u0061\u0062\u006c\u0065\u0020\u0074\u006f\u0020\u006co\u0061\u0064\u0020\u006c\u0069\u0063\u0065\u006e\u0073\u0065:\u0020\u0025\u0076",_cgf );return ;};};func _gdd (_cc string )(LicenseKey ,error ){var _db LicenseKey ;_af ,_ggg :=_gd (_ed ,_efc ,_cc );if _ggg !=nil {return _db ,_ggg ;};_caa ,_ggg :=_fc (_fed ,_af );if _ggg !=nil {return _db ,_ggg ;};_ggg =_ef .Unmarshal (_caa ,&_db );if _ggg !=nil {return _db ,_ggg ;};_db .CreatedAt =_f .Unix (_db .CreatedAtInt ,0);if _db .ExpiresAtInt > 0{_ad :=_f .Unix (_db .ExpiresAtInt ,0);_db .ExpiresAt =&_ad ;};return _db ,nil ;};