Class: GoogleJWT

Inherits:
Object
  • Object
show all
Defined in:
lib/google-jwt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(claim_set, private_key, password, header = nil) ⇒ GoogleJWT

Returns a new instance of GoogleJWT.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google-jwt.rb', line 19

def initialize(claim_set, private_key, password, header = nil)
  # Symbolize the keys.
  self.claim_set = claim_set.inject({}){|item,(k,v)| item[k.to_sym] = v; item}

  # Remove unknown/invalid keys.
  self.claim_set = self.claim_set.delete_if {|k,v| ![:iss, :scope, :aud, :exp, :iat, :prn].include?(k)}

  # Make sure we have all the required keys.
  [:iss, :scope, :aud].each {|r| raise RuntimeError "Missing required claim key: #{r}" unless self.claim_set[r]}

  # Set defaults for create/expire time.
  self.claim_set[:exp] ||= Time.now.to_i + 3600
  self.claim_set[:iat] ||= Time.now.to_i

  # Make sure the header is clean and has required items.
  self.header ||= {:alg => "RS256", :typ => "JWT"}
  self.header = self.header.inject({}){|item,(k,v)| item[k.to_sym] = v; item}
  self.header = self.header.delete_if {|k,v| ![:alg, :typ].include?(k)}
  [:alg, :typ].each {|r| raise RuntimeError "Missing required header key: #{r}" unless self.header[r]}

  # Make sure we were passed the private key and password.
  raise RuntimeError "Missing required private key parameter." unless private_key
  raise RuntimeError "Missing required password parameter." unless password

  # Set the private key.
  self.private_key = key_from_pkcs12(private_key, password)
end

Instance Attribute Details

#claim_setObject

Returns the value of attribute claim_set.



18
19
20
# File 'lib/google-jwt.rb', line 18

def claim_set
  @claim_set
end

#headerObject

Returns the value of attribute header.



18
19
20
# File 'lib/google-jwt.rb', line 18

def header
  @header
end

#private_keyObject

Returns the value of attribute private_key.



18
19
20
# File 'lib/google-jwt.rb', line 18

def private_key
  @private_key
end

Instance Method Details

#jwtObject



47
48
49
50
51
52
# File 'lib/google-jwt.rb', line 47

def jwt
  header = self.header.to_json.clean_base64_encode
  claim_set = self.claim_set.to_json.clean_base64_encode
  signature = "#{header}.#{claim_set}".sign(self.private_key).clean_base64_encode
  "#{header}.#{claim_set}.#{signature}"
end