Sha256: a4015a546ba3b45bb0c047345c29494b238477257d541f1ee051341f0e226234

Contents?: true

Size: 1.98 KB

Versions: 37

Compression:

Stored size: 1.98 KB

Contents

module DeployGate
  class BrowserLogin
    DEFAULT_PORT = 64126
    LOGIN_URL = "#{DeployGate::API::V1::Base::BASE_URL}/cli/login"
    CREDENTIAL_URL = "#{DeployGate::API::V1::Base::BASE_URL}/cli/credential"
    NOTIFY_URL = "#{DeployGate::API::V1::Base::BASE_URL}/cli/notify"

    # @param [Fixnum] port
    def initialize(port = nil)
      @port = port || DEFAULT_PORT
      @login_uri = URI(LOGIN_URL)
      @login_uri.query = {port: @port, client: 'dg'}.to_query

      @credential_uri = URI(CREDENTIAL_URL)
      @notify_uri = URI(NOTIFY_URL)
    end

    def start
      server = WEBrick::HTTPServer.new(
          :Port => @port,
          :BindAddress =>"localhost",
          :Logger => WEBrick::Log.new(STDOUT, 0),
          :AccessLog => []
      )

      begin
        Signal.trap("INT") { server.shutdown }

        server.mount_proc '/' do |req, res|
          res.status = WEBrick::HTTPStatus::RC_NO_CONTENT

          cancel = req.query['cancel']
          notify_key = req.query['key']

          unless cancel
            credential = get_credential(notify_key)
            DeployGate::Session.save(credential['name'], credential['token'])
            notify_finish(notify_key)

            DeployGate::Commands::Login.login_success()
          end

          server.stop
        end

        Launchy.open(@login_uri.to_s)
        server.start
      ensure
        server.shutdown
      end
    end

    private

    # @param [String] notify_key
    # @return [Hash]
    def get_credential(notify_key)
      res = HTTPClient.new(:agent_name => "dg/#{DeployGate::VERSION}").get(@credential_uri.to_s, {key: notify_key})
      JSON.parse(res.body)
    end

    # @param [String] notify_key
    def notify_finish(notify_key)
      notify_post(notify_key, 'credential_saved')
      notify_post(notify_key, 'finished')
    end

    def notify_post(notify_key, action)
      HTTPClient.new(:agent_name => "dg/#{DeployGate::VERSION}").post(@notify_uri.to_s, {key: notify_key, command_action: action})
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
deploygate-0.9.1 lib/deploygate/browser_login.rb
deploygate-0.9.0 lib/deploygate/browser_login.rb
deploygate-0.8.6 lib/deploygate/browser_login.rb
deploygate-0.8.5 lib/deploygate/browser_login.rb
deploygate-0.8.4 lib/deploygate/browser_login.rb
deploygate-0.8.3 lib/deploygate/browser_login.rb
deploygate-0.8.2 lib/deploygate/browser_login.rb
deploygate-0.8.1 lib/deploygate/browser_login.rb
deploygate-0.8.0 lib/deploygate/browser_login.rb
deploygate-0.7.0 lib/deploygate/browser_login.rb
deploygate-0.6.8 lib/deploygate/browser_login.rb
deploygate-0.6.7 lib/deploygate/browser_login.rb
deploygate-0.6.6 lib/deploygate/browser_login.rb
deploygate-0.6.5 lib/deploygate/browser_login.rb
deploygate-0.6.4 lib/deploygate/browser_login.rb
deploygate-0.6.3 lib/deploygate/browser_login.rb
deploygate-0.6.2 lib/deploygate/browser_login.rb
deploygate-0.6.1 lib/deploygate/browser_login.rb
deploygate-0.6.0 lib/deploygate/browser_login.rb
deploygate-0.5.6 lib/deploygate/browser_login.rb