2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserApiKeysController do
|
2016-08-15 03:58:33 -04:00
|
|
|
let :public_key do
|
2017-08-31 00:06:56 -04:00
|
|
|
<<~TXT
|
|
|
|
-----BEGIN PUBLIC KEY-----
|
|
|
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDh7BS7Ey8hfbNhlNAW/47pqT7w
|
|
|
|
IhBz3UyBYzin8JurEQ2pY9jWWlY8CH147KyIZf1fpcsi7ZNxGHeDhVsbtUKZxnFV
|
|
|
|
p16Op3CHLJnnJKKBMNdXMy0yDfCAHZtqxeBOTcCo1Vt/bHpIgiK5kmaekyXIaD0n
|
|
|
|
w0z/BYpOgZ8QwnI5ZwIDAQAB
|
|
|
|
-----END PUBLIC KEY-----
|
|
|
|
TXT
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let :private_key do
|
2017-08-31 00:06:56 -04:00
|
|
|
<<~TXT
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIICWwIBAAKBgQDh7BS7Ey8hfbNhlNAW/47pqT7wIhBz3UyBYzin8JurEQ2pY9jW
|
|
|
|
WlY8CH147KyIZf1fpcsi7ZNxGHeDhVsbtUKZxnFVp16Op3CHLJnnJKKBMNdXMy0y
|
|
|
|
DfCAHZtqxeBOTcCo1Vt/bHpIgiK5kmaekyXIaD0nw0z/BYpOgZ8QwnI5ZwIDAQAB
|
|
|
|
AoGAeHesbjzCivc+KbBybXEEQbBPsThY0Y+VdgD0ewif2U4UnNhzDYnKJeTZExwQ
|
|
|
|
vAK2YsRDV3KbhljnkagQduvmgJyCKuV/CxZvbJddwyIs3+U2D4XysQp3e1YZ7ROr
|
|
|
|
YlOIoekHCx1CNm6A4iImqGxB0aJ7Owdk3+QSIaMtGQWaPTECQQDz2UjJ+bomguNs
|
|
|
|
zdcv3ZP7W3U5RG+TpInSHiJXpt2JdNGfHItozGJCxfzDhuKHK5Cb23bgldkvB9Xc
|
|
|
|
p/tngTtNAkEA7S4cqUezA82xS7aYPehpRkKEmqzMwR3e9WeL7nZ2cdjZAHgXe49l
|
|
|
|
3mBhidEyRmtPqbXo1Xix8LDuqik0IdnlgwJAQeYTnLnHS8cNjQbnw4C/ECu8Nzi+
|
|
|
|
aokJ0eXg5A0tS4ttZvGA31Z0q5Tz5SdbqqnkT6p0qub0JZiZfCNNdsBe9QJAaGT5
|
|
|
|
fJDwfGYW+YpfLDCV1bUFhMc2QHITZtSyxL0jmSynJwu02k/duKmXhP+tL02gfMRy
|
|
|
|
vTMorxZRllgYeCXeXQJAEGRXR8/26jwqPtKKJzC7i9BuOYEagqj0nLG2YYfffCMc
|
|
|
|
d3JGCf7DMaUlaUE8bJ08PtHRJFSGkNfDJLhLKSjpbw==
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
TXT
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let :args do
|
|
|
|
{
|
2016-10-14 01:05:27 -04:00
|
|
|
scopes: "read",
|
2016-08-15 03:58:33 -04:00
|
|
|
client_id: "x" * 32,
|
|
|
|
auth_redirect: "http://over.the/rainbow",
|
|
|
|
application_name: "foo",
|
|
|
|
public_key: public_key,
|
|
|
|
nonce: SecureRandom.hex,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#new" do
|
2016-08-16 19:58:19 -04:00
|
|
|
it "supports a head request cleanly" do
|
2018-06-03 23:00:08 -04:00
|
|
|
head "/user-api-key/new"
|
|
|
|
expect(response.status).to eq(200)
|
2019-04-01 13:18:53 -04:00
|
|
|
expect(response.headers["Auth-Api-Version"]).to eq("4")
|
2016-08-16 19:58:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#create" do
|
2016-08-15 03:58:33 -04:00
|
|
|
it "does not allow anon" do
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
2018-01-11 22:15:10 -05:00
|
|
|
expect(response.status).to eq(403)
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "refuses to redirect to disallowed place" do
|
2018-06-03 23:00:08 -04:00
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(403)
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
2016-09-12 01:42:06 -04:00
|
|
|
it "will allow tokens for staff without TL" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
|
2016-09-12 01:42:06 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect]
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 1, moderator: true, refresh_auto_groups: true)
|
2016-09-12 01:42:06 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
sign_in(user)
|
2016-09-12 01:42:06 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(302)
|
2016-09-12 01:42:06 -04:00
|
|
|
end
|
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
it "will not create token unless TL is met" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
|
2016-08-15 03:58:33 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect]
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 1, refresh_auto_groups: true)
|
2018-06-03 23:00:08 -04:00
|
|
|
sign_in(user)
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(403)
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "will deny access if requesting more rights than allowed" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
|
2016-08-15 03:58:33 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect]
|
2016-10-14 01:05:27 -04:00
|
|
|
SiteSetting.allow_user_api_key_scopes = "write"
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
|
2018-06-03 23:00:08 -04:00
|
|
|
sign_in(user)
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(403)
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
2016-09-02 03:08:46 -04:00
|
|
|
it "allows for a revoke with no id" do
|
|
|
|
key = Fabricate(:readonly_user_api_key)
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key/revoke.json", headers: { HTTP_USER_API_KEY: key.key }
|
2016-09-02 03:08:46 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
key.reload
|
|
|
|
expect(key.revoked_at).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
2016-09-02 02:57:41 -04:00
|
|
|
it "will not allow readonly api keys to revoke others" do
|
|
|
|
key1 = Fabricate(:readonly_user_api_key)
|
|
|
|
key2 = Fabricate(:readonly_user_api_key)
|
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key/revoke.json",
|
|
|
|
params: {
|
|
|
|
id: key2.id,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
HTTP_USER_API_KEY: key1.key,
|
|
|
|
}
|
2016-09-02 02:57:41 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will allow readonly api keys to revoke self" do
|
|
|
|
key = Fabricate(:readonly_user_api_key)
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key/revoke.json",
|
|
|
|
params: {
|
|
|
|
id: key.id,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
HTTP_USER_API_KEY: key.key,
|
|
|
|
}
|
2016-09-02 02:57:41 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
key.reload
|
|
|
|
expect(key.revoked_at).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
2019-12-17 05:56:16 -05:00
|
|
|
it "will not allow revoking another users key" do
|
|
|
|
key = Fabricate(:readonly_user_api_key)
|
|
|
|
acting_user = Fabricate(:user)
|
|
|
|
sign_in(acting_user)
|
|
|
|
|
|
|
|
post "/user-api-key/revoke.json", params: { id: key.id }
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
key.reload
|
|
|
|
expect(key.revoked_at).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2016-08-25 23:12:38 -04:00
|
|
|
it "will not return p access if not yet configured" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
|
2016-08-25 23:12:38 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect]
|
|
|
|
|
2016-10-14 01:05:27 -04:00
|
|
|
args[:scopes] = "push,read"
|
2016-08-25 23:12:38 -04:00
|
|
|
args[:push_url] = "https://push.it/here"
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
|
2018-06-03 23:00:08 -04:00
|
|
|
sign_in(user)
|
2016-08-25 23:12:38 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(302)
|
2016-08-25 23:12:38 -04:00
|
|
|
|
|
|
|
uri = URI.parse(response.redirect_url)
|
|
|
|
|
|
|
|
query = uri.query
|
|
|
|
payload = query.split("payload=")[1]
|
|
|
|
encrypted = Base64.decode64(CGI.unescape(payload))
|
|
|
|
|
|
|
|
key = OpenSSL::PKey::RSA.new(private_key)
|
|
|
|
|
|
|
|
parsed = JSON.parse(key.private_decrypt(encrypted))
|
|
|
|
|
|
|
|
expect(parsed["nonce"]).to eq(args[:nonce])
|
2016-10-14 01:05:27 -04:00
|
|
|
expect(parsed["push"]).to eq(false)
|
2019-04-01 13:18:53 -04:00
|
|
|
expect(parsed["api"]).to eq(4)
|
2016-08-25 23:12:38 -04:00
|
|
|
|
2016-08-25 23:18:20 -04:00
|
|
|
key = user.user_api_keys.first
|
2020-09-29 05:57:48 -04:00
|
|
|
expect(key.scopes.map(&:name)).to include("push")
|
2016-08-25 23:18:20 -04:00
|
|
|
expect(key.push_url).to eq("https://push.it/here")
|
2016-08-25 23:12:38 -04:00
|
|
|
end
|
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
it "will redirect correctly with valid token" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
|
2016-08-15 03:58:33 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect]
|
|
|
|
SiteSetting.allowed_user_api_push_urls = "https://push.it/here"
|
|
|
|
|
2019-04-01 13:18:53 -04:00
|
|
|
args[:scopes] = "push,notifications,message_bus,session_info,one_time_password"
|
2016-08-15 03:58:33 -04:00
|
|
|
args[:push_url] = "https://push.it/here"
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
|
2018-06-03 23:00:08 -04:00
|
|
|
sign_in(user)
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(302)
|
2016-08-15 03:58:33 -04:00
|
|
|
|
|
|
|
uri = URI.parse(response.redirect_url)
|
|
|
|
|
|
|
|
query = uri.query
|
|
|
|
payload = query.split("payload=")[1]
|
|
|
|
encrypted = Base64.decode64(CGI.unescape(payload))
|
|
|
|
|
|
|
|
key = OpenSSL::PKey::RSA.new(private_key)
|
|
|
|
|
|
|
|
parsed = JSON.parse(key.private_decrypt(encrypted))
|
|
|
|
|
|
|
|
expect(parsed["nonce"]).to eq(args[:nonce])
|
2016-10-14 01:05:27 -04:00
|
|
|
expect(parsed["push"]).to eq(true)
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2020-04-07 09:42:52 -04:00
|
|
|
api_key = UserApiKey.with_key(parsed["key"]).first
|
2016-08-15 03:58:33 -04:00
|
|
|
|
|
|
|
expect(api_key.user_id).to eq(user.id)
|
2020-09-29 05:57:48 -04:00
|
|
|
expect(api_key.scopes.map(&:name).sort).to eq(
|
|
|
|
%w[push message_bus notifications session_info one_time_password].sort,
|
|
|
|
)
|
2016-08-15 03:58:33 -04:00
|
|
|
expect(api_key.push_url).to eq("https://push.it/here")
|
|
|
|
|
|
|
|
uri.query = ""
|
|
|
|
expect(uri.to_s).to eq(args[:auth_redirect] + "?")
|
|
|
|
|
2016-08-16 03:06:33 -04:00
|
|
|
# should overwrite if needed
|
|
|
|
args["access"] = "pr"
|
2018-06-03 23:00:08 -04:00
|
|
|
post "/user-api-key.json", params: args
|
2016-08-16 03:06:33 -04:00
|
|
|
|
2018-06-03 23:00:08 -04:00
|
|
|
expect(response.status).to eq(302)
|
2019-04-01 13:18:53 -04:00
|
|
|
|
|
|
|
one_time_password = query.split("oneTimePassword=")[1]
|
|
|
|
encrypted_otp = Base64.decode64(CGI.unescape(one_time_password))
|
|
|
|
|
|
|
|
parsed_otp = key.private_decrypt(encrypted_otp)
|
|
|
|
redis_key = "otp_#{parsed_otp}"
|
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
expect(Discourse.redis.get(redis_key)).to eq(user.username)
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
2019-01-03 22:46:18 -05:00
|
|
|
|
|
|
|
it "will just show the payload if no redirect" do
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
|
2019-01-03 22:46:18 -05:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
args.delete(:auth_redirect)
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
|
2019-01-03 22:46:18 -05:00
|
|
|
post "/user-api-key", params: args
|
|
|
|
expect(response.status).not_to eq(302)
|
2020-05-04 23:46:57 -04:00
|
|
|
payload = Nokogiri.HTML5(response.body).at("code").content
|
2019-01-03 22:46:18 -05:00
|
|
|
encrypted = Base64.decode64(payload)
|
|
|
|
key = OpenSSL::PKey::RSA.new(private_key)
|
|
|
|
parsed = JSON.parse(key.private_decrypt(encrypted))
|
2020-04-07 09:42:52 -04:00
|
|
|
api_key = UserApiKey.with_key(parsed["key"]).first
|
2019-01-03 22:46:18 -05:00
|
|
|
expect(api_key.user_id).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will just show the JSON payload if no redirect" do
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
|
2019-01-03 22:46:18 -05:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
args.delete(:auth_redirect)
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
|
2019-01-03 22:46:18 -05:00
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).not_to eq(302)
|
2020-05-07 11:04:12 -04:00
|
|
|
payload = response.parsed_body["payload"]
|
2019-01-03 22:46:18 -05:00
|
|
|
encrypted = Base64.decode64(payload)
|
|
|
|
key = OpenSSL::PKey::RSA.new(private_key)
|
|
|
|
parsed = JSON.parse(key.private_decrypt(encrypted))
|
2020-04-07 09:42:52 -04:00
|
|
|
api_key = UserApiKey.with_key(parsed["key"]).first
|
2019-01-03 22:46:18 -05:00
|
|
|
expect(api_key.user_id).to eq(user.id)
|
|
|
|
end
|
2019-02-26 11:03:20 -05:00
|
|
|
|
|
|
|
it "will allow redirect to wildcard urls" do
|
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect] + "/*"
|
|
|
|
args[:auth_redirect] = args[:auth_redirect] + "/bluebirds/fly"
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
sign_in(Fabricate(:user, refresh_auto_groups: true))
|
2019-02-26 11:03:20 -05:00
|
|
|
|
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
end
|
2019-07-23 12:16:03 -04:00
|
|
|
|
|
|
|
it "will keep query_params added in auth_redirect" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
|
2019-07-23 12:16:03 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect] + "/*"
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 0, refresh_auto_groups: true)
|
2019-07-23 12:16:03 -04:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
query_str = "/?param1=val1"
|
|
|
|
args[:auth_redirect] = args[:auth_redirect] + query_str
|
|
|
|
|
|
|
|
post "/user-api-key.json", params: args
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
|
|
|
|
uri = URI.parse(response.redirect_url)
|
|
|
|
expect(uri.to_s).to include(query_str)
|
|
|
|
end
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
2019-04-01 13:18:53 -04:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#create-one-time-password" do
|
2019-04-01 13:18:53 -04:00
|
|
|
let :otp_args do
|
|
|
|
{
|
|
|
|
auth_redirect: "http://somewhere.over.the/rainbow",
|
|
|
|
application_name: "foo",
|
|
|
|
public_key: public_key,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow anon" do
|
|
|
|
post "/user-api-key/otp", params: otp_args
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "refuses to redirect to disallowed place" do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
post "/user-api-key/otp", params: otp_args
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will allow one-time-password for staff without TL" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
|
2019-04-01 13:18:53 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = otp_args[:auth_redirect]
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 1, moderator: true, refresh_auto_groups: true)
|
2019-04-01 13:18:53 -04:00
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/user-api-key/otp", params: otp_args
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not allow one-time-password unless TL is met" do
|
2024-01-18 22:25:24 -05:00
|
|
|
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
|
2019-04-01 13:18:53 -04:00
|
|
|
SiteSetting.allowed_user_api_auth_redirects = otp_args[:auth_redirect]
|
|
|
|
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, trust_level: 1, refresh_auto_groups: true)
|
2019-04-01 13:18:53 -04:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/user-api-key/otp", params: otp_args
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not allow one-time-password if one_time_password scope is disallowed" do
|
|
|
|
SiteSetting.allow_user_api_key_scopes = "read|write"
|
|
|
|
SiteSetting.allowed_user_api_auth_redirects = otp_args[:auth_redirect]
|
|
|
|
user = Fabricate(:user)
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/user-api-key/otp", params: otp_args
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will return one-time-password when args are valid" do
|
|
|
|
SiteSetting.allowed_user_api_auth_redirects = otp_args[:auth_redirect]
|
2024-01-18 22:25:24 -05:00
|
|
|
user = Fabricate(:user, refresh_auto_groups: true)
|
2019-04-01 13:18:53 -04:00
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/user-api-key/otp", params: otp_args
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
|
|
|
|
uri = URI.parse(response.redirect_url)
|
|
|
|
|
|
|
|
query = uri.query
|
|
|
|
payload = query.split("oneTimePassword=")[1]
|
|
|
|
encrypted = Base64.decode64(CGI.unescape(payload))
|
|
|
|
key = OpenSSL::PKey::RSA.new(private_key)
|
|
|
|
|
|
|
|
parsed = key.private_decrypt(encrypted)
|
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
expect(Discourse.redis.get("otp_#{parsed}")).to eq(user.username)
|
2019-04-01 13:18:53 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|