Module: Ditty::Services::Authentication

Defined in:
lib/ditty/services/authentication.rb

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



20
21
22
# File 'lib/ditty/services/authentication.rb', line 20

def [](key)
  config[key]
end

.configObject



48
49
50
# File 'lib/ditty/services/authentication.rb', line 48

def config
  @config ||= default.merge(::Ditty::Services::Settings.values(:authentication) || {})
end

.defaultObject



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

def default
  require 'ditty/models/identity'
  require 'ditty/controllers/auth_controller'
  {
    identity: {
      available: true,
      arguments: [
        {
          fields: [:username],
          model: ::Ditty::Identity,
          on_login: ::Ditty::AuthController,
          on_registration: ::Ditty::AuthController,
          locate_conditions: ->(req) { { username: req['username'] } }
        }
      ]
    }
  }
end

.providersObject



24
25
26
# File 'lib/ditty/services/authentication.rb', line 24

def providers
  config.compact.keys.select { |e| config[e][:available] && config[e][:enabled] != false }
end

.provides?(provider) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/ditty/services/authentication.rb', line 52

def provides?(provider)
  provider = provider.to_sym
  providers.include?(provider) && config[provider][:available] && config.dig(provider, :enabled) != false
end

.setupObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ditty/services/authentication.rb', line 28

def setup
  config.compact.each_key do |provider|
    ::Ditty::Services::Logger.debug "Loading authentication provider #{provider}"
    req = if config.dig(provider, :require)
      [config[provider][:require]]
    else
      ["omniauth/#{provider}", "omniauth-#{provider}"]
    end
    req.find do |e|
      require e
      config[provider][:available] = true
      true
    rescue LoadError
      ::Ditty::Services::Logger.warn "Could not load authentication provider #{provider} using #{e}"
      config[provider][:available] = false
      false
    end
  end
end