Sha256: 79d1eacdd938d688df9e25ac7dbb84d4602e788f2e643d3a85d798efe8a18a34

Contents?: true

Size: 971 Bytes

Versions: 1

Compression:

Stored size: 971 Bytes

Contents

# frozen_string_literal: true

class GoogleOAuthCli
  class Server
    def initialize(port:, state:)
      @server = TCPServer.new port
      @state = state
    end

    def start
      loop do
        connection = server.accept
        request = connection.gets
        data = handle_request(request)
        connection.write <<~DOC
          HTTP/1.1 200 OK
          Content-Type: text/plain; charset=UTF-8

          OAuth request received. You can close this window now.
        DOC
        connection.close
        if data
          server.close
          return data
        end
      end
      server.close
    end

    private

    attr_reader :server, :state

    def handle_request(request)
      _, full_path = request.split
      return if URI(full_path).path != "/authorize"

      params = CGI.parse(URI.parse(full_path).query)
      raise(Error, "Invalid oauth request received") if @state != params["state"][0]

      params["code"][0]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google-oauth-cli-0.1.0 lib/google-oauth-cli/server.rb