|
| 1 | +# Code0::License |
| 2 | + |
| 3 | +`Code0::License` can create and validate software licenses. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install the gem and add to the application's Gemfile by executing: |
| 8 | + |
| 9 | + $ bundle add code0-license |
| 10 | + |
| 11 | +If bundler is not being used to manage dependencies, install the gem by executing: |
| 12 | + |
| 13 | + $ gem install code0-license |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +To start using the gem, you need to have a public and private RSA key. |
| 18 | +If you don't have a key pair, you can generate one by running `bin/create_key` |
| 19 | + |
| 20 | +To generate a license, use the private key as encryption key: |
| 21 | + |
| 22 | +```ruby |
| 23 | +require "code0/license" |
| 24 | + |
| 25 | +# read private key from file |
| 26 | +private_key_file = File.read('license_key.key') |
| 27 | +private_key = OpenSSL::PKey::RSA.new(private_key_file) |
| 28 | + |
| 29 | +# set private key as encryption key |
| 30 | +Code0::License.encryption_key = private_key |
| 31 | + |
| 32 | +# create a license |
| 33 | +license = Code0::License.new( |
| 34 | + { |
| 35 | + 'licensee' => { 'company' => 'Code0' }, # content of licensee can be as you want, it just can't be empty |
| 36 | + 'start_date' => '2024-05-01', # when is the first date where this license is valid? |
| 37 | + 'end_date' => '2025-05-01', # until when is the license valid? |
| 38 | + 'restrictions' => {}, # content can be as you wish, can be used to semantically store some restrictions that are evaluated by your application |
| 39 | + 'options' => {}, # content can be as you wish, can be used to semantically provide some options of this license |
| 40 | + } |
| 41 | +) |
| 42 | + |
| 43 | +# export the license |
| 44 | +File.write('license.txt', Code0::License.export(license, 'CODE0')) |
| 45 | +``` |
| 46 | + |
| 47 | +To verify the license in your application, use the public key |
| 48 | + |
| 49 | +```ruby |
| 50 | +require "code0/license" |
| 51 | + |
| 52 | +# read public key from file |
| 53 | +public_key_file = File.read('license_key.pub') |
| 54 | +public_key = OpenSSL::PKey::RSA.new(public_key_file) |
| 55 | + |
| 56 | +# set public key as encryption key |
| 57 | +Code0::License.encryption_key = public_key |
| 58 | + |
| 59 | +# load the license |
| 60 | +license = Code0::License.load(File.read('license.txt')) |
| 61 | + |
| 62 | +# exit if license is valid or outside of the valid time |
| 63 | +exit unless license.valid? |
| 64 | +exit unless license.in_active_time? |
| 65 | + |
| 66 | +# for example, exit if users exceed licensed amount |
| 67 | +exit if User.count > license.restrictions['user_count'] |
| 68 | +``` |
0 commit comments