Skip to content

HTTP Basic Authentication API examples for "Pages" section #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DanRathbun opened this issue Jun 19, 2018 · 2 comments
Open

HTTP Basic Authentication API examples for "Pages" section #139

DanRathbun opened this issue Jun 19, 2018 · 2 comments

Comments

@DanRathbun
Copy link

I give permission to use code posted here ...

https://forums.sketchup.com/t/htmldialog-make-a-get-request-with-authentication-header/70220/5

... as the basis for HTTP Basic Authentication API example.

It need not be exact as shown (a subclass that works in both WebDialog and HtmlDialog,) but can be separated to show how it was done with Ruby standard library, and then using the new SketchUp API classes.

(IE, it may be a bit confusing to show the compound meta programming of a cross-version class definition for doc examples.)

@thomthom thomthom transferred this issue from SketchUp/ruby-api-stubs Nov 1, 2018
@DanRathbun
Copy link
Author

DanRathbun commented Aug 28, 2021

We also have an examples / tutorials issue open ...
Issue 1 : Add Sketchup::Http::Request and Response Tutorial and Example

I have taken the old snippets (from the above linked forum thread,) and separated them into 2 subclass definitions (rather than using conditional metaprogramming which could be confusing,) ... each subclass for < SU2017 (UI::WebDialog) and SU2017+ (UI::HtmlDialog).

This is for a subclass to the UI::HtmlDialog API class.
(Refer to the API documentation for this superclass for examples and use of other methods.)

# encoding: UTF-8

module SomeAuthor  # <---<<< Change to YOUR unique top level namespace name
  module SomePlugin # <---<<< Change to YOUR extension submodule name

    # A subclass of the SketchUp API's UI::HtmlDialog class that uses
    # the SketchUp Ruby API's HTTP classes to perform authentication.
    #
    class AuthenticationHtmlDialog < UI::HtmlDialog

      # (IETF) RFC7617 : The 'Basic' HTTP Authentication Scheme
      # https://tools.ietf.org/html/rfc7617

      # Tell Ruby to load a couple of the standard libraries:
      require 'uri'
      require 'base64'

      # This method gets a page that requires authentication and then
      # displays the page's body in the dialog.
      def get_page( url, username, password, **form )

        if username.include?(':')
          fail(ArgumentError, "username cannot contain a colon!",caller)
        end

        uri = URI(url)
        creds = "#{username}:#{password}"

        @req = Sketchup::Http::Request.new(uri.to_s, Sketchup::Http::GET)
        @req.headers= { 'Authorization' => 'Basic '<< Base64.encode64(creds) }
        @req.body= URI.encode_www_form(form)

        @req.start do |request, response|
          self.set_html(response.body)
          self.visible? ? self.bring_to_front : self.show
        end

      end ### get_page()

    end # AuthenticationHtmlDialog subclass

  end # SketchUp extension submodule
end # namespace module 

@DanRathbun
Copy link
Author

DanRathbun commented Aug 28, 2021

This (for what it's worth) this is for a subclass to the deprecated UI::WebDialog API class.
(Refer to the API documentation for this superclass for examples and use of other methods.)

# encoding: UTF-8

module SomeAuthor  # <---<<< Change to YOUR unique top level namespace name
  module SomePlugin # <---<<< Change to YOUR extension submodule name

    # A subclass of the SketchUp API's UI::WebDialog class that uses the
    # Standard Ruby Library Net::HTTP classes to perform authentication.
    #
    class AuthenticationWebDialog < UI::WebDialog

      # (IETF) RFC7617 : The 'Basic' HTTP Authentication Scheme
      # https://tools.ietf.org/html/rfc7617
   
      # Tell Ruby to load the Net::HTTP library:
      require 'net/http' # loads 'uri'

      # This method gets a page that requires authentication and then
      # displays the page's body in the dialog.
      def get_page( url, username, password, **form )

        if username.include?(':')
          fail(ArgumentError, "username cannot contain a colon!",caller)
        end

        uri = URI(url)
        req = Net::HTTP::Get.new(uri)
        req.basic_auth(username, password)
        req.set_form_data(form)

        UI.start_timer(0,false) do
          @res = Net::HTTP.start(uri.hostname, uri.port) {|http|
            http.request(req) do |response|
              self.set_html(response.body)
              self.visible? ? self.bring_to_front : self.show
            end
          }
        end

      end ### get_page()

    end # AuthenticationWebDialog subclass

  end # SketchUp extension submodule
end # namespace module 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants