-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KD-2391: CORS - Integrate CORS into Mojolicious::Plugin::OpenAPI
Rebased from https://github.com/KohaSuomi/swagger2/tree/cors Includes tests.
- Loading branch information
Olli-Antti Kivilahti
authored and
Lari Taskula
committed
Dec 12, 2017
1 parent
6b29f29
commit 9bab266
Showing
4 changed files
with
473 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package t::Mojolicious::Plugin::OpenAPI::CORS::Api; | ||
use Mojo::Base 'Mojolicious::Controller'; | ||
|
||
sub add_pet { | ||
my $c = shift->openapi->valid_input or return; | ||
$c->render(openapi => $c->validation->params->to_hash, status => 200); | ||
} | ||
sub cors_list_pets { | ||
my $c = shift->openapi->valid_input or return; | ||
$c->render(openapi => {pet1 => 'George', pet2 => 'Georgina'}, status => 200); | ||
} | ||
sub cors_list_humans { | ||
my $c = shift->openapi->valid_input or return; | ||
$c->render(openapi => {pet1 => 'George', pet2 => 'Georgina'}, status => 200); | ||
} | ||
sub cors_delete_pets { | ||
my $c = shift->openapi->valid_input or return; | ||
$c->render(openapi => {delete => 'ok'}, status => 204); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package t::Mojolicious::Plugin::OpenAPI::CORS::Helpers; | ||
|
||
=head1 IN THIS FILE | ||
We implement test subroutines to test CORS operations. | ||
=cut | ||
|
||
=head2 origin_whitelist | ||
Used to test the "x-cors-access-control-allow-origin-list" CORS option. | ||
@param {Mojolicious::Controller} $c | ||
@param {String} $origin, the origin to accept or deny. | ||
@returns {String or undef}, The $origin if it is accepted or undef. | ||
=cut | ||
|
||
use Scalar::Util qw(blessed); | ||
|
||
sub origin_whitelist { | ||
my ($c, $origin) = @_; | ||
my @cc = caller(0); | ||
die $cc[3]."($c, $origin):> \$c '$c' is not a Mojolicious::Controller!" unless(blessed($c) && $c->isa('Mojolicious::Controller')); | ||
return $origin if($origin && $origin =~ /example/); | ||
return undef; | ||
} | ||
|
||
=head2 fake_authenticate | ||
Implements the OpenAPI::Guides::ProtectedApi to authenticate using x-mojo-around-action | ||
By default fails all requests with HTTP status 401 | ||
Increments $ENV{'OPENAPI-CORS-FAKE-AUTHENTICATE'} every time this subroutine is called. | ||
@returns {undef} but renders 401 and JSON error. | ||
=cut | ||
|
||
sub fake_authenticate { | ||
my ($next, $c, $opObj) = @_; | ||
|
||
$ENV{'OPENAPI-CORS-FAKE-AUTHENTICATE'} = 0 unless $ENV{'OPENAPI-CORS-FAKE-AUTHENTICATE'}; | ||
$ENV{'OPENAPI-CORS-FAKE-AUTHENTICATE'}++; | ||
|
||
return $c->render( | ||
json => {errors => [{message => "Always fail auth", path => "/"}]}, | ||
status => 401 | ||
); | ||
} | ||
|
||
1; #Make compiler happy! |
Oops, something went wrong.