Class: Evil::Client::Resolver::Security

Inherits:
Evil::Client::Resolver show all
Defined in:
lib/evil/client/resolver/security.rb

Overview

Resolves security definitions from operation settings and schema. Defines helpers for different methods of the authentication.

Instance Method Summary collapse

Methods inherited from Evil::Client::Resolver

call, #to_s

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Evil::Client::Resolver

Instance Method Details

#basic_auth(user, password) ⇒ Hash<:headers, Hash<Symbol, String>>

DSL method to provide basic authentication schema by user name and password

It provides base64-encoded “user:password” token and adds it to the “Authorization” header with a “Basic” prefix.

Examples:

operation do
  option :user
  option :password

  security { basic_auth user, password }
end

Parameters:

  • user (#to_s)

    User name

  • password (#to_s)

    Password

Returns:

  • (Hash<:headers, Hash<Symbol, String>>)


26
27
28
29
# File 'lib/evil/client/resolver/security.rb', line 26

def basic_auth(user, password)
  token = Base64.encode64("#{user}:#{password}").delete("\n")
  token_auth(token, prefix: "Basic")
end

#key_auth(key, value, inside: :headers) ⇒ Hash<Symbol, Hash<Symbol, String>>

DSL method to provide the key-based authentication schema

Examples:

operation do
  option :key
  security { key_auth "Authorize", key }
end

Parameters:

  • key (#to_s)

    Name of the parameter

  • value (#to_s)

    Value of the parameter

  • [:headers, (Hash)

    a customizable set of options

Returns:

  • (Hash<Symbol, Hash<Symbol, String>>)


72
73
74
# File 'lib/evil/client/resolver/security.rb', line 72

def key_auth(key, value, inside: :headers)
  { inside => { key.to_s => value.to_s } }
end

#token_auth(token, inside: :headers, prefix: nil) ⇒ Hash<Symbol, Hash<Symbol, String>>

DSL method to provide token-based authentication schema

It places the token under either standard “Authorization” header, or standard “access_token” parameter of body or query. If you need custom key use [#key_auth] schema instead.

Examples:

operation do
  option :token
  security { token_auth token, prefix: "Bearer" }
end

Parameters:

  • token (#to_s)

    User secret token

  • [#to_s, (Hash)

    a customizable set of options

  • [:headers, (Hash)

    a customizable set of options

Returns:

  • (Hash<Symbol, Hash<Symbol, String>>)


49
50
51
52
53
54
55
56
# File 'lib/evil/client/resolver/security.rb', line 49

def token_auth(token, inside: :headers, prefix: nil)
  if inside == :headers
    prefixed_token = [prefix&.to_s&.capitalize, token].compact.join(" ")
    key_auth("Authorization", prefixed_token, inside: :headers)
  else
    key_auth("access_token", token, inside: inside)
  end
end