.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Swagger2::Guides::ProtectedApi 3pm" .TH Swagger2::Guides::ProtectedApi 3pm "2016-04-17" "perl v5.22.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Swagger2::Guides::ProtectedApi \- Protected API Guide .SH "OVERVIEW" .IX Header "OVERVIEW" It is possible to protect your \s-1API:\s0 You can either use a \*(L"Custom route\*(R" or an \*(L"Around action hook\*(R". Both can serve the same purpose, but the around action hook can be customized for every \s-1API\s0 resource. .SH "TUTORIAL" .IX Header "TUTORIAL" .SS "Around action hook" .IX Subsection "Around action hook" The \f(CW\*(C`x\-mojo\-around\-action\*(C'\fR value is optional, but can hold the name of a method to call, which wraps around the autogenerated action which does input and output validation. This means that any data sent to the server is not yet converted into \f(CW$input\fR to your action. .PP Here is an example method which match the \f(CW\*(C`x\-mojo\-around\-action\*(C'\fR from \&\*(L"Swagger specification\*(R", \f(CW\*(C`MyApp::authenticate_api_request\*(C'\fR: .PP .Vb 1 \& package MyApp; \& \& sub authenticate_api_request { \& my ($next, $c, $action_spec) = @_; \& \& # Go to the action if the Authorization header is valid \& return $next\->($c) if $c\->req\->headers\->authorization eq "s3cret!"; \& \& # ...or render an error if not \& return $c\->render_swagger( \& {errors => [{message => "Invalid authorization key", path => "/"}]}, \& {}, \& 401 \& ); \& } .Ve .PP \&\f(CW\*(C`x\-mojo\-around\-action\*(C'\fR is also inherited from most levels, meaning that you define it globally for your whole \s-1API\s0 if you like: .PP .Vb 11 \& { \& "x\-mojo\-around\-action": "MyApp::protect_any_resource", \& "paths": { \& "/pets": { \& "x\-mojo\-around\-action": "MyApp::protect_any_method_under_foo", \& "get": { \& "x\-mojo\-around\-action": "MyApp::protect_just_this_resource" \& } \& } \& } \& } .Ve .SS "Custom route" .IX Subsection "Custom route" .Vb 1 \& use Mojolicious::Lite; \& \& my $route = app\->routes\->under\->to( \& cb => sub { \& my $c = shift; \& return 1 if $c\->param(\*(Aqsecret\*(Aq); \& return $c\->render(json => {error => "Not authenticated"}, status => 401); \& } \& ); \& \& plugin Swagger2 => { \& route => $route, \& url => "data://api.json", \& }; \& \& _\|_DATA_\|_ \& @@ api.json \& {"swagger":"2.0", ...} .Ve .SH "AUTHOR" .IX Header "AUTHOR" Jan Henning Thorsen \- \f(CW\*(C`jhthorsen@cpan.org\*(C'\fR