googleauth 0.14.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +11 -0
  3. data/CHANGELOG.md +72 -10
  4. data/README.md +48 -13
  5. data/SECURITY.md +7 -0
  6. data/lib/googleauth/application_default.rb +10 -25
  7. data/lib/googleauth/client_id.rb +10 -25
  8. data/lib/googleauth/compute_engine.rb +16 -30
  9. data/lib/googleauth/credentials.rb +178 -74
  10. data/lib/googleauth/credentials_loader.rb +23 -44
  11. data/lib/googleauth/default_credentials.rb +10 -25
  12. data/lib/googleauth/iam.rb +11 -26
  13. data/lib/googleauth/id_tokens/errors.rb +9 -23
  14. data/lib/googleauth/id_tokens/key_sources.rb +26 -38
  15. data/lib/googleauth/id_tokens/verifier.rb +16 -32
  16. data/lib/googleauth/id_tokens.rb +9 -23
  17. data/lib/googleauth/json_key_reader.rb +10 -25
  18. data/lib/googleauth/scope_util.rb +11 -26
  19. data/lib/googleauth/service_account.rb +60 -59
  20. data/lib/googleauth/signet.rb +22 -28
  21. data/lib/googleauth/stores/file_token_store.rb +11 -25
  22. data/lib/googleauth/stores/redis_token_store.rb +11 -25
  23. data/lib/googleauth/token_store.rb +10 -25
  24. data/lib/googleauth/user_authorizer.rb +10 -25
  25. data/lib/googleauth/user_refresh.rb +15 -27
  26. data/lib/googleauth/version.rb +11 -26
  27. data/lib/googleauth/web_user_authorizer.rb +14 -32
  28. data/lib/googleauth.rb +10 -25
  29. metadata +26 -97
  30. data/.github/CODEOWNERS +0 -7
  31. data/.github/CONTRIBUTING.md +0 -74
  32. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -36
  33. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -21
  34. data/.github/ISSUE_TEMPLATE/support_request.md +0 -7
  35. data/.gitignore +0 -36
  36. data/.kokoro/build.bat +0 -16
  37. data/.kokoro/build.sh +0 -4
  38. data/.kokoro/continuous/common.cfg +0 -24
  39. data/.kokoro/continuous/linux.cfg +0 -25
  40. data/.kokoro/continuous/osx.cfg +0 -8
  41. data/.kokoro/continuous/post.cfg +0 -30
  42. data/.kokoro/continuous/windows.cfg +0 -29
  43. data/.kokoro/osx.sh +0 -4
  44. data/.kokoro/presubmit/common.cfg +0 -24
  45. data/.kokoro/presubmit/linux.cfg +0 -24
  46. data/.kokoro/presubmit/osx.cfg +0 -8
  47. data/.kokoro/presubmit/windows.cfg +0 -29
  48. data/.kokoro/release.cfg +0 -94
  49. data/.kokoro/trampoline.bat +0 -10
  50. data/.kokoro/trampoline.sh +0 -4
  51. data/.repo-metadata.json +0 -5
  52. data/.rspec +0 -2
  53. data/.rubocop.yml +0 -19
  54. data/Gemfile +0 -30
  55. data/Rakefile +0 -132
  56. data/googleauth.gemspec +0 -38
  57. data/integration/helper.rb +0 -31
  58. data/integration/id_tokens/key_source_test.rb +0 -74
  59. data/rakelib/devsite_builder.rb +0 -45
  60. data/rakelib/link_checker.rb +0 -64
  61. data/rakelib/repo_metadata.rb +0 -59
  62. data/spec/googleauth/apply_auth_examples.rb +0 -171
  63. data/spec/googleauth/client_id_spec.rb +0 -160
  64. data/spec/googleauth/compute_engine_spec.rb +0 -160
  65. data/spec/googleauth/credentials_spec.rb +0 -478
  66. data/spec/googleauth/get_application_default_spec.rb +0 -286
  67. data/spec/googleauth/iam_spec.rb +0 -80
  68. data/spec/googleauth/scope_util_spec.rb +0 -77
  69. data/spec/googleauth/service_account_spec.rb +0 -489
  70. data/spec/googleauth/signet_spec.rb +0 -142
  71. data/spec/googleauth/stores/file_token_store_spec.rb +0 -57
  72. data/spec/googleauth/stores/redis_token_store_spec.rb +0 -50
  73. data/spec/googleauth/stores/store_examples.rb +0 -58
  74. data/spec/googleauth/user_authorizer_spec.rb +0 -343
  75. data/spec/googleauth/user_refresh_spec.rb +0 -359
  76. data/spec/googleauth/web_user_authorizer_spec.rb +0 -172
  77. data/spec/spec_helper.rb +0 -92
  78. data/test/helper.rb +0 -33
  79. data/test/id_tokens/key_sources_test.rb +0 -240
  80. data/test/id_tokens/verifier_test.rb +0 -269
@@ -2,31 +2,17 @@
2
2
 
3
3
  # Copyright 2020 Google LLC
4
4
  #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are
7
- # met:
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
8
  #
9
- # * Redistributions of source code must retain the above copyright
10
- # notice, this list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above
12
- # copyright notice, this list of conditions and the following disclaimer
13
- # in the documentation and/or other materials provided with the
14
- # distribution.
15
- # * Neither the name of Google Inc. nor the names of its
16
- # contributors may be used to endorse or promote products derived from
17
- # this software without specific prior written permission.
9
+ # http://www.apache.org/licenses/LICENSE-2.0
18
10
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
30
16
 
31
17
 
32
18
  module Google
@@ -2,31 +2,18 @@
2
2
 
3
3
  # Copyright 2020 Google LLC
4
4
  #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are
7
- # met:
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
8
  #
9
- # * Redistributions of source code must retain the above copyright
10
- # notice, this list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above
12
- # copyright notice, this list of conditions and the following disclaimer
13
- # in the documentation and/or other materials provided with the
14
- # distribution.
15
- # * Neither the name of Google Inc. nor the names of its
16
- # contributors may be used to endorse or promote products derived from
17
- # this software without specific prior written permission.
9
+ # http://www.apache.org/licenses/LICENSE-2.0
18
10
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
30
17
 
31
18
  require "base64"
32
19
  require "json"
@@ -143,13 +130,8 @@ module Google
143
130
  end
144
131
  n_bn = OpenSSL::BN.new n_data, 2
145
132
  e_bn = OpenSSL::BN.new e_data, 2
146
- rsa_key = OpenSSL::PKey::RSA.new
147
- if rsa_key.respond_to? :set_key
148
- rsa_key.set_key n_bn, e_bn, nil
149
- else
150
- rsa_key.n = n_bn
151
- rsa_key.e = e_bn
152
- end
133
+ sequence = [OpenSSL::ASN1::Integer.new(n_bn), OpenSSL::ASN1::Integer.new(e_bn)]
134
+ rsa_key = OpenSSL::PKey::RSA.new OpenSSL::ASN1::Sequence(sequence).to_der
153
135
  rsa_key.public_key
154
136
  end
155
137
 
@@ -171,10 +153,16 @@ module Google
171
153
  curve_name = CURVE_NAME_MAP[jwk[:crv]]
172
154
  raise KeySourceError, "Unsupported EC curve #{jwk[:crv]}" unless curve_name
173
155
  group = OpenSSL::PKey::EC::Group.new curve_name
174
- bn = OpenSSL::BN.new ["04" + x_data.unpack1("H*") + y_data.unpack1("H*")].pack("H*"), 2
175
- key = OpenSSL::PKey::EC.new curve_name
176
- key.public_key = OpenSSL::PKey::EC::Point.new group, bn
177
- key
156
+ x_hex = x_data.unpack1 "H*"
157
+ y_hex = y_data.unpack1 "H*"
158
+ bn = OpenSSL::BN.new ["04#{x_hex}#{y_hex}"].pack("H*"), 2
159
+ point = OpenSSL::PKey::EC::Point.new group, bn
160
+ sequence = OpenSSL::ASN1::Sequence([
161
+ OpenSSL::ASN1::Sequence([OpenSSL::ASN1::ObjectId("id-ecPublicKey"),
162
+ OpenSSL::ASN1::ObjectId(curve_name)]),
163
+ OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed))
164
+ ])
165
+ OpenSSL::PKey::EC.new sequence.to_der
178
166
  end
179
167
  end
180
168
  end
@@ -284,10 +272,10 @@ module Google
284
272
  raise KeySourceError, "Unable to retrieve data from #{uri}" unless response.is_a? Net::HTTPSuccess
285
273
 
286
274
  data = begin
287
- JSON.parse response.body
288
- rescue JSON::ParserError
289
- raise KeySourceError, "Unable to parse JSON"
290
- end
275
+ JSON.parse response.body
276
+ rescue JSON::ParserError
277
+ raise KeySourceError, "Unable to parse JSON"
278
+ end
291
279
 
292
280
  @current_keys = Array(interpret_json(data))
293
281
  end
@@ -2,31 +2,17 @@
2
2
 
3
3
  # Copyright 2020 Google LLC
4
4
  #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are
7
- # met:
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
8
  #
9
- # * Redistributions of source code must retain the above copyright
10
- # notice, this list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above
12
- # copyright notice, this list of conditions and the following disclaimer
13
- # in the documentation and/or other materials provided with the
14
- # distribution.
15
- # * Neither the name of Google Inc. nor the names of its
16
- # contributors may be used to endorse or promote products derived from
17
- # this software without specific prior written permission.
9
+ # http://www.apache.org/licenses/LICENSE-2.0
18
10
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
30
16
 
31
17
  require "jwt"
32
18
 
@@ -105,15 +91,13 @@ module Google
105
91
  def decode_token token, keys, aud, azp, iss
106
92
  payload = nil
107
93
  keys.find do |key|
108
- begin
109
- options = { algorithms: key.algorithm }
110
- decoded_token = JWT.decode token, key.key, true, options
111
- payload = decoded_token.first
112
- rescue JWT::ExpiredSignature
113
- raise ExpiredTokenError, "Token signature is expired"
114
- rescue JWT::DecodeError
115
- nil # Try the next key
116
- end
94
+ options = { algorithms: key.algorithm }
95
+ decoded_token = JWT.decode token, key.key, true, options
96
+ payload = decoded_token.first
97
+ rescue JWT::ExpiredSignature
98
+ raise ExpiredTokenError, "Token signature is expired"
99
+ rescue JWT::DecodeError
100
+ nil # Try the next key
117
101
  end
118
102
 
119
103
  normalize_and_verify_payload payload, aud, azp, iss
@@ -2,31 +2,17 @@
2
2
 
3
3
  # Copyright 2020 Google LLC
4
4
  #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are
7
- # met:
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
8
  #
9
- # * Redistributions of source code must retain the above copyright
10
- # notice, this list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above
12
- # copyright notice, this list of conditions and the following disclaimer
13
- # in the documentation and/or other materials provided with the
14
- # distribution.
15
- # * Neither the name of Google Inc. nor the names of its
16
- # contributors may be used to endorse or promote products derived from
17
- # this software without specific prior written permission.
9
+ # http://www.apache.org/licenses/LICENSE-2.0
18
10
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
30
16
 
31
17
  require "googleauth/id_tokens/errors"
32
18
  require "googleauth/id_tokens/key_sources"
@@ -1,31 +1,16 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
1
+ # Copyright 2015 Google, Inc.
3
2
  #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
7
6
  #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
7
+ # http://www.apache.org/licenses/LICENSE-2.0
17
8
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
29
14
 
30
15
  module Google
31
16
  # Module Auth provides classes that provide Google-specific authorization
@@ -1,31 +1,16 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
1
+ # Copyright 2015 Google, Inc.
3
2
  #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
7
6
  #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
7
+ # http://www.apache.org/licenses/LICENSE-2.0
17
8
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
29
14
 
30
15
  require "googleauth/signet"
31
16
  require "googleauth/credentials_loader"
@@ -51,7 +36,7 @@ module Google
51
36
  when Array
52
37
  scope
53
38
  when String
54
- scope.split " "
39
+ scope.split
55
40
  else
56
41
  raise "Invalid scope value. Must be string or array"
57
42
  end
@@ -1,31 +1,16 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
1
+ # Copyright 2015 Google, Inc.
3
2
  #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
7
6
  #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
7
+ # http://www.apache.org/licenses/LICENSE-2.0
17
8
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
29
14
 
30
15
  require "googleauth/signet"
31
16
  require "googleauth/credentials_loader"
@@ -53,12 +38,18 @@ module Google
53
38
  attr_reader :project_id
54
39
  attr_reader :quota_project_id
55
40
 
41
+ def enable_self_signed_jwt?
42
+ @enable_self_signed_jwt
43
+ end
44
+
56
45
  # Creates a ServiceAccountCredentials.
57
46
  #
58
47
  # @param json_key_io [IO] an IO from which the JSON key can be read
59
48
  # @param scope [string|array|nil] the scope(s) to access
60
49
  def self.make_creds options = {}
61
- json_key_io, scope, target_audience = options.values_at :json_key_io, :scope, :target_audience
50
+ json_key_io, scope, enable_self_signed_jwt, target_audience, audience, token_credential_uri =
51
+ options.values_at :json_key_io, :scope, :enable_self_signed_jwt, :target_audience,
52
+ :audience, :token_credential_uri
62
53
  raise ArgumentError, "Cannot specify both scope and target_audience" if scope && target_audience
63
54
 
64
55
  if json_key_io
@@ -71,14 +62,15 @@ module Google
71
62
  end
72
63
  project_id ||= CredentialsLoader.load_gcloud_project_id
73
64
 
74
- new(token_credential_uri: TOKEN_CRED_URI,
75
- audience: TOKEN_CRED_URI,
76
- scope: scope,
77
- target_audience: target_audience,
78
- issuer: client_email,
79
- signing_key: OpenSSL::PKey::RSA.new(private_key),
80
- project_id: project_id,
81
- quota_project_id: quota_project_id)
65
+ new(token_credential_uri: token_credential_uri || TOKEN_CRED_URI,
66
+ audience: audience || TOKEN_CRED_URI,
67
+ scope: scope,
68
+ enable_self_signed_jwt: enable_self_signed_jwt,
69
+ target_audience: target_audience,
70
+ issuer: client_email,
71
+ signing_key: OpenSSL::PKey::RSA.new(private_key),
72
+ project_id: project_id,
73
+ quota_project_id: quota_project_id)
82
74
  .configure_connection(options)
83
75
  end
84
76
 
@@ -94,30 +86,35 @@ module Google
94
86
  def initialize options = {}
95
87
  @project_id = options[:project_id]
96
88
  @quota_project_id = options[:quota_project_id]
89
+ @enable_self_signed_jwt = options[:enable_self_signed_jwt] ? true : false
97
90
  super options
98
91
  end
99
92
 
100
- # Extends the base class.
101
- #
102
- # If scope(s) is not set, it creates a transient
103
- # ServiceAccountJwtHeaderCredentials instance and uses that to
104
- # authenticate instead.
93
+ # Extends the base class to use a transient
94
+ # ServiceAccountJwtHeaderCredentials for certain cases.
105
95
  def apply! a_hash, opts = {}
106
- # Use the base implementation if scopes are set
107
- unless scope.nil? && target_audience.nil?
96
+ # Use a self-singed JWT if there's no information that can be used to
97
+ # obtain an OAuth token, OR if there are scopes but also an assertion
98
+ # that they are default scopes that shouldn't be used to fetch a token.
99
+ if target_audience.nil? && (scope.nil? || enable_self_signed_jwt?)
100
+ apply_self_signed_jwt! a_hash
101
+ else
108
102
  super
109
- return
110
103
  end
104
+ end
105
+
106
+ private
111
107
 
108
+ def apply_self_signed_jwt! a_hash
112
109
  # Use the ServiceAccountJwtHeaderCredentials using the same cred values
113
- # if no scopes are set.
114
110
  cred_json = {
115
- private_key: @signing_key.to_s,
116
- client_email: @issuer
111
+ private_key: @signing_key.to_s,
112
+ client_email: @issuer,
113
+ project_id: @project_id,
114
+ quota_project_id: @quota_project_id
117
115
  }
118
- alt_clz = ServiceAccountJwtHeaderCredentials
119
116
  key_io = StringIO.new MultiJson.dump(cred_json)
120
- alt = alt_clz.make_creds json_key_io: key_io
117
+ alt = ServiceAccountJwtHeaderCredentials.make_creds json_key_io: key_io, scope: scope
121
118
  alt.apply! a_hash
122
119
  end
123
120
  end
@@ -142,15 +139,13 @@ module Google
142
139
  attr_reader :project_id
143
140
  attr_reader :quota_project_id
144
141
 
145
- # make_creds proxies the construction of a credentials instance
146
- #
147
- # make_creds is used by the methods in CredentialsLoader.
142
+ # Create a ServiceAccountJwtHeaderCredentials.
148
143
  #
149
- # By default, it calls #new with 2 args, the second one being an
150
- # optional scope. Here's the constructor only has one param, so
151
- # we modify make_creds to reflect this.
152
- def self.make_creds *args
153
- new json_key_io: args[0][:json_key_io]
144
+ # @param json_key_io [IO] an IO from which the JSON key can be read
145
+ # @param scope [string|array|nil] the scope(s) to access
146
+ def self.make_creds options = {}
147
+ json_key_io, scope = options.values_at :json_key_io, :scope
148
+ new json_key_io: json_key_io, scope: scope
154
149
  end
155
150
 
156
151
  # Initializes a ServiceAccountJwtHeaderCredentials.
@@ -169,6 +164,7 @@ module Google
169
164
  end
170
165
  @project_id ||= CredentialsLoader.load_gcloud_project_id
171
166
  @signing_key = OpenSSL::PKey::RSA.new @private_key
167
+ @scope = options[:scope]
172
168
  end
173
169
 
174
170
  # Construct a jwt token if the JWT_AUD_URI key is present in the input
@@ -177,7 +173,7 @@ module Google
177
173
  # The jwt token is used as the value of a 'Bearer '.
178
174
  def apply! a_hash, opts = {}
179
175
  jwt_aud_uri = a_hash.delete JWT_AUD_URI_KEY
180
- return a_hash if jwt_aud_uri.nil?
176
+ return a_hash if jwt_aud_uri.nil? && @scope.nil?
181
177
  jwt_token = new_jwt_token jwt_aud_uri, opts
182
178
  a_hash[AUTH_METADATA_KEY] = "Bearer #{jwt_token}"
183
179
  a_hash
@@ -193,22 +189,27 @@ module Google
193
189
  # Returns a reference to the #apply method, suitable for passing as
194
190
  # a closure
195
191
  def updater_proc
196
- lambda(&method(:apply))
192
+ proc { |a_hash, opts = {}| apply a_hash, opts }
197
193
  end
198
194
 
199
195
  protected
200
196
 
201
197
  # Creates a jwt uri token.
202
- def new_jwt_token jwt_aud_uri, options = {}
198
+ def new_jwt_token jwt_aud_uri = nil, options = {}
203
199
  now = Time.new
204
200
  skew = options[:skew] || 60
205
201
  assertion = {
206
202
  "iss" => @issuer,
207
203
  "sub" => @issuer,
208
- "aud" => jwt_aud_uri,
209
204
  "exp" => (now + EXPIRY).to_i,
210
205
  "iat" => (now - skew).to_i
211
206
  }
207
+
208
+ jwt_aud_uri = nil if @scope
209
+
210
+ assertion["scope"] = Array(@scope).join " " if @scope
211
+ assertion["aud"] = jwt_aud_uri if jwt_aud_uri
212
+
212
213
  JWT.encode assertion, @signing_key, SIGNING_ALGORITHM
213
214
  end
214
215
  end
@@ -1,31 +1,16 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
1
+ # Copyright 2015 Google, Inc.
3
2
  #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
7
6
  #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
7
+ # http://www.apache.org/licenses/LICENSE-2.0
17
8
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
29
14
 
30
15
  require "signet/oauth_2/client"
31
16
 
@@ -44,12 +29,21 @@ module Signet
44
29
  self
45
30
  end
46
31
 
32
+ # The token type as symbol, either :id_token or :access_token
33
+ def token_type
34
+ target_audience ? :id_token : :access_token
35
+ end
36
+
37
+ # Whether the id_token or access_token is missing or about to expire.
38
+ def needs_access_token?
39
+ send(token_type).nil? || expires_within?(60)
40
+ end
41
+
47
42
  # Updates a_hash updated with the authentication token
48
43
  def apply! a_hash, opts = {}
49
44
  # fetch the access token there is currently not one, or if the client
50
45
  # has expired
51
- token_type = target_audience ? :id_token : :access_token
52
- fetch_access_token! opts if send(token_type).nil? || expires_within?(60)
46
+ fetch_access_token! opts if needs_access_token?
53
47
  a_hash[AUTH_METADATA_KEY] = "Bearer #{send token_type}"
54
48
  end
55
49
 
@@ -63,7 +57,7 @@ module Signet
63
57
  # Returns a reference to the #apply method, suitable for passing as
64
58
  # a closure
65
59
  def updater_proc
66
- lambda(&method(:apply))
60
+ proc { |a_hash, opts = {}| apply a_hash, opts }
67
61
  end
68
62
 
69
63
  def on_refresh &block