lisensya

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2020 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Lisensya The lisensya package is a simple license key generator that manages your license key requirements for your Go's application.

Installation

go get -u github.com/itrepablik/lisensya

Usage

For this example, this is how you can use the main function to check your license key which is most likely in your main.go file.

package main

import (
	"fmt"

	"github.com/itrepablik/itrlog"
	"github.com/itrepablik/lisensya"
)

// Keep this secret
const (
	secretKey              = "abc&1*~#^2^#s0^=)^^7%b34"
	appName                = "NiceApp"
	licenseExpiryDelimiter = "--expiry:"
)

// IsLicenseKeyValid is declared globally at your main.go check either the license key is expired or not.
var IsLicenseKeyValid bool = false

func main() {
	// This is the main validation for the license key.
	IsLicenseKeyValid, err := lisensya.IsLicenseKeyValid(appName, secretKey, licenseExpiryDelimiter)
	if err != nil {
		msg := `error getting license key: `
		fmt.Println(msg, err)
		itrlog.Fatal(msg, err)
	}

	// Check if the license key is valid or not.
	if !IsLicenseKeyValid {
		msg := `oops, invalid license key`
		itrlog.Error(msg)
		fmt.Println(msg)
		return
	}
}

This is to generate a new license key:

package main

import (
	"fmt"

	"github.com/itrepablik/itrlog"
	"github.com/itrepablik/lisensya"
)

// Keep this secret
const (
	secretKey              = "abc&1*~#^2^#s0^=)^^7%b34"
	appName                = "NiceApp"
	licenseExpiryDelimiter = "--expiry:"
	APInewlicense          = "" // e.g. https://your_site_name.com/api/your_api_url
	expiredInDays          = 30
)

// IsLicenseKeyValid is declared globally at your main.go check either the license key is expired or not.
var IsLicenseKeyValid bool = false
var hostName string = ""

func main() {
	// Get the hostname
	hostName, err := lisensya.GetHostName()
	if err != nil {
		itrlog.Fatal("error getting hostname: ", err)
	}

	// Starts writing a new license key
	userName := "username"
	isNewLicenseOK, err := lisensya.GenerateLicenseKey(APInewlicense, appName,
		secretKey, licenseExpiryDelimiter, expiredInDays, hostName, userName, secretKey)

	if err != nil {
		itrlog.Fatal("error generating new license key: ", err)
	}

	// Inform user about the new license key successfully generated.
	if isNewLicenseOK {
		fmt.Println("You've successfully generated your gokopy's license key, you can now use this software.")
	}
}

This is to revoke existing license key:

package main

import (
	"fmt"

	"github.com/itrepablik/itrlog"
	"github.com/itrepablik/lisensya"
)

// Keep this secret
const (
	secretKey              = "abc&1*~#^2^#s0^=)^^7%b34"
	appName                = "NiceApp"
	licenseExpiryDelimiter = "--expiry:"
	APInewlicense          = "" // e.g. https://your_site_name.com/api/your_api_url
	expiredInDays          = 30
)

// IsLicenseKeyValid is declared globally at your main.go check either the license key is expired or not.
var IsLicenseKeyValid bool = false
var hostName string = ""

func main() {
	userName := "username"

	// Get the existing license key from a file.
	licenseKey, err := lisensya.ReadLicenseKey(appName)
	if err != nil {
		itrlog.Fatal("error getting current license key: ", err)
	}

	// Update the license key at the backend
	rLicKey, err := lisensya.RevokeLicenseKey(APInewlicense, appName, hostName, licenseKey, userName, secretKey)
	if err != nil {
		itrlog.Fatal("error revoking your existing license key: ", err)
	}

	if rLicKey {
		fmt.Println("Successfully revoked your current gokopy's license key.")
	} else {
		fmt.Println("Oops!, so far probably you don't have any existing license key to be revoked.")
	}
}

Sample scripts on how you can handle the values at your backend API.

// APIGenerateLicenseKey generate's the license key at your backend system.
func APIGenerateLicenseKey(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)

	body, errBody := ioutil.ReadAll(r.Body)
	if errBody != nil {
		panic(errBody.Error())
	}

	keyVal := make(map[string]string)
	json.Unmarshal(body, &keyVal)

	hostName := strings.TrimSpace(keyVal["0"])
	userName := strings.TrimSpace(keyVal["1"])
	secretKey := Decrypt(keyVal["2"])
	licenseKey := keyVal["3"]
}

Subscribe to Maharlikans Code Youtube Channel:

Please consider subscribing to my Youtube Channel to recognize my work on this package. Thank you for your support! https://www.youtube.com/channel/UCdAVUmldU9Jn2VntuQChHqQ/

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Documentation

Overview

Package lisensya is the simple license key generator tool for your Go's applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIEndPoint

func APIEndPoint(API string, payLoad []interface{}) (bool, error)

APIEndPoint is the backend endpoint for your application when required to be triggered.

func APISubmitNewLicenseKey

func APISubmitNewLicenseKey(API string, payLoad []interface{}) (bool, error)

APISubmitNewLicenseKey to submit new license key information to any backend API endpoint.

func ClearLicenseKeyFile

func ClearLicenseKeyFile(appName string) error

ClearLicenseKeyFile remove license key from a file.

func ExtractLicenseKey

func ExtractLicenseKey(licenseKey, expiryDelimiter string) string

ExtractLicenseKey extract the license key without the expiry date.

func GenerateLicenseKey

func GenerateLicenseKey(API, appName, secretKey, expiryDelimeter string, expiredInDays int, payLoad ...interface{}) (bool, error)

GenerateLicenseKey writes the new license key to a custom file and stores in the root directory of your app directory, e.g appname.license

func GetHostName

func GetHostName() (string, error)

GetHostName gets the source machine's hostname.

func IsLicenseKeyExpired

func IsLicenseKeyExpired(licenseKey, expiryDelimiter string) bool

IsLicenseKeyExpired ensure that the license key expiration date is still valid or not.

func IsLicenseKeyValid

func IsLicenseKeyValid(appName, secretKey, expiryDelimiter string) (bool, error)

IsLicenseKeyValid is the main license key's validation.

func ReadLicenseKey

func ReadLicenseKey(appName string) (string, error)

ReadLicenseKey reads the license key if found from a custom file, otherwise, throws an error.

func RevokeLicenseKey

func RevokeLicenseKey(API, appName string, payLoad ...interface{}) (bool, error)

RevokeLicenseKey revokes existing gokopy license key.

Types

This section is empty.

Jump to

Keyboard shortcuts

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