rodauth-oauth 0.0.1 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cea0f9a4896e57d535c2ef73eff0c1a9e6b4e25f4d53740c65171b8c098a8ac1
4
- data.tar.gz: 96f78feb4157d6f940700fe658b7817b95bd79b61a6f02b9cc530947512a8ff0
3
+ metadata.gz: 73f73e4143c1c646b3a6f2a5e87fb925f38f18e157366d7522328b5cbcd2fbb7
4
+ data.tar.gz: bce8a4532e365328bb46197e72b05b78beca19b00587630f234cff0c8d51bc35
5
5
  SHA512:
6
- metadata.gz: 3b2bc30f8793a0ae0ef8a48b46fcd896494d6a941e57e66481d8b8197424c5a6da80761237e26b4eb70acf73ae933be40484d7ca06cf5191790ee83b62eb7411
7
- data.tar.gz: 7a1cb3061c3eb9c271b04c35e970306a96018a40408c63c5fd5c815d62f3b4bfdf8c84f32c6947015c8a99a810a8cc904a1f6e10ca3f9eba842ec8575f975a11
6
+ metadata.gz: 46053f71e35baad7b3c217bfbca0a259d07fd6909713805db23ff92c0503c0907903483e659fe988af774b26a87b274f8b72006983b1acc191bccb7d00919a86
7
+ data.tar.gz: 35305a71ea2b4035933d93e3fa5a3e9b388f32b2301ff05b87a86af45defa494cefc4d6d9adb823822c82acb995e57794fb710b1935aaab10430c1898d1a55b0
@@ -1,5 +1,152 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.0.1
3
+ ## master
4
+
5
+ ### 0.0.6
6
+
7
+ #### Features
8
+
9
+ The `oauth_jwt` feature now supports JWT Secured Authorization Request (JAR) (see https://tools.ietf.org/html/draft-ietf-oauth-jwsreq-20). This means that client applications can send the authorization parameters inside a signed JWT. The client applications keeps the private key, while the authorization server **must** store a public key for the client application. For encrypted JWTs, the client application should use one of the public encryption keys exposed in the JWKs URI, to encrypt the JWT. Remember, **tokens must be signed then encrypted** (or just signed).
10
+
11
+ ###### Options:
12
+
13
+ * `:oauth_application_jws_jwk_column`: db column where the public key is stored; since it's stored in the JWS format, it can be stored either as a String (JSON-encoded), or as an hstore (if you're using postgresql);
14
+ * `:oauth_jwt_jwe_key`: key used to decrypt the request JWT;
15
+ * `:oauth_jwt_jwe_public_key`: key used to encrypt the request JWT, and which will be exposed in the JWKs URI in the JWK format;
16
+
17
+
18
+ #### Improvements
19
+
20
+ * Removing all `_param` options; these defined the URL params, however we're using protocol-defined params, so it's unlikely (and undesired) that these'll change.
21
+ * Hitting the revoke endpoint with a JWT access token returns a 400 error;
22
+
23
+ #### Chore
24
+
25
+ Removed React Javascript from example applications.
26
+
27
+
28
+ ### 0.0.5 (26/6/2020)
29
+
30
+ #### Features
31
+
32
+ * new option: `oauth_scope_separator` (default: `" "`), to define how scopes are stored;
33
+
34
+ ##### Resource Server mode
35
+
36
+ `rodauth-oauth` can now be used in a resource server, i.e. only for authorizing access to resources:
37
+
38
+
39
+ ```ruby
40
+ plugin :rodauth do
41
+ enable :oauth
42
+
43
+ is_authorization_server? false
44
+ authorization_server_url "https://auth-server"
45
+ end
46
+ ```
47
+
48
+ It **requires** the authorization to implement the server metadata endpoint (`/.well-known/oauth-authorization-server`), and if using JWS, the JWKs URI endpoint (unless `oauth_jwt_public_key` is defined).
49
+
50
+ #### Improvements
51
+
52
+ * Multiple Redirect URIs are now allowed for client applications out-of-the-box. In order to use it in API mode, you can pass the `redirect_uri` with an array of strings (the URLs) as values; in the new client application form, you can add several input fields with name field as `redirect_uri[]`. **ATTENTION!!** When using multiple redirect URIs, passing the desired redirect URI to the authorize form becomes mandatory.
53
+ * store scopes with whitespace instead of comma; set separator as `oauth_scope_separator` option, to keep backwards-compatibility;
54
+ * client application can now store multiple redirect uris; the POST API parameters can accept the redirect_uri param value both as a string or an array of string; internally, they'll be stored in a whitespace-separated string;
55
+
56
+ #### Bugfixes
57
+
58
+ * Fixed `RETURNING` support in the databases supporting it (such as postgres).
59
+
60
+ #### Chore
61
+
62
+ * option `scopes_param` renamed to `scope_param`;
63
+ *
64
+
65
+ ## 0.0.4 (13/6/2020)
66
+
67
+ ### Features
68
+
69
+ #### Token introspection
70
+
71
+ `rodauth-oauth` now ships with an introspection endpoint (`/oauth-introspect`).
72
+
73
+ #### Authorization Server Metadata
74
+
75
+ `rodauth-oauth` now allows to define an authorization metadata endpoint, which has to be defined at the route of the router:
76
+
77
+ ```ruby
78
+ route do |r|
79
+ r.rodauth
80
+ rodauth.oauth_server_metadata
81
+ ...
82
+ ```
83
+
84
+ #### JWKs URI
85
+
86
+ the `oauth_jwt` feature now ships with an endpoint, `/oauth-jwks`, where client applications can retrieve the JWK set to verify generated tokens.
87
+
88
+ #### JWT access tokens as authorization grants
89
+
90
+ The `oauth_jwt` feature now allows the usage of access tokens to authorize the generation of new tokens, [as per the RFC](https://tools.ietf.org/html/rfc7523#section-4);
91
+
92
+ ### Improvements
93
+
94
+ * using `client_secret_basic` authorization where client id/secret params were allowed (i.e. in the token and revoke endpoints, for example);
95
+ * improved JWK usage for both supported jwt libraries;
96
+ * marked `fetch_access_token` as auth_value_method, thereby allowing users to fetch the access token from other sources than the "Authorization" header (i.e. form body, query params, etc...)
97
+
98
+ ### Bugfixes
99
+
100
+ * Fixed scope claim of JWT ("scopes" -> "scope");
101
+
102
+ ## 0.0.3 (5/6/2020)
103
+
104
+ ### Features
105
+
106
+ #### `:oauth_http_mac`
107
+
108
+ A new feature builds on top of `:oauth` to allow MAC authorization.
109
+
110
+ ```ruby
111
+ plugin :rodauth do
112
+ enable :oauth_http_mac
113
+ # options here...
114
+ end
115
+ ```
116
+
117
+ #### `:oauth_jwt`
118
+
119
+ Another new feature, this time supporting the generation of JWT access tokens.
120
+
121
+ ```ruby
122
+ plugin :rodauth do
123
+ enable :oauth_jwt
124
+ # options here...
125
+ end
126
+ ```
127
+
128
+ ### Improvements
129
+
130
+ * added options for disabling pkce and access type (respectively, `use_oauth_pkce?` and `use_oauth_access_type?`);
131
+ * renamed the existing `use_oauth_implicit_grant_type` to `use_oauth_implicit_grant_type?`;
132
+ * It's now usable as JSON API (small caveat: POST authorize will still redirect on success...);
133
+
134
+ ## 0.0.2 (29/5/2020)
135
+
136
+ ### Features
137
+
138
+ * Implementation of PKCE by OAuth Public Clients (https://tools.ietf.org/html/rfc7636);
139
+ * Implementation of grants using "access_type" and "approval_prompt" ([similar to what Google OAuth 2.0 API does](https://wiki.scn.sap.com/wiki/display/Security/Access+Google+APIs+using+the+OAuth+2.0+Client+API));
140
+
141
+ ### Improvements
142
+
143
+ * Store token/refresh token hashes in the database, instead of the "plain" tokens;
144
+ * Client secret hashed by default, and provided by the application owner;
145
+
146
+ ### Fix
147
+
148
+ * usage of client secret for authorizing the generation of tokens, as the spec mandates (and refraining from them when doing PKCE).
149
+
150
+ ## 0.0.1 (14/5/2020)
4
151
 
5
152
  Initial implementation of the Oauth 2.0 framework, with an example app done using roda.
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ https://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
+ Copyright 2013-2017 Docker, Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ https://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![pipeline status](https://gitlab.com/honeyryderchuck/rodauth-oauth/badges/master/pipeline.svg)](https://gitlab.com/honeyryderchuck/rodauth-oauth/-/commits/master)
4
4
  [![coverage report](https://gitlab.com/honeyryderchuck/rodauth-oauth/badges/master/coverage.svg)](https://gitlab.com/honeyryderchuck/rodauth-oauth/-/commits/master)
5
5
 
6
- This is an extension to the `rodauth` gem which adds support for the [OAuth 2.0 protocol](https://tools.ietf.org/html/rfc6749).
6
+ This is an extension to the `rodauth` gem which implements the [OAuth 2.0 framework](https://tools.ietf.org/html/rfc6749) for an authorization server.
7
7
 
8
8
  ## Features
9
9
 
@@ -13,12 +13,17 @@ This gem implements:
13
13
  * [Authorization grant flow](https://tools.ietf.org/html/rfc6749#section-1.3);
14
14
  * [Access Token generation](https://tools.ietf.org/html/rfc6749#section-1.4);
15
15
  * [Access Token refresh](https://tools.ietf.org/html/rfc6749#section-1.5);
16
- * [Token revocation](https://tools.ietf.org/html/rfc7009);
17
16
  * [Implicit grant (off by default)[https://tools.ietf.org/html/rfc6749#section-4.2];
17
+ * [Token revocation](https://tools.ietf.org/html/rfc7009);
18
+ * [Token introspection](https://tools.ietf.org/html/rfc7662);
19
+ * [Authorization Server Metadata](https://tools.ietf.org/html/rfc8414);
20
+ * [PKCE](https://tools.ietf.org/html/rfc7636);
18
21
  * Access Type (Token refresh online and offline);
22
+ * [MAC Authentication Scheme](https://tools.ietf.org/html/draft-hammer-oauth-v2-mac-token-02);
23
+ * [JWT Acess Tokens](https://tools.ietf.org/html/draft-ietf-oauth-access-token-jwt-07);
24
+ * [JWT Secured Authorization Requests](https://tools.ietf.org/html/draft-ietf-oauth-jwsreq-20);
19
25
  * OAuth application and token management dashboards;
20
26
 
21
-
22
27
  This gem supports also rails (through [rodauth-rails]((https://github.com/janko/rodauth-rails))).
23
28
 
24
29
 
@@ -85,7 +90,7 @@ You'll have to do a bit more boilerplate, so here's the instructions.
85
90
 
86
91
  ### Example (TL;DR)
87
92
 
88
- If you're familiar with the technology and want to skip the next paragraphs, just [check our roda example](https://gitlab.com/honeyryderchuck/rodauth-oauth/-/tree/master/examples/roda).
93
+ If you're familiar with the technology and want to skip the next paragraphs, just [check our example applications](https://gitlab.com/honeyryderchuck/rodauth-oauth/-/tree/master/examples/).
89
94
 
90
95
 
91
96
  Generating tokens happens mostly server-to-server, so here's an example using:
@@ -96,22 +101,21 @@ Generating tokens happens mostly server-to-server, so here's an example using:
96
101
 
97
102
  ```ruby
98
103
  require "httpx"
99
- httpx = HTTPX.plugin(:authorization)
100
- response = httpx.with(headers: { "X-your-auth-scheme" => ENV["SERVER_KEY"] })
101
- .post("https://auth_server/oauth-token",json: {
104
+ response = HTTPX.post("https://auth_server/oauth-token",json: {
102
105
  client_id: ENV["OAUTH_CLIENT_ID"],
106
+ client_secret: ENV["OAUTH_CLIENT_SECRET"],
103
107
  grant_type: "authorization_code",
104
108
  code: "oiweicnewdh32fhoi3hf3ihfo2ih3f2o3as"
105
109
  })
106
110
  response.raise_for_status
107
111
  payload = JSON.parse(response.to_s)
108
- puts payload #=> {"token" => "awr23f3h8f9d2h89...", "refresh_token" => "23fkop3kr290kc..." ....
112
+ puts payload #=> {"access_token" => "awr23f3h8f9d2h89...", "refresh_token" => "23fkop3kr290kc..." ....
109
113
  ```
110
114
 
111
115
  ##### cURL
112
116
 
113
117
  ```
114
- > curl -H "X-your-auth-scheme: $SERVER_KEY" --data '{"client_id":"$OAUTH_CLIENT_ID","grant_type":"authorization_code","code":"oiweicnewdh32fhoi3hf3ihfo2ih3f2o3as"}' https://auth_server/oauth-token
118
+ > curl --data '{"client_id":"$OAUTH_CLIENT_ID","client_secret":"$OAUTH_CLIENT_SECRET","grant_type":"authorization_code","code":"oiweicnewdh32fhoi3hf3ihfo2ih3f2o3as"}' https://auth_server/oauth-token
115
119
  ```
116
120
 
117
121
  #### Refresh Token
@@ -122,22 +126,21 @@ Refreshing expired tokens also happens mostly server-to-server, here's an exampl
122
126
 
123
127
  ```ruby
124
128
  require "httpx"
125
- httpx = HTTPX.plugin(:authorization)
126
- response = httpx.with(headers: { "X-your-auth-scheme" => ENV["SERVER_KEY"] })
127
- .post("https://auth_server/oauth-token",json: {
129
+ response = HTTPX.post("https://auth_server/oauth-token",json: {
128
130
  client_id: ENV["OAUTH_CLIENT_ID"],
131
+ client_secret: ENV["OAUTH_CLIENT_SECRET"],
129
132
  grant_type: "refresh_token",
130
133
  token: "2r89hfef4j9f90d2j2390jf390g"
131
134
  })
132
135
  response.raise_for_status
133
136
  payload = JSON.parse(response.to_s)
134
- puts payload #=> {"token" => "awr23f3h8f9d2h89...", "token_type" => "Bearer" ....
137
+ puts payload #=> {"access_token" => "awr23f3h8f9d2h89...", "token_type" => "Bearer" ....
135
138
  ```
136
139
 
137
140
  ##### cURL
138
141
 
139
142
  ```
140
- > curl -H "X-your-auth-scheme: $SERVER_KEY" --data '{"client_id":"$OAUTH_CLIENT_ID","grant_type":"token","token":"2r89hfef4j9f90d2j2390jf390g"}' https://auth_server/oauth-token
143
+ > curl -H "X-your-auth-scheme: $SERVER_KEY" --data '{"client_id":"$OAUTH_CLIENT_ID","client_secret":"$OAUTH_CLIENT_SECRET","grant_type":"token","token":"2r89hfef4j9f90d2j2390jf390g"}' https://auth_server/oauth-token
141
144
  ```
142
145
 
143
146
  #### Revoking tokens
@@ -146,16 +149,38 @@ Token revocation can be done both by the idenntity owner or the application owne
146
149
 
147
150
  ```ruby
148
151
  require "httpx"
149
- httpx = HTTPX.plugin(:authorization)
150
- response = httpx.with(headers: { "X-your-auth-scheme" => ENV["SERVER_KEY"] })
152
+ httpx = HTTPX.plugin(:basic_authorization)
153
+ response = httpx.basic_authentication(ENV["CLIENT_ID"], ENV["CLIENT_SECRET"])
151
154
  .post("https://auth_server/oauth-revoke",json: {
152
- client_id: ENV["OAUTH_CLIENT_ID"],
153
155
  token_type_hint: "access_token", # can also be "refresh:tokn"
154
156
  token: "2r89hfef4j9f90d2j2390jf390g"
155
157
  })
156
158
  response.raise_for_status
157
159
  payload = JSON.parse(response.to_s)
158
- puts payload #=> {"token" => "awr23f3h8f9d2h89...", "token_type" => "Bearer" ....
160
+ puts payload #=> {"access_token" => "awr23f3h8f9d2h89...", "token_type" => "Bearer" ....
161
+ ```
162
+
163
+ ##### cURL
164
+
165
+ ```
166
+ > curl -H "X-your-auth-scheme: $SERVER_KEY" --data '{"client_id":"$OAUTH_CLIENT_ID","token_type_hint":"access_token","token":"2r89hfef4j9f90d2j2390jf390g"}' https://auth_server/oauth-revoke
167
+ ```
168
+
169
+ #### Token introspection
170
+
171
+ Token revocation can be used to determine the state of a token (whether active, what's the scope...) . Here's an example using server-to-server:
172
+
173
+ ```ruby
174
+ require "httpx"
175
+ httpx = HTTPX.plugin(:basic_authorization)
176
+ response = httpx.basic_authentication(ENV["CLIENT_ID"], ENV["CLIENT_SECRET"])
177
+ .post("https://auth_server/oauth-introspect",json: {
178
+ token_type_hint: "access_token", # can also be "refresh:tokn"
179
+ token: "2r89hfef4j9f90d2j2390jf390g"
180
+ })
181
+ response.raise_for_status
182
+ payload = JSON.parse(response.to_s)
183
+ puts payload #=> {"active" => true, "scope" => "read write" ....
159
184
  ```
160
185
 
161
186
  ##### cURL
@@ -164,6 +189,32 @@ puts payload #=> {"token" => "awr23f3h8f9d2h89...", "token_type" => "Bearer" ...
164
189
  > curl -H "X-your-auth-scheme: $SERVER_KEY" --data '{"client_id":"$OAUTH_CLIENT_ID","token_type_hint":"access_token","token":"2r89hfef4j9f90d2j2390jf390g"}' https://auth_server/oauth-revoke
165
190
  ```
166
191
 
192
+ ### Authorization Server Metadata
193
+
194
+ The Authorization Server Metadata endpoint can be used by clients to obtain the information needed to interact with an
195
+ OAuth 2.0 authorization server, i.e. know which endpoint is used to authorize clients.
196
+
197
+ Because this endpoint **must be https://AUTHSERVER/.well-known/oauth-authorization-server**, you'll have to define it at the root-level of your app:
198
+
199
+ ```ruby
200
+ plugin :rodauth do
201
+ # enable it in the plugin
202
+ enable :login, :oauth
203
+ oauth_application_default_scope %w[profile.read]
204
+ oauth_application_scopes %w[profile.read profile.write]
205
+ end
206
+
207
+ # then, inside roda
208
+
209
+ route do |r|
210
+ r.rodauth
211
+ # server metadata endpoint
212
+ rodauth.oauth_server_metadata
213
+
214
+ # now, your oauth and app code...
215
+
216
+ ```
217
+
167
218
  ### Database migrations
168
219
 
169
220
  You have to generate database tables for Oauth applications, grants and tokens. In order for you to hit the ground running, [here's a set of migrations (using `sequel`) to generate the needed tables](https://gitlab.com/honeyryderchuck/rodauth-oauth/-/tree/master/test/migrate) (omit the first 2 if you already have account tables).
@@ -291,13 +342,66 @@ end
291
342
 
292
343
  In this section, the non-standard features are going to be described in more detail.
293
344
 
345
+ ### Token / Secrets Hashing
346
+
347
+ Although not human-friendly as passwords, for security reasons, you might not want to store access (and refresh) tokens in the database. If that is the case, You'll have to add the respective hash columns in the table:
348
+
349
+ ```ruby
350
+ # in migration
351
+ String :token_hash, null: false, token: true
352
+ String :refresh_token_hash, token, true
353
+ # and you DO NOT NEED the token and refresh_token columns anymore!
354
+ ```
355
+
356
+ And declare them in the plugin:
357
+
358
+ ```ruby
359
+ plugin :rodauth do
360
+ enable :oauth
361
+ oauth_tokens_token_hash_column :token_hash
362
+ oauth_tokens_token_hash_column :refresh_token_hash
363
+ ```
364
+
365
+ #### Client Secret
366
+
367
+ By default, it's expected that the "client secret" property from an OAuth application is only known by the owner, and only the hash is stored in the database; this way, the authorization server doesn't know what the client secret is, only the application owner. The provided [OAuth Applications Extensions](#oauth-applications) application form contains a "Client Secret" input field for this reason.
368
+
369
+ However, this extension is optional, and you might want to generate the secrets and store them as is. In that case, you'll have to re-define some options:
370
+
371
+ ```ruby
372
+ plugin :rodauth do
373
+ enable :oauth
374
+ secret_matches? ->(application, secret){ application[:client_secret] == secret }
375
+ end
376
+ ```
377
+
294
378
  ### Access Type (default: "offline")
295
379
 
296
380
  The "access_type" feature allows the authorization server to emit access tokens with no associated refresh token. This means that users with expired access tokens will have to go through the OAuth flow everytime they need a new one.
297
381
 
298
382
  In order to enable this option, add "access_type=online" to the query params section of the authorization url.
299
383
 
300
- **Note**: this feature does not yet support the "approval_prompt" feature.
384
+ #### Approval Prompt
385
+
386
+ When using "online grants", one can use an extra query param in the URL, "approval_prompt", which when set to "auto", will skip the authorization form (on the other hand, if one wants to force the authorization form for all grants, then you can set it to "force", or don't set it at all, as it's the default).
387
+
388
+ This will only work **if there was a previous successful online grant** for the same application, scopes and redirect URI.
389
+
390
+ #### DB schema
391
+
392
+ the "oauth_grants" table will have to include the "access_type" row:
393
+
394
+ ```ruby
395
+ # in migration
396
+ String :access_type, null: false, default: "offline"
397
+ ```
398
+
399
+ If you want to disable this flow altogether, you can:
400
+
401
+ ```ruby
402
+ enable :oauth
403
+ use_oauth_access_type? false
404
+ ```
301
405
 
302
406
 
303
407
  ### Implicit Grant (default: disabled)
@@ -315,13 +419,197 @@ end
315
419
 
316
420
  And add "response_type=token" to the query params section of the authorization url.
317
421
 
422
+ ### PKCE
423
+
424
+ The "Proof Key for Code Exchange by OAuth Public Clients" (aka PKCE) flow, which is **particularly recommended for OAuth integration in mobile apps**, is transparently supported by `rodauth-oauth`, by adding the `code_challenge_method=S256&code_challenge=$YOUR_CODE_CHALLENGE` query params to the authorization url. Once you do that, you'll have to pass the `code_verifier` when generating a token:
425
+
426
+ ```ruby
427
+ # with httpx
428
+ require "httpx"
429
+ response = HTTPX.post("https://auth_server/oauth-token",json: {
430
+ client_id: ENV["OAUTH_CLIENT_ID"],
431
+ grant_type: "authorization_code",
432
+ code: "oiweicnewdh32fhoi3hf3ihfo2ih3f2o3as",
433
+ code_verifier: your_code_verifier_here
434
+ })
435
+ response.raise_for_status
436
+ payload = JSON.parse(response.to_s)
437
+ puts payload #=> {"access_token" => ....
438
+ ```
439
+
440
+ By default, the pkce integration sets "S256" as the default challenge method. If you value security, you **should not use plain**. However, if you really need to, you can set it in the `rodauth` plugin:
441
+
442
+ ```ruby
443
+ plugin :rodauth do
444
+ enable :oauth
445
+ oauth_pkce_challenge_method "plain"
446
+ end
447
+ ```
448
+
449
+ Although PKCE flow is supported out-of-the-box, it's not enforced by default. If you want to, you can force it, thereby forcing clients to generate a challenge:
450
+
451
+ ```ruby
452
+ plugin :rodauth do
453
+ enable :oauth
454
+ oauth_require_pkce true
455
+ end
456
+ ```
457
+
458
+ If you want, on the other hand. to disable this flow altogether, you can:
459
+
460
+ ```ruby
461
+ enable :oauth
462
+ use_oauth_pkce? false
463
+ ```
464
+
465
+ ### HTTP Mac Authentication
466
+
467
+ You can enable HTTP MAC authentication like this:
468
+
469
+ ```ruby
470
+ plugin :rodauth do
471
+ enable :oauth_http_mac
472
+ end
473
+ ```
474
+
475
+ Generating an access token will deliver the following fields:
476
+
477
+ ```ruby
478
+ # with httpx
479
+ require "httpx"
480
+ response = httpx.post("https://auth_server/oauth-token",json: {
481
+ client_id: env["oauth_client_id"],
482
+ client_secret: env["oauth_client_secret"],
483
+ grant_type: "authorization_code",
484
+ code: "oiweicnewdh32fhoi3hf3ihfo2ih3f2o3as"
485
+ })
486
+ response.raise_for_status
487
+ payload = json.parse(response.to_s)
488
+ puts payload #=> {
489
+ # "access_token" => ....
490
+ # "mac_key" => ....
491
+ # "mac_algorithm" =>
492
+ ```
493
+
494
+ which you'll be able to use to generate the mac signature to send in the "Authorization" header.
495
+
496
+ #### DB schema
497
+
498
+ the "oauth_tokens" table will have to include a column for the mac key:
499
+
500
+ ```ruby
501
+ # in migration
502
+ String :mac_key, token: true
503
+ ```
504
+
505
+
506
+ ### JWT Access Tokens
507
+
508
+ JWT Acess Tokens are great to avoid DB lookups when validation the authorization token. Quoting the RFC, *The approach is particularly common in topologies where the authorization server and resource server are not co-located, are not run by the same entity, or are otherwise separated by some boundary.*
509
+
510
+ You can enable JWT Access tokens by doing:
511
+
512
+ ```ruby
513
+ plugin :rodauth do
514
+ enable :oauth_jwt
515
+ end
516
+ ```
517
+
518
+ This will, by default, use the OAuth application as HMAC signature and "HS256" as the algorithm to sign the resulting JWT access tokens. You can tweak those features by editing the following options:
519
+
520
+ ```ruby
521
+ enable :oauth_jwt
522
+ oauth_jwt_secret "SECRET"
523
+ oauth_jwt_algorithm "HS512"
524
+ ```
525
+
526
+ You can look for other options in [the jwt gem documentation](https://github.com/jwt/ruby-jwt), as this is used under the hood.
527
+
528
+ #### Pub/Priv key
529
+
530
+ You can decide to keep a private key to encode the JWT token, while other clients hace the public key to decode it. You can then do it like:
531
+
532
+ ```ruby
533
+ rsa_private = OpenSSL::PKey::RSA.generate 2048
534
+ rsa_public = rsa_private.public_key
535
+
536
+ plugin :rodauth do
537
+ enable :oauth_jwt
538
+ oauth_jwt_key rsa_private
539
+ oauth_jwt_public_key rsa_public
540
+ oauth_jwt_algorithm "RS256"
541
+ end
542
+ ```
543
+
544
+ #### JWK
545
+
546
+ One can further encode the JWT token using JSON Web Keys. Here's how you could enable the feature:
547
+
548
+ ```ruby
549
+ rsa_private = OpenSSL::PKey::RSA.generate 2048
550
+ rsa_public = rsa_private.public_key
551
+
552
+ plugin :rodauth do
553
+ enable :oauth_jwt
554
+ oauth_jwt_jwk_key rsa_private
555
+ oauth_jwt_jwk_public_key rsa_public
556
+ oauth_jwt_jwk_algorithm "RS256"
557
+ end
558
+ ```
559
+
560
+ #### JWE
561
+
562
+ You can further instruct the jwt feature to encrypt the encoded token using JSON Web Encryption standard:
563
+
564
+ ```ruby
565
+ jwe_key = OpenSSL::PKey::RSA.new(2048)
566
+
567
+ plugin :rodauth do
568
+ oauth_jwt_secret "SECRET"
569
+ oauth_jwt_algorithm "HS256"
570
+ oauth_jwt_jwe_key jwe_key
571
+ oauth_jwt_jwe_encryption_method "A192GCM"
572
+ end
573
+ ```
574
+
575
+ which adds an extra layer of protection.
576
+
577
+ #### JWKS URI
578
+
579
+ A route is defined for getting the JWK Set in a JSON format; this is typically used by client applications, who need the JWK set to decode the JWT token. This URL is typically `https://oauth-server/oauth-jwks`.
580
+
581
+ #### JWT Bearer as authorization grant
582
+
583
+ One can emit a new access token by using the bearer access token as grant. This can be done emitting a request similar to this:
584
+
585
+ ```ruby
586
+ # with httpx
587
+ require "httpx"
588
+ response = httpx.post("https://auth_server/oauth-token",json: {
589
+ grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
590
+ assertion: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6IkV4YW1wbGUiLCJpYXQiOjE1OTIwMDk1MDEsImNsaWVudF9pZCI6IkNMSUVOVF9JRCIsImV4cCI6MTU5MjAxMzEwMSwiYXVkIjpudWxsLCJzY29wZSI6InVzZXIucmVhZCB1c2VyLndyaXRlIiwianRpIjoiOGM1NTVjMjdiOWRjNDdmOTcyNWRkYzBhMjk0NzA1ZTA4NzFkY2JlN2Q5ZTNlMmVkNGE1ZTBiOGZlNTZlYzcxMSJ9.AlxKRtE3ec0mtyBSDx4VseND4eC6cH5ubtv8gfYxxsc"
591
+ })
592
+ response.raise_for_status
593
+ payload = json.parse(response.to_s)
594
+ puts payload #=> {
595
+ # "access_token" => "ey....
596
+ ```
597
+
598
+ #### DB Schema
599
+
600
+ You'll still need the "oauth_tokens" table, however you can remove the "token" column.
601
+
602
+ #### Caveats
603
+
604
+ Although very handy for the mentioned use case, one can't revoke a JWT token on demand (it must expire first).
605
+
318
606
  ## Ruby support policy
319
607
 
320
- The minimum Ruby version required to run `rodauth-oauth` is 2.3 . Besides that, it should support all rubies that rodauth and roda support.
608
+ The minimum Ruby version required to run `rodauth-oauth` is 2.3 . Besides that, it should support all rubies that rodauth and roda support, including JRuby and (potentially, I don't know yet) truffleruby.
321
609
 
322
610
  ### JRuby
323
611
 
324
- If you're interested in using this library in rails, be warned that `rodauth-rails` doesn't support it yet (although, this is expected to change at some point).
612
+ If you're interested in using this library in rails, be sure to check `rodauth-rails` policy, as it supports rails 5.2 upwards.
325
613
 
326
614
  ## Development
327
615