Sha256: 8afe6934473cd09bd20592ee9b3529c431033da104036b033c4c46022efcb66b

Contents?: true

Size: 1.13 KB

Versions: 15

Compression:

Stored size: 1.13 KB

Contents

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

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

    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
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
shipit-engine-0.31.0 app/models/shipit/api_client.rb
shipit-engine-0.30.0 app/models/shipit/api_client.rb
shipit-engine-0.29.0 app/models/shipit/api_client.rb
shipit-engine-0.28.1 app/models/shipit/api_client.rb
shipit-engine-0.28.0 app/models/shipit/api_client.rb
shipit-engine-0.27.1 app/models/shipit/api_client.rb
shipit-engine-0.27.0 app/models/shipit/api_client.rb
shipit-engine-0.26.0 app/models/shipit/api_client.rb
shipit-engine-0.25.1 app/models/shipit/api_client.rb
shipit-engine-0.25.0 app/models/shipit/api_client.rb
shipit-engine-0.24.0 app/models/shipit/api_client.rb
shipit-engine-0.23.1 app/models/shipit/api_client.rb
shipit-engine-0.23.0 app/models/shipit/api_client.rb
shipit-engine-0.22.0 app/models/shipit/api_client.rb
shipit-engine-0.21.0 app/models/shipit/api_client.rb