google-auth-token_validator 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
+ SHA1:
3
+ metadata.gz: 9c85cade77aab2f374eac0dff1223b436a149202
4
+ data.tar.gz: c7b99dd26296a90cffb992a955a302511ac9427d
5
+ SHA512:
6
+ metadata.gz: 1d8b6992a81e86cb64a17d1bbb037e7199ac4810813ee15f1d53478475c1808c04875d55e7a81144b41e62d47c944acde50430886c8f75497e3275974976dc98
7
+ data.tar.gz: cc20fbe4694191a04a6d5ad86b9b7ddfe44e02161ecfd96d1dd3b06b025e5fcff147fb69394fd94fe59f8b34750a9d628d3aebe8b9ee50d30438d0f748443588
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ # rspec failure tracking
53
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.15.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0 - September 17, 2017
2
+
3
+ - initial implementation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in google_signin_token_validator.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google-auth-token_validator (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ coveralls (0.8.21)
11
+ json (>= 1.8, < 3)
12
+ simplecov (~> 0.14.1)
13
+ term-ansicolor (~> 1.3)
14
+ thor (~> 0.19.4)
15
+ tins (~> 1.6)
16
+ diff-lcs (1.3)
17
+ docile (1.1.5)
18
+ dotenv (2.2.1)
19
+ json (2.1.0)
20
+ method_source (0.8.2)
21
+ pry (0.10.4)
22
+ coderay (~> 1.1.0)
23
+ method_source (~> 0.8.1)
24
+ slop (~> 3.4)
25
+ rake (10.5.0)
26
+ rspec (3.6.0)
27
+ rspec-core (~> 3.6.0)
28
+ rspec-expectations (~> 3.6.0)
29
+ rspec-mocks (~> 3.6.0)
30
+ rspec-core (3.6.0)
31
+ rspec-support (~> 3.6.0)
32
+ rspec-expectations (3.6.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.6.0)
35
+ rspec-mocks (3.6.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.6.0)
38
+ rspec-support (3.6.0)
39
+ simplecov (0.14.1)
40
+ docile (~> 1.1.0)
41
+ json (>= 1.8, < 3)
42
+ simplecov-html (~> 0.10.0)
43
+ simplecov-html (0.10.2)
44
+ slop (3.6.0)
45
+ term-ansicolor (1.6.0)
46
+ tins (~> 1.0)
47
+ thor (0.19.4)
48
+ tins (1.15.0)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ bundler (~> 1.15)
55
+ coveralls (~> 0.8)
56
+ dotenv (~> 2.2)
57
+ google-auth-token_validator!
58
+ pry (~> 0.10)
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+
62
+ BUNDLED WITH
63
+ 1.16.0.pre.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Hamza Tayeb
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Google Auth Token Validator
2
+
3
+ [![Build Status](https://travis-ci.org/hamza/google-auth-token-validator-ruby.svg?branch=master)](https://travis-ci.org/hamza/google-auth-token-validator-ruby)
4
+ [![Coverage Status](https://coveralls.io/repos/github/hamza/google-auth-token-validator-ruby/badge.svg?branch=master)](https://coveralls.io/github/hamza/google-auth-token-validator-ruby?branch=master)
5
+
6
+ The Google Sign-In API gives OAuth2 JSON Web Tokens (JWT) as response data upon user sign-in. A necessary step for a service provider to trust such a token involves validating the token, rather than simply trusting the token, which would allow a malicious client to simply assert itself.
7
+
8
+ Google provides libraries in several languages (https://goo.gl/jkzS18) to serve this function, as well as an API endpoint that can outsource the task to Google's own servers (thereby introducing an additional network round trip into every authentication step), but a Ruby implementation is missing. This gem fills that gap.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'google-auth-token_validator' # doesn't work yet! stay tuned …
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install google-auth-token_validator
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ require "googleauth/token_validator"
30
+
31
+ client_id = "<your client ID>"
32
+ id_token = "<user's ID token>"
33
+
34
+ begin
35
+ valid = Google::Auth::TokenValidator.new(id_token, client_id).validate
36
+ rescue Google::Auth::TokenValidator::Error => e
37
+ puts e.message
38
+ valid = false
39
+ end
40
+
41
+ if valid
42
+ # trust this token
43
+ end
44
+ ```
45
+
46
+ ## Development
47
+
48
+ 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.
49
+
50
+ This gem uses [`dotenv`](https://github.com/bkeepers/dotenv) to set up the test environment. To enable local testing, you'll need to set up the following -
51
+
52
+ |Key|Description|
53
+ |---|---|
54
+ |`TEST_CLIENT_ID`|A Google OAuth client ID, used to compare ID tokens against|
55
+ |`TEST_CLIENT_SECRET`|A Google OAuth client secret, used to authorize token refreshes|
56
+ |`REFRESH_TOKEN`|A refresh token for a test user. It is recommended to use the [Google OAuth Playground](https://developers.google.com/oauthplayground/) to generate one|
57
+
58
+ **NOTE**: it is not recommended to use your real account for this. Instead use a test user.
59
+
60
+ Use the [Google Cloud Console](https://console.cloud.google.com) to set up an OAuth id/secret pair for use with testing.
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on [the repo](https://github.com/hamza/google_signin_token_validator).
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "googleauth/token_validator"
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,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "googleauth/token_validator/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "google-auth-token_validator"
9
+ spec.version = Google::Auth::TokenValidator::VERSION
10
+ spec.date = Time.now.strftime('%Y-%m-%d')
11
+ spec.authors = ["Hamza Tayeb"]
12
+ spec.email = ["hamza.tayeb@gmail.com"]
13
+
14
+ spec.summary = "Ruby gem to validate signed JSON Web Tokens from Google's sign-in API"
15
+ spec.description = <<-DESC
16
+ The Google Sign-In API gives OAuth2 JSON Web Tokens (JWT) as response data upon user sign-in.
17
+ A necessary step for a service provider to trust such a token involves validating the token,
18
+ rather than simply trusting the token, which would allow a malicious client to simply assert
19
+ itself.
20
+
21
+ Google provides libraries in several languages (https://goo.gl/jkzS18) to serve this function,
22
+ as well as an API endpoint that can outsource the task to Google's own servers (thereby
23
+ introducing an additional network round trip into every authentication step), but a Ruby
24
+ implementation is missing. This gem fills that gap.
25
+ DESC
26
+
27
+ spec.homepage = "https://github.com/hamza/google-signin-token-validator"
28
+ spec.license = "MIT"
29
+
30
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
31
+ f.match(%r{^(test|spec|features)/})
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.15"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "coveralls", "~> 0.8"
41
+ spec.add_development_dependency "pry", "~> 0.10"
42
+ spec.add_development_dependency "dotenv", "~> 2.2"
43
+ end
@@ -0,0 +1,87 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "base64"
4
+ require "openssl"
5
+
6
+ class Google::Auth::TokenValidator
7
+ GOOGLE_SIGNON_CERTS_URL = "https://www.googleapis.com/oauth2/v1/certs".freeze
8
+ ISSUERS = %w(accounts.google.com https://accounts.google.com).freeze
9
+ CLOCK_SKEW_SECONDS = 300 # five minutes
10
+ MAX_TOKEN_LIFETIME_SECONDS = 86400 # one day
11
+
12
+ attr_reader :signed, :signature, :envelope, :payload, :client_id
13
+
14
+ def initialize jwt, client_id
15
+ segments = jwt.split(".")
16
+
17
+ fail Error, "Wrong number of segments in token: #{jwt}" unless segments.size.eql? 3
18
+
19
+ @client_id = client_id
20
+
21
+ @signed = "#{segments[0]}.#{segments[1]}"
22
+ @signature = segments[2]
23
+
24
+ @envelope = JSON.parse Base64.decode64(segments[0])
25
+ @payload = JSON.parse Base64.decode64(segments[1])
26
+ end
27
+
28
+ def validate max_expiry = MAX_TOKEN_LIFETIME_SECONDS
29
+ unless [@signed, @signature, @envelope, @payload].all?
30
+ fail Error, "Validator was not properly initialized"
31
+ end
32
+
33
+ unless self.class._certs.keys.include? @envelope["kid"]
34
+ fail Error, "No matching Google cert found for envelope: #{@envelope}"
35
+ end
36
+
37
+ pem = self.class._certs[envelope["kid"]]
38
+ cert = OpenSSL::X509::Certificate.new pem
39
+ digest = OpenSSL::Digest::SHA256.new
40
+
41
+ verified = cert.public_key.verify(digest, Base64.urlsafe_decode64(@signature), @signed)
42
+
43
+ fail Error, "Token signature invalid" unless verified
44
+
45
+ fail Error, "No issue time in token" if @payload["iat"].to_s.empty?
46
+ fail Error, "No expiration time in token" if @payload["exp"].to_s.empty?
47
+
48
+ now = Time.now.to_i
49
+ earliest = @payload["iat"] - CLOCK_SKEW_SECONDS
50
+ latest = @payload["exp"] + CLOCK_SKEW_SECONDS
51
+
52
+ fail Error, "Expiration time too far in future" if @payload["exp"] >= Time.now.to_i + max_expiry
53
+ fail Error, "Token used too early" if now < earliest
54
+ fail Error, "Token used too late" if now > latest
55
+
56
+ unless ISSUERS.include? @payload["iss"]
57
+ fail Error, "Invalid issuer. Expected one of #{ISSUERS}, but got #{@payload["iss"]}"
58
+ end
59
+
60
+ if @client_id.is_a? Array
61
+ aud_verified = @client_id.include?(@payload["aud"])
62
+ elsif @client_id.is_a? String
63
+ aud_verified = @payload["aud"] == @client_id
64
+ else
65
+ fail Error, "Invalid client id(s)"
66
+ end
67
+
68
+ unless aud_verified
69
+ fail Error, "Wrong recipient - payload audience doesn't match required audience"
70
+ end
71
+
72
+ return true
73
+ end
74
+
75
+ class << self
76
+ attr_accessor :_cached_certs
77
+
78
+ def _certs
79
+ @_cached_certs ||= begin
80
+ uri = URI(GOOGLE_SIGNON_CERTS_URL)
81
+ JSON.parse Net::HTTP.get(uri)
82
+ end
83
+ end
84
+ end
85
+
86
+ class Error < StandardError; end
87
+ end
@@ -0,0 +1,7 @@
1
+ module Google
2
+ module Auth
3
+ class TokenValidator
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-auth-token_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hamza Tayeb
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-17 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.2'
97
+ description: |2
98
+ The Google Sign-In API gives OAuth2 JSON Web Tokens (JWT) as response data upon user sign-in.
99
+ A necessary step for a service provider to trust such a token involves validating the token,
100
+ rather than simply trusting the token, which would allow a malicious client to simply assert
101
+ itself.
102
+
103
+ Google provides libraries in several languages (https://goo.gl/jkzS18) to serve this function,
104
+ as well as an API endpoint that can outsource the task to Google's own servers (thereby
105
+ introducing an additional network round trip into every authentication step), but a Ruby
106
+ implementation is missing. This gem fills that gap.
107
+ email:
108
+ - hamza.tayeb@gmail.com
109
+ executables: []
110
+ extensions: []
111
+ extra_rdoc_files: []
112
+ files:
113
+ - ".coveralls.yml"
114
+ - ".gitignore"
115
+ - ".rspec"
116
+ - ".travis.yml"
117
+ - CHANGELOG.md
118
+ - Gemfile
119
+ - Gemfile.lock
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - bin/console
124
+ - bin/setup
125
+ - google-auth_token_validator.gemspec
126
+ - lib/googleauth/token_validator.rb
127
+ - lib/googleauth/token_validator/version.rb
128
+ homepage: https://github.com/hamza/google-signin-token-validator
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.6.8
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Ruby gem to validate signed JSON Web Tokens from Google's sign-in API
152
+ test_files: []