-
Notifications
You must be signed in to change notification settings - Fork 10
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
Comments
We also have an examples / tutorials issue open ... 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 ( This is for a subclass to the # 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 |
This (for what it's worth) this is for a subclass to the deprecated # 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 |
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.)
The text was updated successfully, but these errors were encountered: