2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe 'OAuth tokens', feature_category: :system_access do
|
2020-06-23 00:09:42 +05:30
|
|
|
include HttpBasicAuthHelpers
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
context 'Resource Owner Password Credentials' do
|
2020-06-23 00:09:42 +05:30
|
|
|
def request_oauth_token(user, headers = {})
|
|
|
|
post '/oauth/token',
|
|
|
|
params: { username: user.username, password: user.password, grant_type: 'password' },
|
|
|
|
headers: headers
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
let_it_be(:client) { create(:oauth_application) }
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
context 'when user has 2FA enabled' do
|
|
|
|
it 'does not create an access token' do
|
|
|
|
user = create(:user, :two_factor)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2016-09-13 17:45:13 +05:30
|
|
|
expect(json_response['error']).to eq('invalid_grant')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-29 17:44:30 +05:30
|
|
|
context 'when 2FA enforced' do
|
|
|
|
let_it_be(:user) { create(:user, otp_grace_period_started_at: 1.day.ago) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(require_two_factor_authentication: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when grace period expired' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(two_factor_grace_period: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create an access token' do
|
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['error']).to eq('invalid_grant')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when grace period is not expired' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(two_factor_grace_period: 72)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an access token' do
|
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['access_token']).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
context 'when user does not have 2FA enabled' do
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when no client credentials provided' do
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'creates an access token' do
|
2020-06-23 00:09:42 +05:30
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
request_oauth_token(user)
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['access_token']).to be_present
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when client credentials provided' do
|
|
|
|
context 'with valid credentials' do
|
|
|
|
it 'creates an access token' do
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['access_token']).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid credentials' do
|
|
|
|
it 'does not create an access token' do
|
|
|
|
user = create(:user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
request_oauth_token(user, basic_auth_header(client.uid, 'invalid secret'))
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(json_response['error']).to eq('invalid_client')
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-02-01 01:16:34 +05:30
|
|
|
shared_examples 'does not create an access token' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it { expect(response).to have_gitlab_http_status(:bad_request) }
|
2020-02-01 01:16:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is blocked' do
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
user.block
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2020-02-01 01:16:34 +05:30
|
|
|
|
|
|
|
include_examples 'does not create an access token'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-02-01 01:16:34 +05:30
|
|
|
context 'when user is ldap_blocked' do
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
user.ldap_block
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
2020-02-01 01:16:34 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-02-01 01:16:34 +05:30
|
|
|
include_examples 'does not create an access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user account is not confirmed' do
|
|
|
|
before do
|
2023-05-27 22:25:52 +05:30
|
|
|
stub_application_setting_enum('email_confirmation_setting', 'soft')
|
|
|
|
|
2020-02-01 01:16:34 +05:30
|
|
|
user.update!(confirmed_at: nil)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2020-02-01 01:16:34 +05:30
|
|
|
|
|
|
|
include_examples 'does not create an access token'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|