Welcome to mirror list, hosted at ThFree Co, Russian Federation.

token_authenticatable.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 66db4bd92de75b6bda58f858c9b8ecf4ced4ed40 (plain)
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
# frozen_string_literal: true

module TokenAuthenticatable
  extend ActiveSupport::Concern

  private

  class_methods do
    private # rubocop:disable Lint/UselessAccessModifier

    def add_authentication_token_field(token_field, options = {})
      @token_fields = [] unless @token_fields

      if @token_fields.include?(token_field)
        raise ArgumentError.new("#{token_field} already configured via add_authentication_token_field")
      end

      @token_fields << token_field

      attr_accessor :cleartext_tokens

      strategy = if options[:digest]
                   TokenAuthenticatableStrategies::Digest.new(self, token_field, options)
                 else
                   TokenAuthenticatableStrategies::Insecure.new(self, token_field, options)
                 end

      define_singleton_method("find_by_#{token_field}") do |token|
        strategy.find_token_authenticatable(token)
      end

      define_method(token_field) do
        strategy.get_token(self)
      end

      define_method("set_#{token_field}") do |token|
        strategy.set_token(self, token)
      end

      define_method("ensure_#{token_field}") do
        strategy.ensure_token(self)
      end

      # Returns a token, but only saves when the database is in read & write mode
      define_method("ensure_#{token_field}!") do
        strategy.ensure_token!(self)
      end

      # Resets the token, but only saves when the database is in read & write mode
      define_method("reset_#{token_field}!") do
        strategy.reset_token!(self)
      end
    end
  end
end