Module: Ey::Hmac

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

Defined Under Namespace

Classes: Adapter, Faraday, Rack

Constant Summary collapse

Error =
Class.new(StandardError)
MissingSecret =
Class.new(Error)
MissingAuthorization =
Class.new(Error)
SignatureMismatch =
Class.new(Error)
ExpiredHmac =
Class.new(Error)
VERSION =
"2.2.0"

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:



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

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:



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

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



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

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



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

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:



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

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