Module: Ey::Hmac

Defined in:
lib/ey-hmac/version.rb,
lib/ey-hmac.rb

Defined Under Namespace

Classes: Adapter, Faraday, Rack

Constant Summary collapse

VERSION =
'2.4.0'
Error =
Class.new(StandardError)
MissingSecret =
Class.new(Error)
MissingAuthorization =
Class.new(Error)
SignatureMismatch =
Class.new(Error)
ExpiredHmac =
Class.new(Error)

Class Method Summary collapse

Class Method Details

.authenticate!(request, options = {}) {|key_id| ... } ⇒ TrueClass

Examples:

Ey::Hmac.authenticate! do |key_id|
  @consumer = Consumer.where(auth_id: key_id).first
  @consumer && @consumer.auth_key
end

Parameters:

  • request (Hash)

    request environment

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :adapter (Ey::Hmac::Adapter) — default: {#default_adapter}

    adapter to verify request with

Yield Parameters:

  • key_id (String)

    public HMAC key

Returns:

  • (TrueClass)

    if authentication was successful

Raises:

See Also:



95
96
97
98
99
100
101
# File 'lib/ey-hmac.rb', line 95

def self.authenticate!(request, options = {}, &block)
  adapter = options[:adapter] || Ey::Hmac.default_adapter

  raise ArgumentError, 'Missing adapter and Ey::Hmac.default_adapter' unless adapter

  adapter.new(request, options).authenticate!(&block)
end

.authenticated?(request, options = {}) {|key_id| ... } ⇒ Boolean

Returns success of authentication.

Examples:

Ey::Hmac.authenticated? do |key_id|
  @consumer = Consumer.where(auth_id: key_id).first
  @consumer && @consumer.auth_key
end

Parameters:

  • request (Hash)

    request environment

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :adapter (Ey::Hmac::Adapter) — default: {#default_adapter}

    adapter to verify request with

Yield Parameters:

  • key_id (String)

    public HMAC key

Returns:

  • (Boolean)

    success of authentication

Raises:

  • (ArgumentError)

See Also:



69
70
71
72
73
74
75
# File 'lib/ey-hmac.rb', line 69

def self.authenticated?(request, options = {}, &block)
  adapter = options[:adapter] || Ey::Hmac.default_adapter

  raise ArgumentError, 'Missing adapter and Ey::Hmac.default_adapter' unless adapter

  adapter.new(request, options).authenticated?(&block)
end

.default_adapterObject



26
27
28
29
30
31
32
# File 'lib/ey-hmac.rb', line 26

def self.default_adapter
  @default_adapter ||= if defined?(::Rack) || defined?(::Rails)
                         Ey::Hmac::Adapter::Rack
                       elsif defined?(::Faraday)
                         Ey::Hmac::Adapter::Faraday
                       end
end

.default_adapter=(default_adapter) ⇒ Object



22
23
24
# File 'lib/ey-hmac.rb', line 22

def self.default_adapter=(default_adapter)
  @default_adapter = default_adapter
end

.sign!(request, key_id, key_secret, options = {}) ⇒ String

Signs request by calculating signature and adding it to the specified header

Examples:

Ey::Hmac.sign!(env, @key_id, @key_secret)

Parameters:

  • request (Hash)

    request environment

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :adapter (Ey::Hmac::Adapter) — default: #default_adapterr}

    adapter to sign request with

  • :version (Integer) — default: nil

    signature version

  • :authorization_header (String) — default: 'Authorization'

    Authorization header key.

  • :service (String) — default: 'EyHmac'

    service name prefixed to Ey::Hmac::Adapter#authorization

Returns:

  • (String)

    authorization signature

Raises:

  • (ArgumentError)

See Also:



47
48
49
50
51
52
53
# File 'lib/ey-hmac.rb', line 47

def self.sign!(request, key_id, key_secret, options = {})
  adapter = options[:adapter] || Ey::Hmac.default_adapter

  raise ArgumentError, 'Missing adapter and Ey::Hmac.default_adapter' unless adapter

  adapter.new(request, options).sign!(key_id, key_secret)
end