firebase-verifier 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b177ce09f0ee70dc1152afeaf1c3c103921ca2bc4c1b4149f4b87a68b9d3c61
4
+ data.tar.gz: 20214dd3e882de788fc5e61a92f1c2148228f567873fdeb4a210ee6aba5828fc
5
+ SHA512:
6
+ metadata.gz: 4f3b1a1fc559f4315f9b93dae117624e70650016343b5bfc0fc1f80bbb8962ddedaa3e799cd0c666383d9c2d0a80d8bdc9820eb23dd8fc69d839a856fe8c5964
7
+ data.tar.gz: 5298cfb493e98980c3de2552d821f12a5fb81f563e54f6768106283f5ceeb549480a689fc82551e5a6ad074daeee195ef364146450d6445004e8403a3ba375d1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in firebase-verifier.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Firebase::Verifier
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/firebase/verifier`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add firebase-verifier
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install firebase-verifier
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Deployment
28
+
29
+ Build the gem:
30
+
31
+ $ gem build firebase-verifier.gemspec
32
+
33
+ Deploy the gem:
34
+
35
+ $ gem push firebase-verifier-0.1.0.gem
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/firebase-verifier.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FirebaseVerifier
4
+ module Verifier
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "verifier/version"
4
+ require 'net/http'
5
+ require 'json'
6
+ require 'jwt'
7
+
8
+ module FirebaseVerifier
9
+ module Verifier
10
+ class Error < StandardError; end
11
+ VALID_JWT_PUBLIC_KEYS_RESPONSE_CACHE_KEY = "firebase_jwt_public_keys_cache_key"
12
+ JWT_ALGORITHM = 'RS256'
13
+
14
+ def initialize(firebase_project_id)
15
+ @firebase_project_id = firebase_project_id
16
+ end
17
+
18
+ def decode(id_token)
19
+ payload, header = decode_jwt_token(id_token, @firebase_project_id)
20
+
21
+ alg = header['alg']
22
+ raise "Invalid access token 'alg' header (#{alg}). Must be '#{JWT_ALGORITHM}'." unless alg == JWT_ALGORITHM
23
+
24
+ typ = header['typ']
25
+ raise "Invalid access token 'typ' header (#{typ}). Must be 'JWT'." unless typ == 'JWT'
26
+
27
+ iss = payload['iss']
28
+ issuer = "https://securetoken.google.com/#{@firebase_project_id}"
29
+ raise "Invalid access token 'iss' payload (#{iss}). Must be '#{issuer}'." unless iss == issuer
30
+
31
+ exp = payload['exp']
32
+ raise "Invalid access token 'exp' payload (#{exp}). Token has expired." unless Time.at(exp) > Time.now
33
+
34
+ aud = payload['aud']
35
+ audience = @firebase_project_id
36
+ raise "Invalid access token 'aud' payload (#{aud}). Must be '#{audience}'." unless aud == audience
37
+
38
+ kid = header['kid']
39
+ valid_public_keys = retrieve_and_cache_jwt_valid_public_keys
40
+ raise "Invalid access token 'kid' header, do not correspond to valid public keys." unless valid_public_keys.keys.include?(kid)
41
+
42
+ sub = payload['sub']
43
+ raise "Invalid access token. 'Subject' (sub) must be a non-empty string." if sub.nil? || sub.empty?
44
+
45
+ payload
46
+ end
47
+
48
+ private
49
+
50
+ def decode_jwt_token(firebase_jwt_token, firebase_project_id)
51
+ public_key = nil
52
+ custom_options = {
53
+ verify_iat: true,
54
+ verify_aud: true,
55
+ aud: firebase_project_id,
56
+ verify_iss: true,
57
+ iss: "https://securetoken.google.com/#{firebase_project_id}"
58
+ }
59
+
60
+ custom_options[:algorithm] = JWT_ALGORITHM unless public_key.nil?
61
+
62
+ begin
63
+ decoded_token = JWT.decode(firebase_jwt_token, public_key, !public_key.nil?, custom_options)
64
+ rescue JWT::ExpiredSignature
65
+ return nil, "Invalid access token. 'Expiration time' (exp) must be in the future."
66
+ rescue JWT::InvalidIatError
67
+ return nil, "Invalid access token. 'Issued-at time' (iat) must be in the past."
68
+ rescue JWT::InvalidAudError
69
+ return nil, "Invalid access token. 'Audience' (aud) must be your Firebase project ID, the unique identifier for your Firebase project."
70
+ rescue JWT::InvalidIssuerError
71
+ return nil, "Invalid access token. 'Issuer' (iss) Must be 'https://securetoken.google.com/<projectId>', where <projectId> is your Firebase project ID."
72
+ rescue JWT::VerificationError
73
+ return nil, "Invalid access token. Signature verification failed."
74
+ end
75
+
76
+ return decoded_token
77
+ end
78
+
79
+ def retrieve_and_cache_jwt_valid_public_keys
80
+ valid_public_keys = Rails.cache.read(VALID_JWT_PUBLIC_KEYS_RESPONSE_CACHE_KEY)
81
+ if valid_public_keys.nil?
82
+ uri = URI("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com")
83
+ response = Net::HTTP.get_response(uri)
84
+
85
+ if response.code != '200'
86
+ raise "Something went wrong: can't obtain valid JWT public keys from Google."
87
+ end
88
+
89
+ valid_public_keys = JSON.parse(response.body)
90
+
91
+ cc = response["cache-control"]
92
+ max_age = cc[/max-age=(\d+?),/m, 1]
93
+
94
+ Rails.cache.write(VALID_JWT_PUBLIC_KEYS_RESPONSE_CACHE_KEY, valid_public_keys, :expires_in => max_age.to_i)
95
+ end
96
+
97
+ valid_public_keys
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,6 @@
1
+ module Firebase
2
+ module Verifier
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase-verifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - KuruStudio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Verifier of Firebase Token. This is used to extract important details
14
+ in a Firebase Token.
15
+ email:
16
+ - mail@kuru.studio
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - README.md
23
+ - Rakefile
24
+ - lib/firebase-verifier/verifier.rb
25
+ - lib/firebase-verifier/verifier/version.rb
26
+ - sig/firebase-verifier/verifier.rbs
27
+ homepage: https://kuru.studio
28
+ licenses: []
29
+ metadata:
30
+ allowed_push_host: https://rubygems.org
31
+ homepage_uri: https://kuru.studio
32
+ source_code_uri: https://github.com/kuru-studio/firebase-verifier
33
+ changelog_uri: https://kuru.studio
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.6.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.3.7
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Verifier of Firebase Token
53
+ test_files: []