Class: YiffSpace::Auth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yiffspace/auth/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yiffspace/auth/client.rb', line 16

def initialize(name)
  @name                    = name.to_sym
  @scopes                  = %i[openid offline_access profile roles identities custom_data]
  @permissions             = []
  @redirect_uri            = "http://127.0.0.1:3000/auth/cb"
  @server_url              = "https://auth.yiff.space"
  @auth_session_key        = :"yiffspace_auth_#{name}"
  @user_session_key        = :"yiffspace_user_#{name}"
  @update_discord_images   = true
  @permissions_separator   = ":"
  @logto_webhook_secret    = nil
end

Instance Attribute Details

#auth_session_keyObject

Returns the value of attribute auth_session_key.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def auth_session_key
  @auth_session_key
end

#client_idObject

Returns the value of attribute client_id.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def client_secret
  @client_secret
end

#logto_webhook_secretObject

Returns the value of attribute logto_webhook_secret.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def logto_webhook_secret
  @logto_webhook_secret
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/yiffspace/auth/client.rb', line 14

def name
  @name
end

#permissionsObject

Returns the value of attribute permissions.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def permissions
  @permissions
end

#permissions_separatorObject

Returns the value of attribute permissions_separator.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def permissions_separator
  @permissions_separator
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def redirect_uri
  @redirect_uri
end

#resourceObject

Returns the value of attribute resource.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def resource
  @resource
end

#scopesObject

Returns the value of attribute scopes.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def scopes
  @scopes
end

#server_urlObject

Returns the value of attribute server_url.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def server_url
  @server_url
end

#update_discord_imagesObject

Returns the value of attribute update_discord_images.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def update_discord_images
  @update_discord_images
end

#user_session_keyObject

Returns the value of attribute user_session_key.



9
10
11
# File 'lib/yiffspace/auth/client.rb', line 9

def user_session_key
  @user_session_key
end

Instance Method Details

#fetch_discord_user(id) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/yiffspace/auth/client.rb', line 49

def fetch_discord_user(id)
  response = HTTParty.get("https://discord.com/api/v10/users/#{id}", { headers: { "Authorization" => "Bot #{YiffSpace.config.discord_bot_token}" } })
  return nil if response.code == 404
  raise("failed to fetch discord user: #{response.code} #{response.message}\n#{response.parsed_response.inspect}") if response.code != 200

  DiscordInfo.new(response.parsed_response)
end

#logto(controller) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yiffspace/auth/client.rb', line 29

def logto(controller)
  LogtoClient.new(
    config:   LogtoClient::Config.new(
      endpoint:   server_url,
      app_id:     client_id,
      app_secret: client_secret,
      scopes:     scopes + permissions,
      resources:  [resource].compact,
    ),
    # Allow the client to redirect to other hosts (i.e. your Logto tenant)
    navigate: ->(uri) { controller.redirect_to(uri, allow_other_host: true) },
    # Controller has access to the session object
    storage:  LogtoClient::NamedSessionStorage.new("yiffspace", controller.session, app_id: @name),
  )
end

#logto_managementObject



45
46
47
# File 'lib/yiffspace/auth/client.rb', line 45

def logto_management
  @logto_management ||= LogtoManagementClient.new(self)
end