gicense

package module
v0.0.0-...-378dc36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2021 License: MIT Imports: 9 Imported by: 0

README

Gicense

Go library for generating license keys based on jwt tokens.

Usage

Without encryption

Generate a simple license in jwt token format with signing method HS256. In this case secret is empty because no encryption was given.

// Create a new Generator
licenseGenerator := NewLicenseGenerator()

// Generates a new license
license, err := licenseGenerator.GenerateLicense("Owner", "Customer", "CookiesGenerator", time.Now().Add(time.Minute), []byte("Password"))

// Print license key
fmt.Println(license.Key)

//Check if license is valid.
isValid, err := license.IsValid([]byte("Password"))

// Returns the payload of the license
payload, err := license.GetPayload([]byte("Password"))

// Returns the payload without validation
payload, err := license.GetPayloadWithoutValidation([]byte("Password"))
Custom Payload

You can also add custom data to your payload.


// create payload
customPayload := NewPayload(licenseGenerator.generateID(), "Owner", "Customer", "CustomCookieMonster", time.Now().Add(time.Hour), map[string]interface{}{"version": "0.5.0"})	

licenseGenerator.GenerateLicenseFromPayload(customPaylaod, []byte("Password"))

Encrytion

You can encrypt the token with any encryption method of your choice, implement the interface Encryption

The encryption secret is generated depending on the payload and time. The Secret can also be changed, implement the interface Secret

The default encryption is AES with a md5 hash secret.

// Use dependecy injection to define the encryption
licenseOption := NewLicenseGenerator(DefaultEncryption)

If encryption is provided the generated license contains the secret and encryption.

fmt.Println(license.Secret)
fmt.Println(license.Encryption)

TODO

  • Add tests
  • Add all signing methods
  • Add MD5, UUID and file format
  • Create Rest API and CLI

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorsPayload        = errorx.NewNamespace("Payload")
	ErrorPayloadNotValid = errorx.NewType(ErrorsPayload, "NotValid")
)
View Source
var (
	ErrorsToken        = errorx.NewNamespace("Token")
	ErrorTokenNotValid = errorx.NewType(ErrorsToken, "NotValid")
	ErrorTokenSigning  = errorx.NewType(ErrorsToken, "ISigning")
)

Functions

func DefaultEncryption

func DefaultEncryption(option *LicenseGenerator)

Default IEncryption Method

func DefaultSecret

func DefaultSecret(option *LicenseGenerator)

Default Secret Method

func DefaultSigning

func DefaultSigning(option *LicenseGenerator)

Default ISigning Method

Types

type License

type License struct {
	Key        string
	Secret     secret.Secret
	Encryption encryption.IEncryption
}

func (License) Decrypt

func (l License) Decrypt() (License, error)

Decrypts the License with a Secret. The Encryption type is needed

func (License) GetPayload

func (l License) GetPayload(verify interface{}) (*Payload, error)

Returns the Payload if valid

func (License) GetPayloadWithoutValidation

func (l License) GetPayloadWithoutValidation(verify interface{}) (*Payload, error)

Returns the Payload, ignores validation errors

func (License) IsValid

func (l License) IsValid(verify interface{}) (bool, error)

Checks if license is valid

type LicenseGenerator

type LicenseGenerator struct {
	Signing    signing.ISigning
	Secret     secret.ISecret
	Encryption encryption.IEncryption // optional
}

func NewLicenseGenerator

func NewLicenseGenerator(options ...Options) *LicenseGenerator

Creates a new LicenseGenerator

func (*LicenseGenerator) GenerateLicense

func (l *LicenseGenerator) GenerateLicense(from string, to string, product string, end time.Time, sign interface{}) (License, error)

Generates a encrypted License with a Secret

func (*LicenseGenerator) GenerateLicenseFromPayload

func (l *LicenseGenerator) GenerateLicenseFromPayload(payload Payload, sign interface{}) (License, error)

Generate a License from a Payload. Returns the License and a Secret needed to encrypt to License

type Options

type Options func(option *LicenseGenerator)

type Payload

type Payload struct {
	jwt.StandardClaims
	Data map[string]interface{}
}

func NewPayload

func NewPayload(id string, from string, to string, product string, end time.Time, additional map[string]interface{}) *Payload

Creates a new payload

func (*Payload) IsValid

func (p *Payload) IsValid() error

Check if the Payload is Valid

func (*Payload) StartAt

func (p *Payload) StartAt(start time.Time)

Sets the the start time the token starts to be valid

type Token

type Token struct {
	*jwt.Token
}

func NewToken

func NewToken(payload Payload, signing signing.ISigning) Token

Creates a new Token

func (*Token) GetPayload

func (t *Token) GetPayload() (*Payload, error)

Returns the tokens Payload

func (*Token) Sign

func (t *Token) Sign(sign interface{}) (TokenString, error)

Signs the Token with a secret message

type TokenString

type TokenString string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL