File: multi_password.rb

package info (click to toggle)
ruby-omniauth-multipassword 2.0.0~rc1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 160 kB
  • sloc: ruby: 243; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require 'omniauth'
require 'omniauth/multipassword/base'

module OmniAuth
  module Strategies
    class MultiPassword
      include OmniAuth::Strategy
      include OmniAuth::MultiPassword::Base

      def initialize(app, *args, &block)
        super(app, *args) do
          # Do pass an empty block, as otherwise the captured block would be
          # passed to `super`, but this needs to be evaluate inside this
          # middleware, not omniauth's Rack builder instance.
        end

        if block.arity.zero?
          instance_eval(&block)
        else
          yield self
        end
      end

      def options
        yield @options if block_given?
        @options
      end

      def authenticator(klass, *args, &block)
        unless klass.is_a?(Class)
          begin
            klass = OmniAuth::Strategies.const_get(OmniAuth::Utils.camelize(klass.to_s).to_s)
          rescue NameError
            raise LoadError.new("Could not find matching strategy for #{klass.inspect}." \
                                "You may need to install an additional gem (such as omniauth-#{klass}).")
          end
        end

        args << block if block
        @authenticators ||= []
        @authenticators  << [klass, args]
      end

      def callback_phase
        username = request[username_id].to_s
        password = request[password_id].to_s
        if authenticate(username, password)
          super
        else
          fail!(:invalid_credentials)
        end
      end

      def authenticate(username, password)
        @authenticators.each do |auth|
          begin
            @authenticator = auth[0].new @app, *auth[1]
            @authenticator.init_authenticator(@request, @env, username)
            return true if @authenticator.authenticate(username, password)
          rescue Error => e
            OmniAuth.logger.warn "OmniAuth ERR >>> #{e}"
          end
          @authenticator = nil
        end
        false
      end

      def name
        return @authenticator.name if @authenticator

        super
      end

      info do
        info = @authenticator.info if @authenticator
        info = {} unless info.is_a?(Hash)
        info
      end
    end
  end
end