Class: Auth::DiscordAuthenticator

Inherits:
ManagedAuthenticator show all
Defined in:
lib/auth/discord_authenticator.rb

Defined Under Namespace

Classes: DiscordStrategy

Instance Method Summary collapse

Methods inherited from ManagedAuthenticator

#after_create_account, #always_update_user_email?, #can_connect_existing_user?, #can_revoke?, #description_for_auth_hash, #description_for_user, #find_user_by_email, #find_user_by_username, #is_managed?, #match_by_email, #match_by_username, #retrieve_avatar, #retrieve_profile, #revoke

Methods inherited from Authenticator

#after_create_account, #can_connect_existing_user?, #can_revoke?, #description_for_auth_hash, #description_for_user, #provides_groups?, #revoke

Instance Method Details

#after_authenticate(auth_token, existing_account: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/auth/discord_authenticator.rb', line 58

def after_authenticate(auth_token, existing_account: nil)
  allowed_guild_ids = SiteSetting.discord_trusted_guilds.split("|")

  if allowed_guild_ids.length > 0
    user_guild_ids = auth_token.extra[:raw_info][:guilds].map { |g| g["id"] }
    if (user_guild_ids & allowed_guild_ids).empty? # User is not in any allowed guilds
      return(
        Auth::Result.new.tap do |auth_result|
          auth_result.failed = true
          auth_result.failed_reason = I18n.t("discord.not_in_allowed_guild")
        end
      )
    end
  end

  super
end

#enabled?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/auth/discord_authenticator.rb', line 44

def enabled?
  SiteSetting.enable_discord_logins?
end

#nameObject



40
41
42
# File 'lib/auth/discord_authenticator.rb', line 40

def name
  "discord"
end

#primary_email_verified?(auth_token) ⇒ Boolean

the ‘info` block above only picks the email from Discord API if it’s verified

Returns:

  • (Boolean)


77
78
79
# File 'lib/auth/discord_authenticator.rb', line 77

def primary_email_verified?(auth_token)
  true
end

#register_middleware(omniauth) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/auth/discord_authenticator.rb', line 48

def register_middleware(omniauth)
  omniauth.provider DiscordStrategy,
                    setup:
                      lambda { |env|
                        strategy = env["omniauth.strategy"]
                        strategy.options[:client_id] = SiteSetting.discord_client_id
                        strategy.options[:client_secret] = SiteSetting.discord_secret
                      }
end