NameL3ss

Create Firebase Token

Aug 15th, 2021 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class CreateFirebaseToken
  2. FIREBASE_URL = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=#{API_KEY}'
  3. URL = 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit'
  4. SERVICE_ACCOUNT_EMAIL = 'firebase-adminsdk-j4rgm@auth-rails-bdc20.iam.gserviceaccount.com'
  5. PRIVATE_KEY = OpenSSL::PKey::RSA.new(File.read('/PATH/Downloads/firebase.key'))
  6.  
  7. class << self
  8. def call
  9. @payload = create_payload
  10. token = encode_payload
  11. res = RestClient.post(FIREBASE_URL, {token: token}, content_type: :json)
  12. JSON.parse(res.body)['idToken']
  13. end
  14.  
  15. def create_payload
  16. {
  17. iss: SERVICE_ACCOUNT_EMAIL,
  18. sub: SERVICE_ACCOUNT_EMAIL,
  19. aud: URL,
  20. iat: now_seconds,
  21. exp: now_seconds+(60*60),
  22. uid: SecureRandom.hex(6),
  23. claims: {
  24. premium_account: false
  25. }
  26. }
  27. end
  28.  
  29. private
  30.  
  31. def encode_payload
  32. JWT.encode(@payload, PRIVATE_KEY, 'RS256')
  33. end
  34.  
  35. def self.now_seconds
  36. Time.now.to_i
  37. end
  38. end
  39. end
Add Comment
Please, Sign In to add comment