Module: MagentaSSO

Defined in:
lib/magentasso.rb,
lib/magentasso/version.rb

Overview

A library to implement a MagentaSSO provider or client.

Defined Under Namespace

Classes: MagentaError, Request, Response, SignatureError

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.encode_and_sign(payload, secret) ⇒ Object

Encode the payload and generate a signature with the secret.



19
20
21
22
23
24
25
26
27
28
# File 'lib/magentasso.rb', line 19

def encode_and_sign(payload, secret)
  secret = Base32.decode(secret)

  payload = JSON.generate(payload)
  payload = Base64.urlsafe_encode64(payload, padding: true)
  signature = OpenSSL::HMAC.digest("SHA256", secret, payload)
  signature = Base64.urlsafe_encode64(signature, padding: true)

  [payload, signature]
end

.verify_and_decode(payload, signature, secret) ⇒ Object

Verify the signature using the secret, and return the decoded payload.



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

def verify_and_decode(payload, signature, secret)
  secret = Base32.decode(secret)

  signature = Base64.urlsafe_decode64(signature)
  our_signature = OpenSSL::HMAC.digest("SHA256", secret, payload)
  raise MagentaSSO::SignatureError unless signature == our_signature

  payload = Base64.urlsafe_decode64(payload)
  JSON.parse(payload)
end