encrypted.rb 3.0 KB
Newer Older
1 2 3 4
# frozen_string_literal: true

module TokenAuthenticatableStrategies
  class Encrypted < Base
5 6 7 8
    def initialize(*)
      super

      if migrating? && fallback?
9
        raise ArgumentError, '`fallback` and `migrating` options are not compatible!'
10 11 12
      end
    end

13
    def find_token_authenticatable(token, unscoped = false)
14
      return if token.blank?
15 16 17 18

      if fully_encrypted?
        return find_by_encrypted_token(token, unscoped)
      end
19

20 21 22 23
      if fallback?
        find_by_encrypted_token(token, unscoped) ||
          find_by_plaintext_token(token, unscoped)
      elsif migrating?
24
        find_by_plaintext_token(token, unscoped)
25
      else
26
        raise ArgumentError, 'Unknown encryption phase!'
27
      end
28 29 30 31 32 33 34
    end

    def ensure_token(instance)
      # TODO, tech debt, because some specs are testing migrations, but are still
      # using factory bot to create resources, it might happen that a database
      # schema does not have "#{token_name}_encrypted" field yet, however a bunch
      # of models call `ensure_#{token_name}` in `before_save`.
35 36 37
      #
      # In that case we are using insecure strategy, but this should only happen
      # in tests, because otherwise `encrypted_field` is going to exist.
38 39 40
      #
      # Another use case is when we are caching resources / columns, like we do
      # in case of ApplicationSetting.
41 42 43

      return super if instance.has_attribute?(encrypted_field)

44 45
      if fully_encrypted?
        raise ArgumentError, 'Using encrypted strategy when encrypted field is missing!'
46
      else
47
        insecure_strategy.ensure_token(instance)
48
      end
49 50 51
    end

    def get_token(instance)
52
      return insecure_strategy.get_token(instance) if migrating?
53

54 55
      encrypted_token = instance.read_attribute(encrypted_field)
      token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
56

57
      token || (insecure_strategy.get_token(instance) if fallback?)
58 59 60
    end

    def set_token(instance, token)
61
      raise ArgumentError unless token.present?
62

63
      instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
64
      instance[token_field] = token if migrating?
65
      instance[token_field] = nil if fallback?
66
      token
67 68
    end

69 70 71 72
    def fully_encrypted?
      !migrating? && !fallback?
    end

73 74
    protected

75 76 77 78 79 80 81 82 83 84 85
    def find_by_plaintext_token(token, unscoped)
      insecure_strategy.find_token_authenticatable(token, unscoped)
    end

    def find_by_encrypted_token(token, unscoped)
      encrypted_value = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
      relation(unscoped).find_by(encrypted_field => encrypted_value)
    end

    def insecure_strategy
      @insecure_strategy ||= TokenAuthenticatableStrategies::Insecure
86
        .new(klass, token_field, options)
87 88 89
    end

    def token_set?(instance)
90
      raw_token = instance.read_attribute(encrypted_field)
91 92 93 94

      unless fully_encrypted?
        raw_token ||= insecure_strategy.get_token(instance)
      end
95

96
      raw_token.present?
97 98
    end

99 100
    def encrypted_field
      @encrypted_field ||= "#{@token_field}_encrypted"
101 102 103
    end
  end
end