Class: Oauth2HmacSign::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2_hmac_sign/signature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def ext
  @ext
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def host
  @host
end

#macObject (readonly)

Returns the value of attribute mac.



10
11
12
# File 'lib/oauth2_hmac_sign/signature.rb', line 10

def mac
  @mac
end

#methodObject (readonly)

Returns the value of attribute method.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def method
  @method
end

#nonceObject (readonly)

Returns the value of attribute nonce.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def nonce
  @nonce
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def port
  @port
end

#tsObject (readonly)

Returns the value of attribute ts.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def ts
  @ts
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/oauth2_hmac_sign/signature.rb', line 9

def uri
  @uri
end

Class Method Details

.generate(algorithm, key, method, uri, host, port = 443, ext = '') ⇒ Object

Generate oauth2 hmac signature with required and optional vars

Parameters:

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 signature and required variables to verify it.
ts

The timestamp value calculated for the signature.

nonce

The nonce value generated for the signature.

ext

The value of passed or assigned for ext

mac

The signature



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oauth2_hmac_sign/signature.rb', line 48

def generate(algorithm, key, method, uri, host, port = 443, ext = '')
  @ts = Time.now.to_i
  @nonce = generate_nonce
  @method = method
  @uri = uri
  @host = host
  @port = port
  @ext = ext
  @mac = calculate(
    algorithm_constructor(algorithm),
    key,
    normalized_request_string
  )
  return @ts, @nonce, @ext, @mac
end

.is_valid?(mac, algorithm, key, ts, nonce, method, uri, host, port, ext) ⇒ Boolean

Validate oauth2 hmac signature with required and optional vars

Parameters:

mac

Signature for validation

algorithm

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

key

Key for hmac algorithm

ts

The timestamp value calculated for the request.

nonce

The nonce value generated for the request.

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:

Boolean: true for succesfully verified mac signature and false for invalid mac signature

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/oauth2_hmac_sign/signature.rb', line 96

def is_valid?(mac, algorithm, key, ts, nonce, method, uri, host, port, ext)
  @ts = ts
  @nonce = nonce
  @method = method
  @uri = uri
  @host = host
  @port = port
  @ext = ext
  mac.eql?(calculate(
      algorithm_constructor(algorithm),
      key,
      normalized_request_string
    )
  )
end