2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe 'OpenID Connect requests' do
|
2018-06-13 16:32:21 -04:00
|
|
|
let(:user) do
|
|
|
|
create(
|
|
|
|
:user,
|
|
|
|
name: 'Alice',
|
|
|
|
username: 'alice',
|
|
|
|
email: 'private@example.com',
|
|
|
|
website_url: 'https://example.com',
|
|
|
|
avatar: fixture_file_upload('spec/fixtures/dk.png')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
let(:access_grant) { create :oauth_access_grant, application: application, resource_owner_id: user.id }
|
|
|
|
let(:access_token) { create :oauth_access_token, application: application, resource_owner_id: user.id }
|
|
|
|
|
2018-06-13 16:32:21 -04:00
|
|
|
let(:hashed_subject) do
|
|
|
|
Digest::SHA256.hexdigest("#{user.id}-#{Rails.application.secrets.secret_key_base}")
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:id_token_claims) do
|
|
|
|
{
|
2022-08-23 05:09:29 -04:00
|
|
|
'sub' => user.id.to_s,
|
2018-06-13 16:32:21 -04:00
|
|
|
'sub_legacy' => hashed_subject
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user_info_claims) do
|
|
|
|
{
|
2022-08-23 05:09:29 -04:00
|
|
|
'name' => 'Alice',
|
|
|
|
'nickname' => 'alice',
|
|
|
|
'email' => 'public@example.com',
|
2020-05-27 17:08:05 -04:00
|
|
|
'email_verified' => true,
|
2022-08-23 05:09:29 -04:00
|
|
|
'website' => 'https://example.com',
|
|
|
|
'profile' => 'http://localhost/alice',
|
|
|
|
'picture' => "http://localhost/uploads/-/system/user/avatar/#{user.id}/dk.png",
|
|
|
|
'groups' => kind_of(Array),
|
|
|
|
'https://gitlab.org/claims/groups/owner' => kind_of(Array),
|
2021-12-12 04:12:10 -05:00
|
|
|
'https://gitlab.org/claims/groups/maintainer' => kind_of(Array),
|
2022-08-23 05:09:29 -04:00
|
|
|
'https://gitlab.org/claims/groups/developer' => kind_of(Array)
|
2018-06-13 16:32:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-06-09 11:10:05 -04:00
|
|
|
let(:cors_request_headers) { { 'Origin' => 'http://notgitlab.com' } }
|
|
|
|
|
2018-06-13 16:32:21 -04:00
|
|
|
def request_access_token!
|
2016-12-09 12:36:50 -05:00
|
|
|
login_as user
|
|
|
|
|
|
|
|
post '/oauth/token',
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
grant_type: 'authorization_code',
|
|
|
|
code: access_grant.token,
|
|
|
|
redirect_uri: application.redirect_uri,
|
|
|
|
client_id: application.uid,
|
|
|
|
client_secret: application.secret
|
|
|
|
}
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
|
2018-06-13 16:32:21 -04:00
|
|
|
def request_user_info!
|
2018-12-17 17:52:17 -05:00
|
|
|
get '/oauth/userinfo', params: {}, headers: { 'Authorization' => "Bearer #{access_token.token}" }
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
|
2020-05-27 17:08:05 -04:00
|
|
|
before do
|
|
|
|
email = create(:email, :confirmed, email: 'public@example.com', user: user)
|
|
|
|
user.update!(public_email: email.email)
|
|
|
|
end
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
context 'Application without OpenID scope' do
|
|
|
|
let(:application) { create :oauth_application, scopes: 'api' }
|
|
|
|
|
|
|
|
it 'token response does not include an ID token' do
|
2018-06-13 16:32:21 -04:00
|
|
|
request_access_token!
|
2016-12-09 12:36:50 -05:00
|
|
|
|
|
|
|
expect(json_response).to include 'access_token'
|
|
|
|
expect(json_response).not_to include 'id_token'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'userinfo response is unauthorized' do
|
2018-06-13 16:32:21 -04:00
|
|
|
request_user_info!
|
2016-12-09 12:36:50 -05:00
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2016-12-09 12:36:50 -05:00
|
|
|
expect(response.body).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-09 11:10:05 -04:00
|
|
|
shared_examples 'cross-origin GET request' do
|
|
|
|
it 'allows cross-origin request' do
|
|
|
|
expect(response.headers['Access-Control-Allow-Origin']).to eq '*'
|
|
|
|
expect(response.headers['Access-Control-Allow-Methods']).to eq 'GET, HEAD'
|
|
|
|
expect(response.headers['Access-Control-Allow-Headers']).to be_nil
|
|
|
|
expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'cross-origin GET and POST request' do
|
|
|
|
it 'allows cross-origin request' do
|
|
|
|
expect(response.headers['Access-Control-Allow-Origin']).to eq '*'
|
2022-06-09 14:08:13 -04:00
|
|
|
expect(response.headers['Access-Control-Allow-Methods']).to eq 'GET, HEAD, POST, OPTIONS'
|
2021-06-09 11:10:05 -04:00
|
|
|
expect(response.headers['Access-Control-Allow-Headers']).to be_nil
|
|
|
|
expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
context 'Application with OpenID scope' do
|
|
|
|
let(:application) { create :oauth_application, scopes: 'openid' }
|
|
|
|
|
|
|
|
it 'token response includes an ID token' do
|
2018-06-13 16:32:21 -04:00
|
|
|
request_access_token!
|
2016-12-09 12:36:50 -05:00
|
|
|
|
|
|
|
expect(json_response).to include 'id_token'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'UserInfo payload' do
|
2018-02-22 06:57:56 -05:00
|
|
|
let!(:group1) { create :group }
|
|
|
|
let!(:group2) { create :group }
|
|
|
|
let!(:group3) { create :group, parent: group2 }
|
|
|
|
let!(:group4) { create :group, parent: group3 }
|
2017-05-30 02:06:00 -04:00
|
|
|
|
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
group1.add_member(user, GroupMember::OWNER)
|
|
|
|
group3.add_member(user, Gitlab::Access::DEVELOPER)
|
|
|
|
group4.add_member(user, Gitlab::Access::MAINTAINER)
|
2018-06-13 16:32:21 -04:00
|
|
|
|
|
|
|
request_user_info!
|
2017-05-30 02:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes all user information and group memberships' do
|
2018-06-13 16:32:21 -04:00
|
|
|
expect(json_response).to match(id_token_claims.merge(user_info_claims))
|
2018-02-13 12:20:01 -05:00
|
|
|
|
2018-02-22 06:57:56 -05:00
|
|
|
expected_groups = [group1.full_path, group3.full_path]
|
2019-07-24 05:20:54 -04:00
|
|
|
expected_groups << group4.full_path
|
2018-02-13 12:20:01 -05:00
|
|
|
expect(json_response['groups']).to match_array(expected_groups)
|
2021-12-12 04:12:10 -05:00
|
|
|
|
|
|
|
expect(json_response['https://gitlab.org/claims/groups/owner']).to match_array([group1.full_path])
|
|
|
|
expect(json_response['https://gitlab.org/claims/groups/maintainer']).to match_array([group4.full_path])
|
|
|
|
expect(json_response['https://gitlab.org/claims/groups/developer']).to match_array([group3.full_path])
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
2018-06-13 16:32:21 -04:00
|
|
|
|
|
|
|
it 'does not include any unknown claims' do
|
|
|
|
expect(json_response.keys).to eq %w[sub sub_legacy] + user_info_claims.keys
|
|
|
|
end
|
2019-02-06 11:48:36 -05:00
|
|
|
|
|
|
|
it 'includes email and email_verified claims' do
|
|
|
|
expect(json_response.keys).to include('email', 'email_verified')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has public email in email claim' do
|
|
|
|
expect(json_response['email']).to eq(user.public_email)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has false in email_verified claim' do
|
2020-05-27 17:08:05 -04:00
|
|
|
expect(json_response['email_verified']).to eq(true)
|
2019-02-06 11:48:36 -05:00
|
|
|
end
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'ID token payload' do
|
2021-09-02 05:11:35 -04:00
|
|
|
let!(:group1) { create :group }
|
|
|
|
let!(:group2) { create :group }
|
|
|
|
let!(:group3) { create :group, parent: group2 }
|
|
|
|
let!(:group4) { create :group, parent: group3 }
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
group1.add_member(user, Gitlab::Access::OWNER)
|
|
|
|
group3.add_member(user, Gitlab::Access::DEVELOPER)
|
2021-09-02 05:11:35 -04:00
|
|
|
|
2018-06-13 16:32:21 -04:00
|
|
|
request_access_token!
|
2016-12-09 12:36:50 -05:00
|
|
|
@payload = JSON::JWT.decode(json_response['id_token'], :skip_verification)
|
|
|
|
end
|
|
|
|
|
2018-06-13 16:32:21 -04:00
|
|
|
it 'includes the subject claims' do
|
|
|
|
expect(@payload).to match(a_hash_including(id_token_claims))
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
|
2018-09-21 08:05:37 -04:00
|
|
|
it 'includes the GitLab root URL' do
|
2018-06-13 16:32:21 -04:00
|
|
|
expect(@payload['iss']).to eq Gitlab.config.gitlab.url
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
it 'includes the time of the last authentication', :clean_gitlab_redis_shared_state do
|
2016-12-09 12:36:50 -05:00
|
|
|
expect(@payload['auth_time']).to eq user.current_sign_in_at.to_i
|
|
|
|
end
|
|
|
|
|
2020-07-09 08:08:56 -04:00
|
|
|
it 'has public email in email claim' do
|
|
|
|
expect(@payload['email']).to eq(user.public_email)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has true in email_verified claim' do
|
|
|
|
expect(@payload['email_verified']).to eq(true)
|
|
|
|
end
|
|
|
|
|
2016-12-09 12:36:50 -05:00
|
|
|
it 'does not include any unknown properties' do
|
2021-09-02 05:11:35 -04:00
|
|
|
expect(@payload.keys).to eq %w[iss sub aud exp iat auth_time sub_legacy email email_verified groups_direct]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does include groups' do
|
|
|
|
expected_groups = [group1.full_path, group3.full_path]
|
|
|
|
expect(@payload['groups_direct']).to match_array(expected_groups)
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is blocked' do
|
2019-10-14 11:06:07 -04:00
|
|
|
it 'redirects to login page' do
|
2016-12-09 12:36:50 -05:00
|
|
|
access_grant
|
2018-06-13 16:32:21 -04:00
|
|
|
user.block!
|
2016-12-09 12:36:50 -05:00
|
|
|
|
2019-10-14 11:06:07 -04:00
|
|
|
request_access_token!
|
|
|
|
|
|
|
|
expect(response).to redirect_to('/users/sign_in')
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is ldap_blocked' do
|
2019-10-14 11:06:07 -04:00
|
|
|
it 'redirects to login page' do
|
2016-12-09 12:36:50 -05:00
|
|
|
access_grant
|
2018-06-13 16:32:21 -04:00
|
|
|
user.ldap_block!
|
2016-12-09 12:36:50 -05:00
|
|
|
|
2019-10-14 11:06:07 -04:00
|
|
|
request_access_token!
|
|
|
|
|
|
|
|
expect(response).to redirect_to('/users/sign_in')
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
|
|
|
end
|
2021-06-09 11:10:05 -04:00
|
|
|
|
|
|
|
context 'OpenID Discovery keys' do
|
|
|
|
context 'with a cross-origin request' do
|
|
|
|
before do
|
|
|
|
get '/oauth/discovery/keys', headers: cors_request_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns data' do
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a cross-origin preflight OPTIONS request' do
|
|
|
|
before do
|
|
|
|
options '/oauth/discovery/keys', headers: cors_request_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET request'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'OpenID WebFinger endpoint' do
|
|
|
|
context 'with a cross-origin request' do
|
|
|
|
before do
|
|
|
|
get '/.well-known/webfinger', headers: cors_request_headers, params: { resource: 'user@example.com' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns data' do
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['subject']).to eq('user@example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET request'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a cross-origin preflight OPTIONS request' do
|
|
|
|
before do
|
|
|
|
options '/.well-known/webfinger', headers: cors_request_headers, params: { resource: 'user@example.com' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET request'
|
|
|
|
end
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|
2018-04-26 06:05:18 -04:00
|
|
|
|
|
|
|
context 'OpenID configuration information' do
|
|
|
|
it 'correctly returns the configuration' do
|
|
|
|
get '/.well-known/openid-configuration'
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-05-07 04:33:35 -04:00
|
|
|
expect(json_response['issuer']).to eq('http://localhost')
|
|
|
|
expect(json_response['jwks_uri']).to eq('http://www.example.com/oauth/discovery/keys')
|
2022-02-04 07:17:40 -05:00
|
|
|
expect(json_response['scopes_supported']).to match_array %w[api read_user read_api read_repository write_repository sudo openid profile email]
|
2019-02-06 11:48:36 -05:00
|
|
|
end
|
2021-06-09 11:10:05 -04:00
|
|
|
|
|
|
|
context 'with a cross-origin request' do
|
|
|
|
before do
|
|
|
|
get '/.well-known/openid-configuration', headers: cors_request_headers
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['issuer']).to eq('http://localhost')
|
|
|
|
expect(json_response['jwks_uri']).to eq('http://www.example.com/oauth/discovery/keys')
|
2022-02-04 07:17:40 -05:00
|
|
|
expect(json_response['scopes_supported']).to match_array %w[api read_user read_api read_repository write_repository sudo openid profile email]
|
2021-06-09 11:10:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a cross-origin preflight OPTIONS request' do
|
|
|
|
before do
|
|
|
|
options '/.well-known/openid-configuration', headers: cors_request_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET request'
|
|
|
|
end
|
2019-02-06 11:48:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Application with OpenID and email scopes' do
|
|
|
|
let(:application) { create :oauth_application, scopes: 'openid email' }
|
|
|
|
|
|
|
|
it 'token response includes an ID token' do
|
|
|
|
request_access_token!
|
|
|
|
|
|
|
|
expect(json_response).to include 'id_token'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'UserInfo payload' do
|
|
|
|
before do
|
|
|
|
request_user_info!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the email and email_verified claims' do
|
|
|
|
expect(json_response.keys).to include('email', 'email_verified')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has private email in email claim' do
|
|
|
|
expect(json_response['email']).to eq(user.email)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has true in email_verified claim' do
|
|
|
|
expect(json_response['email_verified']).to eq(true)
|
|
|
|
end
|
2021-06-09 11:10:05 -04:00
|
|
|
|
|
|
|
context 'with a cross-origin request' do
|
|
|
|
before do
|
|
|
|
get '/oauth/userinfo', headers: cors_request_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET and POST request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a cross-origin POST request' do
|
|
|
|
before do
|
|
|
|
post '/oauth/userinfo', headers: cors_request_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET and POST request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a cross-origin preflight OPTIONS request' do
|
|
|
|
before do
|
|
|
|
options '/oauth/userinfo', headers: cors_request_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'cross-origin GET and POST request'
|
|
|
|
end
|
2018-04-26 06:05:18 -04:00
|
|
|
end
|
2020-07-09 08:08:56 -04:00
|
|
|
|
|
|
|
context 'ID token payload' do
|
2021-09-02 05:11:35 -04:00
|
|
|
let!(:group1) { create :group }
|
|
|
|
let!(:group2) { create :group }
|
|
|
|
let!(:group3) { create :group, parent: group2 }
|
|
|
|
let!(:group4) { create :group, parent: group3 }
|
|
|
|
|
2020-07-09 08:08:56 -04:00
|
|
|
before do
|
2022-07-05 14:08:43 -04:00
|
|
|
group1.add_member(user, Gitlab::Access::OWNER)
|
|
|
|
group3.add_member(user, Gitlab::Access::DEVELOPER)
|
2021-09-02 05:11:35 -04:00
|
|
|
|
2020-07-09 08:08:56 -04:00
|
|
|
request_access_token!
|
|
|
|
@payload = JSON::JWT.decode(json_response['id_token'], :skip_verification)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has private email in email claim' do
|
|
|
|
expect(@payload['email']).to eq(user.email)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has true in email_verified claim' do
|
|
|
|
expect(@payload['email_verified']).to eq(true)
|
|
|
|
end
|
2021-09-02 05:11:35 -04:00
|
|
|
|
|
|
|
it 'does include groups' do
|
|
|
|
expected_groups = [group1.full_path, group3.full_path]
|
|
|
|
expect(@payload['groups_direct']).to match_array(expected_groups)
|
|
|
|
end
|
2020-07-09 08:08:56 -04:00
|
|
|
end
|
2018-04-26 06:05:18 -04:00
|
|
|
end
|
2016-12-09 12:36:50 -05:00
|
|
|
end
|