Sha256: f01f6605992cb13a75cf35f7becccd06270d3ebb0c6336646efbc5281139236e

Contents?: true

Size: 1.03 KB

Versions: 22

Compression:

Stored size: 1.03 KB

Contents

class ApiClient < ActiveRecord::Base
  InsufficientPermission = Class.new(StandardError)

  belongs_to :creator, class_name: 'User'
  belongs_to :stack

  validates :creator, :name, presence: true

  serialize :permissions, Array
  PERMISSIONS = %w(
    read:stack
    write:stack
    deploy:stack
    lock:stack
    read:hook
    write:hook
  ).freeze
  validates :permissions, subset: {of: PERMISSIONS}

  class << self
    def authenticate(token)
      find_by_id(message_verifier.verify(token).to_i)
    rescue Shipit::SimpleMessageVerifier::InvalidSignature
    end

    def message_verifier
      @message_verifier ||= Shipit::SimpleMessageVerifier.new(Shipit.api_clients_secret)
    end
  end

  def authentication_token
    self.class.message_verifier.generate(id)
  end

  def check_permissions!(operation, scope)
    required_permission = "#{operation}:#{scope}"
    unless permissions.include?(required_permission)
      raise InsufficientPermission, "This operation requires the `#{required_permission}` permission"
    end
    true
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
shipit-engine-0.5.2 app/models/api_client.rb
shipit-engine-0.5.1 app/models/api_client.rb
shipit-engine-0.5.0 app/models/api_client.rb
shipit-engine-0.4.10 app/models/api_client.rb
shipit-engine-0.4.9 app/models/api_client.rb
shipit-engine-0.4.8 app/models/api_client.rb
shipit-engine-0.4.7 app/models/api_client.rb
shipit-engine-0.4.6 app/models/api_client.rb
shipit-engine-0.4.5 app/models/api_client.rb
shipit-engine-0.4.4 app/models/api_client.rb
shipit-engine-0.4.3 app/models/api_client.rb
shipit-engine-0.4.2 app/models/api_client.rb
shipit-engine-0.4.1 app/models/api_client.rb
shipit-engine-0.4.0 app/models/api_client.rb
shipit-engine-0.3.1 app/models/api_client.rb
shipit-engine-0.3.0 app/models/api_client.rb
shipit-engine-0.2.3 app/models/api_client.rb
shipit-engine-0.2.2 app/models/api_client.rb
shipit-engine-0.2.1 app/models/api_client.rb
shipit-engine-0.2.0 app/models/api_client.rb