2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
class UserApiKey < ActiveRecord::Base
|
2024-05-06 14:18:53 -04:00
|
|
|
self.ignored_columns = [
|
2024-05-06 23:06:31 -04:00
|
|
|
"scopes", # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy
|
2024-11-08 12:05:03 -05:00
|
|
|
"client_id", # TODO: Add post-migration to remove column after 3.4.0 stable release (not before early 2025)
|
|
|
|
"application_name", # TODO: Add post-migration to remove column after 3.4.0 stable release (not before early 2025)
|
2024-05-06 14:18:53 -04:00
|
|
|
]
|
|
|
|
|
2020-10-06 12:20:15 -04:00
|
|
|
REVOKE_MATCHER = RouteMatcher.new(actions: "user_api_keys#revoke", methods: :post, params: [:id])
|
2016-10-14 01:05:27 -04:00
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
belongs_to :user
|
2024-11-08 12:05:03 -05:00
|
|
|
belongs_to :client, class_name: "UserApiKeyClient", foreign_key: "user_api_key_client_id"
|
2020-09-29 05:57:48 -04:00
|
|
|
has_many :scopes, class_name: "UserApiKeyScope", dependent: :destroy
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2020-04-07 09:42:52 -04:00
|
|
|
scope :active, -> { where(revoked_at: nil) }
|
|
|
|
scope :with_key, ->(key) { where(key_hash: ApiKey.hash_key(key)) }
|
|
|
|
|
|
|
|
after_initialize :generate_key
|
|
|
|
|
|
|
|
def generate_key
|
|
|
|
if !self.key_hash
|
|
|
|
@key ||= SecureRandom.hex
|
|
|
|
self.key_hash = ApiKey.hash_key(@key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
unless key_available?
|
|
|
|
raise ApiKey::KeyAccessError.new "API key is only accessible immediately after creation"
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2020-04-07 09:42:52 -04:00
|
|
|
@key
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_available?
|
|
|
|
@key.present?
|
|
|
|
end
|
|
|
|
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-17 15:27:30 -05:00
|
|
|
def ensure_allowed!(env)
|
|
|
|
raise Discourse::InvalidAccess.new if !allow?(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_last_used(client_id)
|
|
|
|
update_args = { last_used_at: Time.zone.now }
|
2024-11-08 12:05:03 -05:00
|
|
|
if client_id.present? && client_id != self.client.client_id
|
|
|
|
new_client =
|
|
|
|
UserApiKeyClient.create!(
|
|
|
|
client_id: client_id,
|
|
|
|
application_name: self.client.application_name,
|
|
|
|
)
|
|
|
|
update_args[:user_api_key_client_id] = new_client.id
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-17 15:27:30 -05:00
|
|
|
end
|
|
|
|
self.update_columns(**update_args)
|
|
|
|
end
|
|
|
|
|
2020-09-29 05:57:48 -04:00
|
|
|
# Scopes allowed to be requested by external services
|
2016-10-14 01:05:27 -04:00
|
|
|
def self.allowed_scopes
|
|
|
|
Set.new(SiteSetting.allow_user_api_key_scopes.split("|"))
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
2016-10-14 01:05:27 -04:00
|
|
|
|
|
|
|
def self.available_scopes
|
2020-10-06 12:20:15 -04:00
|
|
|
@available_scopes ||= Set.new(UserApiKeyScopes.all_scopes.keys.map(&:to_s))
|
2016-10-14 01:05:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_push?
|
2020-09-29 05:57:48 -04:00
|
|
|
scopes.any? { |s| s.name == "push" || s.name == "notifications" } && push_url.present? &&
|
|
|
|
SiteSetting.allowed_user_api_push_urls.include?(push_url)
|
2016-10-14 01:05:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def allow?(env)
|
2020-10-06 12:20:15 -04:00
|
|
|
scopes.any? { |s| s.permits?(env) } || is_revoke_self_request?(env)
|
2016-10-14 01:05:27 -04:00
|
|
|
end
|
|
|
|
|
2020-09-29 05:57:48 -04:00
|
|
|
private
|
|
|
|
|
2020-10-06 12:20:15 -04:00
|
|
|
def revoke_self_matcher
|
|
|
|
REVOKE_MATCHER.with_allowed_param_values({ "id" => [nil, id.to_s] })
|
|
|
|
end
|
|
|
|
|
2020-09-29 05:57:48 -04:00
|
|
|
def is_revoke_self_request?(env)
|
2020-10-06 12:20:15 -04:00
|
|
|
revoke_self_matcher.match?(env: env)
|
2020-09-29 05:57:48 -04:00
|
|
|
end
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_api_keys
|
|
|
|
#
|
2024-11-08 12:05:03 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# push_url :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# revoked_at :datetime
|
|
|
|
# last_used_at :datetime not null
|
|
|
|
# key_hash :string not null
|
|
|
|
# user_api_key_client_id :bigint
|
2016-08-15 03:58:33 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2024-11-08 12:05:03 -05:00
|
|
|
# index_user_api_keys_on_client_id (client_id) UNIQUE
|
|
|
|
# index_user_api_keys_on_key_hash (key_hash) UNIQUE
|
|
|
|
# index_user_api_keys_on_user_api_key_client_id (user_api_key_client_id)
|
|
|
|
# index_user_api_keys_on_user_id (user_id)
|
2016-08-15 03:58:33 -04:00
|
|
|
#
|