Sha256: 8ddb03eeb3fa18c50286f2e4dd1d8f665a0c7c08dc62b4dc9919cfcfceac65e2

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'open-uri'

class FirebaseRepository < Ibrain::BaseRepository
  def initialize(record, params)
    super(nil, record)

    @private_key_json = load_private_file
    @firebase_owner_email = Ibrain::Auth::Config.firebase_owner_email
    @params = params
  end

  def generate_custom_token!
    iat = Time.now.to_i
    exp = 60.minutes.from_now.to_i

    payload = {
      iss: firebase_owner_email,
      sub: firebase_owner_email,
      aud: Ibrain::Auth::Config.firebase_auth_url,
      iat: iat,
      exp: exp,
      uid: params[:uid],
      claims: {}
    }

    JWT.encode payload, private_key, "RS256"
  end

  private

  attr_reader :private_key_json, :firebase_owner_email, :params

  def json_firebase
    JSON.parse(private_key_json, symbolize_names: true)
  end

  def private_key
    OpenSSL::PKey::RSA.new json_firebase[:private_key]
  end

  def load_private_file
    is_remote = Ibrain::Auth::Config.firebase_private_key_path.include?("http")

    if is_remote
      uri = URI.parse(Ibrain::Auth::Config.firebase_private_key_path)
      content = uri.open { |f| f.read }

      return content
    end

    File.open(Ibrain::Auth::Config.firebase_private_key_path).read
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ibrain-auth-0.3.14 app/repositories/firebase_repository.rb