## 2 Materials and Methods
### Mfa_R Processes
#### 2.3.2 Authentication Process
```
Result: result
1 Functionauthenticate():booleanis
2\(result=\) FALSE;
3\(resp\_step1=\)send_step1(\(login\),senha);
4 wait(callback_step1);
5 query=resp_step1.query;
6\(auth\_code=resp\_step1.auth\_code\);
7\(step2=\)query_api.resolve(device_data,query);
8\(resp\_step2=\)send_step2(auth_code,step2);
9 wait(callback_step2);
10\(result=resp\_step2\);
11 returnresult;
12
13 end
```  
**Algorithm 3**Device authentication process--client-side  
```
Result: result
1 Functiondo_step1(request: Request): booleanis
2\(result=\) FALSE;
3\(step1=\)receive_step1();
4\(login=step1.login\);
5\(senha=step1.senha\);
6 ifvalid(\(login\),senha)then
7 device_data = retrieve(\(login\));
8 ifdevice_data.legacy is TRUEthen
9\(auth\_code=login\);
10
11 authInspector.level(\(auth\_code\),COMPATIBILITY_MODE);
12
13 else
14\(query\_resp=\)query_api.generate(device_data);
15\(auth\_code=\)authInspector.generate_code();
16 authInspector.save(\(auth\_code\),resp);
17 authInspector.level(\(auth\_code\),AUTH1);
18 resp_step2(query);
19
20 end
```  
**Algorithm 4**Device authentication process (STEP1)--server-side  
The algorithm that describes the behavior of the device during authentication (Algorithm 3) has important aspects that must be observed. Some of these aspects involve the callback functions that must be handled (lines 4 and 9) and the use of the query API to resolve the second authentication factor (line 7). The second algorithm (Algorithm 4) provides password-based authentication and ensures compatibility with legacy devices. As can be seen, after validating the device login (line 6), the server checks if the device is in compatibility mode (line 8). If it is a legacy device, the device will be registered in \(authInspector\) as compatibility mode (COMPATIBILITY_MODE). If it is not legacy, a challenge-response factor will be generated through the query API (line 12), saved in \(authInspector\) along with the device access code (line 14), where it is still registered that the device is in the first level authentication (line 15). In the end, the query is sent to the device to solve it (line 16). Finally, there is the resolution algorithm for the second authentication factor (Algorithm 5). This second factor is analyzed through a single algorithm, the server-side, which shows the steps taken by the server in validating the challenge-response (CR) factor. After receiving the response from the device and decoding it (lines 3 to 5), \(authInspector\) is consulted to validate the response using \(auth\_code\) as a search key. Once validated, a record is generated that the device is authenticated in the second factor (line 7) and an initial reputation score is assigned to it (line 8). Note that the interaction between \(authServ\) and \(authInspector\) is essential for the success of the authentication process and the maintenance of the reputation score, as shown by the previous algorithms. ```
Result: result
1 Functiondo_step2(request: Request) : booleanis
2\(result\) = FALSE;
3\(step2\) = receive_step2();
4\(auth\_code\) = \(step2\_auth\_code\);
5\(resp\) = \(step2\_query\_response\);
6 if\(authInspector.valid(auth\_code,resp)\)then
7 authInspector.level(\(auth\_code,\)AUTH2);
8 authInspector.reputation_score(\(auth\_code,\)INITIAL_REPUTATION);
9\(result\) = TRUE;
10
11 end if
12
13 returnresult;
14
15 end for
```  
**Algorithm 5**Device authentication process (STEP2)--server-side