Class: Oauth2HmacHeader::AuthorizationHeader

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Defined in:
lib/oauth2_hmac_header/authorization_header.rb

Overview

The “Authorization” Request Header

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



10
11
12
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 10

def ext
  @ext
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 10

def id
  @id
end

#macObject (readonly)

Returns the value of attribute mac.



10
11
12
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 10

def mac
  @mac
end

#nonceObject (readonly)

Returns the value of attribute nonce.



10
11
12
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 10

def nonce
  @nonce
end

#tsObject (readonly)

Returns the value of attribute ts.



10
11
12
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 10

def ts
  @ts
end

Class Method Details

.generate(id, ts, nonce, ext, mac) ⇒ Object

Generates oauth2 hmac authorization header

Parameters:

id

Client id for mac auth

ts

The timestamp value calculated for the request.

nonce

The nonce value generated for the request.

ext

The value of the “ext” “Authorization” request header field attribute if one was included in the request, otherwise, an empty string.

mac

The signature

Returns:

Returns the generated header as string


32
33
34
35
36
37
38
39
40
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 32

def generate(id, ts, nonce, ext, mac)
  header = "MAC "
  header << "id=\"#{id}\", "
  header << "ts=\"#{ts}\", "
  header << "nonce=\"#{nonce}\", "
  header << "ext=\"#{ext}\", " if (!ext.nil? && !ext.empty?)
  header << "mac=\"#{mac}\""
  header
end

.generate_with_new_signature(id, algorithm, key, method, uri, host, port, ext = '') ⇒ Object

Generates oauth2 hmac authorization header

Parameters:

id

Client id for mac auth

algorithm

Name of the algorithm valid vars are hmac-sha256, hmac-sha1

key

Key for hmac algorithm

method

The HTTP request method in upper case. For example: “HEAD”, “GET”, “POST”, etc.

uri

The HTTP request-URI as defined by tools.ietf.org/html/rfc2616#section-5.1.2

host

The hostname included in the HTTP request using the “Host” request header field in lower case.

port

The port as included in the HTTP request using the “Host” request header field. If the header field does not include a port, the default value for the scheme MUST be used (e.g. 80 for HTTP and 443 for HTTPS).

ext

The value of the “ext” “Authorization” request header field attribute if one was included in the request, otherwise, an empty string.

Returns:

Returns the generated header as string


70
71
72
73
74
75
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 70

def generate_with_new_signature(id, algorithm, key, method, uri, host, port, ext = '')
  ts, nonce, ext, mac  = Oauth2HmacSign::Signature.generate(
    algorithm, key, method, uri, host, port, ext
  )
  generate(id, ts, nonce, ext, mac)
end

.parse(header) ⇒ Object

Parses oauth2 hmac header

Parameters:

header

Client id for mac auth

Returns:

Returns the generated header as string
id

Client id for mac auth

ts

The timestamp value calculated for the request.

nonce

The nonce value generated for the request.

ext

The value of the “ext” “Authorization” request header field attribute if one was included in the request, otherwise, an empty string.

mac

The signature



97
98
99
100
101
102
103
# File 'lib/oauth2_hmac_header/authorization_header.rb', line 97

def parse(header)
  pattern = Regexp.new "(id|ts|nonce|ext|mac)=(\"[^\"]+\")"
  results = Hash[header.scan pattern]
  validate_presence_of_keys_and_values(results, ['id', 'ts', 'nonce', 'mac'])
  results = clean_quotes(results)
  return results['id'], results['ts'], results['nonce'], results['ext'], results['mac']
end