github.com/unidoc/unidoc@v2.2.0+incompatible/common/license/key.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package license 7 8 import ( 9 "fmt" 10 "time" 11 12 "github.com/unidoc/unidoc/common" 13 ) 14 15 const ( 16 LicenseTierUnlicensed = "unlicensed" 17 LicenseTierCommunity = "community" 18 LicenseTierIndividual = "individual" 19 LicenseTierBusiness = "business" 20 ) 21 22 // Make sure all time is at least after this for sanity check. 23 var testTime = time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC) 24 25 // Old licenses had expiry that were not meant to expire. Only checking expiry 26 // on licenses issued later than this date. 27 var startCheckingExpiry = time.Date(2018, 8, 1, 0, 0, 0, 0, time.UTC) 28 29 type LicenseKey struct { 30 LicenseId string `json:"license_id"` 31 CustomerId string `json:"customer_id"` 32 CustomerName string `json:"customer_name"` 33 Tier string `json:"tier"` 34 CreatedAt time.Time `json:"-"` 35 CreatedAtInt int64 `json:"created_at"` 36 ExpiresAt *time.Time `json:"-"` 37 ExpiresAtInt int64 `json:"expires_at"` 38 CreatedBy string `json:"created_by"` 39 CreatorName string `json:"creator_name"` 40 CreatorEmail string `json:"creator_email"` 41 } 42 43 func (this *LicenseKey) isExpired() bool { 44 if this.ExpiresAt == nil { 45 return false 46 } 47 48 if this.CreatedAt.Before(startCheckingExpiry) { 49 return false 50 } 51 52 utcNow := time.Now().UTC() 53 return utcNow.After(*this.ExpiresAt) 54 } 55 56 func (this *LicenseKey) Validate() error { 57 if len(this.LicenseId) < 10 { 58 return fmt.Errorf("Invalid license: License Id") 59 } 60 61 if len(this.CustomerId) < 10 { 62 return fmt.Errorf("Invalid license: Customer Id") 63 } 64 65 if len(this.CustomerName) < 1 { 66 return fmt.Errorf("Invalid license: Customer Name") 67 } 68 69 if testTime.After(this.CreatedAt) { 70 return fmt.Errorf("Invalid license: Created At is invalid") 71 } 72 73 if this.ExpiresAt != nil { 74 if this.CreatedAt.After(*this.ExpiresAt) { 75 return fmt.Errorf("Invalid license: Created At cannot be Greater than Expires At") 76 } 77 } 78 79 if this.isExpired() { 80 return fmt.Errorf("Invalid license: The license has already expired") 81 } 82 83 if len(this.CreatorName) < 1 { 84 return fmt.Errorf("Invalid license: Creator name") 85 } 86 87 if len(this.CreatorEmail) < 1 { 88 return fmt.Errorf("Invalid license: Creator email") 89 } 90 91 return nil 92 } 93 94 func (this *LicenseKey) TypeToString() string { 95 if this.Tier == LicenseTierUnlicensed { 96 return "Unlicensed" 97 } 98 99 if this.Tier == LicenseTierCommunity { 100 return "AGPLv3 Open Source Community License" 101 } 102 103 if this.Tier == LicenseTierIndividual || this.Tier == "indie" { 104 return "Commercial License - Individual" 105 } 106 107 return "Commercial License - Business" 108 } 109 110 func (this *LicenseKey) ToString() string { 111 str := fmt.Sprintf("License Id: %s\n", this.LicenseId) 112 str += fmt.Sprintf("Customer Id: %s\n", this.CustomerId) 113 str += fmt.Sprintf("Customer Name: %s\n", this.CustomerName) 114 str += fmt.Sprintf("Tier: %s\n", this.Tier) 115 str += fmt.Sprintf("Created At: %s\n", common.UtcTimeFormat(this.CreatedAt)) 116 117 if this.ExpiresAt == nil { 118 str += fmt.Sprintf("Expires At: Never\n") 119 } else { 120 str += fmt.Sprintf("Expires At: %s\n", common.UtcTimeFormat(*this.ExpiresAt)) 121 } 122 123 str += fmt.Sprintf("Creator: %s <%s>\n", this.CreatorName, this.CreatorEmail) 124 return str 125 } 126 127 func (lk *LicenseKey) IsLicensed() bool { 128 return lk.Tier != LicenseTierUnlicensed 129 } 130 131 func MakeUnlicensedKey() *LicenseKey { 132 lk := LicenseKey{} 133 lk.CustomerName = "Unlicensed" 134 lk.Tier = LicenseTierUnlicensed 135 lk.CreatedAt = time.Now().UTC() 136 lk.CreatedAtInt = lk.CreatedAt.Unix() 137 return &lk 138 }