forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_api_keys_controller.rb
More file actions
288 lines (234 loc) · 8.28 KB
/
user_api_keys_controller.rb
File metadata and controls
288 lines (234 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# frozen_string_literal: true
class UserApiKeysController < ApplicationController
layout "no_ember"
requires_login only: %i[create create_otp revoke undo_revoke]
skip_before_action :redirect_to_login_if_required,
:redirect_to_profile_if_required,
only: %i[new otp]
skip_before_action :check_xhr, :preload_json
AUTH_API_VERSION = 4
ALLOWED_PADDING_MODES = %w[pkcs1 oaep].freeze
def new
if request.head?
head :ok, auth_api_version: AUTH_API_VERSION
return
end
require_params
find_client
require_client_params
validate_params
validate_auth_redirect
unless current_user
cookies[:destination_url] = request.fullpath
if SiteSetting.enable_discourse_connect?
redirect_to path("/session/sso")
else
redirect_to path("/login")
end
return
end
unless meets_tl?
@no_trust_level = true
return
end
@application_name = params[:application_name] || @client&.application_name
@public_key = params[:public_key] || @client&.public_key
@nonce = params[:nonce]
@client_id = params[:client_id]
@auth_redirect = params[:auth_redirect]
@redirect_uri =
if @auth_redirect.present?
begin
if @auth_redirect == "discourse://auth_redirect"
nil
else
uri = URI.parse(@auth_redirect)
if uri.port.nil? || [80, 443].include?(uri.port)
uri.host
else
uri.host + ":" + uri.port.to_s
end
end
rescue StandardError
nil
end
else
nil
end
@push_url = params[:push_url]
@localized_scopes = params[:scopes].split(",").map { |s| I18n.t("user_api_key.scopes.#{s}") }
@scopes = params[:scopes]
@padding = params[:padding]
rescue Discourse::InvalidAccess
@generic_error = true
end
def create
require_params
find_client
require_client_params
validate_params
validate_auth_redirect
raise Discourse::InvalidAccess unless meets_tl?
scopes = params[:scopes].split(",")
@client = UserApiKeyClient.new(client_id: params[:client_id]) if @client.blank?
@client.application_name = params[:application_name] if params[:application_name].present?
@client.save! if @client.new_record? || @client.changed?
# destroy any old keys the user had with the client
@client.keys.where(user_id: current_user.id).destroy_all
key =
@client.keys.create!(
user_id: current_user.id,
push_url: params[:push_url],
scopes: scopes.map { |name| UserApiKeyScope.new(name: name) },
)
# we keep the payload short so it encrypts easily with public key
# it is often restricted to 128 chars
@payload = {
key: key.key,
nonce: params[:nonce],
push: key.has_push?,
api: AUTH_API_VERSION,
}.to_json
validate_payload_size_for_oaep!(@payload, parsed_public_key)
@payload = Base64.encode64(rsa_encrypt(parsed_public_key, @payload))
if scopes.include?("one_time_password")
# encrypt one_time_password separately to bypass 128 chars encryption limit
otp_payload = one_time_password(parsed_public_key, current_user.username)
end
if params[:auth_redirect]
uri = URI.parse(params[:auth_redirect])
query_attributes = [uri.query, "payload=#{CGI.escape(@payload)}"]
if scopes.include?("one_time_password")
query_attributes << "oneTimePassword=#{CGI.escape(otp_payload)}"
end
uri.query = query_attributes.compact.join("&")
redirect_to(uri.to_s, allow_other_host: true)
else
respond_to do |format|
format.html { render :show }
format.json do
instructions =
I18n.t("user_api_key.instructions", application_name: @client.application_name)
render json: { payload: @payload, instructions: instructions }
end
end
end
end
def otp
require_params_otp
validate_params_otp
unless current_user
cookies[:destination_url] = request.fullpath
if SiteSetting.enable_discourse_connect?
redirect_to path("/session/sso")
else
redirect_to path("/login")
end
return
end
@application_name = params[:application_name]
@public_key = params[:public_key]
@auth_redirect = params[:auth_redirect]
@padding = params[:padding]
end
def create_otp
require_params_otp
validate_params_otp
validate_auth_redirect
raise Discourse::InvalidAccess unless meets_tl?
otp_payload = one_time_password(parsed_public_key, current_user.username)
redirect_path = "#{params[:auth_redirect]}?oneTimePassword=#{CGI.escape(otp_payload)}"
redirect_to(redirect_path, allow_other_host: true)
end
def revoke
current_key = request.env["HTTP_USER_API_KEY"]
revoke_key = find_key if params[:id]
revoke_key ||= UserApiKey.with_key(current_key).first if current_key.present?
raise Discourse::NotFound unless revoke_key
revoke_key.update_columns(revoked_at: Time.zone.now)
render json: success_json
end
def undo_revoke
find_key.update_columns(revoked_at: nil)
render json: success_json
end
def find_key
key = UserApiKey.find(params[:id])
raise Discourse::InvalidAccess unless current_user.admin || key.user_id == current_user.id
key
end
def find_client
@client = UserApiKeyClient.find_by(client_id: params[:client_id])
end
def require_params
%i[nonce scopes client_id].each { |p| params.require(p) }
end
def require_client_params
params.require(:public_key) if @client&.public_key.blank?
params.require(:application_name) if @client&.application_name.blank?
end
def validate_params
requested_scopes = Set.new(params[:scopes].split(","))
raise Discourse::InvalidAccess unless UserApiKey.allowed_scopes.superset?(requested_scopes)
if @client&.scopes.present? && !@client.allowed_scopes.superset?(requested_scopes)
raise Discourse::InvalidAccess
end
parsed_public_key if public_key_str.present?
validate_padding
end
def require_params_otp
%i[public_key auth_redirect application_name].each { |p| params.require(p) }
end
def validate_params_otp
parsed_public_key
validate_padding
end
def validate_padding
return if params[:padding].blank?
return if ALLOWED_PADDING_MODES.include?(params[:padding])
raise Discourse::InvalidParameters.new(:padding)
end
def validate_auth_redirect
return unless params.key?(:auth_redirect)
if UserApiKeyClient.invalid_auth_redirect?(params[:auth_redirect], client: @client)
raise Discourse::InvalidAccess
end
end
def public_key_str
@client&.public_key.presence || params[:public_key]
end
def parsed_public_key
@parsed_public_key ||= OpenSSL::PKey::RSA.new(public_key_str)
end
def meets_tl?
current_user.staff? || current_user.in_any_groups?(SiteSetting.user_api_key_allowed_groups_map)
end
def one_time_password(public_key, username)
unless UserApiKey.allowed_scopes.superset?(Set.new(["one_time_password"]))
raise Discourse::InvalidAccess
end
otp = SecureRandom.hex
Discourse.redis.setex "otp_#{otp}", 10.minutes, username
Base64.encode64(rsa_encrypt(public_key, otp))
end
def rsa_encrypt(public_key, data)
# OAEP padding is recommended for new applications and required for FIPS 140-3 compliance.
# PKCS1 padding is kept as default for backwards compatibility with existing clients.
padding_mode = params[:padding] == "oaep" ? "oaep" : "pkcs1"
public_key.encrypt(data, { "rsa_padding_mode" => padding_mode })
end
def validate_payload_size_for_oaep!(payload, public_key)
return unless params[:padding] == "oaep"
# RSA-OAEP max payload = key_size_bytes - 2*hash_size_bytes - 2
# OpenSSL uses SHA-1 (20 bytes) by default for OAEP
key_size_bytes = public_key.n.num_bytes
max_payload_size = key_size_bytes - 2 * 20 - 2
if payload.bytesize > max_payload_size
raise Discourse::InvalidParameters.new(
"Payload too large for OAEP encryption with this key size. " \
"Maximum: #{max_payload_size} bytes, got: #{payload.bytesize} bytes. " \
"Try using a shorter nonce or a larger RSA key (minimum 2048-bit recommended).",
)
end
end
end