Module: ApiWarden::Helpers

Defined in:
lib/api_warden/helpers.rb,
lib/api_warden/helpers/accessable.rb,
lib/api_warden/helpers/refreshable.rb

Defined Under Namespace

Modules: Accessable, Refreshable

Class Method Summary collapse

Class Method Details

.define_helpers(scope) ⇒ Object



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
84
85
# File 'lib/api_warden/helpers.rb', line 8

def self.define_helpers(scope)
  name = scope.name

  class_eval <<-METHODS, __FILE__, __LINE__ + 1
    def ward_by_#{name}
      ward_by("#{name}")
    end

    def ward_by_#{name}!
      ward_by!("#{name}")
    end

    def current_#{name}_authentication
      current_authentication_for("#{name}")
    end

    def current_#{name}_id
      current_#{name}_authentication.id
    end

    def current_#{name}_value_for_access_token
      current_#{name}_authentication.value_for_access_token
    end        

    def #{name}_signed_in?
      current_#{name}_authentication.authenticated?
    end

    def generate_access_token_for_#{name}(id, *args)
      generate_access_token_for("#{name}", id, *args)
    end
  METHODS

  if scope.load_owner.respond_to?(:call)
    class_eval <<-METHODS, __FILE__, __LINE__ + 1
      def current_#{name}
        unless @current_#{name}
          scope = ApiWarden.find_scope("#{name}")
          @current_#{name} = scope.load_owner.call(
            current_#{name}_id, 
            current_#{name}_value_for_access_token, 
            current_#{name}_authentication
          )
        end
        @current_#{name}
      end
    METHODS
  end

  unless scope.disable_refresh_token?
    class_eval <<-METHODS, __FILE__, __LINE__ + 1
      def generate_refresh_token_for_#{name}(id, *args)
        generate_refresh_token_for("#{name}", id, *args)
      end

      def generate_tokens_for_#{name}(id, *args)
        [generate_access_token_for_#{name}(id, *args), generate_refresh_token_for_#{name}(id, *args)]
      end

      def validate_refresh_token_for_#{name}!
        validate_refresh_token_for!("#{name}")
      end          
    METHODS
  end

  ActiveSupport.on_load(:action_controller) do
    include ApiWarden::Helpers, Accessable
    include Refreshable unless scope.disable_refresh_token?

    if respond_to?(:helper_method)
      helper_method "current_#{name}_authentication", "current_#{name}_id", "current_#{name}_value_for_access_token", "#{name}_signed_in?"

      if scope.load_owner.respond_to?(:call)
        helper_method "current_#{name}"
      end
    end
  end
end

.remove_helpers(scope) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/api_warden/helpers.rb', line 87

def self.remove_helpers(scope)
  name = scope.name

  ["ward_by_#{name}",
   "ward_by_#{name}!",
   "current_#{name}_authentication",
   "current_#{name}_id",
   "current_#{name}_value_for_access_token",
   "#{name}_signed_in?",
   "generate_access_token_for_#{name}"].each { |s| undef_method s }

  unless scope.disable_refresh_token?
    ["generate_refresh_token_for_#{name}",
     "generate_tokens_for_#{name}",
     "validate_refresh_token_for_#{name}!"].each { |s| undef_method s }
  end
end