Class: Wikidotrb::Module::HTTPAuthentication

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidotrb/module/auth.rb

Class Method Summary collapse

Class Method Details

.login(client, username, password)

Login with username and password

Parameters:

  • client (Client)

    The client

  • username (String)

    Username

  • password (String)

    Password

Raises:

  • (SessionCreateException)

    When session creation fails



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.(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
  set_cookie_header = 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 set_cookie_header.nil? || set_cookie_header.empty?

  # Get session cookie
  session_cookie = set_cookie_header.match(/WIKIDOT_SESSION_ID=([^;]+)/)

  raise Wikidotrb::Common::Exceptions::SessionCreateException, "Login attempt failed due to invalid cookies" unless session_cookie

  # Set session cookie
  client.amc_client.header.set_cookie("WIKIDOT_SESSION_ID", session_cookie[1])
end

.logout(client)

Logout

Parameters:

  • client (Client)

    The client



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.delete_cookie("WIKIDOT_SESSION_ID")
end