firebase_auth 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 149cb50806f0f024c102a3b43a43b7fb52bcb11deb55639321dc1e5f0b7b0a0c
4
+ data.tar.gz: 3c6f03309195161399b618f17a98b914e0f7a1329c82087a2d5d5597570488c6
5
+ SHA512:
6
+ metadata.gz: 79d19e062517ea6a1ac84fd56f15250c6fa3aa4947dfa11039e0023db0cbdcb2b430a82eb08cadaa615fafaab8e7e4bf13b591080ffdc186c4dc6ee90afa46e4
7
+ data.tar.gz: 753f332ba91bca2c64e1596d693dd465a707f505d9e7f1dc76d7b0933691eaa2ba1f5022a46901ee12c2026751f35aebbb28b59eb809439f045cac369b13c4a3
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake', '~> 12.0'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Injung Chung
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.
@@ -0,0 +1,40 @@
1
+ # FirebaseAuth
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`. 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'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install firebase_auth
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. 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 tags, 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.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ require_relative 'lib/firebase_auth/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'firebase_auth'
5
+ spec.version = FirebaseAuth::VERSION
6
+ spec.authors = ['mu29']
7
+ spec.email = ['mu29@yeoubi.net']
8
+
9
+ spec.summary = 'Verify & decode Firebase ID token on Ruby'
10
+ spec.homepage = 'https://github.com/mu29/firebase_auth'
11
+ spec.license = 'MIT'
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+
14
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'httparty'
20
+ end
@@ -0,0 +1,42 @@
1
+ require 'httparty'
2
+ require 'firebase_auth/id_token_verifier'
3
+ require 'firebase_auth/public_keys'
4
+
5
+ module FirebaseAuth
6
+ class Auth
7
+ include Singleton
8
+
9
+ def initialize
10
+ refresh
11
+ end
12
+
13
+ def public_keys
14
+ resolve { @public_keys }
15
+ end
16
+
17
+ def verify_id_token(id_token)
18
+ result = resolve { @id_token_verifier.verify(id_token) }
19
+
20
+ if result
21
+ OpenStruct.new(result.payload)
22
+ end
23
+ end
24
+
25
+ class << self
26
+ delegate :verify_id_token, :public_keys, to: :instance
27
+ end
28
+
29
+ private
30
+
31
+ def refresh
32
+ @public_keys = PublicKeys.new
33
+ @id_token_verifier = IDTokenVerifier.new(@public_keys)
34
+ end
35
+
36
+ def resolve
37
+ refresh unless @public_keys.valid?
38
+
39
+ yield
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module FirebaseAuth
2
+ class IDTokenVerifier
3
+ JWT_OPTIONS = { algorithm: 'RS256', verify_iat: true }
4
+
5
+ def initialize(public_keys)
6
+ @public_keys = public_keys
7
+ end
8
+
9
+ def verify(id_token)
10
+ kid = JWT.decode(id_token, nil, false).last['kid'] rescue nil
11
+ decode_jwt(id_token, @public_keys.look_up(kid))
12
+ end
13
+
14
+ private
15
+
16
+ def decode_jwt(id_token, x509)
17
+ JWT.decode(id_token, x509.public_key, true, JWT_OPTIONS) rescue nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,39 @@
1
+ module FirebaseAuth
2
+ class PublicKeys
3
+ URL = 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com'
4
+ EXPIRES_HEADER = 'expires'
5
+
6
+ attr_reader :response
7
+
8
+ delegate :keys, :values, to: :data
9
+
10
+ def initialize
11
+ @response = fetch
12
+ end
13
+
14
+ def valid?
15
+ Time.now.utc < time_to_expire
16
+ end
17
+
18
+ def data
19
+ @parsed_body ||= JSON.parse(response.body)
20
+ end
21
+
22
+ def look_up(kid)
23
+ @certificate_hash ||= Hash[data.map { |k, v| [k, OpenSSL::X509::Certificate.new(v)] }]
24
+ @certificate_hash[kid]
25
+ end
26
+
27
+ private
28
+
29
+ def time_to_expire
30
+ @time_to_expire ||= Time.parse(
31
+ response.headers[EXPIRES_HEADER]
32
+ )
33
+ end
34
+
35
+ def fetch
36
+ HTTParty.get(URL)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module FirebaseAuth
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - mu29
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - mu29@yeoubi.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - firebase_auth.gemspec
40
+ - lib/firebase_auth.rb
41
+ - lib/firebase_auth/id_token_verifier.rb
42
+ - lib/firebase_auth/public_keys.rb
43
+ - lib/firebase_auth/version.rb
44
+ homepage: https://github.com/mu29/firebase_auth
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Verify & decode Firebase ID token on Ruby
67
+ test_files: []