authlete 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in authlete.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ authlete-ruby-gem
2
+ =================
3
+
4
+ # Overview
5
+
6
+ Ruby library for [Authlete Web APIs](https://www.authlete.com/authlete_web_apis.html).
7
+
8
+
9
+ # License
10
+
11
+ Apache License, Version 2.0
12
+
13
+
14
+ # See Also
15
+
16
+ * [Authlete Website](https://www.authlete.com/)
17
+ * [Authlete Facebook](https://www.facebook.com/authlete)
18
+ * [Authelte Twitter](https://twitter.com/authlete)
19
+ * [Authlete GitHub](https://github.com/authlete)
20
+ * [Authlete Email](mailto:support@authlete.com)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/authlete.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'authlete/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "authlete"
8
+ spec.version = Authlete::VERSION
9
+ spec.authors = ["Takahiko Kawasaki"]
10
+ spec.email = ["taka@authlete.com"]
11
+ spec.summary = "A library for Authlete Web APIs"
12
+ spec.description = "A library for Authlete Web APIs. See https://www.authlete.com/authlete_web_apis.html for details."
13
+ spec.homepage = "https://www.authlete.com/"
14
+ spec.license = "Apache License, Version 2.0"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "rest-client", "~> 1.7.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/lib/authlete.rb ADDED
@@ -0,0 +1,42 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ require 'authlete/version'
19
+
20
+
21
+ # == Authlete Module
22
+ #
23
+ # A library for {Authlete Web APIs}[https://www.authlete.com/authlete_web_apis.html].
24
+ #
25
+ module Authlete
26
+ # The host of Authlete Web APIs for evaluation.
27
+ HOST_EVALUATION = 'https://evaluation-dot-authlete.appspot.com'
28
+
29
+ autoload :AuthenticationServer, 'authlete/authentication-server'
30
+ autoload :Client, 'authlete/client'
31
+ autoload :Utility, 'authlete/utility'
32
+
33
+ module Request
34
+ autoload :AuthenticationCallbackRequest, 'authlete/request/authentication-callback-request'
35
+ end
36
+
37
+ module Response
38
+ autoload :AuthenticationCallbackResponse, 'authlete/response/authentication-callback-response'
39
+ autoload :BaseResponse, 'authlete/response/base-response'
40
+ autoload :IntrospectionResponse, 'authlete/response/introspection-response'
41
+ end
42
+ end
@@ -0,0 +1,221 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ require 'rack'
19
+
20
+
21
+ module Authlete
22
+ # == Authlete::AuthenticationServer class
23
+ #
24
+ # This class is a base class for an authentication server based on Rack.
25
+ # Some method must/should be overridden by subclasses.
26
+ #
27
+ # 1. <tt>authenticate_api_call</tt>
28
+ # 2. <tt>authenticate_user</tt>
29
+ # 3. <tt>collect_claims</tt>
30
+ # 4. <tt>authentication_callback_endpoint_path</tt>
31
+ class AuthenticationServer
32
+ def initialize(app = nil)
33
+ # Accept 'app' so that this class can work as a Rack middleware
34
+ # as well as a Rack application.
35
+ @app = app
36
+ end
37
+
38
+ def call(env)
39
+ # Request
40
+ request = Rack::Request.new(env)
41
+
42
+ # If the path is different from the name of the authentication
43
+ # callback endpoint.
44
+ if request.post? == false || request.path_info != authentication_callback_endpoint_path()
45
+ # If this class is used as a Rack middleware.
46
+ if @app && @app.respond_to?(:call)
47
+ # Call chain to the next Rack middleware.
48
+ return @app.call(env)
49
+ else
50
+ # 404 Not Found
51
+ return generate_not_found(request)
52
+ end
53
+ end
54
+
55
+ # Basic Authentication for the API call.
56
+ authenticated = do_authenticate_api_call(env)
57
+ if authenticated == false
58
+ # 401 Unauthorized
59
+ return generate_api_call_authentication_failure()
60
+ end
61
+
62
+ begin
63
+ # Parse the request body as AuthenticationCallbackRequest.
64
+ req = parse_authentication_callback_request(request)
65
+ rescue => e
66
+ # 400 Bad Request
67
+ return generate_authentication_callback_request_format_error(e)
68
+ end
69
+
70
+ # Prepare an empty response.
71
+ res = Authlete::Response::AuthenticationCallbackResponse.new
72
+
73
+ # Let the subclass authenticate the end-user.
74
+ # When authenticated successfully, a non-nil value is returned.
75
+ subject = authenticate_user(req)
76
+ if subject.nil?
77
+ # End-user authentication failed.
78
+ # Return {"authenticated": false} to Authlete.
79
+ res.authenticated = false
80
+ return res.to_rack_response
81
+ end
82
+
83
+ # The end-user has been authenticated successfully.
84
+ res.authenticated = true
85
+ res.subject = subject
86
+
87
+ # Make the subclass collect values of the requested claims.
88
+ res.claims = collect_claims(req, subject)
89
+
90
+ # Return {"authenticated": true, ...} to Authlete.
91
+ return res.to_rack_response
92
+ end
93
+
94
+ private
95
+
96
+ def do_authenticate_api_call(env)
97
+ # API key and secret for the API call to the authentication endpoint.
98
+ api_key, api_secret = nil, nil
99
+
100
+ # Basic Authentication for the API call.
101
+ auth = Rack::Auth::Basic::Request.new(env)
102
+
103
+ if auth.provided? && auth.basic?
104
+ api_key, api_secret = *auth.credentials
105
+ end
106
+
107
+ # Let the subclass authenticate the API call.
108
+ authenticate_api_call(api_key, api_secret)
109
+ end
110
+
111
+ # 404 Not Found
112
+ def generate_not_found(request)
113
+ [
114
+ 404,
115
+ {
116
+ 'Content-Type' => 'text/plain'
117
+ },
118
+ [
119
+ "'#{request.request_method} #{request.path_info}' is not found on this server."
120
+ ]
121
+ ]
122
+ end
123
+
124
+ # 401 Unauthorized
125
+ def generate_api_call_authentication_failure
126
+ [
127
+ 401,
128
+ {
129
+ 'Content-Type' => 'text/plain',
130
+ 'WWW-Authenticate' => 'Basic realm="Authentication Callback Endpoint"'
131
+ },
132
+ [
133
+ 'Authentication of the API call to the authentication callback endpoint failed.'
134
+ ]
135
+ ]
136
+ end
137
+
138
+ # 400 Bad Request
139
+ def generate_authentication_callback_request_format_error(exception)
140
+ p exception
141
+ [
142
+ 400,
143
+ {
144
+ 'Content-Type' => 'text/plain',
145
+ },
146
+ [
147
+ 'The format of the authentication callback request was wrong.'
148
+ ]
149
+ ]
150
+ end
151
+
152
+ def parse_authentication_callback_request(request)
153
+ # In case someone has already read it.
154
+ request.body.rewind
155
+
156
+ # Parse the authentication callback request.
157
+ Authlete::Request::AuthenticationCallbackRequest.parse(request.body.read)
158
+ end
159
+
160
+ protected
161
+
162
+ # Authenticate the API call to the authentication callback endpoint
163
+ # from Authlete. Subclasses must override this method. This default
164
+ # implementation returns false, meaning 'unauthorized API call'.
165
+ #
166
+ # The argument of this method is the API key and the API secret that
167
+ # the caller has presented. When the pair of the API credentials is
168
+ # valid, return true. Otherwise, return false.
169
+ def authenticate_api_call(api_key, api_secret)
170
+ # Unauthorized API call.
171
+ false
172
+ end
173
+
174
+ # Authenticate the end-user. Subclasses must override this method.
175
+ # This default implementation returns nil, meaning 'invalid end-user'.
176
+ #
177
+ # The argument of this method is an instance of
178
+ # <tt>Authlete::Request::AuthenticationCallbackRequest</tt>.
179
+ # When the end-user is successfully authenticated, this method must
180
+ # return a unique identifier (= subject) of the end-user. Otherwise,
181
+ # nil must be returned to indicate authentication failure.
182
+ def authenticate_user(req)
183
+ # Invalid end-user.
184
+ nil
185
+ end
186
+
187
+ # Collect claim values of the end-user. Subclasses should override this
188
+ # method. This default implementation returns nil, meaning 'no claim values'.
189
+ #
190
+ # The argument of this method is an instance of
191
+ # <tt>Authlete::Request::AuthenticationCallbackRequest</tt> and the subject
192
+ # which has been returned from the preceding call of <tt>authenticate_user</tt>
193
+ # method.
194
+ #
195
+ # 'Claim' is a piece of information about an end-user. A subclass should
196
+ # collect values of requested claims (<tt>req.claims</tt>) as many as
197
+ # possible and format them in JSON format like below.
198
+ #
199
+ # {
200
+ # "name": "Takahiko Kawasaki",
201
+ # "gender": "male"
202
+ # }
203
+ #
204
+ # Collected claim values are used to generate an {ID token}
205
+ # [http://openid.net/specs/openid-connect-core-1_0.html#IDToken].
206
+ #
207
+ # If no claim values are available, or if you don't want to provide any
208
+ # claim value, return nil.
209
+ def collect_claims(req, subject)
210
+ # No claim values.
211
+ nil
212
+ end
213
+
214
+ # Get the path of the authentication callback endpoint. This default
215
+ # implementation returns '/authentication'. Subclasses may override
216
+ # this method to change the path.
217
+ def authentication_callback_endpoint_path
218
+ '/authentication'
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,120 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ require 'json'
19
+ require 'rest-client'
20
+
21
+
22
+ module Authlete
23
+ # == Authlete::Client Module
24
+ #
25
+ # A web client that accesses Authlete Web APIs.
26
+ #
27
+ class Client
28
+ include Authlete::Utility
29
+
30
+ # The host which provides Authlete Web APIs.
31
+ # For example, <tt>https://evaluation-dot-authlete.appspot.com</tt>
32
+ attr_accessor :host
33
+
34
+ # The API key of a service owner.
35
+ attr_accessor :service_owner_api_key
36
+
37
+ # The API secret of a service owner.
38
+ attr_accessor :service_owner_api_secret
39
+
40
+ # The API key of a service.
41
+ attr_accessor :service_api_key
42
+
43
+ # The API secret of a service.
44
+ attr_accessor :service_api_secret
45
+
46
+ # The constructor which takes a hash containing configuration
47
+ # parameters. Valid configuration parameter names are as follows.
48
+ #
49
+ # - <tt>:host</tt>
50
+ # - <tt>:service_owner_api_key</tt>
51
+ # - <tt>:service_owner_api_secret</tt>
52
+ # - <tt>:service_api_key</tt>
53
+ # - <tt>:service_api_secret</tt>
54
+ #
55
+ def initialize(config = {})
56
+ @host = extract_value(config, :host)
57
+ @service_owner_api_key = extract_value(config, :service_owner_api_key)
58
+ @service_owner_api_secret = extract_value(config, :service_owner_api_secret)
59
+ @service_api_key = extract_value(config, :service_api_key)
60
+ @service_api_secret = extract_value(config, :service_api_secret]
61
+ end
62
+
63
+ private
64
+
65
+ def call_api(method, path, content_type, payload, user, password)
66
+ response = RestClient::Request.new(
67
+ :method => :method,
68
+ :url => @host + path,
69
+ :headers => { :content_type => content_type },
70
+ :payload => payload,
71
+ :user => user,
72
+ :password => password
73
+ ).execute
74
+
75
+ JSON.parse(response.to_str, :symbolize_names => true)
76
+ end
77
+
78
+ def call_api_json(path, body, user, password)
79
+ call_api(:post, path, 'application/json;charset=UTF-8', JSON.generate(body), user, password)
80
+ end
81
+
82
+ def call_api_json_servie_owner(path, body)
83
+ call_api_json(path, body, @service_owner_api_key, @service_owner_api_secret)
84
+ end
85
+
86
+ def call_api_json_service(path, body)
87
+ call_api_json(path, body, @service_api_key, @service_api_secret)
88
+ end
89
+
90
+ public
91
+
92
+ # Call Authlete's {/auth/introspection}
93
+ # [https://www.authlete.com/authlete_web_apis_introspection.html#auth_introspection]
94
+ # API.
95
+ #
96
+ # <tt>token</tt> is an access token presented by a client application.
97
+ # This is a must parameter. In a typical case, a client application uses
98
+ # one of the means listed in {RFC 6750}[https://tools.ietf.org/html/rfc6750]
99
+ # to present an access token to a {protected resource endpoint}
100
+ # [https://tools.ietf.org/html/rfc6749#section-7].
101
+ #
102
+ # <tt>scopes</tt> is an array of scope names. This is an optional parameter.
103
+ # When the specified scopes are not covered by the access token, Authlete
104
+ # prepares the content of the error response.
105
+ #
106
+ # <tt>subject</tt> is a unique identifier of an end-user. This is an optional
107
+ # parameter. When the access token is not associated with the specified
108
+ # subject, Authlete prepares the content of the error response.
109
+ #
110
+ # On success, this method returns an instance of
111
+ # <tt>Authlete::Response::IntrospectionResponse</tt>. On error, this method
112
+ # throws <tt>RestClient::Exception</tt>.
113
+ def introspection(token, scopes = nil, subject = nil)
114
+ hash = call_api_json_service('/api/auth/introspection',
115
+ :token => token, :scopes => scopes, :subject => subject)
116
+
117
+ Authlete::Response::IntrospectionResponse.new(hash)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,58 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ require 'json'
19
+
20
+
21
+ module Authlete
22
+ module Request
23
+ # == Authlete::Request::AuthenticationCallbackRequest class
24
+ #
25
+ # This class represents a request to an authentication callback endpoint.
26
+ class AuthenticationCallbackRequest
27
+ include Authlete::Utility
28
+
29
+ # The login ID that the end-user input to the login ID field.
30
+ attr_accessor :id
31
+
32
+ # The password that the end-user input to the password field.
33
+ attr_accessor :password
34
+
35
+ # Names of requested claims (string array).
36
+ attr_accessor :claims
37
+
38
+ # Names of requested claim locales (string array). The values come
39
+ # from 'claims_locales' request parameter of an authorization request.
40
+ attr_accessor :claimsLocales
41
+
42
+ # The constructor which takes a hash that represents a JSON request
43
+ # to an authentication callback endpoint.
44
+ def initialize(hash = {})
45
+ @id = extract_value(hash, :id)
46
+ @password = extract_value(hash, :password)
47
+ @claims = extract_value(hash, :claims)
48
+ @claimsLocales = extract_value(hash, :claimsLocales)
49
+ end
50
+
51
+ # Parse a JSON string which represents a request to an authentication
52
+ # callback endpoint and generate an instance of AuthenticationCallbackRequest.
53
+ def self.parse(json)
54
+ AuthenticationCallbackRequest.new(JSON.parse(json))
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,63 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ module Authlete
19
+ module Response
20
+ # == Authlete::Response::AuthenticationCallbackResponse class
21
+ #
22
+ # This class represents a response from an authentication callback endpoint.
23
+ class AuthenticationCallbackResponse
24
+ include Authlete::Utility
25
+
26
+ # True when the end-user has been authenticated (= is a valid user).
27
+ attr_accessor :authenticated
28
+
29
+ # The unique identifier of the end-user.
30
+ attr_accessor :subject
31
+
32
+ # Pieces of information about the end-user in JSON format.
33
+ attr_accessor :claims
34
+
35
+ # The constructor which takes a hash that represents a JSON response
36
+ # from an authentication callback endpoint.
37
+ def initialize(hash = {})
38
+ @authenticated = extract_boolean_value(hash, :authenticated)
39
+ @subject = extract_value(hash, :subject)
40
+ @claims = extract_value(hash, :claims)
41
+ end
42
+
43
+ # Generate an array which is usable as a Rack response from this instance.
44
+ def to_rack_response
45
+ [
46
+ 200,
47
+ {
48
+ 'Content-Type' => 'application/json;charset=UTF-8',
49
+ 'Cache-Control' => 'no-store',
50
+ 'Pragma' => 'no-cache'
51
+ },
52
+ [
53
+ JSON.generate(
54
+ :authenticated => @authenticated,
55
+ :subject => @subject,
56
+ :claims => @claims
57
+ )
58
+ ]
59
+ ]
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,41 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ module Authlete
19
+ module Response
20
+ # == Authlete::Response::Base
21
+ #
22
+ # The base class of responses from Authlete Web APIs.
23
+ #
24
+ class BaseResponse
25
+ include Authlete::Utility
26
+
27
+ # The code assigned to the result of the API call.
28
+ attr_accessor :result_code
29
+
30
+ # The message about the result of the API call.
31
+ attr_accessor :result_message
32
+
33
+ # The constructor which takes a hash that represents a JSON response
34
+ # from an Authlete API.
35
+ def initialize(hash = {})
36
+ @result_code = extract_value(hash, :resultCode)
37
+ @result_message = extract_value(hash, :resultMessage)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,86 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ module Authlete
19
+ module Response
20
+ # == Authlete::Response::IntrospectionResponse class
21
+ #
22
+ # A class that represents a response from Authlete's
23
+ # {/auth/introspection}[https://www.authlete.com/authlete_web_apis_introspection.html#auth_introspection]
24
+ # API.
25
+ #
26
+ class IntrospectionResponse < Athlete::Response::BaseResponse
27
+ # The next action which the caller of the API should take next.
28
+ attr_accessor :action
29
+
30
+ # The ID of the client application which is associated with
31
+ # the access token.
32
+ attr_accessor :client_id
33
+
34
+ # The subject which is associated with the access token.
35
+ # This is <tt>nil</tt> if the access token was created
36
+ # through {Client Credentials Flow}[https://tools.ietf.org/html/rfc6749#section-4.4].
37
+ attr_accessor :subject
38
+
39
+ # The scopes which is associated with the access token.
40
+ attr_accessor :scopes
41
+
42
+ # True when the access token exists.
43
+ attr_accessor :existent
44
+
45
+ # True when the access token is usable (= exists and has not expired).
46
+ attr_accessor :usable
47
+
48
+ # True when the access token covers all the scopes (if specified).
49
+ attr_accessor :sufficient
50
+
51
+ # True when the access token can be refreshed using its corresponding
52
+ # refresh token.
53
+ attr_accessor :refreshable
54
+
55
+ # The content of the error response that the service implementation
56
+ # should return to the client application.
57
+ attr_accessor :response_content
58
+
59
+ # The constructor which takes a hash that represents a JSON response from
60
+ # {/auth/introspection}[https://www.authlete.com/authlete_web_apis_introspection.html#auth_introspection]
61
+ # API.
62
+ def initialize(hash = {})
63
+ super(hash)
64
+
65
+ @action = extract_value(hash, :action)
66
+ @client_id = extract_value(hash, :clientId)
67
+ @subject = extract_value(hash, :subject)
68
+ @scopes = extract_value(hash, :scopes)
69
+ @existent = extract_boolean_value(hash, :existent)
70
+ @usable = extract_boolean_value(hash, :usable)
71
+ @sufficient = extract_boolean_value(hash, :sufficient)
72
+ @refreshable = extract_boolean_value(hash, :refreshable)
73
+ @response_content = extract_value(hash, :responseContent)
74
+ end
75
+
76
+ alias_method :existent?, :existent
77
+ alias_method :exists, :existent
78
+ alias_method :exists?, :existent
79
+ alias_method :exist, :existent
80
+ alias_method :exist?, :existent
81
+ alias_method :usable?, :usable
82
+ alias_method :sufficient?, :sufficient
83
+ alias_method :refreshable?, :refreshable
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,34 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014 Authlete, Inc.
4
+ #
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
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
+
17
+
18
+ module Authlete
19
+ module Utility
20
+ def extract_value(hash, key)
21
+ if hash.has_key?(key)
22
+ hash[key]
23
+ else
24
+ hash[key.to_s]
25
+ end
26
+ end
27
+
28
+ def extract_boolean_value(hash, key)
29
+ value = extract_value(hash, key)
30
+
31
+ return (value == true || value == 'true')
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Authlete
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authlete
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takahiko Kawasaki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.7.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.7.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.7'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.7'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ description: A library for Authlete Web APIs. See https://www.authlete.com/authlete_web_apis.html
63
+ for details.
64
+ email:
65
+ - taka@authlete.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - authlete.gemspec
76
+ - lib/authlete.rb
77
+ - lib/authlete/authentication-server.rb
78
+ - lib/authlete/client.rb
79
+ - lib/authlete/request/authentication-callback-request.rb
80
+ - lib/authlete/response/authentication-callback-response.rb
81
+ - lib/authlete/response/base-response.rb
82
+ - lib/authlete/response/introspection-response.rb
83
+ - lib/authlete/utility.rb
84
+ - lib/authlete/version.rb
85
+ homepage: https://www.authlete.com/
86
+ licenses:
87
+ - Apache License, Version 2.0
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.28
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: A library for Authlete Web APIs
110
+ test_files: []