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
# File 'lib/firebase_token_generator.rb', line 13

def initialize(secret)
  @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 an invalid option



28
29
30
31
32
33
34
35
36
37
# File 'lib/firebase_token_generator.rb', line 28

def create_token(auth_data, options = {})
  if auth_data.empty? and options.empty?
    raise ArgumentError, "FirebaseTokenGenerator.create_token: data is empty and no options are set.  This token will have no effect on Firebase."
  end
  claims = create_options_claims(options)
  claims[:v] = TOKEN_VERSION
  claims[:iat] = Time.now.to_i
  claims[:d] = auth_data
  encode_token(claims)
end