Last active
June 3, 2024 00:04
-
-
Save avosalmon/965fdcbac9568ead2a24eb3cfc388204 to your computer and use it in GitHub Desktop.
Verify Firebase auth JWT token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'base64' | |
require 'httparty' | |
require 'jwt' | |
class FirebaseToken | |
JWT_ALGORITHM = 'RS256'.freeze | |
PUBLIC_KEY_URL = 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com'.freeze | |
def initialize(token) | |
@token = token | |
end | |
def verify(firebase_project_id) | |
header = decode_header | |
alg = header['alg'] | |
kid = header['kid'] | |
if alg != JWT_ALGORITHM | |
raise "Invalid token 'alg' header (#{alg}). Must be '#{JWT_ALGORITHM}'." | |
end | |
public_key = get_public_key(kid) | |
verify_jwt(public_key, firebase_project_id) | |
end | |
private | |
def decode_header | |
encoded_header = @token.split('.').first | |
JSON.parse(Base64.decode64(encoded_header)) | |
end | |
def get_public_key(kid) | |
response = HTTParty.get(PUBLIC_KEY_URL) | |
unless response.success? | |
raise "Failed to fetch JWT public keys from Google." | |
end | |
public_keys = response.parsed_response | |
# TODO: cache public_keys to avoid downloading it every time. | |
# Use the cache-control header for TTL. | |
unless public_keys.include?(kid) | |
raise "Invalid token 'kid' header, do not correspond to valid public keys." | |
end | |
OpenSSL::X509::Certificate.new(public_keys[kid]).public_key | |
end | |
def verify_jwt(public_key, firebase_project_id) | |
options = { | |
algorithm: JWT_ALGORITHM, | |
verify_iat: true, | |
verify_aud: true, | |
aud: firebase_project_id, | |
verify_iss: true, | |
iss: "https://securetoken.google.com/#{firebase_project_id}" | |
} | |
JWT.decode(@token, public_key, true, options) | |
end | |
end | |
firebase_project_id = 'your-project-id' | |
# retrieve the token from HTTP header in the real app | |
id_token = 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjOGUyYjI5NmM2ZjMyODRlYzMwYjg4NjVkNzI5M2U2MjdmYTJiOGY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6JR8ZdnMG_yi3yHsA' | |
token = FirebaseToken.new(id_token) | |
decoded_token = token.verify(firebase_project_id) | |
p decoded_token |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment