class Miasma::Contrib::Google::Signature::Jwt

JSON Web Token signature

Constants

REQUIRED_CLAIMS

Required items within claims

Public Class Methods

new(private_key_path, i_claims) click to toggle source

Create a new JWT signature instance

@param private_key_path [String] private signing key path @param claims [Hash] request claims @return [self]

Calls superclass method Miasma::Contrib::Google::Signature::new
# File lib/miasma/contrib/google.rb, line 58
def initialize(private_key_path, i_claims)
  super('RS256', 'JWT', i_claims)
  claims[:iat] ||= Time.now.to_i
  claims[:exp] ||= Time.now.to_i + 120
  @private_key = private_key_path
  validate_claims!
  validate_key!
end

Public Instance Methods

encoded_claims() click to toggle source

@return [String] encoded claims set

# File lib/miasma/contrib/google.rb, line 88
def encoded_claims
  t_claims = claims.to_smash
  if(t_claims.key?(:scope))
    t_claims[:scope] = [t_claims[:scope]].flatten.compact.join(' ')
  end
  Base64.urlsafe_encode64(t_claims.to_json)
end
encoded_header() click to toggle source

@return [String] encoded header

# File lib/miasma/contrib/google.rb, line 75
def encoded_header
  Base64.urlsafe_encode64(header.to_json)
end
encoded_signature() click to toggle source

@return [String] encoded signature

# File lib/miasma/contrib/google.rb, line 97
def encoded_signature
  Base64.urlsafe_encode64(signature)
end
generate() click to toggle source

Generate signature

@return [String]

# File lib/miasma/contrib/google.rb, line 70
def generate
  "#{encoded_header}.#{encoded_claims}.#{encoded_signature}"
end
header() click to toggle source

@return [String] header

# File lib/miasma/contrib/google.rb, line 80
def header
  Smash.new(
    :alg => algorithm,
    :typ => format
  )
end
signature() click to toggle source

@return [String] JWT signature

# File lib/miasma/contrib/google.rb, line 102
def signature
  token = "#{encoded_header}.#{encoded_claims}"
  hasher = OpenSSL::Digest::SHA256.new
  author = OpenSSL::PKey::RSA.new(File.read(@private_key))
  author.sign(hasher, token)
end
validate_claims!() click to toggle source

Check for required claims and raise error if unset

@return [TrueClass] @raises [KeyError]

# File lib/miasma/contrib/google.rb, line 113
def validate_claims!
  REQUIRED_CLAIMS.each do |claim|
    unless(claims.key?(claim))
      raise KeyError.new "Missing required claim key `#{claim}`"
    end
  end
  true
end
validate_key!() click to toggle source

Check that the private key exists, is readable, and is

# File lib/miasma/contrib/google.rb, line 123
def validate_key!
end