Sha256: 1b9a81237e6bd0b0330ab7c6f4c264430ef07d54384767fa3ec9f2e886504c8a

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 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

    uid = LineRepository.singleton.retrieve_uid(
      code: params[:code],
      redirect_uri: params[:redirect_uri]
    )

    raise IbrainErrors::UnknownError.new I18n.t("ibrain.errors.custom_token.not_retrieve_uid") unless uid

    payload = {
      iss: firebase_owner_email,
      sub: firebase_owner_email,
      aud: Ibrain::Auth::Config.firebase_auth_url,
      iat: iat,
      exp: exp,
      uid: 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.15 app/repositories/firebase_repository.rb