Module: BetterAuth::Plugins

Defined in:
lib/better_auth/plugins/api_key.rb

Constant Summary collapse

API_KEY_ERROR_CODES =
BetterAuth::APIKey::ERROR_CODES
API_KEY_TABLE_NAME =
BetterAuth::APIKey::Types::API_KEY_TABLE_NAME
API_KEY_PLUGIN_IMPLEMENTATION =
true

Class Method Summary collapse

Class Method Details

.api_key(configurations = {}, options = nil) ⇒ Object



42
43
44
# File 'lib/better_auth/plugins/api_key.rb', line 42

def api_key(configurations = {}, options = nil)
  BetterAuth::APIKey::PluginFactory.build(configurations, options)
end

.api_key_auth_required?(ctx) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/better_auth/plugins/api_key.rb', line 292

def api_key_auth_required?(ctx)
  BetterAuth::APIKey::Utils.auth_required?(ctx)
end

.api_key_authorize_reference!(ctx, config, user_id, reference_id, action) ⇒ Object



120
121
122
# File 'lib/better_auth/plugins/api_key.rb', line 120

def api_key_authorize_reference!(ctx, config, user_id, reference_id, action)
  BetterAuth::APIKey::OrgAuthorization.authorize_reference!(ctx, config, user_id, reference_id, action)
end

.api_key_background_tasks?(ctx) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/better_auth/plugins/api_key.rb', line 288

def api_key_background_tasks?(ctx)
  BetterAuth::APIKey::Utils.background_tasks?(ctx)
end

.api_key_check_org_permission!(ctx, user_id, organization_id, action) ⇒ Object



124
125
126
# File 'lib/better_auth/plugins/api_key.rb', line 124

def api_key_check_org_permission!(ctx, user_id, organization_id, action)
  BetterAuth::APIKey::OrgAuthorization.check_permission!(ctx, user_id, organization_id, action)
end

.api_key_check_permissions!(record, required) ⇒ Object



300
301
302
# File 'lib/better_auth/plugins/api_key.rb', line 300

def api_key_check_permissions!(record, required)
  BetterAuth::APIKey::Validation.check_permissions!(record, required)
end

.api_key_config(configurations, options = nil) ⇒ Object



48
49
50
# File 'lib/better_auth/plugins/api_key.rb', line 48

def api_key_config(configurations, options = nil)
  BetterAuth::APIKey::Configuration.normalize(configurations, options)
end

.api_key_config_id_matches?(record_config_id, expected_config_id) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/better_auth/plugins/api_key.rb', line 96

def api_key_config_id_matches?(record_config_id, expected_config_id)
  BetterAuth::APIKey::Routes.config_id_matches?(record_config_id, expected_config_id)
end

.api_key_create_endpoint(config) ⇒ Object



60
61
62
# File 'lib/better_auth/plugins/api_key.rb', line 60

def api_key_create_endpoint(config)
  BetterAuth::APIKey::Routes::CreateAPIKey.endpoint(config)
end

.api_key_create_reference_id!(ctx, body, session, config) ⇒ Object



100
101
102
# File 'lib/better_auth/plugins/api_key.rb', line 100

def api_key_create_reference_id!(ctx, body, session, config)
  BetterAuth::APIKey::OrgAuthorization.create_reference_id!(ctx, body, session, config)
end

.api_key_decode_json(value) ⇒ Object



308
309
310
# File 'lib/better_auth/plugins/api_key.rb', line 308

def api_key_decode_json(value)
  BetterAuth::APIKey::Utils.decode_json(value)
end

.api_key_default_config_id?(value) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/better_auth/plugins/api_key.rb', line 92

def api_key_default_config_id?(value)
  BetterAuth::APIKey::Routes.default_config_id?(value)
end

.api_key_default_permissions(config, reference_id, ctx) ⇒ Object



116
117
118
# File 'lib/better_auth/plugins/api_key.rb', line 116

def api_key_default_permissions(config, reference_id, ctx)
  BetterAuth::APIKey::Types.default_permissions(config, reference_id, ctx)
end

.api_key_delete_endpoint(config) ⇒ Object



76
77
78
# File 'lib/better_auth/plugins/api_key.rb', line 76

def api_key_delete_endpoint(config)
  BetterAuth::APIKey::Routes::DeleteAPIKey.endpoint(config)
end

.api_key_delete_expired(context, config, bypass_last_check: false) ⇒ Object



232
233
234
# File 'lib/better_auth/plugins/api_key.rb', line 232

def api_key_delete_expired(context, config, bypass_last_check: false)
  BetterAuth::APIKey::Routes.delete_expired(context, config, bypass_last_check: bypass_last_check)
end

.api_key_delete_expired_endpoint(config) ⇒ Object



84
85
86
# File 'lib/better_auth/plugins/api_key.rb', line 84

def api_key_delete_expired_endpoint(config)
  BetterAuth::APIKey::Routes::DeleteAllExpiredAPIKeys.endpoint(config)
end

.api_key_delete_record(ctx, record, config) ⇒ Object



220
221
222
# File 'lib/better_auth/plugins/api_key.rb', line 220

def api_key_delete_record(ctx, record, config)
  BetterAuth::APIKey::Adapter.delete_record(ctx, record, config)
end

.api_key_deserialize_storage_record(record) ⇒ Object



276
277
278
# File 'lib/better_auth/plugins/api_key.rb', line 276

def api_key_deserialize_storage_record(record)
  BetterAuth::APIKey::Adapter.deserialize_record(record)
end

.api_key_encode_json(value) ⇒ Object



304
305
306
# File 'lib/better_auth/plugins/api_key.rb', line 304

def api_key_encode_json(value)
  BetterAuth::APIKey::Utils.encode_json(value)
end

.api_key_error_code(error) ⇒ Object



136
137
138
# File 'lib/better_auth/plugins/api_key.rb', line 136

def api_key_error_code(error)
  BetterAuth::APIKey::Utils.error_code(error)
end

.api_key_error_payload(error) ⇒ Object



140
141
142
# File 'lib/better_auth/plugins/api_key.rb', line 140

def api_key_error_payload(error)
  BetterAuth::APIKey::Utils.error_payload(error)
end

.api_key_expires_at(body, config) ⇒ Object



192
193
194
# File 'lib/better_auth/plugins/api_key.rb', line 192

def api_key_expires_at(body, config)
  BetterAuth::APIKey::Keys.expires_at(body, config)
end

.api_key_find_by_hash(ctx, hashed, config) ⇒ Object



200
201
202
# File 'lib/better_auth/plugins/api_key.rb', line 200

def api_key_find_by_hash(ctx, hashed, config)
  BetterAuth::APIKey::Adapter.find_by_hash(ctx, hashed, config)
end

.api_key_find_by_id(ctx, id, config) ⇒ Object



204
205
206
# File 'lib/better_auth/plugins/api_key.rb', line 204

def api_key_find_by_id(ctx, id, config)
  BetterAuth::APIKey::Adapter.find_by_id(ctx, id, config)
end

.api_key_generate_key(config, prefix) ⇒ Object



180
181
182
# File 'lib/better_auth/plugins/api_key.rb', line 180

def api_key_generate_key(config, prefix)
  BetterAuth::APIKey::Keys.generate(config, prefix)
end

.api_key_get_endpoint(config) ⇒ Object



68
69
70
# File 'lib/better_auth/plugins/api_key.rb', line 68

def api_key_get_endpoint(config)
  BetterAuth::APIKey::Routes::GetAPIKey.endpoint(config)
end

.api_key_get_from_headers(ctx, config) ⇒ Object



296
297
298
# File 'lib/better_auth/plugins/api_key.rb', line 296

def api_key_get_from_headers(ctx, config)
  BetterAuth::APIKey::Keys.from_headers(ctx, config)
end

.api_key_hash(key, config) ⇒ Object



184
185
186
# File 'lib/better_auth/plugins/api_key.rb', line 184

def api_key_hash(key, config)
  BetterAuth::APIKey::Keys.hash(key, config)
end

.api_key_list_endpoint(config) ⇒ Object



80
81
82
# File 'lib/better_auth/plugins/api_key.rb', line 80

def api_key_list_endpoint(config)
  BetterAuth::APIKey::Routes::ListAPIKeys.endpoint(config)
end

.api_key_list_for_reference(ctx, reference_id, config) ⇒ Object



212
213
214
# File 'lib/better_auth/plugins/api_key.rb', line 212

def api_key_list_for_reference(ctx, reference_id, config)
  BetterAuth::APIKey::Adapter.list_for_reference(ctx, reference_id, config)
end

.api_key_list_for_user(ctx, user_id, config) ⇒ Object



208
209
210
# File 'lib/better_auth/plugins/api_key.rb', line 208

def api_key_list_for_user(ctx, user_id, config)
  api_key_list_for_reference(ctx, user_id, config)
end

.api_key_migrate_legacy_metadata(ctx, record, config) ⇒ Object



284
285
286
# File 'lib/better_auth/plugins/api_key.rb', line 284

def (ctx, record, config)
  BetterAuth::APIKey::Adapter.(ctx, record, config)
end

.api_key_next_request_count(record, now) ⇒ Object



168
169
170
# File 'lib/better_auth/plugins/api_key.rb', line 168

def api_key_next_request_count(record, now)
  BetterAuth::APIKey::RateLimit.next_request_count(record, now)
end

.api_key_normalize_body(raw) ⇒ Object



188
189
190
# File 'lib/better_auth/plugins/api_key.rb', line 188

def api_key_normalize_body(raw)
  BetterAuth::APIKey::Keys.normalize_body(raw)
end

.api_key_normalize_time(value) ⇒ Object



312
313
314
# File 'lib/better_auth/plugins/api_key.rb', line 312

def api_key_normalize_time(value)
  BetterAuth::APIKey::Utils.normalize_time(value)
end

.api_key_public(record, reveal_key: nil, include_key_field: false) ⇒ Object



280
281
282
# File 'lib/better_auth/plugins/api_key.rb', line 280

def api_key_public(record, reveal_key: nil, include_key_field: false)
  BetterAuth::APIKey::Utils.public_record(record, reveal_key: reveal_key, include_key_field: include_key_field)
end

.api_key_rate_limit_counts_requests?(record, config) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/better_auth/plugins/api_key.rb', line 164

def api_key_rate_limit_counts_requests?(record, config)
  BetterAuth::APIKey::RateLimit.counts_requests?(record, config)
end

.api_key_rate_limit_try_again_in(record, config, now) ⇒ Object



160
161
162
# File 'lib/better_auth/plugins/api_key.rb', line 160

def api_key_rate_limit_try_again_in(record, config, now)
  BetterAuth::APIKey::RateLimit.try_again_in(record, config, now)
end

.api_key_record_config_id(record) ⇒ Object



112
113
114
# File 'lib/better_auth/plugins/api_key.rb', line 112

def api_key_record_config_id(record)
  BetterAuth::APIKey::Types.record_config_id(record)
end

.api_key_record_reference_id(record) ⇒ Object



104
105
106
# File 'lib/better_auth/plugins/api_key.rb', line 104

def api_key_record_reference_id(record)
  BetterAuth::APIKey::Types.record_reference_id(record)
end

.api_key_record_user_id(record) ⇒ Object



108
109
110
# File 'lib/better_auth/plugins/api_key.rb', line 108

def api_key_record_user_id(record)
  BetterAuth::APIKey::Types.record_user_id(record)
end

.api_key_ref_list_add(storage, user_key, id) ⇒ Object



252
253
254
# File 'lib/better_auth/plugins/api_key.rb', line 252

def api_key_ref_list_add(storage, user_key, id)
  BetterAuth::APIKey::Adapter.ref_list_add(storage, user_key, id)
end

.api_key_ref_list_remove(storage, user_key, id) ⇒ Object



256
257
258
# File 'lib/better_auth/plugins/api_key.rb', line 256

def api_key_ref_list_remove(storage, user_key, id)
  BetterAuth::APIKey::Adapter.ref_list_remove(storage, user_key, id)
end

.api_key_resolve_config(context, config, config_id = nil) ⇒ Object



88
89
90
# File 'lib/better_auth/plugins/api_key.rb', line 88

def api_key_resolve_config(context, config, config_id = nil)
  BetterAuth::APIKey::Routes.resolve_config(context, config, config_id)
end

.api_key_safe_parse_id_list(raw) ⇒ Object



260
261
262
# File 'lib/better_auth/plugins/api_key.rb', line 260

def api_key_safe_parse_id_list(raw)
  BetterAuth::APIKey::Adapter.safe_parse_id_list(raw)
end

.api_key_schedule_cleanup(ctx, config) ⇒ Object



228
229
230
# File 'lib/better_auth/plugins/api_key.rb', line 228

def api_key_schedule_cleanup(ctx, config)
  BetterAuth::APIKey::Routes.schedule_cleanup(ctx, config)
end

.api_key_schedule_record_delete(ctx, record, config) ⇒ Object



224
225
226
# File 'lib/better_auth/plugins/api_key.rb', line 224

def api_key_schedule_record_delete(ctx, record, config)
  BetterAuth::APIKey::Adapter.schedule_record_delete(ctx, record, config)
end

.api_key_schema(config, custom_schema = nil) ⇒ Object



56
57
58
# File 'lib/better_auth/plugins/api_key.rb', line 56

def api_key_schema(config, custom_schema = nil)
  BetterAuth::APIKey::SchemaDefinition.schema(config, custom_schema)
end

.api_key_session_header_config(ctx, config) ⇒ Object



144
145
146
# File 'lib/better_auth/plugins/api_key.rb', line 144

def api_key_session_header_config(ctx, config)
  BetterAuth::APIKey::Session.header_config(ctx, config)
end

.api_key_session_hook(ctx, config) ⇒ Object



148
149
150
# File 'lib/better_auth/plugins/api_key.rb', line 148

def api_key_session_hook(ctx, config)
  BetterAuth::APIKey::Session.hook(ctx, config)
end

.api_key_single_config(options) ⇒ Object



52
53
54
# File 'lib/better_auth/plugins/api_key.rb', line 52

def api_key_single_config(options)
  BetterAuth::APIKey::Configuration.single(options)
end

.api_key_sort_records(records, sort_by, direction) ⇒ Object



128
129
130
# File 'lib/better_auth/plugins/api_key.rb', line 128

def api_key_sort_records(records, sort_by, direction)
  BetterAuth::APIKey::Utils.sort_records(records, sort_by, direction)
end

.api_key_storage(config, context = nil) ⇒ Object



236
237
238
# File 'lib/better_auth/plugins/api_key.rb', line 236

def api_key_storage(config, context = nil)
  BetterAuth::APIKey::Adapter.storage(config, context)
end

.api_key_storage_batch(storage, &block) ⇒ Object



264
265
266
# File 'lib/better_auth/plugins/api_key.rb', line 264

def api_key_storage_batch(storage, &block)
  BetterAuth::APIKey::Adapter.batch(storage, &block)
end

.api_key_storage_delete(ctx, record, config) ⇒ Object



248
249
250
# File 'lib/better_auth/plugins/api_key.rb', line 248

def api_key_storage_delete(ctx, record, config)
  BetterAuth::APIKey::Adapter.delete(ctx, record, config)
end

.api_key_storage_get(ctx, key, config) ⇒ Object



240
241
242
# File 'lib/better_auth/plugins/api_key.rb', line 240

def api_key_storage_get(ctx, key, config)
  BetterAuth::APIKey::Adapter.get(ctx, key, config)
end

.api_key_storage_populate_reference(ctx, reference_id, records, config) ⇒ Object



268
269
270
# File 'lib/better_auth/plugins/api_key.rb', line 268

def api_key_storage_populate_reference(ctx, reference_id, records, config)
  BetterAuth::APIKey::Adapter.populate_reference(ctx, reference_id, records, config)
end

.api_key_storage_record(record) ⇒ Object



272
273
274
# File 'lib/better_auth/plugins/api_key.rb', line 272

def api_key_storage_record(record)
  BetterAuth::APIKey::Adapter.storage_record(record)
end

.api_key_storage_set(ctx, record, config) ⇒ Object



244
245
246
# File 'lib/better_auth/plugins/api_key.rb', line 244

def api_key_storage_set(ctx, record, config)
  BetterAuth::APIKey::Adapter.set(ctx, record, config)
end

.api_key_store(ctx, data, config) ⇒ Object



196
197
198
# File 'lib/better_auth/plugins/api_key.rb', line 196

def api_key_store(ctx, data, config)
  BetterAuth::APIKey::Adapter.store(ctx, data, config)
end

.api_key_update_endpoint(config) ⇒ Object



72
73
74
# File 'lib/better_auth/plugins/api_key.rb', line 72

def api_key_update_endpoint(config)
  BetterAuth::APIKey::Routes::UpdateAPIKey.endpoint(config)
end

.api_key_update_payload(body, config) ⇒ Object



176
177
178
# File 'lib/better_auth/plugins/api_key.rb', line 176

def api_key_update_payload(body, config)
  BetterAuth::APIKey::Validation.update_payload(body, config)
end

.api_key_update_record(ctx, record, update, config, defer: false) ⇒ Object



216
217
218
# File 'lib/better_auth/plugins/api_key.rb', line 216

def api_key_update_record(ctx, record, update, config, defer: false)
  BetterAuth::APIKey::Adapter.update_record(ctx, record, update, config, defer: defer)
end

.api_key_usage_update(record, config) ⇒ Object



156
157
158
# File 'lib/better_auth/plugins/api_key.rb', line 156

def api_key_usage_update(record, config)
  BetterAuth::APIKey::Validation.usage_update(record, config)
end

.api_key_validate!(ctx, key, config, permissions: nil) ⇒ Object



152
153
154
# File 'lib/better_auth/plugins/api_key.rb', line 152

def api_key_validate!(ctx, key, config, permissions: nil)
  BetterAuth::APIKey::Validation.validate_api_key!(ctx, key, config, permissions: permissions)
end

.api_key_validate_create_update!(body, config, create:, client:) ⇒ Object



172
173
174
# File 'lib/better_auth/plugins/api_key.rb', line 172

def api_key_validate_create_update!(body, config, create:, client:)
  BetterAuth::APIKey::Validation.validate_create_update!(body, config, create: create, client: client)
end

.api_key_validate_list_query!(query) ⇒ Object



132
133
134
# File 'lib/better_auth/plugins/api_key.rb', line 132

def api_key_validate_list_query!(query)
  BetterAuth::APIKey::Utils.validate_list_query!(query)
end

.api_key_verify_endpoint(config) ⇒ Object



64
65
66
# File 'lib/better_auth/plugins/api_key.rb', line 64

def api_key_verify_endpoint(config)
  BetterAuth::APIKey::Routes::VerifyAPIKey.endpoint(config)
end

.default_api_key_hasher(key) ⇒ Object



38
39
40
# File 'lib/better_auth/plugins/api_key.rb', line 38

def default_api_key_hasher(key)
  BetterAuth::APIKey::Keys.default_hasher(key)
end