Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CreateFirebaseToken
- FIREBASE_URL = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=#{API_KEY}'
- URL = 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit'
- SERVICE_ACCOUNT_EMAIL = 'firebase-adminsdk-j4rgm@auth-rails-bdc20.iam.gserviceaccount.com'
- PRIVATE_KEY = OpenSSL::PKey::RSA.new(File.read('/PATH/Downloads/firebase.key'))
- class << self
- def call
- @payload = create_payload
- token = encode_payload
- res = RestClient.post(FIREBASE_URL, {token: token}, content_type: :json)
- JSON.parse(res.body)['idToken']
- end
- def create_payload
- {
- iss: SERVICE_ACCOUNT_EMAIL,
- sub: SERVICE_ACCOUNT_EMAIL,
- aud: URL,
- iat: now_seconds,
- exp: now_seconds+(60*60),
- uid: SecureRandom.hex(6),
- claims: {
- premium_account: false
- }
- }
- end
- private
- def encode_payload
- JWT.encode(@payload, PRIVATE_KEY, 'RS256')
- end
- def self.now_seconds
- Time.now.to_i
- end
- end
- end
Add Comment
Please, Sign In to add comment