MCPcopy Create free account
hub / github.com/Stiffstream/restinio / authorization_handler

Function authorization_handler

dev/sample/extra_data_factory/main.cpp:141–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

139}
140
141restinio::request_handling_status_t authorization_handler(
142 const restinio::generic_request_handle_t< per_request_data_t > & req )
143{
144 // Authorization required if user asks for '/admin' or '/stats'.
145 if( "/admin" == req->header().path() || "/stats" == req->header().path() )
146 {
147 const auto & ud = req->extra_data();
148 if( !ud.m_auth_result.m_credentials_provided )
149 {
150 // User should provide credentials.
151 init_resp( req->create_response( restinio::status_unauthorized() ) )
152 .append_header(
153 restinio::http_field::content_type,
154 "text/plain; charset=utf-8" )
155 .append_header(
156 restinio::http_field::www_authenticate,
157 R"(Basic realm="Username/Password required", charset="utf-8")" )
158 .set_body( "Unauthorized access forbidden")
159 .done();
160
161 // Mark the request as processed.
162 return restinio::request_accepted();
163 }
164
165 bool has_permission = [&req]( const auto & permissions ) {
166 if( "/admin" == req->header().path() )
167 return permissions.m_admin_view;
168 if( "/stats" == req->header().path() )
169 return permissions.m_stats_view;
170 return false;
171 }( ud.m_auth_result.m_identity.m_permissions );
172
173 if( !has_permission )
174 {
175 init_resp( req->create_response( restinio::status_unauthorized() ) )
176 .append_header(
177 restinio::http_field::content_type,
178 "text/plain; charset=utf-8" )
179 .set_body( "Access without permissions prohibited")
180 .done();
181
182 // Mark the request as processed.
183 return restinio::request_accepted();
184 }
185 }
186
187 return restinio::request_not_handled();
188}
189
190auto create_request_handler()
191{

Callers

nothing calls this directly

Calls 7

status_unauthorizedFunction · 0.85
request_acceptedFunction · 0.85
request_not_handledFunction · 0.85
pathMethod · 0.80
create_responseMethod · 0.80
init_respFunction · 0.70
doneMethod · 0.45

Tested by

no test coverage detected