Module: GoInstant::Auth

Defined in:
lib/goinstant/auth.rb,
lib/goinstant/auth/signer.rb

Overview

Authentication classes and functions for use with GoInstant

Defined Under Namespace

Classes: Signer, SignerError

Class Method Summary collapse

Class Method Details

.compact_decode(str) ⇒ Hash|String|Object

Decode the Compact Serialization of a thing.

Parameters:

  • str (String)

Returns:

  • (Hash|String|Object)


40
41
42
# File 'lib/goinstant/auth.rb', line 40

def self.compact_decode(str)
  return JSON.parse(decode64(str))
end

.compact_encode(thing) ⇒ String

Create a Compact Serialization of a thing.

Parameters:

  • thing (Hash|String|Object)

    anything, passed to JSON.generate.

Returns:

  • (String)


47
48
49
# File 'lib/goinstant/auth.rb', line 47

def self.compact_encode(thing)
  return encode64(JSON.generate(thing))
end

.decode64(str) ⇒ String

Decodes base64 and base64url encoded strings.

Parameters:

  • str (String)

Returns:

  • (String)


32
33
34
35
# File 'lib/goinstant/auth.rb', line 32

def self.decode64(str)
  str = str.gsub(/\s+/,'').tr('-_','+/').sub(/=+$/,'')
  return Base64.decode64(pad64(str))
end

.encode64(str) ⇒ String

Encodes base64 and base64url encoded strings.

Parameters:

  • str (String)

Returns:

  • (String)


25
26
27
# File 'lib/goinstant/auth.rb', line 25

def self.encode64(str)
  return Base64.urlsafe_encode64(str).sub(/=+$/,'')
end

.pad64(str) ⇒ String

Pads base64 strings.

Parameters:

  • str (String)

Returns:

  • (String)

    padded (extra ‘=’ added to multiple of 4).



14
15
16
17
18
19
20
# File 'lib/goinstant/auth.rb', line 14

def self.pad64(str)
  rem = str.size % 4
  if rem > 0 then
    str << ("=" * (4-rem))
  end
  return str
end