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

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)


290
291
292
# File 'lib/better_auth/plugins/api_key.rb', line 290

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



118
119
120
# File 'lib/better_auth/plugins/api_key.rb', line 118

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)


286
287
288
# File 'lib/better_auth/plugins/api_key.rb', line 286

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



122
123
124
# File 'lib/better_auth/plugins/api_key.rb', line 122

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



298
299
300
# File 'lib/better_auth/plugins/api_key.rb', line 298

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

.api_key_config(configurations, options = nil) ⇒ Object



46
47
48
# File 'lib/better_auth/plugins/api_key.rb', line 46

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)


94
95
96
# File 'lib/better_auth/plugins/api_key.rb', line 94

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



58
59
60
# File 'lib/better_auth/plugins/api_key.rb', line 58

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

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



98
99
100
# File 'lib/better_auth/plugins/api_key.rb', line 98

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



306
307
308
# File 'lib/better_auth/plugins/api_key.rb', line 306

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

.api_key_default_config_id?(value) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/better_auth/plugins/api_key.rb', line 90

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

.api_key_default_permissions(config, reference_id, ctx) ⇒ Object



114
115
116
# File 'lib/better_auth/plugins/api_key.rb', line 114

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



74
75
76
# File 'lib/better_auth/plugins/api_key.rb', line 74

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

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



230
231
232
# File 'lib/better_auth/plugins/api_key.rb', line 230

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



82
83
84
# File 'lib/better_auth/plugins/api_key.rb', line 82

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

.api_key_delete_record(ctx, record, config) ⇒ Object



218
219
220
# File 'lib/better_auth/plugins/api_key.rb', line 218

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

.api_key_deserialize_storage_record(record) ⇒ Object



274
275
276
# File 'lib/better_auth/plugins/api_key.rb', line 274

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

.api_key_encode_json(value) ⇒ Object



302
303
304
# File 'lib/better_auth/plugins/api_key.rb', line 302

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

.api_key_error_code(error) ⇒ Object



134
135
136
# File 'lib/better_auth/plugins/api_key.rb', line 134

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

.api_key_error_payload(error) ⇒ Object



138
139
140
# File 'lib/better_auth/plugins/api_key.rb', line 138

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

.api_key_expires_at(body, config) ⇒ Object



190
191
192
# File 'lib/better_auth/plugins/api_key.rb', line 190

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

.api_key_find_by_hash(ctx, hashed, config) ⇒ Object



198
199
200
# File 'lib/better_auth/plugins/api_key.rb', line 198

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



202
203
204
# File 'lib/better_auth/plugins/api_key.rb', line 202

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



178
179
180
# File 'lib/better_auth/plugins/api_key.rb', line 178

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

.api_key_get_endpoint(config) ⇒ Object



66
67
68
# File 'lib/better_auth/plugins/api_key.rb', line 66

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

.api_key_get_from_headers(ctx, config) ⇒ Object



294
295
296
# File 'lib/better_auth/plugins/api_key.rb', line 294

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

.api_key_hash(key, config) ⇒ Object



182
183
184
# File 'lib/better_auth/plugins/api_key.rb', line 182

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

.api_key_list_endpoint(config) ⇒ Object



78
79
80
# File 'lib/better_auth/plugins/api_key.rb', line 78

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

.api_key_list_for_reference(ctx, reference_id, config) ⇒ Object



210
211
212
# File 'lib/better_auth/plugins/api_key.rb', line 210

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



206
207
208
# File 'lib/better_auth/plugins/api_key.rb', line 206

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



282
283
284
# File 'lib/better_auth/plugins/api_key.rb', line 282

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

.api_key_next_request_count(record, now) ⇒ Object



166
167
168
# File 'lib/better_auth/plugins/api_key.rb', line 166

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

.api_key_normalize_body(raw) ⇒ Object



186
187
188
# File 'lib/better_auth/plugins/api_key.rb', line 186

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

.api_key_normalize_time(value) ⇒ Object



310
311
312
# File 'lib/better_auth/plugins/api_key.rb', line 310

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



278
279
280
# File 'lib/better_auth/plugins/api_key.rb', line 278

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)


162
163
164
# File 'lib/better_auth/plugins/api_key.rb', line 162

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



158
159
160
# File 'lib/better_auth/plugins/api_key.rb', line 158

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



110
111
112
# File 'lib/better_auth/plugins/api_key.rb', line 110

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

.api_key_record_reference_id(record) ⇒ Object



102
103
104
# File 'lib/better_auth/plugins/api_key.rb', line 102

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

.api_key_record_user_id(record) ⇒ Object



106
107
108
# File 'lib/better_auth/plugins/api_key.rb', line 106

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



250
251
252
# File 'lib/better_auth/plugins/api_key.rb', line 250

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



254
255
256
# File 'lib/better_auth/plugins/api_key.rb', line 254

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



86
87
88
# File 'lib/better_auth/plugins/api_key.rb', line 86

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



258
259
260
# File 'lib/better_auth/plugins/api_key.rb', line 258

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

.api_key_schedule_cleanup(ctx, config) ⇒ Object



226
227
228
# File 'lib/better_auth/plugins/api_key.rb', line 226

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

.api_key_schedule_record_delete(ctx, record, config) ⇒ Object



222
223
224
# File 'lib/better_auth/plugins/api_key.rb', line 222

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



54
55
56
# File 'lib/better_auth/plugins/api_key.rb', line 54

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

.api_key_session_header_config(ctx, config) ⇒ Object



142
143
144
# File 'lib/better_auth/plugins/api_key.rb', line 142

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

.api_key_session_hook(ctx, config) ⇒ Object



146
147
148
# File 'lib/better_auth/plugins/api_key.rb', line 146

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

.api_key_single_config(options) ⇒ Object



50
51
52
# File 'lib/better_auth/plugins/api_key.rb', line 50

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

.api_key_sort_records(records, sort_by, direction) ⇒ Object



126
127
128
# File 'lib/better_auth/plugins/api_key.rb', line 126

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



234
235
236
# File 'lib/better_auth/plugins/api_key.rb', line 234

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

.api_key_storage_batch(storage, &block) ⇒ Object



262
263
264
# File 'lib/better_auth/plugins/api_key.rb', line 262

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

.api_key_storage_delete(ctx, record, config) ⇒ Object



246
247
248
# File 'lib/better_auth/plugins/api_key.rb', line 246

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

.api_key_storage_get(ctx, key, config) ⇒ Object



238
239
240
# File 'lib/better_auth/plugins/api_key.rb', line 238

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



266
267
268
# File 'lib/better_auth/plugins/api_key.rb', line 266

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



270
271
272
# File 'lib/better_auth/plugins/api_key.rb', line 270

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

.api_key_storage_set(ctx, record, config) ⇒ Object



242
243
244
# File 'lib/better_auth/plugins/api_key.rb', line 242

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

.api_key_store(ctx, data, config) ⇒ Object



194
195
196
# File 'lib/better_auth/plugins/api_key.rb', line 194

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

.api_key_update_endpoint(config) ⇒ Object



70
71
72
# File 'lib/better_auth/plugins/api_key.rb', line 70

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

.api_key_update_payload(body, config) ⇒ Object



174
175
176
# File 'lib/better_auth/plugins/api_key.rb', line 174

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



214
215
216
# File 'lib/better_auth/plugins/api_key.rb', line 214

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



154
155
156
# File 'lib/better_auth/plugins/api_key.rb', line 154

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

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



150
151
152
# File 'lib/better_auth/plugins/api_key.rb', line 150

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



170
171
172
# File 'lib/better_auth/plugins/api_key.rb', line 170

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



130
131
132
# File 'lib/better_auth/plugins/api_key.rb', line 130

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

.api_key_verify_endpoint(config) ⇒ Object



62
63
64
# File 'lib/better_auth/plugins/api_key.rb', line 62

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