firebase_id_token 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +178 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/firebase_id_token.gemspec +38 -0
- data/lib/firebase_id_token.rb +57 -0
- data/lib/firebase_id_token/certificates.rb +170 -0
- data/lib/firebase_id_token/configuration.rb +12 -0
- data/lib/firebase_id_token/exceptions/certificates_request_error.rb +11 -0
- data/lib/firebase_id_token/exceptions/certificates_ttl_error.rb +11 -0
- data/lib/firebase_id_token/exceptions/no_certificates_error.rb +10 -0
- data/lib/firebase_id_token/signature.rb +105 -0
- data/lib/firebase_id_token/version.rb +3 -0
- metadata +206 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9e4caa21b36b9c1bc8383297066ee34391d8c3f5
|
4
|
+
data.tar.gz: 424f1dadf56dc853f1f3ce2b2130aa272494dca0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3070735217a03083dd6f0ced71fc57f52954786b7adabcfdfb1f32f77aed9ab5904aac657012923d2ff68457250ee4f2948880372bed6f12868c9bcc4dbcea42
|
7
|
+
data.tar.gz: a6ffd3d4eabf61cfc7ce157dd749ae25923835f59360f171be37e076db8489cd4ba16412b3cf062c065aea7d00ec706b88a176a2503d3f3a8f504417ef9d108d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at f.schuindtcs@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Fernando Schuindt
|
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,178 @@
|
|
1
|
+
# Ruby Firebase ID Token verifier (pre-release)
|
2
|
+
|
3
|
+
![Alt text](https://api.travis-ci.org/fschuindt/firebase_id_token.svg?branch=master)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/fschuindt/firebase_id_token/badges/gpa.svg)](https://codeclimate.com/github/fschuindt/firebase_id_token)
|
5
|
+
[![Issue Count](https://codeclimate.com/github/fschuindt/firebase_id_token/badges/issue_count.svg)](https://codeclimate.com/github/fschuindt/firebase_id_token)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/fschuindt/firebase_id_token/badges/coverage.svg)](https://codeclimate.com/github/fschuindt/firebase_id_token/coverage)
|
7
|
+
|
8
|
+
A Ruby gem to verify the signature of Firebase ID Tokens. It uses Redis to store Google's x509 certificates and manage their expiration time, so you don't need to request Google's API in every execution and can access it as fast as reading from memory.
|
9
|
+
|
10
|
+
It also checks the JWT payload parameters as recommended [here](https://firebase.google.com/docs/auth/admin/verify-id-tokens) by Firebase official documentation.
|
11
|
+
|
12
|
+
## Pre-release Notes
|
13
|
+
|
14
|
+
**This gem was developed recently and needs real world feedback.**
|
15
|
+
|
16
|
+
If you are going to use it in production environment, please note that I am still testing it. It has realistc RSpec examples that uses real X509 certificates and signed JWT to perform tests and I can say it's working great. But using it implies in security risks, you should be aware.
|
17
|
+
|
18
|
+
Feel free to open any issue or to [contact me](https://fschuindt.github.io/blog/about/) regarding it's performance.
|
19
|
+
|
20
|
+
## Docs
|
21
|
+
|
22
|
+
+ http://www.rubydoc.info/gems/firebase_id_token
|
23
|
+
|
24
|
+
## Requirements
|
25
|
+
|
26
|
+
+ Redis
|
27
|
+
|
28
|
+
## Installing
|
29
|
+
|
30
|
+
```
|
31
|
+
gem install firebase_id_token
|
32
|
+
```
|
33
|
+
|
34
|
+
or in your Gemfile
|
35
|
+
```
|
36
|
+
gem 'firebase_id_token'
|
37
|
+
```
|
38
|
+
then
|
39
|
+
```
|
40
|
+
bundle install
|
41
|
+
```
|
42
|
+
|
43
|
+
## Configuration
|
44
|
+
|
45
|
+
It's needed to set up your Firebase Project ID.
|
46
|
+
|
47
|
+
If you are using Rails, this should probably go into `config/initializers/firebase_id_token.rb`.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
FirebaseIdToken.configure do |config|
|
51
|
+
config.project_ids = ['your-firebase-project-id']
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
`project_ids` must be a Array.
|
56
|
+
|
57
|
+
*If you want to verify signatures from more than one Firebase project, just add more Project IDs to the list.*
|
58
|
+
|
59
|
+
You can also pass a Redis instance to `config` if you are not using Redis defaults.
|
60
|
+
In this case you must have the gem `redis` in your `Gemfile`.
|
61
|
+
```ruby
|
62
|
+
FirebaseIdToken.configure do |config|
|
63
|
+
config.project_ids = ['your-firebase-project-id']
|
64
|
+
congig.redis = Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
Otherwise it will use just `Redis.new` as the instance.
|
69
|
+
|
70
|
+
## Usage
|
71
|
+
|
72
|
+
### Downloading Certificates
|
73
|
+
|
74
|
+
Before verifying tokens, you need to download Google's x509 certificates.
|
75
|
+
|
76
|
+
To do it simply:
|
77
|
+
```ruby
|
78
|
+
FirebaseIdToken::Certificates.request
|
79
|
+
```
|
80
|
+
|
81
|
+
It will download the certificates and save it in Redis, but only if Redis certificates database is empty. To force download and override Redis database, use:
|
82
|
+
```ruby
|
83
|
+
FirebaseIdToken::Certificates.request_anyway
|
84
|
+
```
|
85
|
+
|
86
|
+
Google give us information about the certificates expiration time, it's used to set a Redis TTL (Time-To-Live) when saving it. By doing so, the certificates will be automatically deleted after it's expiration.
|
87
|
+
|
88
|
+
You can access informations about it:
|
89
|
+
```ruby
|
90
|
+
# Boolean representing the presence of certificates in Redis database.
|
91
|
+
FirebaseIdToken::Certificates.present?
|
92
|
+
=> true
|
93
|
+
|
94
|
+
# How many seconds until the certificates expiration.
|
95
|
+
FirebaseIdToken::Certificates.ttl
|
96
|
+
=> 22352
|
97
|
+
|
98
|
+
# List of all certificates in database.
|
99
|
+
FirebaseIdToken::Certificates.all
|
100
|
+
=> [{"ec8f292sd30224afac5c55540df66d1f999d" => <OpenSSL::X509::Certificate: [...]
|
101
|
+
|
102
|
+
# Returns the respective certificate of a given Key ID.
|
103
|
+
FirebaseIdToken::Certificates.find('ec8f292sd30224afac5c55540df66d1f999d')
|
104
|
+
=> <OpenSSL::X509::Certificate: subject=<OpenSSL::X509 [...]
|
105
|
+
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Downloading in Rails
|
109
|
+
|
110
|
+
If you are using Rails it's preferred that you download the certificates in a background job, you can use [ActiveJob](http://guides.rubyonrails.org/active_job_basics.html) in this case.
|
111
|
+
```ruby
|
112
|
+
class RequestCertificatesJob < ApplicationJob
|
113
|
+
queue_as :default
|
114
|
+
|
115
|
+
def perform
|
116
|
+
FirebaseIdToken::Certificates.request_anyway
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
Then set it as a cron job, I recommend running it once every hour or every 30 minutes, it's up to you. Normally the certificates expiration time is around 5 to 6 hours, but it's good to perform it in a small fraction of this time.
|
122
|
+
|
123
|
+
You can use [whenever](https://github.com/javan/whenever) to do this.
|
124
|
+
|
125
|
+
### Verifying Tokens
|
126
|
+
|
127
|
+
Pass the Firebase ID Token to `FirebaseIdToken::Signature.verify` and it will return the token payload if everything is ok:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
FirebaseIdToken::Signature.verify(token)
|
131
|
+
=> {"iss"=>"https://securetoken.google.com/firebase-id-token", "name"=>"Bob Test", [...]
|
132
|
+
```
|
133
|
+
|
134
|
+
When either the signature is false or the token is invalid, it will return `nil`:
|
135
|
+
```ruby
|
136
|
+
FirebaseIdToken::Signature.verify(fake_token)
|
137
|
+
=> nil
|
138
|
+
|
139
|
+
FirebaseIdToken::Signature.verify('aaaaaa')
|
140
|
+
=> nil
|
141
|
+
```
|
142
|
+
|
143
|
+
**WARNING:** If you try to verify a signature without any certificates in Redis database it will raise a `FirebaseIdToken::Exceptions::NoCertificatesError`.
|
144
|
+
|
145
|
+
#### Payload Structure
|
146
|
+
|
147
|
+
In case you need, here's a example of the payload structure in JSON.
|
148
|
+
```json
|
149
|
+
{
|
150
|
+
"iss":"https://securetoken.google.com/firebase-id-token",
|
151
|
+
"name":"Ugly Bob",
|
152
|
+
"picture":"https://someurl.com/photo.jpg",
|
153
|
+
"aud":"firebase-id-token",
|
154
|
+
"auth_time":1492981192,
|
155
|
+
"user_id":"theUserID",
|
156
|
+
"sub":"theUserID",
|
157
|
+
"iat":1492981200,
|
158
|
+
"exp":33029000017,
|
159
|
+
"email":"uglybob@emailurl.com",
|
160
|
+
"email_verified":true,
|
161
|
+
"firebase":{
|
162
|
+
"identities":{
|
163
|
+
"google.com":[
|
164
|
+
"1010101010101010101"
|
165
|
+
],
|
166
|
+
"email":[
|
167
|
+
"uglybob@emailurl.com"
|
168
|
+
]
|
169
|
+
},
|
170
|
+
"sign_in_provider":"google.com"
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
```
|
175
|
+
|
176
|
+
## License
|
177
|
+
|
178
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "firebase_id_token"
|
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,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'firebase_id_token/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'firebase_id_token'
|
8
|
+
spec.version = FirebaseIdToken::VERSION
|
9
|
+
spec.authors = ['Fernando Schuindt']
|
10
|
+
spec.email = ['f.schuindtcs@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'A Firebase ID Token verifier.'
|
13
|
+
spec.description = "A Ruby gem to verify the signature of Firebase ID "\
|
14
|
+
"Tokens. It uses Redis to store Google's x509 certificates and manage "\
|
15
|
+
"their expiration time, so you don't need to request Google's API in "\
|
16
|
+
"every execution and can access it as fast as reading from memory."
|
17
|
+
spec.homepage = 'https://github.com/fschuindt/firebase_id_token'
|
18
|
+
spec.license = 'MIT'
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_development_dependency 'redcarpet', '~> 3.4.0'
|
31
|
+
spec.add_development_dependency 'simplecov', '~> 0.14.1'
|
32
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
|
33
|
+
|
34
|
+
spec.add_dependency 'redis', '~> 3.3.3'
|
35
|
+
spec.add_dependency 'redis-namespace', '~> 1.5.3'
|
36
|
+
spec.add_dependency 'httparty', '~> 0.14.0'
|
37
|
+
spec.add_dependency 'jwt', '~> 1.5.6'
|
38
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'redis-namespace'
|
3
|
+
require 'httparty'
|
4
|
+
require 'jwt'
|
5
|
+
|
6
|
+
require 'firebase_id_token/version'
|
7
|
+
require 'firebase_id_token/exceptions/no_certificates_error'
|
8
|
+
require 'firebase_id_token/exceptions/certificates_request_error'
|
9
|
+
require 'firebase_id_token/exceptions/certificates_ttl_error'
|
10
|
+
require 'firebase_id_token/configuration'
|
11
|
+
require 'firebase_id_token/certificates'
|
12
|
+
require 'firebase_id_token/signature'
|
13
|
+
|
14
|
+
# ## List of available methods
|
15
|
+
# + {Certificates.request}
|
16
|
+
# + {Certificates.request_anyway}
|
17
|
+
# + {Certificates.present?}
|
18
|
+
# + {Certificates.all}
|
19
|
+
# + {Certificates.ttl}
|
20
|
+
# + {Certificates.find}
|
21
|
+
# + {Signature.verify}
|
22
|
+
#
|
23
|
+
# ## Configuration
|
24
|
+
#
|
25
|
+
# You need to set your Firebase Project ID. Additionally you can set your Redis
|
26
|
+
# server instance in case you don't use Redis defaults.
|
27
|
+
#
|
28
|
+
# **WARNING:** Your `project_ids` must be a `Array`.
|
29
|
+
# ```
|
30
|
+
# FirebaseIdToken.configure do |config|
|
31
|
+
# config.project_ids = ['my-project-id', 'another-project-id']
|
32
|
+
# congig.redis = Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
|
33
|
+
# end
|
34
|
+
# ```
|
35
|
+
#
|
36
|
+
# **Defaults**
|
37
|
+
# + `project_ids` => `[]`
|
38
|
+
# + `redis` => `Redis.new`
|
39
|
+
#
|
40
|
+
module FirebaseIdToken
|
41
|
+
class << self
|
42
|
+
attr_writer :configuration
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.configuration
|
46
|
+
@configuration ||= Configuration.new
|
47
|
+
end
|
48
|
+
|
49
|
+
# Resets Configuration to defaults.
|
50
|
+
def self.reset
|
51
|
+
@configuration = Configuration.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.configure
|
55
|
+
yield configuration
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
module FirebaseIdToken
|
2
|
+
# Manage download and access of Google's x509 certificates. Keeps
|
3
|
+
# certificates on a Redis namespace database.
|
4
|
+
#
|
5
|
+
# ## Download & Access Certificates
|
6
|
+
#
|
7
|
+
# It describes two ways to download it: {.request} and {.request_anyway}.
|
8
|
+
# The first will only do something when Redis certificates database is empty,
|
9
|
+
# the second one will always request a new download to Google's API and
|
10
|
+
# override the database with the response.
|
11
|
+
#
|
12
|
+
# It's important to note that when saving a set of certificates, it will also
|
13
|
+
# set a Redis expiration time to match Google's API header `expires`. **After
|
14
|
+
# this time went out, Redis will automatically delete those certificates.**
|
15
|
+
#
|
16
|
+
# *To know how many seconds left until the expiration you can use {.ttl}.*
|
17
|
+
#
|
18
|
+
# When comes to accessing it, you can either use {.present?} to check if
|
19
|
+
# there's any data inside Redis certificates database or {.all} to obtain a
|
20
|
+
# `Array` of current certificates.
|
21
|
+
#
|
22
|
+
# @example `.request` will only download once
|
23
|
+
# FirebaseIdToken::Certificates.request # Downloads certificates.
|
24
|
+
# FirebaseIdToken::Certificates.request # Won't do anything.
|
25
|
+
# FirebaseIdToken::Certificates.request # Won't do anything either.
|
26
|
+
#
|
27
|
+
# @example `.request_anyway` will download always
|
28
|
+
# FirebaseIdToken::Certificates.request # Downloads certificates.
|
29
|
+
# FirebaseIdToken::Certificates.request_anyway # Downloads certificates.
|
30
|
+
# FirebaseIdToken::Certificates.request_anyway # Downloads certificates.
|
31
|
+
#
|
32
|
+
class Certificates
|
33
|
+
# A Redis instance.
|
34
|
+
attr_reader :redis
|
35
|
+
# Certificates saved in the Redis (JSON `String` or `nil`).
|
36
|
+
attr_reader :local_certs
|
37
|
+
|
38
|
+
# Google's x509 certificates API URL.
|
39
|
+
URL = 'https://www.googleapis.com/robot/v1/metadata/x509/'\
|
40
|
+
'securetoken@system.gserviceaccount.com'
|
41
|
+
|
42
|
+
# Calls {.request_anyway} only if there's no certificates on Redis. It will
|
43
|
+
# return `nil` otherwise.
|
44
|
+
#
|
45
|
+
# It will raise {Exceptions::CertificatesRequestError} if the request
|
46
|
+
# fails or {Exceptions::CertificatesTtlError} when Google responds with a
|
47
|
+
# low TTL, check out {.request_anyway} for more info.
|
48
|
+
#
|
49
|
+
# @return [nil, Hash]
|
50
|
+
# @see Certificates.request_anyway
|
51
|
+
def self.request
|
52
|
+
new.request
|
53
|
+
end
|
54
|
+
|
55
|
+
# Triggers a HTTPS request to Google's x509 certificates API. If it
|
56
|
+
# responds with a status `200 OK`, saves the request body into Redis and
|
57
|
+
# returns it as a `Hash`.
|
58
|
+
#
|
59
|
+
# Otherwise it will raise a {Exceptions::CertificatesRequestError}.
|
60
|
+
#
|
61
|
+
# This is really rare to happen, but Google may respond with a low TTL
|
62
|
+
# certificate. This is a `SecurityError` and will raise a
|
63
|
+
# {Exceptions::CertificatesTtlError}. You are mostly like to never face it.
|
64
|
+
# @return [Hash]
|
65
|
+
def self.request_anyway
|
66
|
+
new.request_anyway
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns `true` if there's certificates data on Redis, `false` otherwise.
|
70
|
+
# @example
|
71
|
+
# FirebaseIdToken::Certificates.present? #=> false
|
72
|
+
# FirebaseIdToken::Certificates.request
|
73
|
+
# FirebaseIdToken::Certificates.present? #=> true
|
74
|
+
def self.present?
|
75
|
+
! new.local_certs.empty?
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns a array of hashes, each hash is a single `{key => value}` pair
|
79
|
+
# containing the certificate KID `String` as key and a
|
80
|
+
# `OpenSSL::X509::Certificate` object of the respective certificate as
|
81
|
+
# value. Returns a empty `Array` when there's no certificates data on
|
82
|
+
# Redis.
|
83
|
+
# @return [Array]
|
84
|
+
# @example
|
85
|
+
# FirebaseIdToken::Certificates.request
|
86
|
+
# certs = FirebaseIdToken::Certificates.all
|
87
|
+
# certs.first #=> {"1d6d01c7[...]" => #<OpenSSL::X509::Certificate[...]}
|
88
|
+
def self.all
|
89
|
+
new.local_certs.map { |kid, cert|
|
90
|
+
{ kid => OpenSSL::X509::Certificate.new(cert) } }
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns a `OpenSSL::X509::Certificate` object of the requested Key ID
|
94
|
+
# (KID) if there's one. Returns `nil` otherwise.
|
95
|
+
#
|
96
|
+
# It will raise a {Exceptions::NoCertificatesError} if the Redis
|
97
|
+
# certificates database is empty.
|
98
|
+
# @param [String] kid Key ID
|
99
|
+
# @return [nil, OpenSSL::X509::Certificate]
|
100
|
+
# @example
|
101
|
+
# FirebaseIdToken::Certificates.request
|
102
|
+
# cert = FirebaseIdToken::Certificates.find "1d6d01f4w7d54c7[...]"
|
103
|
+
# #=> <OpenSSL::X509::Certificate: subject=#<OpenSSL [...]
|
104
|
+
def self.find(kid)
|
105
|
+
certs = new.local_certs
|
106
|
+
raise Exceptions::NoCertificatesError if certs.empty?
|
107
|
+
|
108
|
+
if certs[kid]
|
109
|
+
OpenSSL::X509::Certificate.new certs[kid]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns the current certificates TTL (Time-To-Live) in seconds. *Zero
|
114
|
+
# meaning no certificates.* It's the same as the certificates expiration
|
115
|
+
# time, use it to know when to request again.
|
116
|
+
# @return [Fixnum]
|
117
|
+
def self.ttl
|
118
|
+
ttl = new.redis.ttl('certificates')
|
119
|
+
ttl < 0 ? 0 : ttl
|
120
|
+
end
|
121
|
+
|
122
|
+
# Sets two instance attributes: `:redis` and `:local_certs`. Those are
|
123
|
+
# respectively a Redis instance from {FirebaseIdToken::Configuration} and
|
124
|
+
# the certificates in it.
|
125
|
+
def initialize
|
126
|
+
@redis = Redis::Namespace.new('firebase_id_token',
|
127
|
+
redis: FirebaseIdToken.configuration.redis)
|
128
|
+
@local_certs = read_certificates
|
129
|
+
end
|
130
|
+
|
131
|
+
# @see Certificates.request
|
132
|
+
def request
|
133
|
+
request_anyway if @local_certs.empty?
|
134
|
+
end
|
135
|
+
|
136
|
+
# @see Certificates.request_anyway
|
137
|
+
def request_anyway
|
138
|
+
@request = HTTParty.get URL
|
139
|
+
code = @request.code
|
140
|
+
if code == 200
|
141
|
+
save_certificates
|
142
|
+
else
|
143
|
+
raise Exceptions::CertificatesRequestError.new(code)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
def read_certificates
|
150
|
+
certs = @redis.get 'certificates'
|
151
|
+
certs ? JSON.parse(certs) : {}
|
152
|
+
end
|
153
|
+
|
154
|
+
def save_certificates
|
155
|
+
@redis.setex 'certificates', ttl, @request.body
|
156
|
+
@local_certs = read_certificates
|
157
|
+
end
|
158
|
+
|
159
|
+
def ttl
|
160
|
+
ttl = DateTime.parse(@request.headers['expires']).
|
161
|
+
to_time.to_i - Time.now.to_i
|
162
|
+
|
163
|
+
if ttl > 3600
|
164
|
+
ttl
|
165
|
+
else
|
166
|
+
raise Exceptions::CertificatesTtlError
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module FirebaseIdToken
|
2
|
+
# Handles the configuration object. Check out {FirebaseIdToken} for more
|
3
|
+
# info on how to use it.
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :redis, :project_ids
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@redis = Redis.new
|
9
|
+
@project_ids = []
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module FirebaseIdToken
|
2
|
+
module Exceptions
|
3
|
+
# @see FirebaseIdToken::Certificates.request
|
4
|
+
# @see FirebaseIdToken::Certificates.request_anyway
|
5
|
+
class CertificatesRequestError < StandardError
|
6
|
+
def initialize(code)
|
7
|
+
super "#{code} HTTP status when requesting Google's certificates."
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module FirebaseIdToken
|
2
|
+
module Exceptions
|
3
|
+
# @see FirebaseIdToken::Certificates.request
|
4
|
+
# @see FirebaseIdToken::Certificates.request_anyway
|
5
|
+
class CertificatesTtlError < StandardError
|
6
|
+
def initialize(message = "Google's x509 certificates has a low TTL.")
|
7
|
+
super message
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module FirebaseIdToken
|
2
|
+
# Deals with verifying if a given Firebase ID Token is signed by one of the
|
3
|
+
# Google's x509 certificates that Firebase uses.
|
4
|
+
#
|
5
|
+
# Also checks if the resulting JWT payload hash matches with:
|
6
|
+
# + `exp` Expiration time
|
7
|
+
# + `iat` Issued at time
|
8
|
+
# + User's Firebase Project ID
|
9
|
+
# + Non-empty UID
|
10
|
+
#
|
11
|
+
# ## Verifying a Firebase ID Token
|
12
|
+
#
|
13
|
+
# *Be sure to configure the gem to set your Firebase Project ID and a Redis
|
14
|
+
# server before move any forward.*
|
15
|
+
#
|
16
|
+
# **See the README for a complete guide.**
|
17
|
+
#
|
18
|
+
# **WARNING:** Trying to verify a token without any certificate saved in
|
19
|
+
# Redis certificates database raises a {Exceptions::NoCertificatesError}.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# FirebaseIdToken::Signature.verify(thrusty_token)
|
23
|
+
# => {"iss"=>"https://securetoken.google.com/your-project-id", [...]}
|
24
|
+
#
|
25
|
+
# FirebaseIdToken::Signature.verify(fake_token)
|
26
|
+
# => nil
|
27
|
+
#
|
28
|
+
# @see Signature#verify
|
29
|
+
class Signature
|
30
|
+
# Pre-default JWT algorithm parameters as recommended
|
31
|
+
# [here](https://goo.gl/uOK5Jx).
|
32
|
+
JWT_DEFAULTS = { algorithm: 'RS256', verify_iat: true }
|
33
|
+
|
34
|
+
# Returns the decoded JWT hash payload of the Firebase ID Token if the
|
35
|
+
# signature in the token matches with one of the certificates downloaded
|
36
|
+
# by {FirebaseIdToken::Certificates.request}, returns `nil` otherwise.
|
37
|
+
#
|
38
|
+
# It will also return `nil` when it fails in checking if all the required
|
39
|
+
# JWT fields are valid, as recommended [here](https://goo.gl/yOrZZX) by
|
40
|
+
# Firebase oficial documentation.
|
41
|
+
#
|
42
|
+
# Note that it will raise a {Exceptions::NoCertificatesError} if the Redis
|
43
|
+
# certificates database is empty. Ensure to call {Certificates.request}
|
44
|
+
# before, ideally in a background job if you are using Rails.
|
45
|
+
# @return [nil, Hash]
|
46
|
+
def self.verify(jwt_token)
|
47
|
+
new(jwt_token).verify
|
48
|
+
end
|
49
|
+
|
50
|
+
# Loads attributes: `:project_ids` from {FirebaseIdToken::Configuration},
|
51
|
+
# and `:kid`, `:jwt_token` from the related `jwt_token`.
|
52
|
+
# @param [String] jwt_token Firebase ID Token
|
53
|
+
def initialize(jwt_token)
|
54
|
+
@project_ids = FirebaseIdToken.configuration.project_ids
|
55
|
+
@kid = extract_kid(jwt_token)
|
56
|
+
@jwt_token = jwt_token
|
57
|
+
end
|
58
|
+
|
59
|
+
# @see Signature.verify
|
60
|
+
def verify
|
61
|
+
certificate = FirebaseIdToken::Certificates.find(@kid)
|
62
|
+
if certificate
|
63
|
+
payload = decode_jwt_payload(@jwt_token, certificate.public_key)
|
64
|
+
authorize payload
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def extract_kid(jwt_token)
|
71
|
+
JWT.decode(jwt_token, nil, false).last['kid']
|
72
|
+
rescue StandardError
|
73
|
+
'none'
|
74
|
+
end
|
75
|
+
|
76
|
+
def decode_jwt_payload(token, cert_key)
|
77
|
+
JWT.decode(token, cert_key, true, JWT_DEFAULTS).first
|
78
|
+
rescue StandardError
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
|
82
|
+
def authorize(payload)
|
83
|
+
if payload && authorized?(payload)
|
84
|
+
payload
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def authorized?(payload)
|
89
|
+
still_valid?(payload) &&
|
90
|
+
@project_ids.include?(payload['aud']) &&
|
91
|
+
issuer_authorized?(payload) &&
|
92
|
+
! payload['sub'].empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
def still_valid?(payload)
|
96
|
+
payload['exp'].to_i > Time.now.to_i &&
|
97
|
+
payload['iat'].to_i < Time.now.to_i
|
98
|
+
end
|
99
|
+
|
100
|
+
def issuer_authorized?(payload)
|
101
|
+
issuers = @project_ids.map { |i| "https://securetoken.google.com/#{i}" }
|
102
|
+
issuers.include? payload['iss']
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: firebase_id_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fernando Schuindt
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redcarpet
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.4.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.4.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.14.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.14.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: codeclimate-test-reporter
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.0.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.0.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: redis
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.3.3
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.3.3
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: redis-namespace
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.5.3
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.5.3
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: httparty
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.14.0
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.14.0
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: jwt
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 1.5.6
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.5.6
|
153
|
+
description: A Ruby gem to verify the signature of Firebase ID Tokens. It uses Redis
|
154
|
+
to store Google's x509 certificates and manage their expiration time, so you don't
|
155
|
+
need to request Google's API in every execution and can access it as fast as reading
|
156
|
+
from memory.
|
157
|
+
email:
|
158
|
+
- f.schuindtcs@gmail.com
|
159
|
+
executables: []
|
160
|
+
extensions: []
|
161
|
+
extra_rdoc_files: []
|
162
|
+
files:
|
163
|
+
- ".gitignore"
|
164
|
+
- ".rspec"
|
165
|
+
- ".travis.yml"
|
166
|
+
- CODE_OF_CONDUCT.md
|
167
|
+
- Gemfile
|
168
|
+
- LICENSE.txt
|
169
|
+
- README.md
|
170
|
+
- Rakefile
|
171
|
+
- bin/console
|
172
|
+
- bin/setup
|
173
|
+
- firebase_id_token.gemspec
|
174
|
+
- lib/firebase_id_token.rb
|
175
|
+
- lib/firebase_id_token/certificates.rb
|
176
|
+
- lib/firebase_id_token/configuration.rb
|
177
|
+
- lib/firebase_id_token/exceptions/certificates_request_error.rb
|
178
|
+
- lib/firebase_id_token/exceptions/certificates_ttl_error.rb
|
179
|
+
- lib/firebase_id_token/exceptions/no_certificates_error.rb
|
180
|
+
- lib/firebase_id_token/signature.rb
|
181
|
+
- lib/firebase_id_token/version.rb
|
182
|
+
homepage: https://github.com/fschuindt/firebase_id_token
|
183
|
+
licenses:
|
184
|
+
- MIT
|
185
|
+
metadata: {}
|
186
|
+
post_install_message:
|
187
|
+
rdoc_options: []
|
188
|
+
require_paths:
|
189
|
+
- lib
|
190
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
|
+
requirements:
|
197
|
+
- - ">="
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
version: '0'
|
200
|
+
requirements: []
|
201
|
+
rubyforge_project:
|
202
|
+
rubygems_version: 2.6.11
|
203
|
+
signing_key:
|
204
|
+
specification_version: 4
|
205
|
+
summary: A Firebase ID Token verifier.
|
206
|
+
test_files: []
|