|
Lines 46-275
my $remote_address = '127.0.0.1';
Link Here
|
| 46 |
|
46 |
|
| 47 |
# my $idp_port = t::lib::IdP::ExternalIdP->start; |
47 |
# my $idp_port = t::lib::IdP::ExternalIdP->start; |
| 48 |
|
48 |
|
| 49 |
|
|
|
| 50 |
my $oauth_provider_data = { |
49 |
my $oauth_provider_data = { |
| 51 |
code => 'oauth_test', |
50 |
code => 'oauth_test', |
| 52 |
description => 'OAuth provider', |
51 |
description => 'OAuth provider', |
| 53 |
protocol => 'OAuth', |
52 |
protocol => 'OAuth', |
| 54 |
mapping => { |
53 |
mapping => { |
| 55 |
email => 'users.0.email', |
54 |
email => 'users.0.email', |
| 56 |
firstname => 'users.0.custom_name', |
55 |
firstname => 'users.0.custom_name', |
| 57 |
surname => 'users.0.custom_surname', |
56 |
surname => 'users.0.custom_surname', |
| 58 |
userid => 'users.0.id' |
57 |
userid => 'users.0.id' |
| 59 |
}, |
58 |
}, |
| 60 |
matchpoint => 'email', |
59 |
matchpoint => 'email', |
| 61 |
config => { |
60 |
config => { |
| 62 |
authorize_url => "/idp/test/authorization_endpoint", |
61 |
authorize_url => "/idp/test/authorization_endpoint", |
| 63 |
token_url => "/idp/test/token_endpoint/without_id_token", |
62 |
token_url => "/idp/test/token_endpoint/without_id_token", |
| 64 |
userinfo_url => "/idp/test/userinfo_endpoint", |
63 |
userinfo_url => "/idp/test/userinfo_endpoint", |
| 65 |
key => "client_id", |
64 |
key => "client_id", |
| 66 |
secret => "client_secret" |
65 |
secret => "client_secret" |
| 67 |
} |
66 |
} |
| 68 |
}; |
67 |
}; |
| 69 |
|
68 |
|
| 70 |
my $oidc_with_email_provider_data = { |
69 |
my $oidc_with_email_provider_data = { |
| 71 |
code => 'oidc_email', |
70 |
code => 'oidc_email', |
| 72 |
description => 'OIDC with email provider', |
71 |
description => 'OIDC with email provider', |
| 73 |
protocol => 'OIDC', |
72 |
protocol => 'OIDC', |
| 74 |
mapping => { |
73 |
mapping => { |
| 75 |
email => 'email', |
74 |
email => 'email', |
| 76 |
firstname => 'given_name', |
75 |
firstname => 'given_name', |
| 77 |
surname => 'family_name', |
76 |
surname => 'family_name', |
| 78 |
userid => 'sub' |
77 |
userid => 'sub' |
| 79 |
}, |
78 |
}, |
| 80 |
matchpoint => 'email', |
79 |
matchpoint => 'email', |
| 81 |
config => { |
80 |
config => { |
| 82 |
authorize_url => "/idp/test/authorization_endpoint", |
81 |
authorize_url => "/idp/test/authorization_endpoint", |
| 83 |
well_known_url => "/idp/test/with_email/.well_known", |
82 |
well_known_url => "/idp/test/with_email/.well_known", |
| 84 |
key => "client_id", |
83 |
key => "client_id", |
| 85 |
secret => "client_secret" |
84 |
secret => "client_secret" |
| 86 |
} |
85 |
} |
| 87 |
}; |
86 |
}; |
| 88 |
|
87 |
|
| 89 |
my $oidc_without_email_provider_data = { |
88 |
my $oidc_without_email_provider_data = { |
| 90 |
code => 'oidc_no_email', |
89 |
code => 'oidc_no_email', |
| 91 |
description => 'OIDC without email provider', |
90 |
description => 'OIDC without email provider', |
| 92 |
protocol => 'OIDC', |
91 |
protocol => 'OIDC', |
| 93 |
mapping => { |
92 |
mapping => { |
| 94 |
email => 'users.0.email', |
93 |
email => 'users.0.email', |
| 95 |
firstname => 'given_name', |
94 |
firstname => 'given_name', |
| 96 |
surname => 'family_name', |
95 |
surname => 'family_name', |
| 97 |
userid => 'sub' |
96 |
userid => 'sub' |
| 98 |
}, |
97 |
}, |
| 99 |
matchpoint => 'email', |
98 |
matchpoint => 'email', |
| 100 |
config => { |
99 |
config => { |
| 101 |
authorize_url => "/idp/test/authorization_endpoint", |
100 |
authorize_url => "/idp/test/authorization_endpoint", |
| 102 |
well_known_url => "/idp/test/without_email/.well_known", |
101 |
well_known_url => "/idp/test/without_email/.well_known", |
| 103 |
key => "client_id", |
102 |
key => "client_id", |
| 104 |
secret => "client_secret" |
103 |
secret => "client_secret" |
| 105 |
} |
104 |
} |
| 106 |
}; |
105 |
}; |
| 107 |
|
106 |
|
| 108 |
my $domain_not_matching = { |
107 |
my $domain_not_matching = { |
| 109 |
domain => 'gmail.com', |
108 |
domain => 'gmail.com', |
| 110 |
auto_register => 0, |
109 |
auto_register => 0, |
| 111 |
update_on_auth => 0, |
110 |
update_on_auth => 0, |
| 112 |
default_library_id => undef, |
111 |
default_library_id => undef, |
| 113 |
default_category_id => undef, |
112 |
default_category_id => undef, |
| 114 |
allow_opac => 1, |
113 |
allow_opac => 1, |
| 115 |
allow_staff => 0 |
114 |
allow_staff => 0 |
| 116 |
}; |
115 |
}; |
| 117 |
|
116 |
|
| 118 |
my $domain_no_register = { |
117 |
my $domain_no_register = { |
| 119 |
domain => 'some.library.com', |
118 |
domain => 'some.library.com', |
| 120 |
auto_register => 0, |
119 |
auto_register => 0, |
| 121 |
update_on_auth => 0, |
120 |
update_on_auth => 0, |
| 122 |
default_library_id => undef, |
121 |
default_library_id => undef, |
| 123 |
default_category_id => undef, |
122 |
default_category_id => undef, |
| 124 |
allow_opac => 1, |
123 |
allow_opac => 1, |
| 125 |
allow_staff => 0 |
124 |
allow_staff => 0 |
| 126 |
}; |
125 |
}; |
| 127 |
|
126 |
|
| 128 |
my $library = $builder->build_object({class => 'Koha::Libraries'}); |
127 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 129 |
my $category = $builder->build_object({class => 'Koha::Patron::Categories'}); |
128 |
my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } ); |
| 130 |
|
129 |
|
| 131 |
my $domain_register = { |
130 |
my $domain_register = { |
| 132 |
domain => 'some.library.com', |
131 |
domain => 'some.library.com', |
| 133 |
auto_register => 1, |
132 |
auto_register => 1, |
| 134 |
update_on_auth => 0, |
133 |
update_on_auth => 0, |
| 135 |
default_library_id => $library->branchcode, |
134 |
default_library_id => $library->branchcode, |
| 136 |
default_category_id => $category->categorycode, |
135 |
default_category_id => $category->categorycode, |
| 137 |
allow_opac => 1, |
136 |
allow_opac => 1, |
| 138 |
allow_staff => 1 |
137 |
allow_staff => 1 |
| 139 |
}; |
138 |
}; |
| 140 |
|
139 |
|
| 141 |
my $domain_register_update = { |
140 |
my $domain_register_update = { |
| 142 |
domain => 'some.library.com', |
141 |
domain => 'some.library.com', |
| 143 |
auto_register => 1, |
142 |
auto_register => 1, |
| 144 |
update_on_auth => 1, |
143 |
update_on_auth => 1, |
| 145 |
default_library_id => $library->branchcode, |
144 |
default_library_id => $library->branchcode, |
| 146 |
default_category_id => $category->categorycode, |
145 |
default_category_id => $category->categorycode, |
| 147 |
allow_opac => 1, |
146 |
allow_opac => 1, |
| 148 |
allow_staff => 0 |
147 |
allow_staff => 0 |
| 149 |
}; |
148 |
}; |
| 150 |
|
149 |
|
| 151 |
subtest 'provider endpoint tests' => sub { |
150 |
subtest 'provider endpoint tests' => sub { |
| 152 |
plan tests => 12; |
151 |
plan tests => 12; |
| 153 |
|
152 |
|
| 154 |
$schema->storage->txn_begin; |
153 |
$schema->storage->txn_begin; |
| 155 |
|
154 |
|
| 156 |
Koha::Auth::Identity::Provider::Domains->delete; |
155 |
Koha::Auth::Identity::Provider::Domains->delete; |
| 157 |
Koha::Auth::Identity::Providers->delete; |
156 |
Koha::Auth::Identity::Providers->delete; |
| 158 |
|
157 |
|
| 159 |
my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); |
158 |
my ( $borrowernumber, $session_id ) = create_user_and_session( { authorized => 1 } ); |
| 160 |
|
159 |
|
| 161 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
160 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 162 |
|
161 |
|
| 163 |
my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers", json => $oauth_provider_data ); |
162 |
my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers", json => $oauth_provider_data ); |
| 164 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
163 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 165 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
164 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 166 |
|
165 |
|
| 167 |
$t->request_ok($tx) |
166 |
$t->request_ok($tx)->status_is(201); |
| 168 |
->status_is(201); |
|
|
| 169 |
|
167 |
|
| 170 |
my $provider = Koha::Auth::Identity::Providers->search({code => 'oauth_test'})->next; |
168 |
my $provider = Koha::Auth::Identity::Providers->search( { code => 'oauth_test' } )->next; |
| 171 |
is ($provider->code, 'oauth_test', 'Provider was created'); |
169 |
is( $provider->code, 'oauth_test', 'Provider was created' ); |
| 172 |
|
170 |
|
| 173 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers" ); |
171 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers" ); |
| 174 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
172 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 175 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
173 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 176 |
|
174 |
|
| 177 |
$t->request_ok($tx) |
175 |
$t->request_ok($tx)->json_has( '/0/code', 'oauth_test' ); |
| 178 |
->json_has('/0/code', 'oauth_test'); |
|
|
| 179 |
|
176 |
|
| 180 |
my %modified_provider_data_hash = %{$oauth_provider_data}; |
177 |
my %modified_provider_data_hash = %{$oauth_provider_data}; |
| 181 |
my $modified_provider_data = \%modified_provider_data_hash; |
178 |
my $modified_provider_data = \%modified_provider_data_hash; |
| 182 |
$modified_provider_data->{code} = 'some_code'; |
179 |
$modified_provider_data->{code} = 'some_code'; |
| 183 |
|
180 |
|
| 184 |
$tx = $t->ua->build_tx( PUT => "/api/v1/auth/identity_providers/".$provider->identity_provider_id, json => $modified_provider_data); |
181 |
$tx = $t->ua->build_tx( PUT => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id, json => $modified_provider_data ); |
| 185 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
182 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 186 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
183 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 187 |
|
184 |
|
| 188 |
$t->request_ok($tx) |
185 |
$t->request_ok($tx)->status_is(200); |
| 189 |
->status_is(200); |
|
|
| 190 |
|
186 |
|
| 191 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/".$provider->identity_provider_id); |
187 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id ); |
| 192 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
188 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 193 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
189 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 194 |
|
190 |
|
| 195 |
$t->request_ok($tx) |
191 |
$t->request_ok($tx)->json_has( '/code', 'some_code' ); |
| 196 |
->json_has('/code', 'some_code'); |
|
|
| 197 |
|
192 |
|
| 198 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/".$provider->identity_provider_id); |
193 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id ); |
| 199 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
194 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 200 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
195 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 201 |
|
196 |
|
| 202 |
$t->request_ok($tx) |
197 |
$t->request_ok($tx)->status_is(204); |
| 203 |
->status_is(204); |
|
|
| 204 |
# p $t->tx->res; |
| 205 |
|
198 |
|
| 206 |
$provider = Koha::Auth::Identity::Providers->search->next; |
199 |
$provider = Koha::Auth::Identity::Providers->search->next; |
| 207 |
is ($provider, undef, 'All providers deleted'); |
200 |
is( $provider, undef, 'All providers deleted' ); |
| 208 |
|
201 |
|
| 209 |
$schema->storage->txn_rollback; |
202 |
$schema->storage->txn_rollback; |
| 210 |
}; |
203 |
}; |
| 211 |
|
204 |
|
| 212 |
subtest 'domain endpoint tests' => sub { |
205 |
subtest 'domain endpoint tests' => sub { |
| 213 |
plan tests => 12; |
|
|
| 214 |
|
206 |
|
| 215 |
$schema->storage->txn_begin; |
207 |
plan tests => 12; |
| 216 |
|
208 |
|
| 217 |
Koha::Auth::Identity::Provider::Domains->delete; |
209 |
$schema->storage->txn_begin; |
| 218 |
Koha::Auth::Identity::Providers->delete; |
|
|
| 219 |
|
210 |
|
| 220 |
my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 }); |
211 |
Koha::Auth::Identity::Provider::Domains->delete; |
|
|
212 |
Koha::Auth::Identity::Providers->delete; |
| 221 |
|
213 |
|
| 222 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
214 |
my ( $borrowernumber, $session_id ) = create_user_and_session( { authorized => 1 } ); |
| 223 |
|
215 |
|
| 224 |
my $provider = $builder->build_object({class => 'Koha::Auth::Identity::Providers'}); |
216 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 225 |
|
217 |
|
| 226 |
my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains", json => $domain_not_matching ); |
218 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 227 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
| 228 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 229 |
|
219 |
|
| 230 |
$t->request_ok($tx) |
220 |
my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains", json => $domain_not_matching ); |
| 231 |
->status_is(201); |
221 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
222 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 232 |
|
223 |
|
| 233 |
my $domain = Koha::Auth::Identity::Provider::Domains->search({domain => 'gmail.com'})->next; |
224 |
$t->request_ok($tx)->status_is(201); |
| 234 |
is ($domain->domain, 'gmail.com', 'Provider was created'); |
|
|
| 235 |
|
225 |
|
| 236 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains" ); |
226 |
my $domain = Koha::Auth::Identity::Provider::Domains->search( { domain => 'gmail.com' } )->next; |
| 237 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
227 |
is( $domain->domain, 'gmail.com', 'Provider was created' ); |
| 238 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
|
|
| 239 |
|
228 |
|
| 240 |
$t->request_ok($tx) |
229 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains" ); |
| 241 |
->json_has('/0/domain', 'gmail.com'); |
230 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
231 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 242 |
|
232 |
|
| 243 |
my %modified_domain_data_hash = %{$domain_not_matching}; |
233 |
$t->request_ok($tx)->json_has( '/0/domain', 'gmail.com' ); |
| 244 |
my $modified_domain_data = \%modified_domain_data_hash; |
|
|
| 245 |
$modified_domain_data->{domain} = 'some.domain.com'; |
| 246 |
|
234 |
|
| 247 |
$tx = $t->ua->build_tx( PUT => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains/".$domain->identity_provider_domain_id, json => $modified_domain_data); |
235 |
my %modified_domain_data_hash = %{$domain_not_matching}; |
| 248 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
236 |
my $modified_domain_data = \%modified_domain_data_hash; |
| 249 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
237 |
$modified_domain_data->{domain} = 'some.domain.com'; |
| 250 |
|
238 |
|
| 251 |
$t->request_ok($tx) |
239 |
$tx = $t->ua->build_tx( |
| 252 |
->status_is(200); |
240 |
PUT => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains/" . $domain->identity_provider_domain_id, |
|
|
241 |
json => $modified_domain_data |
| 242 |
); |
| 243 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 244 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 253 |
|
245 |
|
| 254 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains/".$domain->identity_provider_domain_id); |
246 |
$t->request_ok($tx)->status_is(200); |
| 255 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
| 256 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 257 |
|
247 |
|
| 258 |
$t->request_ok($tx) |
248 |
$tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains/" . $domain->identity_provider_domain_id ); |
| 259 |
->json_has('/domain', 'some.domain.com'); |
249 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
250 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 260 |
|
251 |
|
| 261 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains/".$domain->identity_provider_domain_id); |
252 |
$t->request_ok($tx)->json_has( '/domain', 'some.domain.com' ); |
| 262 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
|
|
| 263 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 264 |
|
253 |
|
| 265 |
$t->request_ok($tx) |
254 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains/" . $domain->identity_provider_domain_id ); |
| 266 |
->status_is(204); |
255 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 267 |
# p $t->tx->res; |
256 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 268 |
|
257 |
|
| 269 |
$domain = Koha::Auth::Identity::Provider::Domains->search->next; |
258 |
$t->request_ok($tx)->status_is(204); |
| 270 |
is ($domain, undef, 'All domains deleted'); |
|
|
| 271 |
|
259 |
|
| 272 |
$schema->storage->txn_rollback; |
260 |
$domain = Koha::Auth::Identity::Provider::Domains->search->next; |
|
|
261 |
is( $domain, undef, 'All domains deleted' ); |
| 262 |
|
| 263 |
$schema->storage->txn_rollback; |
| 273 |
}; |
264 |
}; |
| 274 |
|
265 |
|
| 275 |
# subtest 'oauth login tests' => sub { |
266 |
# subtest 'oauth login tests' => sub { |
|
Lines 316-326
sub create_user_and_session {
Link Here
|
| 316 |
my $flags = ( $args->{authorized} ) ? 1 : 0; |
307 |
my $flags = ( $args->{authorized} ) ? 1 : 0; |
| 317 |
|
308 |
|
| 318 |
my $user = $builder->build( |
309 |
my $user = $builder->build( |
| 319 |
{ |
310 |
{ source => 'Borrower', |
| 320 |
source => 'Borrower', |
311 |
value => { flags => $flags } |
| 321 |
value => { |
|
|
| 322 |
flags => $flags |
| 323 |
} |
| 324 |
} |
312 |
} |
| 325 |
); |
313 |
); |
| 326 |
|
314 |
|
|
Lines 334-338
sub create_user_and_session {
Link Here
|
| 334 |
|
322 |
|
| 335 |
return ( $user->{borrowernumber}, $session->id ); |
323 |
return ( $user->{borrowernumber}, $session->id ); |
| 336 |
} |
324 |
} |
| 337 |
|
|
|
| 338 |
1; |
| 339 |
- |