ResourceIpAuth.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  *
8  * @author: 2021 - Slavey Karadzhov <slaff@attachix.com>
9  *
10  ****/
11 
12 #pragma once
13 
14 #include "../HttpResourcePlugin.h"
15 #include <Data/WebHelpers/base64.h>
16 
18 {
19 public:
20  ResourceIpAuth(IpAddress ip, IpAddress netmask) : ip(ip), netmask(netmask)
21  {
22  }
23 
24  bool urlComplete(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response) override
25  {
26  auto remoteIp = connection.getRemoteIp();
27  if(remoteIp.compare(ip, netmask)) {
28  // This IP is allowed to proceed
29  return true;
30  }
31 
32  // specify that the resource is protected...
33  response.code = HTTP_STATUS_UNAUTHORIZED;
34  return false;
35  }
36 
37 private:
38  IpAddress ip;
39  IpAddress netmask;
40 };
IpAddress getRemoteIp() const
Definition: TcpConnection.h:106
Filter plugins run before the resource is invoked.
Definition: HttpResourcePlugin.h:63
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:26
HttpStatus code
The HTTP status response code.
Definition: HttpResponse.h:149
Definition: HttpServerConnection.h:34
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
Definition: ResourceIpAuth.h:18
bool urlComplete(HttpServerConnection &connection, HttpRequest &request, HttpResponse &response) override
Definition: ResourceIpAuth.h:24
ResourceIpAuth(IpAddress ip, IpAddress netmask)
Definition: ResourceIpAuth.h:20