Sha256: 3086d8530c27ab0a7ea80e395fdbfdc45662dc55c3dc1a22ce7287d55c2b183f

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

module Trinidad
  module Sandbox
    module Helpers
      module Auth
        require 'sinatra/authorization'
        include Sinatra::Authorization

        def authorize(user, pass)
          user == sandbox_username && pass == sandbox_password
        end

        def authorized?
          sandbox_username && sandbox_password ? request.env['REMOTE_USER'] && !request.env['REMOTE_USER'].empty? : true
        end

        def authorization_realm; "Trinidad's sandbox"; end

        def basic_auth_required?(request)
          !token_required?(request)
        end

        def token_required?(request)
          request.path == '/deploy'
        end

        def token_required(params, realm = authorization_realm)
          return if authorized_by_token?(params)
          response["WWW-Authenticate"] = %(Basic realm="#{realm}")
          throw :halt, [401, "Deploy Token Required"]
        end

        def authorized_by_token?(params)
          deploy_token.nil? || params[:deploy_token] == deploy_token
        end

        private
        def sandbox_username
          @sandbox_username ||= $servlet_context.getAttribute('sandbox_username')
        end

        def sandbox_password
          @sandbox_password ||= $servlet_context.getAttribute('sandbox_password')
        end

        def deploy_token
          @deploy_token ||= $servlet_context.get_attribute('deploy_token')
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trinidad_sandbox_extension-1.0.2 lib/trinidad_sandbox_extension/app/helpers/auth.rb
trinidad_sandbox_extension-1.0.1 lib/trinidad_sandbox_extension/app/helpers/auth.rb
trinidad_sandbox_extension-1.0.0 lib/trinidad_sandbox_extension/app/helpers/auth.rb