custom-adal 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +25 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +106 -0
  8. data/Rakefile +39 -0
  9. data/adal.gemspec +52 -0
  10. data/contributing.md +127 -0
  11. data/lib/adal/authentication_context.rb +202 -0
  12. data/lib/adal/authentication_parameters.rb +126 -0
  13. data/lib/adal/authority.rb +165 -0
  14. data/lib/adal/cache_driver.rb +171 -0
  15. data/lib/adal/cached_token_response.rb +190 -0
  16. data/lib/adal/client_assertion.rb +63 -0
  17. data/lib/adal/client_assertion_certificate.rb +89 -0
  18. data/lib/adal/client_credential.rb +46 -0
  19. data/lib/adal/core_ext/hash.rb +34 -0
  20. data/lib/adal/core_ext.rb +26 -0
  21. data/lib/adal/jwt_parameters.rb +39 -0
  22. data/lib/adal/logger.rb +90 -0
  23. data/lib/adal/logging.rb +98 -0
  24. data/lib/adal/memory_cache.rb +95 -0
  25. data/lib/adal/mex_request.rb +52 -0
  26. data/lib/adal/mex_response.rb +141 -0
  27. data/lib/adal/noop_cache.rb +38 -0
  28. data/lib/adal/oauth_request.rb +76 -0
  29. data/lib/adal/request_parameters.rb +48 -0
  30. data/lib/adal/self_signed_jwt_factory.rb +96 -0
  31. data/lib/adal/templates/rst.13.xml.erb +35 -0
  32. data/lib/adal/templates/rst.2005.xml.erb +32 -0
  33. data/lib/adal/token_request.rb +231 -0
  34. data/lib/adal/token_response.rb +144 -0
  35. data/lib/adal/user_assertion.rb +57 -0
  36. data/lib/adal/user_credential.rb +152 -0
  37. data/lib/adal/user_identifier.rb +83 -0
  38. data/lib/adal/user_information.rb +49 -0
  39. data/lib/adal/util.rb +49 -0
  40. data/lib/adal/version.rb +36 -0
  41. data/lib/adal/wstrust_request.rb +100 -0
  42. data/lib/adal/wstrust_response.rb +168 -0
  43. data/lib/adal/xml_namespaces.rb +64 -0
  44. data/lib/adal.rb +24 -0
  45. data/samples/authorization_code_example/README.md +10 -0
  46. data/samples/authorization_code_example/web_app.rb +139 -0
  47. data/samples/client_assertion_certificate_example/README.md +42 -0
  48. data/samples/client_assertion_certificate_example/app.rb +55 -0
  49. data/samples/on_behalf_of_example/README.md +35 -0
  50. data/samples/on_behalf_of_example/native_app.rb +52 -0
  51. data/samples/on_behalf_of_example/web_api.rb +71 -0
  52. data/samples/user_credentials_example/README.md +7 -0
  53. data/samples/user_credentials_example/app.rb +52 -0
  54. data/spec/adal/authentication_context_spec.rb +186 -0
  55. data/spec/adal/authentication_parameters_spec.rb +107 -0
  56. data/spec/adal/authority_spec.rb +122 -0
  57. data/spec/adal/cache_driver_spec.rb +191 -0
  58. data/spec/adal/cached_token_response_spec.rb +148 -0
  59. data/spec/adal/client_assertion_certificate_spec.rb +113 -0
  60. data/spec/adal/client_assertion_spec.rb +38 -0
  61. data/spec/adal/core_ext/hash_spec.rb +47 -0
  62. data/spec/adal/logging_spec.rb +48 -0
  63. data/spec/adal/memory_cache_spec.rb +107 -0
  64. data/spec/adal/mex_request_spec.rb +57 -0
  65. data/spec/adal/mex_response_spec.rb +143 -0
  66. data/spec/adal/self_signed_jwt_factory_spec.rb +63 -0
  67. data/spec/adal/token_request_spec.rb +150 -0
  68. data/spec/adal/token_response_spec.rb +102 -0
  69. data/spec/adal/user_credential_spec.rb +125 -0
  70. data/spec/adal/user_identifier_spec.rb +115 -0
  71. data/spec/adal/wstrust_request_spec.rb +51 -0
  72. data/spec/adal/wstrust_response_spec.rb +152 -0
  73. data/spec/fixtures/mex/insecureaddress.xml +924 -0
  74. data/spec/fixtures/mex/invalid_namespaces.xml +916 -0
  75. data/spec/fixtures/mex/malformed.xml +914 -0
  76. data/spec/fixtures/mex/microsoft.xml +916 -0
  77. data/spec/fixtures/mex/multiple_endpoints.xml +922 -0
  78. data/spec/fixtures/mex/no_matching_bindings.xml +916 -0
  79. data/spec/fixtures/mex/no_username_token_policies.xml +914 -0
  80. data/spec/fixtures/mex/no_wstrust_endpoints.xml +838 -0
  81. data/spec/fixtures/mex/only_13.xml +842 -0
  82. data/spec/fixtures/mex/only_2005.xml +842 -0
  83. data/spec/fixtures/oauth/error.json +1 -0
  84. data/spec/fixtures/oauth/success.json +1 -0
  85. data/spec/fixtures/oauth/success_with_id_token.json +1 -0
  86. data/spec/fixtures/wstrust/error.xml +24 -0
  87. data/spec/fixtures/wstrust/invalid_namespaces.xml +136 -0
  88. data/spec/fixtures/wstrust/missing_security_tokens.xml +90 -0
  89. data/spec/fixtures/wstrust/success.xml +136 -0
  90. data/spec/fixtures/wstrust/token.xml +1 -0
  91. data/spec/fixtures/wstrust/too_many_security_tokens.xml +219 -0
  92. data/spec/fixtures/wstrust/unrecognized_token_type.xml +136 -0
  93. data/spec/fixtures/wstrust/wstrust.13.xml +1 -0
  94. data/spec/fixtures/wstrust/wstrust.2005.xml +89 -0
  95. data/spec/spec_helper.rb +53 -0
  96. data/spec/support/fake_data.rb +40 -0
  97. data/spec/support/fake_token_endpoint.rb +108 -0
  98. metadata +264 -0
@@ -0,0 +1,108 @@
1
+ #-------------------------------------------------------------------------------
2
+ # Copyright (c) 2015 Micorosft Corporation
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #-------------------------------------------------------------------------------
22
+
23
+ require_relative './fake_data'
24
+
25
+ require 'json'
26
+ require 'jwt'
27
+ require 'sinatra/base'
28
+
29
+ # A token endpoint that only recognizes one tenant and client id.
30
+ class FakeTokenEndpoint < Sinatra::Base
31
+ include FakeData
32
+
33
+ # Taken from RFC 6749 4.1.2.1.
34
+ module ErrorResponseCodes
35
+ INVALID_REQUEST = 'invalid_request'
36
+ INVALID_CLIENT = 'invalid_client'
37
+ INVALID_GRANT = 'invalid_grant'
38
+ UNAUTHORIZED_CLIENT = 'unauthorized_client'
39
+ UNSUPPORTED_GRANT_TYPE = 'unsupported_grant_type'
40
+ end
41
+
42
+ DEFAULT_EXPIRATION = 3600
43
+ DEFAULT_ID_TOKEN = JWT.encode({ email: USERNAME }, '')
44
+ DEFAULT_TOKEN_TYPE = 'Bearer'
45
+
46
+ post '/:tenant/oauth2/token' do
47
+ if TENANT != params[:tenant] || CLIENT_ID != params[:client_id]
48
+ error_oauth_response(ErrorResponseCodes::INVALID_CLIENT)
49
+ elsif params.key?('code') && AUTH_CODE == params['code'] &&
50
+ REDIRECT_URI == params['redirect_uri']
51
+ successful_oauth_response
52
+ elsif params['code']
53
+ error_oauth_response(ErrorResponseCodes::INVALID_GRANT)
54
+ elsif params['refresh_token'] && REFRESH_TOKEN == params['refresh_token']
55
+ successful_oauth_response
56
+ elsif params['refresh_token']
57
+ error_oauth_response(ErrorResponseCodes::UNAUTHORIZED_CLIENT)
58
+ elsif params['client_secret'] && CLIENT_SECRET == params['client_secret']
59
+ successful_oauth_response
60
+ elsif params.key? 'client_secret'
61
+ error_oauth_response(ErrorResponseCodes::INVALID_CLIENT)
62
+ else
63
+ error_oauth_response(ErrorResponseCodes::INVALID_REQUEST)
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def error_oauth_response(code, description = 'Error from fake endpoint')
70
+ { error: code, error_description: description }.to_json
71
+ end
72
+
73
+ def oauth_response(tenant)
74
+ { access_token: 'test_access_token',
75
+ token_type: 'BEARER',
76
+ tenant: tenant
77
+ }
78
+ end
79
+
80
+ def successful_oauth_response(opts = {})
81
+ res = { access_token: opts[:access_token] || RETURNED_TOKEN,
82
+ token_type: opts[:token_type] || DEFAULT_TOKEN_TYPE,
83
+ id_token: opts[:id_token] || DEFAULT_ID_TOKEN,
84
+ resource: params[:resource],
85
+ expires_in: opts[:expires_in] || DEFAULT_EXPIRATION }
86
+ res[:refresh_token] = opts[:refresh_token] if opts.key? :refresh_token
87
+ res.to_json
88
+ end
89
+
90
+ def try_auth_code(data, params)
91
+ return unless params.key? 'code'
92
+ if (data['codes'].key? params[:code]) &&
93
+ data['codes'][params['code']] == params[:redirect_uri]
94
+ successful_oauth_response
95
+ else
96
+ error_oauth_response(ErrorResponseCodes::INVALID_GRANT)
97
+ end
98
+ end
99
+
100
+ def try_client_secret(data, params)
101
+ return unless params.key? 'client_secret'
102
+ if data['client_secret'] == params[:client_secret]
103
+ successful_oauth_response
104
+ else
105
+ error_oauth_response(ErrorResponseCodes::INVALID_CLIENT)
106
+ end
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,264 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom-adal
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Microsoft Corporation
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: uri_template
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.32'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.32'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sinatra
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.4'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.21'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.21'
139
+ description: Windows Azure Active Directory authentication client library
140
+ email: nugetaad@microsoft.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - ".gitignore"
146
+ - ".rubocop.yml"
147
+ - ".travis.yml"
148
+ - Gemfile
149
+ - LICENSE.txt
150
+ - README.md
151
+ - Rakefile
152
+ - adal.gemspec
153
+ - contributing.md
154
+ - lib/adal.rb
155
+ - lib/adal/authentication_context.rb
156
+ - lib/adal/authentication_parameters.rb
157
+ - lib/adal/authority.rb
158
+ - lib/adal/cache_driver.rb
159
+ - lib/adal/cached_token_response.rb
160
+ - lib/adal/client_assertion.rb
161
+ - lib/adal/client_assertion_certificate.rb
162
+ - lib/adal/client_credential.rb
163
+ - lib/adal/core_ext.rb
164
+ - lib/adal/core_ext/hash.rb
165
+ - lib/adal/jwt_parameters.rb
166
+ - lib/adal/logger.rb
167
+ - lib/adal/logging.rb
168
+ - lib/adal/memory_cache.rb
169
+ - lib/adal/mex_request.rb
170
+ - lib/adal/mex_response.rb
171
+ - lib/adal/noop_cache.rb
172
+ - lib/adal/oauth_request.rb
173
+ - lib/adal/request_parameters.rb
174
+ - lib/adal/self_signed_jwt_factory.rb
175
+ - lib/adal/templates/rst.13.xml.erb
176
+ - lib/adal/templates/rst.2005.xml.erb
177
+ - lib/adal/token_request.rb
178
+ - lib/adal/token_response.rb
179
+ - lib/adal/user_assertion.rb
180
+ - lib/adal/user_credential.rb
181
+ - lib/adal/user_identifier.rb
182
+ - lib/adal/user_information.rb
183
+ - lib/adal/util.rb
184
+ - lib/adal/version.rb
185
+ - lib/adal/wstrust_request.rb
186
+ - lib/adal/wstrust_response.rb
187
+ - lib/adal/xml_namespaces.rb
188
+ - samples/authorization_code_example/README.md
189
+ - samples/authorization_code_example/web_app.rb
190
+ - samples/client_assertion_certificate_example/README.md
191
+ - samples/client_assertion_certificate_example/app.rb
192
+ - samples/on_behalf_of_example/README.md
193
+ - samples/on_behalf_of_example/native_app.rb
194
+ - samples/on_behalf_of_example/web_api.rb
195
+ - samples/user_credentials_example/README.md
196
+ - samples/user_credentials_example/app.rb
197
+ - spec/adal/authentication_context_spec.rb
198
+ - spec/adal/authentication_parameters_spec.rb
199
+ - spec/adal/authority_spec.rb
200
+ - spec/adal/cache_driver_spec.rb
201
+ - spec/adal/cached_token_response_spec.rb
202
+ - spec/adal/client_assertion_certificate_spec.rb
203
+ - spec/adal/client_assertion_spec.rb
204
+ - spec/adal/core_ext/hash_spec.rb
205
+ - spec/adal/logging_spec.rb
206
+ - spec/adal/memory_cache_spec.rb
207
+ - spec/adal/mex_request_spec.rb
208
+ - spec/adal/mex_response_spec.rb
209
+ - spec/adal/self_signed_jwt_factory_spec.rb
210
+ - spec/adal/token_request_spec.rb
211
+ - spec/adal/token_response_spec.rb
212
+ - spec/adal/user_credential_spec.rb
213
+ - spec/adal/user_identifier_spec.rb
214
+ - spec/adal/wstrust_request_spec.rb
215
+ - spec/adal/wstrust_response_spec.rb
216
+ - spec/fixtures/mex/insecureaddress.xml
217
+ - spec/fixtures/mex/invalid_namespaces.xml
218
+ - spec/fixtures/mex/malformed.xml
219
+ - spec/fixtures/mex/microsoft.xml
220
+ - spec/fixtures/mex/multiple_endpoints.xml
221
+ - spec/fixtures/mex/no_matching_bindings.xml
222
+ - spec/fixtures/mex/no_username_token_policies.xml
223
+ - spec/fixtures/mex/no_wstrust_endpoints.xml
224
+ - spec/fixtures/mex/only_13.xml
225
+ - spec/fixtures/mex/only_2005.xml
226
+ - spec/fixtures/oauth/error.json
227
+ - spec/fixtures/oauth/success.json
228
+ - spec/fixtures/oauth/success_with_id_token.json
229
+ - spec/fixtures/wstrust/error.xml
230
+ - spec/fixtures/wstrust/invalid_namespaces.xml
231
+ - spec/fixtures/wstrust/missing_security_tokens.xml
232
+ - spec/fixtures/wstrust/success.xml
233
+ - spec/fixtures/wstrust/token.xml
234
+ - spec/fixtures/wstrust/too_many_security_tokens.xml
235
+ - spec/fixtures/wstrust/unrecognized_token_type.xml
236
+ - spec/fixtures/wstrust/wstrust.13.xml
237
+ - spec/fixtures/wstrust/wstrust.2005.xml
238
+ - spec/spec_helper.rb
239
+ - spec/support/fake_data.rb
240
+ - spec/support/fake_token_endpoint.rb
241
+ homepage: https://github.com/dhamkur/custom-adal
242
+ licenses:
243
+ - MIT
244
+ metadata: {}
245
+ post_install_message:
246
+ rdoc_options: []
247
+ require_paths:
248
+ - lib
249
+ required_ruby_version: !ruby/object:Gem::Requirement
250
+ requirements:
251
+ - - ">="
252
+ - !ruby/object:Gem::Version
253
+ version: 2.1.0
254
+ required_rubygems_version: !ruby/object:Gem::Requirement
255
+ requirements:
256
+ - - ">="
257
+ - !ruby/object:Gem::Version
258
+ version: '0'
259
+ requirements: []
260
+ rubygems_version: 3.3.7
261
+ signing_key:
262
+ specification_version: 4
263
+ summary: ADAL for Ruby
264
+ test_files: []