Class: Itch::Auth

Inherits:
Object
  • Object
show all
Includes:
SimpleInspect
Defined in:
lib/itch/auth.rb

Overview

Authentication flow handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SimpleInspect

#inspect, #pretty_print_instance_variables

Constructor Details

#initialize(agent, username: nil, password: nil, cookie_path: nil) ⇒ Auth

Returns a new instance of Auth.



12
13
14
15
16
17
18
# File 'lib/itch/auth.rb', line 12

def initialize(agent, username: nil, password: nil, cookie_path: nil)
  @agent = agent
  @cookie_path = cookie_path
  @username = username
  @password = password
  @totp = -> {}
end

Instance Attribute Details

#password=(value) ⇒ Object

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



10
11
12
# File 'lib/itch/auth.rb', line 10

def password=(value)
  @password = value
end

#totp=(value) ⇒ Object (writeonly)

Sets the attribute totp

Parameters:

  • value

    the value to set the attribute totp to.



10
11
12
# File 'lib/itch/auth.rb', line 10

def totp=(value)
  @totp = value
end

#username=(value) ⇒ Object

Sets the attribute username

Parameters:

  • value

    the value to set the attribute username to.



10
11
12
# File 'lib/itch/auth.rb', line 10

def username=(value)
  @username = value
end

Instance Method Details

#logged_in?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/itch/auth.rb', line 20

def logged_in?
  @agent.get(Itch::URL::DASHBOARD).uri.to_s == Itch::URL::DASHBOARD
end

#loginObject

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/itch/auth.rb', line 24

def 
  page = @agent.get(Itch::URL::LOGIN)
  return unless page.code == "200"
  raise AuthError, "Email and password are required for login" if @username.nil? || @password.nil?

  page = (page) if page_is_login?(page)
  submit_2fa(page) if page_is_2fa?(page)

  save_cookies

  logged_in?
end

#page_is_2fa?(page) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/itch/auth.rb', line 41

def page_is_2fa?(page)
  page.uri.to_s.start_with?(Itch::URL::TOTP_FRAGMENT)
end

#page_is_login?(page) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/itch/auth.rb', line 37

def page_is_login?(page)
  page.uri.to_s == Itch::URL::LOGIN
end