Class: Wikidotrb::Module::HTTPAuthentication
- Inherits:
-
Object
- Object
- Wikidotrb::Module::HTTPAuthentication
- Defined in:
- lib/wikidotrb/module/auth.rb
Class Method Summary collapse
-
.login(client, username, password)
Login with username and password.
-
.logout(client)
Logout.
Class Method Details
.login(client, username, password)
Login with username and password
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wikidotrb/module/auth.rb', line 16 def self.login(client, username, password) url = "https://www.wikidot.com/default--flow/login__LoginPopupScreen" # Create login request data request_data = { "login" => username, "password" => password, "action" => "Login2Action", "event" => "login" } response = HTTPX.post( url, headers: client.amc_client.header.get_header, form: request_data, timeout: { operation: 20 } ) # Handle error response if response.is_a?(HTTPX::ErrorResponse) raise Wikidotrb::Common::Exceptions::SessionCreateException, "Login attempt failed due to network error: #{response.error}" end # Check status code unless response.status == 200 raise Wikidotrb::Common::Exceptions::SessionCreateException, "Login attempt failed due to HTTP status code: #{response.status}" end # Check response body if response.body.to_s.include?("The login and password do not match") raise Wikidotrb::Common::Exceptions::SessionCreateException, "Login attempt failed due to invalid username or password" end # Check and parse cookies = response.headers["set-cookie"] # Raise error if `set-cookie` doesn't exist or is `nil` raise Wikidotrb::Common::Exceptions::SessionCreateException, "Login attempt failed due to invalid cookies" if .nil? || .empty? # Get session cookie = .match(/WIKIDOT_SESSION_ID=([^;]+)/) raise Wikidotrb::Common::Exceptions::SessionCreateException, "Login attempt failed due to invalid cookies" unless # Set session cookie client.amc_client.header.("WIKIDOT_SESSION_ID", [1]) end |
.logout(client)
Logout
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wikidotrb/module/auth.rb', line 69 def self.logout(client) begin client.amc_client.request( [{ "action" => "Login2Action", "event" => "logout", "moduleName" => "Empty" }] ) rescue StandardError # Ignore exceptions and continue logout process end client.amc_client.header.("WIKIDOT_SESSION_ID") end |