omniauth-cloudiap 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4dd63aa4dba59ff3b4251103f4dc8202f9dfbe39753e967f47133a7cd7b4c907
4
+ data.tar.gz: e8aaf738e50bdb425c8cd6a57ac3bc5a3b9a7945323187e46b1498f2cc89225e
5
+ SHA512:
6
+ metadata.gz: 69b1f7761fd89ff7bcb3974ac08d3b836271020fb50dd324bb1226359a15d71a92c0aeb28d27c1f330eae27f406c8a1868f125666c3d5bf2fb0185a1c1d26b5b
7
+ data.tar.gz: 2270c3f8e4865b62f81825633b88b2fcdb35beb2fec1cbad77c45937f976e2acb1a6c3926f1adfe4e918d984a364011d7f7d78fcedd742d725d9aaea26cc9160
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,8 @@
1
+ Changelog
2
+ ===========
3
+
4
+
5
+ 1.0.0 - 2018-11-21
6
+ --------------------
7
+
8
+ - First release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
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 omniauth-cloudiap.gemspec
6
+ gemspec
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-cloudiap (0.1.0)
5
+ jwt
6
+ omniauth
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ hashie (3.5.7)
12
+ jwt (2.1.0)
13
+ minitest (5.11.3)
14
+ minitest-power_assert (0.3.0)
15
+ minitest
16
+ power_assert (>= 1.1)
17
+ omniauth (1.8.1)
18
+ hashie (>= 3.4.6, < 3.6.0)
19
+ rack (>= 1.6.2, < 3)
20
+ power_assert (1.1.3)
21
+ rack (2.0.6)
22
+ rack-test (1.1.0)
23
+ rack (>= 1.0, < 3)
24
+ rake (10.5.0)
25
+ timecop (0.9.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.17)
32
+ minitest (~> 5.0)
33
+ minitest-power_assert
34
+ omniauth-cloudiap!
35
+ rack-test
36
+ rake (~> 10.0)
37
+ timecop
38
+
39
+ BUNDLED WITH
40
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2018 holysugar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # OmniAuth::Cloudiap
2
+
3
+ OmniAuth strategy for Google Cloud IAP. Useful if you have application using omniauth-google-oauth2 and are moving it behind Google IAP.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "omniauth-cloudiap"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install omniauth-cloudiap
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ use OmniAuth::Builder do
25
+ provider :cloudiap
26
+ end
27
+ ```
28
+
29
+ If you know application's aud, you can validate it:
30
+
31
+ ```ruby
32
+ use OmniAuth::Builder do
33
+ provider :cloudiap, aud: "/projects/9999999999999/global/backendServices/9999999999999999999"
34
+ end
35
+ ```
36
+
37
+ If you don't need jwt verify, you can skip it (not recommended)
38
+
39
+ ```ruby
40
+ use OmniAuth::Builder do
41
+ provider :cloudiap, skip_jwt_verify: true
42
+ end
43
+ ```
44
+
45
+ In default, `env["omniauth.auth"]["info"]["name"]` is same as email. If you have usename dictionary, set :username_callback
46
+
47
+
48
+ ```ruby
49
+ use OmniAuth::Builder do
50
+ provider :cloudiap, username_callback: callback_object
51
+ end
52
+ ```
53
+
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/holysugar/omniauth-cloudiap.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "omniauth/cloudiap"
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__)
@@ -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,10 @@
1
+ require "omniauth/cloudiap/version"
2
+
3
+ module OmniAuth
4
+ module Cloudiap
5
+ class Error < StandardError; end
6
+ end
7
+ end
8
+
9
+ require "omniauth/strategies/cloudiap"
10
+
@@ -0,0 +1,93 @@
1
+ require 'jwt'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ module OmniAuth
6
+ module Cloudiap
7
+ class IAPJWT
8
+
9
+ class InvalidAudError < Error; end
10
+
11
+ def initialize(aud: nil)
12
+ @required_aud = aud
13
+ end
14
+
15
+ def decode_with_validate(token)
16
+ payload, header = validate(token)
17
+ { identifier: payload["sub"], email: payload["email"] }
18
+ end
19
+
20
+ def parse(token)
21
+ JWT.decode token, nil, false
22
+ end
23
+
24
+ def jwk_keys
25
+ @jwk_keys ||= begin
26
+ url = "https://www.gstatic.com/iap/verify/public_key-jwk"
27
+ open(url) { |f| JSON.parse(f.read) }
28
+ end
29
+ end
30
+
31
+ def jwk_key(token)
32
+ _, header = parse(token)
33
+ jwk = jwk_keys["keys"].find{|k| k["kid"] == header["kid"] }
34
+ curve_name = \
35
+ case jwk["crv"]
36
+ when "P-256"
37
+ "prime256v1"
38
+ when "P-384"
39
+ "secp384r1"
40
+ when "P-521"
41
+ "secp521r1"
42
+ else
43
+ raise AugumentError, "Unknown crv: #{jwk["crv"]}"
44
+ end
45
+ x = Base64.urlsafe_decode64(jwk["x"])
46
+ y = Base64.urlsafe_decode64(jwk["y"])
47
+
48
+ key = OpenSSL::PKey::EC.new(curve_name)
49
+ group = OpenSSL::PKey::EC::Group.new(curve_name)
50
+ bn = OpenSSL::BN.new(["04" + x.unpack("H*").first + y.unpack("H*").first].pack("H*"), 2)
51
+ key.public_key = OpenSSL::PKey::EC::Point.new(group, bn)
52
+ key
53
+ end
54
+
55
+ def validate(token)
56
+ iss = "https://cloud.google.com/iap"
57
+ options = {
58
+ algorithm: "ES256",
59
+ verify_expiration: true,
60
+ verify_iat: true,
61
+ verify_aud: true,
62
+ verify_iss: true,
63
+ iss: iss,
64
+ }
65
+ payload, header = JWT.decode(token, jwk_key(token), true, options)
66
+ if @required_aud
67
+ validate_aud(@required_aud, payload["aud"])
68
+ else
69
+ validate_aud_format(payload["aud"])
70
+ end
71
+ [payload, header]
72
+ end
73
+
74
+ private
75
+
76
+ def validate_aud(required_aud, aud)
77
+ if required_aud != aud
78
+ fail InvalidAudError, aud
79
+ end
80
+ end
81
+
82
+ def validate_aud_format(aud)
83
+ case aud
84
+ when %r|/projects/\d+/apps/\d+|, %r|/projects/\d+/global/backendServices/\d+|
85
+ # do nothing
86
+ else
87
+ fail InvalidAudError, aud
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Cloudiap
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,89 @@
1
+ require "omniauth"
2
+ require "omniauth/cloudiap/iapjwt"
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ class Cloudiap
7
+ include OmniAuth::Strategy
8
+
9
+ class HTTPHeaderError < ::OmniAuth::Cloudiap::Error; end
10
+
11
+ option :skip_jwt_verify
12
+ option :aud
13
+
14
+ # callback object from email address to user name
15
+ # this callback must have interface [] (Hash, Proc, or class you define)
16
+ # if none, use email for username
17
+ option :username_callback
18
+
19
+ attr_accessor :userinfo
20
+
21
+ def request_phase
22
+ redirect callback_path
23
+ end
24
+
25
+ def callback_phase
26
+ if jwt_verify?
27
+ begin
28
+ self.userinfo = userinfo_from_jwt
29
+ rescue => e
30
+ return fail!(:invalid_credentials, e)
31
+ end
32
+ else
33
+ self.userinfo = userinfo_from_http_header
34
+ end
35
+
36
+ super
37
+ end
38
+
39
+ def uid
40
+ userinfo[:uid] if userinfo
41
+ end
42
+
43
+ def info
44
+ userinfo
45
+ end
46
+
47
+ private
48
+
49
+ def jwt_verify?
50
+ !options[:skip_jwt_verify]
51
+ end
52
+
53
+ def userinfo_from_jwt
54
+ if token = env["HTTP_X_GOOG_IAP_JWT_ASSERTION"]
55
+ payload, header = ::OmniAuth::Cloudiap::IAPJWT.new(aud: options[:aud]).validate(token)
56
+ uid = payload["sub"].sub(/^accounts.google.com:/, "")
57
+ email = payload["email"]
58
+ result = {
59
+ uid: uid,
60
+ email: email,
61
+ name: username_from_email(email),
62
+ payload: payload.to_json,
63
+ }
64
+ else
65
+ fail HTTPHeaderError, "No x-goog-iap-jwt-assertion Header"
66
+ end
67
+ end
68
+
69
+ def userinfo_from_http_header
70
+ uid = env["HTTP_X_GOOG_AUTHENTICATED_USER_ID"].sub(/^accounts.google.com:/, "")
71
+ email = env["HTTP_X_GOOG_AUTHENTICATED_USER_EMAIL"].sub(/^accounts.google.com:/, "")
72
+
73
+ result = {
74
+ uid: uid,
75
+ email: email,
76
+ name: username_from_email(email),
77
+ }
78
+ end
79
+
80
+ def username_from_email(email)
81
+ if options[:username_callback] && options[:username_callback].respond_to?(:[])
82
+ options[:username_callback][email]
83
+ else
84
+ email
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,48 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "omniauth/cloudiap/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "omniauth-cloudiap"
8
+ spec.version = OmniAuth::Cloudiap::VERSION
9
+ spec.authors = ["HORII Keima"]
10
+ spec.email = ["holysugar@gmail.com"]
11
+ spec.license = "MIT"
12
+
13
+ spec.summary = %q{omniauth strategy using Google Cloud IAP}
14
+ spec.description = %q{omniauth strategy using Google Cloud IAP}
15
+ spec.homepage = "https://github.com/holysugar/omniauth-cloudiap"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+ #spec.metadata["changelog_uri"] = ""
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "minitest", "~> 5.0"
41
+ spec.add_development_dependency "minitest-power_assert"
42
+ spec.add_development_dependency "rack-test"
43
+ spec.add_development_dependency "timecop"
44
+
45
+ spec.add_dependency "omniauth"
46
+ spec.add_dependency "jwt"
47
+ end
48
+
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-cloudiap
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - HORII Keima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-21 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-power_assert
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: omniauth
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: jwt
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: omniauth strategy using Google Cloud IAP
126
+ email:
127
+ - holysugar@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - CHANGELOG.md
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/omniauth/cloudiap.rb
142
+ - lib/omniauth/cloudiap/iapjwt.rb
143
+ - lib/omniauth/cloudiap/version.rb
144
+ - lib/omniauth/strategies/cloudiap.rb
145
+ - omniauth-cloudiap.gemspec
146
+ homepage: https://github.com/holysugar/omniauth-cloudiap
147
+ licenses:
148
+ - MIT
149
+ metadata:
150
+ allowed_push_host: https://rubygems.org
151
+ homepage_uri: https://github.com/holysugar/omniauth-cloudiap
152
+ source_code_uri: https://github.com/holysugar/omniauth-cloudiap
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.7.6
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: omniauth strategy using Google Cloud IAP
173
+ test_files: []