GWY
2022-04-27 12b7399736e90d33bfe0c2d29917d6f075246e00
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
global class TOTPPlugin implements Process.Plugin
{    
    global Process.PluginDescribeResult describe()
    {
        Process.PluginDescribeResult result = new Process.PluginDescribeResult();
        result.description='This plug-in handles salesforce standard two factor authentication methods.';
        result.tag='Identity';
        
        result.inputParameters = new List<Process.PluginDescribeResult.InputParameter> {
                new Process.PluginDescribeResult.InputParameter('OTP_INPUT', Process.PluginDescribeResult.ParameterType.STRING, true),
                new Process.PluginDescribeResult.InputParameter('OTP_REGISTRATION_INPUT', Process.PluginDescribeResult.ParameterType.STRING, true),
                new Process.PluginDescribeResult.InputParameter('SECRET_INPUT', Process.PluginDescribeResult.ParameterType.STRING, true)      
            };
        
        result.outputParameters = new List<Process.PluginDescribeResult.OutputParameter> {
            new Process.PluginDescribeResult.OutputParameter('QR_URL_OUTPUT',
                Process.PluginDescribeResult.ParameterType.STRING),
            new Process.PluginDescribeResult.OutputParameter('SECRET_OUTPUT',
                Process.PluginDescribeResult.ParameterType.STRING),
            new Process.PluginDescribeResult.OutputParameter('IsValid_OUTPUT',
                Process.PluginDescribeResult.ParameterType.Boolean)
        };
        
        return result;
    }
    
    global Process.PluginResult invoke(Process.PluginRequest request)
    {   
        Map<String,String> QR;
        String URL; 
        String otp;
        String secret;
        Boolean status = false;
        
        String userid   = UserInfo.getUserId();  
        
        Map<String, Object> result = new Map<String, Object>();
        
        List<TwoFactorInfo> twoFactors = [SELECT UserId, Type FROM TwoFactorInfo where userID = :userid];
        
        secret = (String)request.inputParameters.get('SECRET_INPUT');
        
        if(twoFactors.isEmpty() && secret == null)
        {
            QR = Auth.SessionManagement.getQrCode();      
            URL = QR.get('qrCodeUrl');
            Secret = QR.get('secret');
            
            result.put('QR_URL_OUTPUT', URL);
            result.put('SECRET_OUTPUT', Secret);
            
            return new Process.PluginResult(result);
        }
         
        
        otp = (String)request.inputParameters.get('OTP_REGISTRATION_INPUT');
        
        if(otp == null)
            otp = (String)request.inputParameters.get('OTP_INPUT');
        
        result.put('IsValid_OUTPUT', validate(otp, secret));
           
        return new Process.PluginResult(result);
    }
      
    
    private Boolean validate(String otp, String secret)
    {
        String userid   = UserInfo.getUserId();   
        Boolean status = false;
        
         
        if(secret == null)
        {
            try {
                status = Auth.SessionManagement.validateTotpTokenForUser(otp);
            } 
            catch(Exception e)
            {
                system.debug('The key is invalid or the current user has attempted too many validations');
            } 
            
            return status;
        }
        
        status = Auth.SessionManagement.validateTotpTokenForKey(secret, otp);
        if(status == true)
        {
           TwoFactorInfo TwoFactor = new TwoFactorInfo(UserId=userid, Type='TOTP', SharedKey=secret);
 
           insert(TwoFactor);
        }
       
        return status;
    }    
}