auth_rails 1.1.0 → 1.1.1

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: c69df81c88cacdf202e2c49cf329423ded23e509fc2dc67fd60d07fda1fbe12e
4
- data.tar.gz: abbad690e4211d181950af7bf542e2df367b79c0c79c049f32c5192188106f4f
3
+ metadata.gz: 15e71bc7d2e92cc25d3433db5adbd5608f70c5fbf9de27a234106d95d54d9957
4
+ data.tar.gz: cfab483acafa215d5530e8ec90904cdceadb55bb53074b69ad73c2ef756ab7ca
5
5
  SHA512:
6
- metadata.gz: b97b6c42ed2386b526ce58bbc9e8fd2982e7b60357adebfb0c547f3b3cbf6d07f6cbca8e4cf13871a42f69935ae09daf2dc7528563dd024dcc95dc95e8611f5a
7
- data.tar.gz: 758425cb02330e2efd68fedc8730e4b8879136098f0ecfb1d9543812e8220fdbc51f8ec1eaa15c19a09d17e29788c4968969121e08c9a6cf922a2ea4b60e005e
6
+ metadata.gz: 92bbbfb5274f625b03f8879d9b5cf3f6e61d577a8c30e53bda9cd374e74b46fc821717c7bac9f116502d64f5f1fb55571cd424d0583de3a4b260726c0d4c5ffc
7
+ data.tar.gz: c4479a4db819b2ba649fed888c074185c751ffbccb71ba4278c9747c01adb9cb3bcf8e0db5eb47c3db60d7b015cb37bb64c4d4c63cb77bab869a4a0ef45d9da2
data/biome.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.4.1/schema.json",
3
+ "organizeImports": {
4
+ "enabled": true
5
+ },
6
+ "linter": {
7
+ "enabled": true,
8
+ "rules": {
9
+ "recommended": true,
10
+ "complexity": {
11
+ "useArrowFunction": "off"
12
+ },
13
+ "style": {
14
+ "noParameterAssign": "off"
15
+ }
16
+ }
17
+ },
18
+ "javascript": {
19
+ "formatter": {
20
+ "indentStyle": "space",
21
+ "quoteStyle": "single"
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,90 @@
1
+ import { defineConfig } from 'vitepress';
2
+
3
+ export default defineConfig({
4
+ title: 'AuthRails',
5
+ description: 'Simple authentication for Rails',
6
+ srcDir: './src',
7
+ base: '/auth_rails/',
8
+ themeConfig: {
9
+ nav: [
10
+ {
11
+ text: 'Guide',
12
+ link: '/introduction/what-is-it',
13
+ },
14
+ ],
15
+ sidebar: [
16
+ {
17
+ text: 'Introduction',
18
+ items: [
19
+ {
20
+ text: 'What is AuthRails?',
21
+ link: '/introduction/what-is-it',
22
+ },
23
+ {
24
+ text: 'Getting Started',
25
+ link: '/introduction/getting-started',
26
+ },
27
+ ],
28
+ },
29
+ {
30
+ text: 'CLI',
31
+ items: [
32
+ {
33
+ text: 'Configuration',
34
+ link: '/cli/configuration',
35
+ },
36
+ {
37
+ text: 'Migration',
38
+ link: '/cli/migration',
39
+ },
40
+ ],
41
+ },
42
+ {
43
+ text: 'Customization',
44
+ items: [
45
+ {
46
+ text: 'Custom Strategy',
47
+ link: '/customization/custom-strategy',
48
+ },
49
+ {
50
+ text: 'Custom Response Data',
51
+ link: '/customization/custom-response',
52
+ },
53
+ {
54
+ text: 'Custom Password Validation',
55
+ link: '/customization/custom-password-validation',
56
+ },
57
+ {
58
+ text: 'Custom Identifier Column',
59
+ link: '/customization/custom-identifier',
60
+ },
61
+ {
62
+ text: 'Complex Retrieve Resource',
63
+ link: '/customization/complex-retrieve-resource',
64
+ },
65
+ ],
66
+ },
67
+ {
68
+ text: 'API Reference',
69
+ link: '/api-reference',
70
+ },
71
+ ],
72
+ outline: {
73
+ level: [2, 3],
74
+ label: 'On this page',
75
+ },
76
+ lastUpdated: {
77
+ text: 'Last updated',
78
+ formatOptions: {
79
+ dateStyle: 'full',
80
+ timeStyle: 'medium',
81
+ },
82
+ },
83
+ socialLinks: [
84
+ {
85
+ icon: 'github',
86
+ link: 'https://github.com/zgid123/auth_rails',
87
+ },
88
+ ],
89
+ },
90
+ });
@@ -0,0 +1,362 @@
1
+ # API Reference
2
+
3
+ All features of AuthRails.
4
+
5
+ ## Configuration
6
+
7
+ ### dig_params
8
+
9
+ - Type: `Proc`
10
+ - Default: `nil`
11
+ - Required: `false`
12
+
13
+ Method to extract `identifier` for [`retrieve_resource`](/api-reference.html#retrieve-resource).
14
+
15
+ ```rb
16
+ # frozen_string_literal: true
17
+
18
+ Rails.application.config.to_prepare do
19
+ AuthRails.configure do |config|
20
+ config.resource_class = User
21
+ config.identifier_name = :username
22
+ config.dig_params = ->(params) { params[:identifier] }
23
+
24
+ config.retrieve_resource = lambda { |identifier|
25
+ User.where(email: identifier)
26
+ .or(User.where(username: identifier))
27
+ .first
28
+ }
29
+ end
30
+ end
31
+ ```
32
+
33
+ `identifier_name` will be used for JWT's payload's `sub` if you have `dig_params` configuration.
34
+
35
+ ### error_class
36
+
37
+ - Type: `Class`
38
+ - Default: `nil`
39
+ - Required: `false`
40
+
41
+ Custom error class for AuthRails.
42
+
43
+ Whenever AuthRails raises error, it will raise your error.
44
+
45
+ ```rb
46
+ # frozen_string_literal: true
47
+
48
+ Rails.application.config.to_prepare do
49
+ AuthRails.configure do |config|
50
+ config.resource_class = User
51
+ config.error_class = YourError
52
+ end
53
+ end
54
+ ```
55
+
56
+ ### authenticate
57
+
58
+ - Type: `Proc`
59
+ - Default: `nil`
60
+ - Required: `false`
61
+
62
+ Custom method to validate your user password. If not provided, you must add `has_secure_password` to your model. Or create a method called `authenticate` to do the validation for your model. Or else it will raise error.
63
+
64
+ ```rb
65
+ # frozen_string_literal: true
66
+
67
+ Rails.application.config.to_prepare do
68
+ AuthRails.configure do |config|
69
+ config.resource_class = User
70
+ config.authenticate = ->(resource, password) { resource.password == password }
71
+ end
72
+ end
73
+ ```
74
+
75
+ ### resource_class
76
+
77
+ - Type: `Class`
78
+ - Default: `nil`
79
+ - Required: `true`
80
+
81
+ Your own class to do sign in. Usually it is `User`.
82
+
83
+ ```rb
84
+ # frozen_string_literal: true
85
+
86
+ Rails.application.config.to_prepare do
87
+ AuthRails.configure do |config|
88
+ config.resource_class = User
89
+ end
90
+ end
91
+ ```
92
+
93
+ ### identifier_name
94
+
95
+ - Type: `String` | `Symbol`
96
+ - Default: `:email`
97
+ - Required: `false`
98
+
99
+ Your resource class identifier.
100
+
101
+ ```rb
102
+ # frozen_string_literal: true
103
+
104
+ Rails.application.config.to_prepare do
105
+ AuthRails.configure do |config|
106
+ config.resource_class = User
107
+ config.identifier_name = :username
108
+ end
109
+ end
110
+ ```
111
+
112
+ ### retrieve_resource
113
+
114
+ - Type: `Proc`
115
+ - Default: `nil`
116
+ - Required: `false`
117
+
118
+ Method to custom how to get resource when your project requires a complex logic.
119
+
120
+ ```rb
121
+ # frozen_string_literal: true
122
+
123
+ Rails.application.config.to_prepare do
124
+ AuthRails.configure do |config|
125
+ config.resource_class = User
126
+ config.identifier_name = :username
127
+ config.dig_params = ->(params) { params[:identifier] }
128
+
129
+ config.retrieve_resource = lambda { |identifier|
130
+ User.where(email: identifier)
131
+ .or(User.where(username: identifier))
132
+ .first
133
+ }
134
+ end
135
+ end
136
+ ```
137
+
138
+ #### config.identifier_name
139
+
140
+ This is used for JWT's payload's `sub`.
141
+
142
+ #### config.dig_params
143
+
144
+ This extracts `identifier` from parameters for the provided method.
145
+
146
+ ## JWT Configuration
147
+
148
+ ### jwt.strategy
149
+
150
+ - Type: `Class`
151
+ - Default: `AuthRails::Strategies::BaseStrategy`
152
+ - Required: `false`
153
+
154
+ Specify which strategy to handle `refresh_token`.
155
+
156
+ ```rb
157
+ # frozen_string_literal: true
158
+
159
+ class YourOwnStrategy < AuthRails::Strategies::BaseStrategy
160
+ end
161
+
162
+ AuthRails.configure do |config|
163
+ config.jwt do |jwt|
164
+ jwt.strategy = YourOwnStrategy
165
+ end
166
+ end
167
+ ```
168
+
169
+ ## JWT Access Token Configuration
170
+
171
+ ### access_token.exp
172
+
173
+ - Type: `ActiveSupport::TimeWithZone`
174
+ - Default: `nil`
175
+ - Required: `false`
176
+
177
+ Expiry time for `access_token`.
178
+
179
+ ```rb
180
+ # frozen_string_literal: true
181
+
182
+ AuthRails.configure do |config|
183
+ config.jwt do |jwt|
184
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
185
+
186
+ jwt.access_token do |access_token|
187
+ access_token.exp = 1.hour.since
188
+ end
189
+ end
190
+ end
191
+ ```
192
+
193
+ ### access_token.algorithm
194
+
195
+ - Type: `string`
196
+ - Default: `HS256`
197
+ - Required: `false`
198
+
199
+ Algorithm for JWT generator.
200
+
201
+ ```rb
202
+ # frozen_string_literal: true
203
+
204
+ AuthRails.configure do |config|
205
+ config.jwt do |jwt|
206
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
207
+
208
+ jwt.access_token do |access_token|
209
+ access_token.exp = 1.hour.since
210
+ access_token.algorithm = 'HS384'
211
+ end
212
+ end
213
+ end
214
+ ```
215
+
216
+ ### access_token.secret_key
217
+
218
+ - Type: `string`
219
+ - Default: `nil`
220
+ - Required: `false`
221
+
222
+ Secret token for JWT generator.
223
+
224
+ ```rb
225
+ # frozen_string_literal: true
226
+
227
+ AuthRails.configure do |config|
228
+ config.jwt do |jwt|
229
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
230
+
231
+ jwt.access_token do |access_token|
232
+ access_token.exp = 1.hour.since
233
+ access_token.algorithm = 'HS384'
234
+ access_token.secret_key = 'My Secret Key'
235
+ end
236
+ end
237
+ end
238
+ ```
239
+
240
+ ## JWT Refresh Token Configuration
241
+
242
+ ### refresh_token.exp
243
+
244
+ - Type: `ActiveSupport::TimeWithZone`
245
+ - Default: `nil`
246
+ - Required: `false`
247
+
248
+ Expiry time for `refresh_token`.
249
+
250
+ ```rb
251
+ # frozen_string_literal: true
252
+
253
+ AuthRails.configure do |config|
254
+ config.jwt do |jwt|
255
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
256
+
257
+ jwt.refresh_token do |refresh_token|
258
+ refresh_token.exp = 1.hour.since
259
+ end
260
+ end
261
+ end
262
+ ```
263
+
264
+ ### refresh_token.algorithm
265
+
266
+ - Type: `string`
267
+ - Default: `nil`
268
+ - Required: `false`
269
+
270
+ Algorithm for JWT generator.
271
+
272
+ ```rb
273
+ # frozen_string_literal: true
274
+
275
+ AuthRails.configure do |config|
276
+ config.jwt do |jwt|
277
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
278
+
279
+ jwt.refresh_token do |refresh_token|
280
+ refresh_token.exp = 1.hour.since
281
+ refresh_token.algorithm = 'HS384'
282
+ end
283
+ end
284
+ end
285
+ ```
286
+
287
+ ### refresh_token.secret_key
288
+
289
+ - Type: `string`
290
+ - Default: `nil`
291
+ - Required: `false`
292
+
293
+ Secret token for JWT generator.
294
+
295
+ ```rb
296
+ # frozen_string_literal: true
297
+
298
+ AuthRails.configure do |config|
299
+ config.jwt do |jwt|
300
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
301
+
302
+ jwt.refresh_token do |refresh_token|
303
+ refresh_token.exp = 1.hour.since
304
+ refresh_token.algorithm = 'HS384'
305
+ refresh_token.secret_key = 'My Secret Key'
306
+ end
307
+ end
308
+ end
309
+ ```
310
+
311
+ ### refresh_token.http_only
312
+
313
+ - Type: `boolean`
314
+ - Default: `false`
315
+ - Required: `false`
316
+
317
+ If true, before respond the `refresh_token`, AuthRails will set `refresh_token` as `httpOnly` cookie.
318
+
319
+ Cookie key will be `ref_tok`.
320
+
321
+ ```rb
322
+ # frozen_string_literal: true
323
+
324
+ AuthRails.configure do |config|
325
+ config.jwt do |jwt|
326
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
327
+
328
+ jwt.refresh_token do |refresh_token|
329
+ refresh_token.http_only = true
330
+ refresh_token.exp = 1.hour.since
331
+ refresh_token.algorithm = 'HS384'
332
+ refresh_token.secret_key = 'My Secret Key'
333
+ end
334
+ end
335
+ end
336
+ ```
337
+
338
+ ### refresh_token.cookie_key
339
+
340
+ - Type: `String` | `Symbol`
341
+ - Default: `false`
342
+ - Required: `false`
343
+
344
+ Set cookie key for AuthRails when [`refresh_token.http_only`](/api-reference.html#refresh-token-http-only) is enabled.
345
+
346
+ ```rb
347
+ # frozen_string_literal: true
348
+
349
+ AuthRails.configure do |config|
350
+ config.jwt do |jwt|
351
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
352
+
353
+ jwt.refresh_token do |refresh_token|
354
+ refresh_token.http_only = true
355
+ refresh_token.exp = 1.hour.since
356
+ refresh_token.algorithm = 'HS384'
357
+ refresh_token.cookie_key = :my_ref_tok
358
+ refresh_token.secret_key = 'My Secret Key'
359
+ end
360
+ end
361
+ end
362
+ ```
@@ -0,0 +1,152 @@
1
+ # CLI to generate Configuration
2
+
3
+ ## Default Option
4
+
5
+ ```sh
6
+ rails g auth_rails
7
+ ```
8
+
9
+ This will create a default configuration for AuthRails.
10
+
11
+ ```rb
12
+ # frozen_string_literal: true
13
+
14
+ AuthRails.configure do |config|
15
+ config.jwt do |jwt|
16
+ jwt.access_token do |access_token|
17
+ access_token.exp = 1.hour.since
18
+ access_token.secret_key = ENV.fetch('JWT_SECRET', '')
19
+ end
20
+
21
+ # jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
22
+
23
+ # if you wanna use refresh token
24
+ # uncomment those lines below
25
+ # jwt.refresh_token do |refresh_token|
26
+ # refresh_token.http_only = true
27
+ # refresh_token.exp = 1.year.since
28
+ # refresh_token.algorithm = 'HS256'
29
+ # refresh_token.cookie_key = :ref_tok
30
+ # refresh_token.secret_key = ENV.fetch('JWT_SECRET', '')
31
+ # end
32
+ end
33
+ end
34
+
35
+ Rails.application.config.to_prepare do
36
+ AuthRails.configure do |config|
37
+ config.resource_class = User
38
+
39
+ # if you wanna use custom error classes
40
+ # uncomment code below
41
+ # config.error_class = AuthError
42
+ end
43
+ end
44
+ ```
45
+
46
+ ## Strategy Option
47
+
48
+ ```sh
49
+ rails g auth_rails --strategy allowed_token
50
+ ```
51
+
52
+ This will create a configuration and enable strategy `AuthRails::Strategies::AlloedTokenStrategy` as default.
53
+
54
+ ```rb
55
+ # frozen_string_literal: true
56
+
57
+ AuthRails.configure do |config|
58
+ config.jwt do |jwt|
59
+ jwt.access_token do |access_token|
60
+ access_token.exp = 1.hour.since
61
+ access_token.secret_key = ENV.fetch('JWT_SECRET', '')
62
+ end
63
+
64
+ jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
65
+
66
+ # remember uncomment those ones
67
+ jwt.refresh_token do |refresh_token|
68
+ refresh_token.http_only = true
69
+ refresh_token.exp = 1.year.since
70
+ refresh_token.algorithm = 'HS256'
71
+ refresh_token.cookie_key = :ref_tok
72
+ refresh_token.secret_key = ENV.fetch('JWT_SECRET', '')
73
+ end
74
+ end
75
+ end
76
+
77
+ Rails.application.config.to_prepare do
78
+ AuthRails.configure do |config|
79
+ config.resource_class = User
80
+
81
+ # if you wanna use custom error classes
82
+ # uncomment code below
83
+ # config.error_class = AuthError
84
+ end
85
+ end
86
+ ```
87
+
88
+ You must modify User model to make this works.
89
+
90
+ ```rb
91
+ # app/models/user.rb
92
+ # frozen_string_literal: true
93
+
94
+ class User < ApplicationRecord
95
+ include AuthRails::Concerns::AllowedTokenStrategy
96
+
97
+ has_secure_password
98
+ end
99
+ ```
100
+
101
+ ## Model Option
102
+
103
+ ```sh
104
+ rails g auth_rails --model CustomUser
105
+ ```
106
+
107
+ This will create a configuration with the `resource_class` as `CustomUser`.
108
+
109
+ ```rb
110
+ # frozen_string_literal: true
111
+
112
+ AuthRails.configure do |config|
113
+ config.jwt do |jwt|
114
+ jwt.access_token do |access_token|
115
+ access_token.exp = 1.hour.since
116
+ access_token.secret_key = ENV.fetch('JWT_SECRET', '')
117
+ end
118
+
119
+ # jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
120
+
121
+ # if you wanna use refresh token
122
+ # uncomment those lines below
123
+ # jwt.refresh_token do |refresh_token|
124
+ # refresh_token.http_only = true
125
+ # refresh_token.exp = 1.year.since
126
+ # refresh_token.algorithm = 'HS256'
127
+ # refresh_token.cookie_key = :ref_tok
128
+ # refresh_token.secret_key = ENV.fetch('JWT_SECRET', '')
129
+ # end
130
+ end
131
+ end
132
+
133
+ Rails.application.config.to_prepare do
134
+ AuthRails.configure do |config|
135
+ config.resource_class = CustomUser
136
+
137
+ # if you wanna use custom error classes
138
+ # uncomment code below
139
+ # config.error_class = AuthError
140
+ end
141
+ end
142
+ ```
143
+
144
+ Remember to modify the `CustomUser` class.
145
+
146
+ ```rb
147
+ # frozen_string_literal: true
148
+
149
+ class CustomUser < ApplicationRecord
150
+ has_secure_password
151
+ end
152
+ ```
@@ -0,0 +1,59 @@
1
+ # CLI to generate Migration
2
+
3
+ This CLI always need to provide a strategy option to know which migration file should be created.
4
+
5
+ ## Default Option
6
+
7
+ ```sh
8
+ rails g auth_rails:migration --strategy allowed_token
9
+ ```
10
+
11
+ This will create a migration file for `AllowedToken` model.
12
+
13
+ ```rb
14
+ # frozen_string_literal: true
15
+
16
+ class CreateAllowedTokens < ActiveRecord::Migration[7.1]
17
+ def change
18
+ create_table :allowed_tokens do |t|
19
+ t.string :jti, null: false
20
+ t.string :aud
21
+ t.datetime :exp, null: false
22
+
23
+ t.timestamps
24
+
25
+ t.references :user, foreign_key: { on_delete: :cascade }, null: false
26
+
27
+ t.index %i[jti aud]
28
+ end
29
+ end
30
+ end
31
+ ```
32
+
33
+ ## Model Option
34
+
35
+ ```sh
36
+ rails g auth_rails:migration --strategy allowed_token --model CustomUser
37
+ ```
38
+
39
+ This will create a migration file for `AllowedToken` model and add reference with `CustomUser`.
40
+
41
+ ```rb
42
+ # frozen_string_literal: true
43
+
44
+ class CreateAllowedTokens < ActiveRecord::Migration[7.1]
45
+ def change
46
+ create_table :allowed_tokens do |t|
47
+ t.string :jti, null: false
48
+ t.string :aud
49
+ t.datetime :exp, null: false
50
+
51
+ t.timestamps
52
+
53
+ t.references :custom_user, foreign_key: { on_delete: :cascade }, null: false
54
+
55
+ t.index %i[jti aud]
56
+ end
57
+ end
58
+ end
59
+ ```