Module: PowerApi::GeneratorHelper::SimpleTokenAuthHelper

Extended by:
ActiveSupport::Concern
Included in:
PowerApi::GeneratorHelpers
Defined in:
lib/power_api/generator_helper/simple_token_auth_helper.rb

Defined Under Namespace

Classes: SimpleTokenAuthResource

Instance Method Summary collapse

Instance Method Details

#authenticated_resource=(value) ⇒ Object



27
28
29
30
31
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 27

def authenticated_resource=(value)
  return if value.blank?

  @authenticated_resource = SimpleTokenAuthResource.new(value)
end

#authenticated_resource?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 33

def authenticated_resource?
  !!authenticated_resource
end

#authenticated_resources=(values) ⇒ Object



23
24
25
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 23

def authenticated_resources=(values)
  @authenticated_resources = values.map { |value| SimpleTokenAuthResource.new(value) }
end

#current_authenticated_resourceObject



41
42
43
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 41

def current_authenticated_resource
  "current_#{authenticated_resource.snake_case}"
end

#owned_by_authenticated_resource?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 37

def owned_by_authenticated_resource?
  owned_by_authenticated_resource && authenticated_resource? && !parent_resource?
end

#simple_token_auth_initializer_pathObject



52
53
54
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 52

def simple_token_auth_initializer_path
  "config/initializers/simple_token_authentication.rb"
end

#simple_token_auth_initializer_tplObject



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 56

def simple_token_auth_initializer_tpl
  <<~INITIALIZER
    SimpleTokenAuthentication.configure do |config|
      # Configure the session persistence policy after a successful sign in,
      # in other words, if the authentication token acts as a signin token.
      # If true, user is stored in the session and the authentication token and
      # email may be provided only once.
      # If false, users must provide their authentication token and email at every request.
      # config.sign_in_token = false

      # Configure the name of the HTTP headers watched for authentication.
      #
      # Default header names for a given token authenticatable entity follow the pattern:
      #   { entity: { authentication_token: 'X-Entity-Token', email: 'X-Entity-Email'} }
      #
      # When several token authenticatable models are defined, custom header names
      # can be specified for none, any, or all of them.
      #
      # Note: when using the identifiers options, this option behaviour is modified.
      # Please see the example below.
      #
      # Examples
      #
      #   Given User and SuperAdmin are token authenticatable,
      #   When the following configuration is used:
      #     `config.header_names = { super_admin: { authentication_token: 'X-Admin-Auth-Token' } }`
      #   Then the token authentification handler for User watches the following headers:
      #     `X-User-Token, X-User-Email`
      #   And the token authentification handler for SuperAdmin watches the following headers:
      #     `X-Admin-Auth-Token, X-SuperAdmin-Email`
      #
      #   When the identifiers option is set:
      #     `config.identifiers = { super_admin: :phone_number }`
      #   Then both the header names identifier key and default value are modified accordingly:
      #     `config.header_names = { super_admin: { phone_number: 'X-SuperAdmin-PhoneNumber' } }`
      #
      # config.header_names = { user: { authentication_token: 'X-User-Token', email: 'X-User-Email' } }

      # Configure the name of the attribute used to identify the user for authentication.
      # That attribute must exist in your model.
      #
      # The default identifiers follow the pattern:
      # { entity: 'email' }
      #
      # Note: the identifer must match your Devise configuration,
      # see https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address#tell-devise-to-use-username-in-the-authentication_keys
      #
      # Note: setting this option does modify the header_names behaviour,
      # see the header_names section above.
      #
      # Example:
      #
      #   `config.identifiers = { super_admin: 'phone_number', user: 'uuid' }`
      #
      # config.identifiers = { user: 'email' }

      # Configure the Devise trackable strategy integration.
      #
      # If true, tracking is disabled for token authentication: signing in through
      # token authentication won't modify the Devise trackable statistics.
      #
      # If false, given Devise trackable is configured for the relevant model,
      # then signing in through token authentication will be tracked as any other sign in.
      #
      # config.skip_devise_trackable = true
    end
  INITIALIZER
end

#simple_token_auth_methodObject



45
46
47
48
49
50
# File 'lib/power_api/generator_helper/simple_token_auth_helper.rb', line 45

def simple_token_auth_method
  <<-METHOD
acts_as_token_authenticatable

  METHOD
end