-
Notifications
You must be signed in to change notification settings - Fork 0
/
FogBlockchainAuthentication.sol
296 lines (251 loc) · 9.71 KB
/
FogBlockchainAuthentication.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
pragma solidity >=0.4.14 <0.6.0;
pragma experimental ABIEncoderV2;
//import './DateTime.sol';
//import './HexUtils.sol';
import './strings.sol';
// import './FogDeviceRegistration.sol';
contract FogBlockchainAuthentication{
using strings for *;
string public s;
address public owner;
struct Authentication {
address requesteddeviceid;
string deviceinfo;
string previousblock;
address accessdeviceid;
string permissions;
string location;
uint timelease;
}
mapping (address => Authentication) fogdeviceauth;
// string regname;
// address regdeviceid; string regdeviceconfiguration;
// uint32 regmanufacturerid;string reglocations;
// string regipfsusercollagepicaddress;
string ipfsusercollagepicaddress;
struct FogDevice {
string modelname;
address deviceid;
uint32 manufacturerid;
string locations;
string usercollagepicaddress;
string deviceconfiguration;
int authcoins;
int registertime;
}
// Stored devices
mapping (address => FogDevice) fogdevices;
// byte32 s4;
event Log(string text1,string text);
event Logs(string text1,uint32 text);
event Logint(string text1,int text);
event Log(string text);
event Logs(uint32 text);
event Logint(int text);
function sendImageHash(string ipfshash) public {
ipfsusercollagepicaddress = ipfshash;
}
function getImageHash() public returns (string x) {
return ipfsusercollagepicaddress;
}
function register(string name, string deviceconfiguration,uint32 manufacturerid,string locations,string ipfsusercollagepicaddress)
public returns (bool res) {
FogDevice dev = fogdevices[msg.sender];
// Return false if already registered.
if ( dev.deviceid != 0x0) {
return false;
}
dev.modelname = name;
dev.deviceid = msg.sender;
dev.registertime = 100000;
dev.manufacturerid = manufacturerid;
dev.deviceconfiguration=deviceconfiguration;
dev.usercollagepicaddress=ipfsusercollagepicaddress;
dev.locations=locations;
dev.authcoins=100;
fogdevices[msg.sender] = dev;
// s4=sha3(dev.modelname, dev.deviceid,dev.manufacturerid,dev.deviceconfiguration,dev.usercollagepicaddress);
Log(name,"Registered Successfully");
return true;
}
function unregister() public returns (bool res) {
FogDevice dev = fogdevices[msg.sender];
delete dev.modelname;
dev.deviceid = 0x0;
dev.registertime = 0;
dev.deviceconfiguration = "";
dev.usercollagepicaddress="";
dev.authcoins=0;
Log(dev.modelname,"UnRegistered Successfully");
return true;
}
function getDevInfo(address devid) public returns (string name, address deviceid, string deviceconfiguration,uint32 manufacturerid,string locations,string ipfsusercollagepicaddress,int authcoins) {
FogDevice dev = fogdevices[devid];
name = dev.modelname;
deviceid = dev.deviceid;
deviceconfiguration = dev.deviceconfiguration;
manufacturerid=dev.manufacturerid;
locations=dev.locations;
ipfsusercollagepicaddress=dev.usercollagepicaddress;
authcoins=dev.authcoins;
Log(name,"Device Registered Successfully");
return(name,deviceid,deviceconfiguration,manufacturerid,locations,ipfsusercollagepicaddress,authcoins);
}
function getDevLocation(address devid) public returns (string locations) {
FogDevice dev = fogdevices[devid];
locations=dev.locations;
return(string(locations));
}
function remove() public returns (bool res) {
if (msg.sender == owner) {
suicide(owner);
Log("Contract","Smart contract suicide Successfully");
}
}
string public fdlocation;
function checkAuthentication(address addr,string name, string deviceconfiguration,uint32 manufacturerid,string locations,string ipfsusercollagepicaddress, uint leasetime)
public returns (bool res) {
var(regname,regdeviceid,regdeviceconfiguration,regmanufacturerid,reglocations,regipfsusercollagepicaddress,regauthcoins)= getDevInfo(addr);
Log("regname ",regname);
Log("name",name);
Logs("regmanufacturerid",regmanufacturerid);
Logs("manufacturerid",manufacturerid);
Log("regipfsusercollagepicaddress",regipfsusercollagepicaddress);
Log("ipfsusercollagepicaddress",ipfsusercollagepicaddress);
Logint("regauthcoins",regauthcoins);
if ( keccak256(regname)==keccak256(name) && regmanufacturerid==manufacturerid && keccak256(regipfsusercollagepicaddress) == keccak256(ipfsusercollagepicaddress) && regauthcoins>1)
{
// var s = reglocations.toSlice();
// var spliinal= reglocations.toSlice().find(locations.toSlice());
//bytes memory tempEmptyStringTest = bytes(spliinal.toString()); // Uses memory
if (bytes(reglocations.toSlice().find(locations.toSlice()).toString()).length == 0) {
// emptyStringTest is an empty string
} else {
//bool success= RequestFogdevice(name,locations);
if(RequestFogdevice(name,locations)==true)
{
regauthcoins=regauthcoins-1;
giveAccesstoFD(addr,deviceconfiguration,manufacturerid,"Write",locations,leasetime);
Log("Access granted Successfully");
return true;
}
// emptyStringTest is not an empty string
}
// var delim = ":".toSlice();
// if(parts[i].toString()=="locations")
// {
// bool success= RequestFogdevice(name,locations);
// if(success==true)
// {
// giveAccesstoFD(addr,deviceconfiguration,manufacturerid,"Write",locations);
// }
// }
}
//feed.info.value(10).gas(800)();
//string name, string deviceconfiguration,uint32 manufacturerid,string locations,string ipfsusercollagepicaddress
Log("Access restricted");
regauthcoins=regauthcoins-50;
return false;
}
string public s2;
function RequestanotherFogdeviceforlocation(string ipfshash, string location) public returns (bool res) {
FogDevice dev12 = fogdevices[msg.sender];
if( keccak256(dev12.usercollagepicaddress) == keccak256(ipfshash)){
s2=location;
Log("Location added Successfully to Authorisation list");
}
}
function RequestFogdevice(string FD, string location) public returns (bool res) {
if( keccak256(getLocation()) == keccak256(location) || keccak256(s2)==keccak256(location))
{
Log("Location FogDevice verified Successfully");
return true;
}
else
Log("Location FogDevice unverified Successfully");
return false;
}
function setLocation(string location, string ipfshash) public returns (bool)
{
FogDevice dev1 = fogdevices[msg.sender];
if( keccak256(dev1.usercollagepicaddress) == keccak256(ipfshash)){
s = dev1.locations.toSlice().concat(location.toSlice());
dev1.locations=s;
Log("Location added Successfully to Authorisation list");
}
}
function setPasskey(string location, string ipfshash) public returns (bool)
{
FogDevice dev1 = fogdevices[msg.sender];
if( keccak256(dev1.usercollagepicaddress) == keccak256(ipfshash)){
dev1.authcoins=100;
Log("Location added Successfully to Authorisation list");
}
}
function getLocation() public returns (string)
{
return fdlocation;
}
function giveAccesstoFD(address requesteddeviceid,
string deviceinfo,
address accessdeviceid,
string permissions,
string location, uint leaseTime) expire(msg.sender) public returns (bool)
{
Authentication devauth = fogdeviceauth[msg.sender];
// Return false if already registered.
devauth.requesteddeviceid=requesteddeviceid;
devauth.deviceinfo=deviceinfo;
devauth.accessdeviceid=accessdeviceid;
devauth.permissions=permissions;
devauth.location=location;
devauth.timelease=block.timestamp + leaseTime;
fogdeviceauth[msg.sender] = devauth;
return true;
}
function getAccessfo(address devid) public returns (address requesteddeviceid,
string deviceinfo,
address accessdeviceid,
string permissions,
string location, uint leaseTime) {
Authentication devauth = fogdeviceauth[msg.sender];
requesteddeviceid= devauth.requesteddeviceid;
deviceinfo=devauth.deviceinfo;
accessdeviceid=devauth.accessdeviceid;
permissions=devauth.permissions;
location=devauth.location;
leaseTime=devauth.timelease-now;
}
function autoDisconnect() public returns (bool)
{
removeAccesstoFD();
}
modifier expire(address _addr) {
if (fogdeviceauth[_addr].timelease >= block.timestamp) {
Authentication devauth = fogdeviceauth[msg.sender];
// Return false if already registered.
devauth.requesteddeviceid=0x0;
devauth.deviceinfo="";
devauth.previousblock="";
devauth.accessdeviceid=0x0;
devauth.permissions="";
devauth.location="";
devauth.timelease=0;
}
_;
}
function removeAccesstoFD() public returns (bool)
{
Authentication devauth = fogdeviceauth[msg.sender];
// Return false if already registered.
devauth.requesteddeviceid=0x0;
devauth.deviceinfo="";
devauth.previousblock="";
devauth.accessdeviceid=0x0;
devauth.permissions="";
devauth.location="";
devauth.timelease=0;
fogdeviceauth[msg.sender] = devauth;
return true;
}
}