Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Taucher2003 committed May 1, 2024
0 parents commit f9376af
Show file tree
Hide file tree
Showing 26 changed files with 917 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint & Test

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.2.2'

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Run the default task
run: bundle exec rake
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Push Gem

on:
push:
tags:
- v*

jobs:
push:
runs-on: ubuntu-latest

permissions:
contents: write
id-token: write

steps:
# Set up
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2.2'

# Release
- uses: rubygems/release-gem@v1
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
.idea/

license_key.key
license_key.pub

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
36 changes: 36 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require:
- rubocop-rake
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.0.0
NewCops: enable

Gemspec/DevelopmentDependencies:
EnforcedStyle: gemspec

Metrics/AbcSize:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120

RSpec/ExampleLength:
Enabled: false
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.2.2
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in code0-license.gemspec
gemspec
76 changes: 76 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
PATH
remote: .
specs:
code0-license (0.1.0.pre.release.testing)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
diff-lcs (1.5.1)
json (2.7.2)
language_server-protocol (3.17.0.3)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
racc (1.7.3)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.0)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.63.4)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.29.1)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)

PLATFORMS
x86_64-linux

DEPENDENCIES
code0-license!
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)
rubocop-rake (~> 0.6)
rubocop-rspec (~> 2.29)

BUNDLED WITH
2.4.12
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Niklas van Schrick

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Code0::License

`Code0::License` can create and validate software licenses.

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add code0-license

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install code0-license

## Usage

To start using the gem, you need to have a public and private RSA key.
If you don't have a key pair, you can generate one by running `bin/create_key`

To generate a license, use the private key as encryption key:

```ruby
require "code0/license"

# read private key from file
private_key_file = File.read('license_key.key')
private_key = OpenSSL::PKey::RSA.new(private_key_file)

# set private key as encryption key
Code0::License.encryption_key = private_key

# create a license
license = Code0::License.new(
{
'licensee' => { 'company' => 'Code0' }, # content of licensee can be as you want, it just can't be empty
'start_date' => '2024-05-01', # when is the first date where this license is valid?
'end_date' => '2025-05-01', # until when is the license valid?
'restrictions' => {}, # content can be as you wish, can be used to semantically store some restrictions that are evaluated by your application
'options' => {}, # content can be as you wish, can be used to semantically provide some options of this license
}
)

# export the license
File.write('license.txt', Code0::License.export(license, 'CODE0'))
```

To verify the license in your application, use the public key

```ruby
require "code0/license"

# read public key from file
public_key_file = File.read('license_key.pub')
public_key = OpenSSL::PKey::RSA.new(public_key_file)

# set public key as encryption key
Code0::License.encryption_key = public_key

# load the license
license = Code0::License.load(File.read('license.txt'))

# exit if license is valid or outside of the valid time
exit unless license.valid?
exit unless license.in_active_time?

# for example, exit if users exceed licensed amount
exit if User.count > license.restrictions['user_count']
```
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[spec rubocop]
17 changes: 17 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "code0/license"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

key_file = File.read("license_key.#{ARGV.shift}")
key = OpenSSL::PKey::RSA.new(key_file)

Code0::License.encryption_key = key

require "irb"
ENV["IRB_USE_AUTOCOMPLETE"] = "false"
IRB.start(__FILE__)
16 changes: 16 additions & 0 deletions bin/create_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "code0/license"

# Generate a key pair. You should do this only once.
key_pair = OpenSSL::PKey::RSA.generate(4096)

# Write it to a file to use in the license generation application.
File.write("license_key.key", key_pair.to_pem)

# Extract the public key.
public_key = key_pair.public_key
# Write it to a file to ship along with the main application.
File.write("license_key.pub", public_key.to_pem)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
37 changes: 37 additions & 0 deletions code0-license.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative "lib/code0/license/version"

Gem::Specification.new do |spec|
spec.name = "code0-license"
spec.version = Code0::License::VERSION
spec.authors = ["Niklas van Schrick"]
spec.email = ["[email protected]"]

spec.summary = "code0-license is used to validate software licenses"
spec.homepage = "https://github.com/code0-tech/code0-license"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.0.0"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ spec/ .git .github])
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rubocop", "~> 1.21"
spec.add_development_dependency "rubocop-rake", "~> 0.6"
spec.add_development_dependency "rubocop-rspec", "~> 2.29"
spec.metadata["rubygems_mfa_required"] = "true"
end
Loading

0 comments on commit f9376af

Please sign in to comment.