firebase_idtoken 0.1.2

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: 3493efd8dfd103e00f1610f82ac6d2e9336803fa551a9d25d614ab5e5b9ec138
4
+ data.tar.gz: bd346bdd9d4b6172ef13a28f0a5ce60d35f9bce74d955732e341e5f24bb913c6
5
+ SHA512:
6
+ metadata.gz: 4e89aca9efae5fd67fd99d7dea16d8458f12cc3327b7d489148fe94135e380ace3001a4906cac7347b481571801fef5345781e5b951dddcd3c389e9ee8340e45
7
+ data.tar.gz: 6d86abecbb28cec372e33dfd72fc729b5dec3073ff365d05531e001bb7a6a7e5e1bc2d97820b85658253549aad3e41ff4593279bf809b1f86cde550853e25bdd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /bundle
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in firebase_idtoken.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
9
+ gem 'pry-stack_explorer'
10
+ gem 'awesome_print'
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ firebase_idtoken (0.1.0)
5
+ jwt (~> 2.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ awesome_print (1.8.0)
11
+ binding_of_caller (0.8.0)
12
+ debug_inspector (>= 0.0.1)
13
+ coderay (1.1.2)
14
+ debug_inspector (0.0.3)
15
+ jwt (2.1.0)
16
+ method_source (0.9.0)
17
+ pry (0.11.3)
18
+ coderay (~> 1.1.0)
19
+ method_source (~> 0.9.0)
20
+ pry-stack_explorer (0.4.9.2)
21
+ binding_of_caller (>= 0.7)
22
+ pry (>= 0.9.11)
23
+ rake (10.5.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ awesome_print
30
+ bundler (~> 1.16)
31
+ firebase_idtoken!
32
+ pry
33
+ pry-stack_explorer
34
+ rake (~> 10.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 shunwitter
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,72 @@
1
+ # FirebaseIdtoken
2
+
3
+ Retrieved auth UID using ID token sent from client. (Ruby implementation)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'firebase_idtoken'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install firebase_idtoken
20
+
21
+ ## Configuration
22
+
23
+ ```ruby
24
+ FirebaseIdtoken.configure do |config|
25
+ config.project_ids = 'my-project-id' # Default: nil
26
+ config.cache_path = 'path/to/cache_file' # Default: tmp/firebase_public_key
27
+ config.cache_time = 60 * 60 * 2 # Default: 60 * 60
28
+ end
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require('firebase_idtoken')
35
+
36
+ # Token comes from client side
37
+ token = 'eyJhbGciOiJSUzI1NiIs.....ajmRoVHvI7A'
38
+ FirebaseIdtoken.verify(token)
39
+
40
+ # Result
41
+ => {"uid"=>"oyvaFVD2xxxxxxxxZPFVnH1X8Xv1",
42
+ "decoded_token"=>
43
+ {:payload=>
44
+ {"iss"=>"https://securetoken.google.com/your-project",
45
+ "name"=>"User Name",
46
+ "picture"=>
47
+ "https://lh5.googleusercontent.com/-xxxxxx/xxxxxxxxx/AAAAAAAAAT4/xxxxxxx/photo.jpg",
48
+ "aud"=>"your-project",
49
+ "auth_time"=>1522240359,
50
+ "user_id"=>"oyvaFVD2xxxxxxxxZPFVnH1X8Xv1",
51
+ "sub"=>"oyvaFVD2xxxxxxxxZPFVnH1X8Xv1",
52
+ "iat"=>1522240388,
53
+ "exp"=>1522243988,
54
+ "email"=>"youremail@example.com",
55
+ "email_verified"=>true,
56
+ "firebase"=>
57
+ {"identities"=>
58
+ {"google.com"=>["108658900000000862749"],
59
+ "email"=>["youremail@example.com"]},
60
+ "sign_in_provider"=>"google.com"}},
61
+ :header=>
62
+ {"alg"=>"RS256", "kid"=>"f5b18276a4866100000000c3e9434974d1f1db51"}}}
63
+ ```
64
+
65
+ ## Related Docs
66
+ - https://firebase.google.com/docs/auth/admin/verify-id-tokens
67
+
68
+
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "firebase_idtoken"
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,23 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'firebase_idtoken/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'firebase_idtoken'
8
+ spec.version = FirebaseIdtoken::VERSION
9
+ spec.authors = ['shunwitter']
10
+
11
+ spec.summary = %q{Ruby implementation to verify Google firebase ID token}
12
+ spec.description = %q{Retrieved UID on server side using ID token sent from client}
13
+ spec.homepage = 'https://github.com/shunwitter/firebase_idtoken'
14
+ spec.license = 'MIT'
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'jwt', '~> 2.1'
21
+ spec.add_development_dependency 'bundler', '~> 1.16'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
@@ -0,0 +1,9 @@
1
+ class FirebaseIdtoken::Configuration
2
+ attr_accessor :project_id, :cache_path, :cache_time
3
+
4
+ def initialize
5
+ @project_id = nil
6
+ @cache_path = 'tmp/firebase_public_key'
7
+ @cache_time = 60 * 60
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module FirebaseIdtoken
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,145 @@
1
+ require 'fileutils'
2
+ require 'net/http'
3
+ require 'jwt'
4
+
5
+ require "firebase_idtoken/version"
6
+ require 'firebase_idtoken/configuration'
7
+
8
+ module FirebaseIdtoken
9
+
10
+ # -------------------------------------------
11
+ # Config
12
+ #
13
+ # FirebaseIdtoken.configure do |config|
14
+ # config.project_ids = 'my-project-id'
15
+ # config.cache_path = 'path/to/cache_file'
16
+ # config.cache_time = 60 * 60 * 2
17
+ # end
18
+ # -------------------------------------------
19
+
20
+ class << self
21
+ attr_accessor :configuration
22
+
23
+ ALGORITHM = 'RS256'
24
+ ISSUER_BASE_URL = 'https://securetoken.google.com/'
25
+ CLIENT_CERT_URL = 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com'
26
+
27
+ def configure
28
+ yield configuration
29
+ end
30
+
31
+ def configuration
32
+ @configuration ||= Configuration.new
33
+ end
34
+
35
+ def verify(token)
36
+ raise 'id token must be a String' unless token.is_a?(String)
37
+
38
+ full_decoded_token = decode_token(token)
39
+
40
+ err_msg = validate_jwt(full_decoded_token)
41
+ raise err_msg if err_msg
42
+
43
+ public_key = fetch_public_keys[full_decoded_token[:header]['kid']]
44
+ unless public_key
45
+ raise 'Firebase ID token has "kid" claim which does not correspond to ' +
46
+ 'a known public key. Most likely the ID token is expired, so get a fresh token from your client ' +
47
+ 'app and try again.'
48
+ end
49
+
50
+ certificate = OpenSSL::X509::Certificate.new(public_key)
51
+ decoded_token = decode_token(token, certificate.public_key, true, { algorithm: ALGORITHM, verify_iat: true })
52
+
53
+ {
54
+ 'uid' => decoded_token[:payload]['sub'],
55
+ 'decoded_token' => decoded_token
56
+ }
57
+ end
58
+
59
+
60
+ private
61
+
62
+ def decode_token(token, key=nil, verify=false, options={})
63
+ begin
64
+ decoded_token = JWT.decode(token, key, verify, options)
65
+ rescue JWT::ExpiredSignature => e
66
+ raise 'Firebase ID token has expired. Get a fresh token from your client app and try again.'
67
+ rescue => e
68
+ raise "Firebase ID token has invalid signature. #{e.message}"
69
+ end
70
+
71
+ {
72
+ payload: decoded_token[0],
73
+ header: decoded_token[1]
74
+ }
75
+ end
76
+
77
+ def fetch_public_keys
78
+ cache_path = configuration.cache_path
79
+ cache_time = configuration.cache_time
80
+ data = nil
81
+ begin
82
+ # Fetch from cache if within an hour (UTC timezone)
83
+ s = File.stat(cache_path)
84
+ File.delete(cache_path) if (Time.now.utc - s.mtime.utc) > cache_time
85
+ data = JSON.parse(open(cache_path).read)
86
+ rescue => error
87
+ puts "#{error.message}: Fetching public key from Google..."
88
+
89
+ # Fetch keys from Google if no cache or after an hour
90
+ uri = URI.parse(CLIENT_CERT_URL)
91
+ https = Net::HTTP.new(uri.host, uri.port)
92
+ https.use_ssl = true
93
+
94
+ res = https.start {
95
+ https.get(uri.request_uri)
96
+ }
97
+ json_string = res.body
98
+ data = JSON.parse(json_string)
99
+
100
+ if (data['error']) then
101
+ msg = 'Error fetching public keys for Google certs: ' + data['error']
102
+ msg += " (#{res['error_description']})" if (data['error_description'])
103
+ raise msg
104
+ else
105
+ save_cache_file(json_string)
106
+ end
107
+ end
108
+ data
109
+ end
110
+
111
+ def save_cache_file(json_string)
112
+ cache_path = configuration.cache_path
113
+ dirname = File.dirname(cache_path)
114
+ unless File.directory?(dirname)
115
+ puts "Creating a directory: #{dirname}"
116
+ FileUtils.mkdir_p(dirname)
117
+ end
118
+ open cache_path, 'w' do |io|
119
+ io.write json_string
120
+ end
121
+ end
122
+
123
+ def validate_jwt(json)
124
+ project_id = configuration.project_id
125
+ raise 'You need to set Firebase project ID' unless project_id
126
+
127
+ payload = json[:payload]
128
+ header = json[:header]
129
+
130
+ return 'Firebase ID token has no "kid" claim.' unless header['kid']
131
+ return "Firebase ID token has incorrect algorithm. Expected \"#{ALGORITHM}\" but got \"#{header['alg']}\"." unless header['alg'] == ALGORITHM
132
+ return "Firebase ID token has incorrect \"aud\" (audience) claim. Expected \"#{project_id}\" but got \"#{payload['aud']}\"." unless payload['aud'] == project_id
133
+
134
+ issuer = ISSUER_BASE_URL + project_id
135
+ return "Firebase ID token has incorrect \"iss\" (issuer) claim. Expected \"#{issuer}\" but got \"#{payload['iss']}\"." unless payload['iss'] == issuer
136
+
137
+ return 'Firebase ID token has no "sub" (subject) claim.' unless payload['sub'].is_a?(String)
138
+ return 'Firebase ID token has an empty string "sub" (subject) claim.' if payload['sub'].empty?
139
+ return 'Firebase ID token has "sub" (subject) claim longer than 128 characters.' if payload['sub'].size > 128
140
+
141
+ nil
142
+ end
143
+
144
+ end
145
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase_idtoken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - shunwitter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Retrieved UID on server side using ID token sent from client
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - firebase_idtoken.gemspec
70
+ - lib/firebase_idtoken.rb
71
+ - lib/firebase_idtoken/configuration.rb
72
+ - lib/firebase_idtoken/version.rb
73
+ homepage: https://github.com/shunwitter/firebase_idtoken
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Ruby implementation to verify Google firebase ID token
97
+ test_files: []