mirror of
https://github.com/neocities/neocities.git
synced 2025-07-03 01:23:22 +02:00
API key support
This commit is contained in:
parent
73ec613283
commit
1274e9fa63
8 changed files with 93 additions and 17 deletions
29
app/api.rb
29
app/api.rb
|
@ -148,24 +148,31 @@ def init_api_credentials
|
|||
auth = request.env['HTTP_AUTHORIZATION']
|
||||
|
||||
begin
|
||||
user, pass = Base64.decode64(auth.match(/Basic (.+)/)[1]).split(':')
|
||||
if bearer_match = auth.match(/^Bearer (.+)/)
|
||||
api_key = bearer_match.captures.first
|
||||
api_error_invalid_auth if api_key.nil? || api_key.empty?
|
||||
else
|
||||
user, pass = Base64.decode64(auth.match(/Basic (.+)/)[1]).split(':')
|
||||
end
|
||||
rescue
|
||||
api_error_invalid_auth
|
||||
end
|
||||
|
||||
if Site.valid_login? user, pass
|
||||
site = Site[username: user]
|
||||
|
||||
if site.nil? || site.is_banned
|
||||
api_error_invalid_auth
|
||||
end
|
||||
|
||||
DB['update sites set api_calls=api_calls+1 where id=?', site.id].first
|
||||
|
||||
session[:id] = site.id
|
||||
if defined?(api_key) && !api_key.blank?
|
||||
site = Site[api_key: api_key]
|
||||
elsif defined?(user) && defined?(pass)
|
||||
site = Site.get_site_from_login user, pass
|
||||
else
|
||||
api_error_invalid_auth
|
||||
end
|
||||
|
||||
if site.nil? || site.is_banned || site.is_deleted
|
||||
api_error_invalid_auth
|
||||
end
|
||||
|
||||
DB['update sites set api_calls=api_calls+1 where id=?', site.id].first
|
||||
|
||||
session[:id] = site.id
|
||||
end
|
||||
|
||||
def api_success(message_or_obj)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue