Class: Firebase::FirebaseTokenGenerator

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

Overview

This class handles generating signed authentication tokens for use with Firebase

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ FirebaseTokenGenerator

When creating an instance of the generator, you must provide your Firebase Application Secret



13
14
15
16
17
18
# File 'lib/firebase_token_generator.rb', line 13

def initialize(secret)
  if (!secret.is_a?(String))
    raise ArgumentError, "FirebaseTokenGenerator: secret must be a string."
  end
  @secret = secret
end

Instance Method Details

#create_token(auth_data, options = {}) ⇒ Object

Returns a signed Firebase Authentication Token Takes the following arguments:

auth_data

A hash of arbitrary data to be included in the token

options

An optional hash of extra claims that may be included in the token. Allowed values are:

expires

Epoch time after which the token will no longer be valid

notBefore

Epoch time before which the token will not be valid

admin

If set to true, this client will bypass all security rules

debug

If set to true, this client will receive debug information about the security rules

simulate

(internal-only for now) Runs security rules but makes no data changes

Throws ArgumentError if given invalid data or an invalid option Throws RuntimeError if generated token is too long



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/firebase_token_generator.rb', line 32

def create_token(auth_data, options = {})
  if (auth_data.nil? or auth_data.empty?) and (options.nil? or options.empty?)
    raise ArgumentError, "FirebaseTokenGenerator.create_token: data is empty and no options are set.  This token will have no effect on Firebase."
  end
  validate_auth_data(auth_data, (!options.nil? and options[:admin] == true))
  claims = create_options_claims(options)
  claims[:v] = TOKEN_VERSION
  claims[:iat] = Time.now.to_i
  claims[:d] = auth_data
  token = encode_token(claims)
  if (token.length > 1024)
    raise RuntimeError, "FirebaseTokenGenerator.create_token: generated token is too long."
  end
  token
end