firebase_auth_for_ruby 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: ab5e95017561f14f74b6da0b558f3998638db1538f2941e456ceb9d8b1ba2ef0
4
+ data.tar.gz: fa2f8ff6115b8ad1c9452e76917c691bc65e3750fca31e20b8800d21c35eaff9
5
+ SHA512:
6
+ metadata.gz: d8e4df7ae07c5f080bc2ef6cc51199f7bcc2177df347b0183aad5c7e57f774c84779856c3d1a5bc48cb7c7f74751c5e86ae26c78e09cec8c3d983875b3ed8557
7
+ data.tar.gz: bcab660680addaa2df329c6e6b0cb01b5b76e0452f08bcf2fca91a5d34d29342744c52f7cf8e0bf9cb4c62ea36b61c8a7e06a1b8f73c20f0d5a41564330d66ca
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,48 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/AsciiComments:
5
+ Enabled: false
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/FrozenStringLiteralComment:
11
+ Enabled: false
12
+
13
+ Style/Lambda:
14
+ Enabled: false
15
+
16
+ Style/ClassAndModuleChildren:
17
+ Enabled: false
18
+
19
+ Naming/FileName:
20
+ Exclude:
21
+ - "Gemfile"
22
+
23
+ Style/StringLiterals:
24
+ Enabled: true
25
+ EnforcedStyle: double_quotes
26
+
27
+ Style/StringLiteralsInInterpolation:
28
+ Enabled: true
29
+ EnforcedStyle: double_quotes
30
+
31
+ Layout/LineLength:
32
+ Max: 120
33
+ Exclude:
34
+ - "lib/firebase/**/*"
35
+
36
+ Metrics/MethodLength:
37
+ Max: 20
38
+ Exclude:
39
+ - "spec/**/*"
40
+
41
+ Metrics/ClassLength:
42
+ Max: 200
43
+
44
+ Metrics/BlockLength:
45
+ Max: 50
46
+ Exclude:
47
+ - "spec/**/*"
48
+ - "lib/tasks/**/*"
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-10-03
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at shun.tagami@i-nations.tokyo . All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in firebase_auth_for_ruby.gemspec
4
+ gemspec
5
+
6
+ gem "jwt"
7
+ gem "rails", "~> 6.1.4"
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "rubocop"
data/Gemfile.lock ADDED
@@ -0,0 +1,183 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ firebase_auth_for_ruby (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (6.1.4.1)
10
+ actionpack (= 6.1.4.1)
11
+ activesupport (= 6.1.4.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.1.4.1)
15
+ actionpack (= 6.1.4.1)
16
+ activejob (= 6.1.4.1)
17
+ activerecord (= 6.1.4.1)
18
+ activestorage (= 6.1.4.1)
19
+ activesupport (= 6.1.4.1)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.1.4.1)
22
+ actionpack (= 6.1.4.1)
23
+ actionview (= 6.1.4.1)
24
+ activejob (= 6.1.4.1)
25
+ activesupport (= 6.1.4.1)
26
+ mail (~> 2.5, >= 2.5.4)
27
+ rails-dom-testing (~> 2.0)
28
+ actionpack (6.1.4.1)
29
+ actionview (= 6.1.4.1)
30
+ activesupport (= 6.1.4.1)
31
+ rack (~> 2.0, >= 2.0.9)
32
+ rack-test (>= 0.6.3)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
+ actiontext (6.1.4.1)
36
+ actionpack (= 6.1.4.1)
37
+ activerecord (= 6.1.4.1)
38
+ activestorage (= 6.1.4.1)
39
+ activesupport (= 6.1.4.1)
40
+ nokogiri (>= 1.8.5)
41
+ actionview (6.1.4.1)
42
+ activesupport (= 6.1.4.1)
43
+ builder (~> 3.1)
44
+ erubi (~> 1.4)
45
+ rails-dom-testing (~> 2.0)
46
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
+ activejob (6.1.4.1)
48
+ activesupport (= 6.1.4.1)
49
+ globalid (>= 0.3.6)
50
+ activemodel (6.1.4.1)
51
+ activesupport (= 6.1.4.1)
52
+ activerecord (6.1.4.1)
53
+ activemodel (= 6.1.4.1)
54
+ activesupport (= 6.1.4.1)
55
+ activestorage (6.1.4.1)
56
+ actionpack (= 6.1.4.1)
57
+ activejob (= 6.1.4.1)
58
+ activerecord (= 6.1.4.1)
59
+ activesupport (= 6.1.4.1)
60
+ marcel (~> 1.0.0)
61
+ mini_mime (>= 1.1.0)
62
+ activesupport (6.1.4.1)
63
+ concurrent-ruby (~> 1.0, >= 1.0.2)
64
+ i18n (>= 1.6, < 2)
65
+ minitest (>= 5.1)
66
+ tzinfo (~> 2.0)
67
+ zeitwerk (~> 2.3)
68
+ ast (2.4.2)
69
+ builder (3.2.4)
70
+ concurrent-ruby (1.1.9)
71
+ crass (1.0.6)
72
+ diff-lcs (1.4.4)
73
+ erubi (1.10.0)
74
+ globalid (0.5.2)
75
+ activesupport (>= 5.0)
76
+ i18n (1.8.10)
77
+ concurrent-ruby (~> 1.0)
78
+ jwt (2.2.3)
79
+ loofah (2.12.0)
80
+ crass (~> 1.0.2)
81
+ nokogiri (>= 1.5.9)
82
+ mail (2.7.1)
83
+ mini_mime (>= 0.1.1)
84
+ marcel (1.0.2)
85
+ method_source (1.0.0)
86
+ mini_mime (1.1.1)
87
+ mini_portile2 (2.6.1)
88
+ minitest (5.14.4)
89
+ nio4r (2.5.8)
90
+ nokogiri (1.12.5)
91
+ mini_portile2 (~> 2.6.1)
92
+ racc (~> 1.4)
93
+ parallel (1.21.0)
94
+ parser (3.0.2.0)
95
+ ast (~> 2.4.1)
96
+ racc (1.5.2)
97
+ rack (2.2.3)
98
+ rack-test (1.1.0)
99
+ rack (>= 1.0, < 3)
100
+ rails (6.1.4.1)
101
+ actioncable (= 6.1.4.1)
102
+ actionmailbox (= 6.1.4.1)
103
+ actionmailer (= 6.1.4.1)
104
+ actionpack (= 6.1.4.1)
105
+ actiontext (= 6.1.4.1)
106
+ actionview (= 6.1.4.1)
107
+ activejob (= 6.1.4.1)
108
+ activemodel (= 6.1.4.1)
109
+ activerecord (= 6.1.4.1)
110
+ activestorage (= 6.1.4.1)
111
+ activesupport (= 6.1.4.1)
112
+ bundler (>= 1.15.0)
113
+ railties (= 6.1.4.1)
114
+ sprockets-rails (>= 2.0.0)
115
+ rails-dom-testing (2.0.3)
116
+ activesupport (>= 4.2.0)
117
+ nokogiri (>= 1.6)
118
+ rails-html-sanitizer (1.4.2)
119
+ loofah (~> 2.3)
120
+ railties (6.1.4.1)
121
+ actionpack (= 6.1.4.1)
122
+ activesupport (= 6.1.4.1)
123
+ method_source
124
+ rake (>= 0.13)
125
+ thor (~> 1.0)
126
+ rainbow (3.0.0)
127
+ rake (13.0.6)
128
+ regexp_parser (2.1.1)
129
+ rexml (3.2.5)
130
+ rspec (3.10.0)
131
+ rspec-core (~> 3.10.0)
132
+ rspec-expectations (~> 3.10.0)
133
+ rspec-mocks (~> 3.10.0)
134
+ rspec-core (3.10.1)
135
+ rspec-support (~> 3.10.0)
136
+ rspec-expectations (3.10.1)
137
+ diff-lcs (>= 1.2.0, < 2.0)
138
+ rspec-support (~> 3.10.0)
139
+ rspec-mocks (3.10.2)
140
+ diff-lcs (>= 1.2.0, < 2.0)
141
+ rspec-support (~> 3.10.0)
142
+ rspec-support (3.10.2)
143
+ rubocop (1.22.0)
144
+ parallel (~> 1.10)
145
+ parser (>= 3.0.0.0)
146
+ rainbow (>= 2.2.2, < 4.0)
147
+ regexp_parser (>= 1.8, < 3.0)
148
+ rexml
149
+ rubocop-ast (>= 1.12.0, < 2.0)
150
+ ruby-progressbar (~> 1.7)
151
+ unicode-display_width (>= 1.4.0, < 3.0)
152
+ rubocop-ast (1.12.0)
153
+ parser (>= 3.0.1.1)
154
+ ruby-progressbar (1.11.0)
155
+ sprockets (4.0.2)
156
+ concurrent-ruby (~> 1.0)
157
+ rack (> 1, < 3)
158
+ sprockets-rails (3.2.2)
159
+ actionpack (>= 4.0)
160
+ activesupport (>= 4.0)
161
+ sprockets (>= 3.0.0)
162
+ thor (1.1.0)
163
+ tzinfo (2.0.4)
164
+ concurrent-ruby (~> 1.0)
165
+ unicode-display_width (2.1.0)
166
+ websocket-driver (0.7.5)
167
+ websocket-extensions (>= 0.1.0)
168
+ websocket-extensions (0.1.5)
169
+ zeitwerk (2.4.2)
170
+
171
+ PLATFORMS
172
+ ruby
173
+
174
+ DEPENDENCIES
175
+ firebase_auth_for_ruby!
176
+ jwt
177
+ rails (~> 6.1.4)
178
+ rake
179
+ rspec
180
+ rubocop
181
+
182
+ BUNDLED WITH
183
+ 2.2.28
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 shuntagami
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # FirebaseAuthForRuby
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_auth_for_ruby`. 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
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'firebase_auth_for_ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install firebase_auth_for_ruby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/firebase_auth_for_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/firebase_auth_for_ruby/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the FirebaseAuthForRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/firebase_auth_for_ruby/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rubocop/rake_task"
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "firebase_auth_for_ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ require_relative "lib/firebase_auth_for_ruby/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "firebase_auth_for_ruby"
5
+ spec.version = FirebaseAuthForRuby::VERSION
6
+ spec.authors = ["shuntagami"]
7
+ spec.email = ["shuntagami23@gmail.com"]
8
+
9
+ spec.summary = "Firebase Authentication for Ruby client"
10
+ spec.homepage = "https://github.com/shuntagami/firebase_auth_for_ruby"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = ">= 3.0.0"
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject do |f|
20
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
21
+ end
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ # Uncomment to register a new dependency of your gem
28
+ # spec.add_dependency "example-gem", "~> 1.0"
29
+
30
+ # For more information and examples about making a new gem, checkout our
31
+ # guide at: https://bundler.io/guides/creating_gem.html
32
+ end
@@ -0,0 +1,117 @@
1
+ require "json"
2
+ require "jwt"
3
+
4
+ module Firebase
5
+ module Authentication
6
+ ALGORITHM = "RS256".freeze
7
+ ISSUER_BASE_URL = "https://securetoken.google.com/".freeze
8
+ CLIENT_CERT_URL = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com".freeze
9
+
10
+ class << self
11
+ def verify(token)
12
+ raise "id token must be a String" unless token.is_a?(String)
13
+
14
+ full_decoded_token = _decode_token(token)
15
+
16
+ err_msg = _validate_jwt(full_decoded_token)
17
+ raise err_msg if err_msg
18
+
19
+ public_key = _fetch_public_keys[full_decoded_token[:header]["kid"]]
20
+ unless public_key
21
+ raise 'Firebase ID token has "kid" claim which does not correspond to a known public key.'\
22
+ "Most likely the ID token is expired, so get a fresh token from your client app and try again."
23
+ end
24
+
25
+ certificate = OpenSSL::X509::Certificate.new(public_key)
26
+ decoded_token = _decode_token(token, certificate.public_key, verify: true,
27
+ options: { algorithm: ALGORITHM, verify_iat: true })
28
+
29
+ {
30
+ "uid" => decoded_token[:payload]["sub"],
31
+ "decoded_token" => decoded_token
32
+ }
33
+ end
34
+
35
+ def create_custom_token(uid, claims = {})
36
+ private_key = OpenSSL::PKey::RSA.new Global.firebase.private_key.gsub('\\n', "\n")
37
+ service_account_email = Global.firebase.client_email
38
+ now_seconds = Time.now.to_i
39
+ payload = { iss: service_account_email,
40
+ sub: service_account_email,
41
+ aud: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
42
+ iat: now_seconds,
43
+ exp: now_seconds + (60 * 60),
44
+ uid: uid,
45
+ claims: claims }
46
+ JWT.encode payload, private_key, "RS256"
47
+ end
48
+
49
+ private
50
+
51
+ def _decode_token(token, key = nil, verify: false, options: {})
52
+ begin
53
+ decoded_token = JWT.decode(token, key, verify, options)
54
+ rescue JWT::ExpiredSignature => e
55
+ raise "Firebase ID token has expired. Get a fresh token from your client app and try again. #{e.message}"
56
+ rescue JWT::InvalidAudError, JWT::DecodeError, JWT::VerificationError => e
57
+ raise "Firebase JWT Error. #{e.message}"
58
+ rescue StandardError => e
59
+ raise "Firebase ID token has invalid signature. #{e.message}"
60
+ end
61
+
62
+ {
63
+ payload: decoded_token[0],
64
+ header: decoded_token[1]
65
+ }
66
+ end
67
+
68
+ def _fetch_public_keys
69
+ uri = URI.parse(CLIENT_CERT_URL)
70
+ https = Net::HTTP.new(uri.host, uri.port)
71
+ https.use_ssl = true
72
+
73
+ res = https.start do
74
+ https.get(uri.request_uri)
75
+ end
76
+ data = JSON.parse(res.body)
77
+
78
+ if data["error"]
79
+ msg = "Error fetching public keys for Google certs: #{data["error"]}"
80
+ msg += " (#{res["error_description"]})" if data["error_description"]
81
+
82
+ raise msg
83
+ end
84
+
85
+ data
86
+ end
87
+
88
+ def _validate_jwt(json)
89
+ error = _validate_jwt_header(json[:header])
90
+ error || _validate_jwt_payload(json[:payload])
91
+ end
92
+
93
+ def _validate_jwt_header(header)
94
+ return 'Firebase ID token has no "kid" claim.' unless header["kid"]
95
+
96
+ return "Firebase ID token has incorrect algorithm. Expected \"#{ALGORITHM}\" but got \"#{header["alg"]}\"."\
97
+ unless header["alg"] == ALGORITHM
98
+ end
99
+
100
+ def _validate_jwt_payload(payload)
101
+ project_id = Global.firebase.project_id
102
+ unless payload["aud"] == project_id
103
+ return "Firebase ID token has incorrect \'aud\' (audience) claim. Expected \"#{project_id}\" but got \"#{payload["aud"]}\"."
104
+ end
105
+
106
+ issuer = ISSUER_BASE_URL + project_id
107
+ unless payload["iss"] == issuer
108
+ return "Firebase ID token has incorrect \'iss\' (issuer) claim. Expected \"#{issuer}\" but got \"#{payload["iss"]}\"."
109
+ end
110
+
111
+ return 'Firebase ID token has no "sub" (subject) claim.' unless payload["sub"].is_a?(String)
112
+ return 'Firebase ID token has an empty string "sub" (subject) claim.' if payload["sub"].empty?
113
+ return 'Firebase ID token has "sub" (subject) claim longer than 128 characters.' if payload["sub"].size > 128
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Firebase
4
+ module Authentication
5
+ module Config
6
+ BASE_URI = "https://identitytoolkit.googleapis.com/v1/accounts:"
7
+
8
+ GET_ACCOUNT_INFO = "lookup?key="
9
+ DELETE_ACCOUNT = "delete?key="
10
+ FETCH_PROVIDERS_FOR_EMAIL = "createAuthUri?key="
11
+ RESET_PASSWORD = "sendOobCode?key="
12
+ SIGN_IN_EMAIL = "signInWithPassword?key="
13
+ SIGN_IN_OAUTH = "signInWithIdp?key="
14
+ SIGN_UP_EMAIL = "signUp?key="
15
+ UPDATE_ACCOUNT_INFO = "update?key="
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,75 @@
1
+ require "json"
2
+ require "net/http"
3
+ require "rails"
4
+ require "uri"
5
+ require "firebase/config"
6
+
7
+ module Firebase
8
+ module Authentication
9
+ class Service
10
+ def initialize(api_key, logger = Rails.logger)
11
+ @logger = logger
12
+ @api_key = api_key
13
+ end
14
+
15
+ def change_email(token, email)
16
+ res = fetch(:post, Config::UPDATE_ACCOUNT_INFO, { idToken: token, email: email, returnSecureToken: true })
17
+ res.value
18
+ res
19
+ end
20
+
21
+ def change_password(token, password)
22
+ res = fetch(:post, Config::UPDATE_ACCOUNT_INFO, { idToken: token, passsord: password, returnSecureToken: true })
23
+ res.value
24
+ res
25
+ end
26
+
27
+ def delete_account(token)
28
+ res = fetch(:post, Config::DELETE_ACCOUNT, { idToken: token })
29
+ res.value
30
+ res
31
+ end
32
+
33
+ def get_account_info(token)
34
+ res = fetch(:post, Config::GET_ACCOUNT_INFO, { idToken: token })
35
+ res.value
36
+ res
37
+ end
38
+
39
+ def send_password_reset_email(email)
40
+ res = fetch(:post, Config::RESET_PASSWORD, { requestType: "PASSWORD_RESET", email: email })
41
+ res.value
42
+ res
43
+ end
44
+
45
+ def sign_in_email(email, password)
46
+ res = fetch(:post, Config::SIGN_IN_EMAIL, { email: email, passsord: password, returnSecureToken: true })
47
+ res.value
48
+ res
49
+ end
50
+
51
+ def sign_up(email, password)
52
+ res = fetch(:post, Config::SIGN_UP_EMAIL, { email: email, password: password, returnSecureToken: true })
53
+ res.value
54
+ res
55
+ end
56
+
57
+ private
58
+
59
+ def fetch(verb, path, body = nil)
60
+ uri = URI.parse(Config::BASE_URI + path + @api_key)
61
+ request = case verb
62
+ when :get
63
+ Net::HTTP::Get.new(uri)
64
+ when :post
65
+ Net::HTTP::Post.new(uri)
66
+ end
67
+ request.content_type = "application/json"
68
+ request.body = JSON.dump(body)
69
+ Net::HTTP.start(uri.host, uri.port, { use_ssl: true }) do |http|
70
+ http.request(request)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module FirebaseAuthForRuby
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,6 @@
1
+ require_relative "firebase_auth_for_ruby/version"
2
+
3
+ module FirebaseAuthForRuby
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase_auth_for_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shuntagami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - shuntagami23@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - firebase_auth_for_ruby.gemspec
32
+ - lib/firebase/authentication.rb
33
+ - lib/firebase/config.rb
34
+ - lib/firebase/service.rb
35
+ - lib/firebase_auth_for_ruby.rb
36
+ - lib/firebase_auth_for_ruby/version.rb
37
+ homepage: https://github.com/shuntagami/firebase_auth_for_ruby
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/shuntagami/firebase_auth_for_ruby
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.2.22
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Firebase Authentication for Ruby client
61
+ test_files: []