What is a ruby ​​authenticator on rails

I am trying to read the open source project code on GitHub and find myself confused looking at the file. What is an “authenticator” in the code below and where is it defined. As far as I understand, it is not defined as a class anywhere. This is the project url: github repository

authenticator :server do
  header "X-Server-API-Key", "The API token for a server that you wish to authenticate with.", :example => 'f29a45f0d4e1744ebaee'
  error 'InvalidServerAPIKey', "The API token provided in X-Server-API-Key was not valid.", :attributes => {:token => "The token that was looked up"}
  error 'ServerSuspended', "The mail server has been suspended"
  lookup do
    if key = request.headers['X-Server-API-Key']
      if credential = Credential.where(:type => 'API', :key => key).first
        if credential.server.suspended?
          error 'ServerSuspended'
        else
          credential.use
          credential
        end
      else
        error 'InvalidServerAPIKey', :token => key
      end
    end
  end
  rule :default, "AccessDenied", "Must be authenticated as a server." do
    identity.is_a?(Credential)
  end
end
+4
source share
2 answers

What is an “authenticator” in the code below and where is it defined.

As for the “what” - authenticatorthis is a method that is called with a positional argument :server and a is a block argument (part do ... end).

"" - Ruby.

IDE, :

$ git clone https://github.com/atech/postal.git
Cloning into 'postal'...
remote: Counting objects: 1139, done.
remote: Total 1139 (delta 0), reused 0 (delta 0), pack-reused 1139
Receiving objects: 100% (1139/1139), 2.09 MiB | 1.22 MiB/s, done.
Resolving deltas: 100% (502/502), done.

$ cd postal
$ bundle install
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Fetching https://github.com/unixcharles/acme-client
Fetching https://github.com/adamcooke/moonrope
Installing rake 11.3.0
Installing multipart-post 2.0.0
Installing concurrent-ruby 1.0.5
...

bundle console

$ bundle console
irb(main):001:0>

, :

irb(main):001:0> method(:authenticator)
NameError: undefined method `authenticator'
...

. , , , :

irb(main):002:0> ObjectSpace.each_object(Module).select { |m| m.instance_methods.include?(:authenticator) }
#=> [Moonrope::Action, Moonrope::DSL::BaseDSL, Moonrope::DSL::ControllerDSL, Moonrope::DSL::ActionDSL, Moonrope::Controller]

Moonrope... . , :

irb(main):003:0> Moonrope::DSL::BaseDSL.instance_method(:authenticator).source_location
#=> ["/Users/sos/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/bundler/gems/moonrope-f56a37b8f121/lib/moonrope/dsl/base_dsl.rb", 73]

, :

irb(main):004:0> ^Z
zsh: suspended  bundle console
$ vi +73 $(bundle show moonrope)/lib/moonrope/dsl/base_dsl.rb

:

Vim

+1

, moonrope.

. "" API. , , . .

authenticator :default do

  header "X-Auth-Token", "The user unique API token.", :example => 'f29a45f0-b6da-44ae-a029-d4e1744ebaee'

  error 'InvalidAPIToken', "The API token provided in X-Auth-Token was not valid.", :attributes => {:token => "The token that was looked up"}

  lookup do
    if token = request.headers['X-Auth-Token']
      if user = User.find_by_api_token(token)
        user
      else
        error 'InvalidAPIToken', :token => token
      end
    end
  end

  rule :default, "AccessDenied", "Must be authenticated as a user." do
    identity.is_a?(User)
  end

  rule :anonymous, "MustBeAnonymous", "Must be anonymous." do
    identity.nil?
  end

end

:

  • . , : default. API.

  • a, X-Auth-Token. .

  • , InvalidAPIToken. , .

  • , , . , API. , , . - , , . , , . , , .

  • , , , . , , . , . - , , . - ( ).

  • , , , .

, .

+2

Source: https://habr.com/ru/post/1676224/


All Articles