lib/gitlab/license.rb in gitlab-license-0.0.1 vs lib/gitlab/license.rb in gitlab-license-0.0.2
- old
+ new
@@ -6,28 +6,44 @@
require "gitlab/license/version"
require "gitlab/license/encryptor"
module Gitlab
class License
+ class Error < StandardError; end
+ class ImportError < Error; end
+ class ValidationError < Error; end
+
class << self
- attr_accessor :encryption_key
+ attr_reader :encryption_key
@encryption_key = nil
- def encryptor
- unless self.encryption_key && self.encryption_key.is_a?(OpenSSL::PKey::RSA)
- raise "No RSA encryption key provided."
+ def encryption_key=(key)
+ if key && !key.is_a?(OpenSSL::PKey::RSA)
+ raise ArgumentError, "No RSA encryption key provided."
end
- Encryptor.new(self.encryption_key)
+ @encryption_key = key
end
- def import(data)
- from_json(encryptor.decrypt(data))
+ def encryptor
+ @encryptor ||= Encryptor.new(self.encryption_key)
end
- def from_json(license_json)
- new(JSON.parse(license_json))
+ def import(data)
+ begin
+ license_json = encryptor.decrypt(data)
+ rescue Encryptor::Error
+ raise ImportError, "License data could not be decrypted."
+ end
+
+ begin
+ attributes = JSON.parse(license_json)
+ rescue JSON::ParseError
+ raise ImportError, "License data is invalid JSON."
+ end
+
+ new(attributes)
end
end
attr_reader :version
attr_accessor :licensee, :issued_at, :expires_at
@@ -49,10 +65,10 @@
true
end
def validate!
- raise "License is invalid" unless valid?
+ raise ValidationError, "License is invalid" unless valid?
end
def will_expire?
self.expires_at
end