P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.icall.callout;
 
import java.io.IOException;
import java.util.Date;
 
import foundation.action.ActionContext;
import foundation.icall.ICall;
import foundation.util.Util;
 
 
public class NCCSource extends HttpServerSource {
    
    private static int TimeOutMinute = 20;
    public Date lastTime;
    private String secret;
    private String clientId;
    private String signature;
    private String token;
    
 
    @Override
    public void login(ActionContext context, ICall iCall) throws Exception {
        if (!tokenExpired()) {
            return;
        }
        
        //1. 生成 secret 及 sing
        createSecretAndSign();
        
        //2. 登录服务器, 获取 token
        getToken();
    }
 
    @Override
    public void logout(ActionContext context, ICall iCall) {
        
    }
 
    @Override
    public ICallRequest createRequest(String url) {
        // TODO Auto-generated method stub
        return null;
    }
    
    private boolean tokenExpired() {
        if (Util.isEmpty(token)) {
            return true;
        }
        
        Date now = new Date();
        boolean result = lastTime.getTime() - now.getTime() >= TimeOutMinute * 60 * 1000;
        return result;
    }
 
    private void createSecretAndSign() throws Exception {
        String publicKey = meta.getString("publicKey");
        String clientSecret = meta.getString("clientSecret");
        clientId = meta.getString("clientId");
        
//        secret = Encryption.pubEncrypt(publicKey, clientSecret);
        
        signature = clientId + clientSecret + publicKey;
//        signature = SHA256Util.getSHA256(signature, publicKey);
    }
 
    private void getToken() throws IOException {
        String url = meta.getString("url");
        String bizCenter = meta.getString("bizCenter");
        String userCode = meta.getString("userCode");
        
        ICallRequest request = new ICallRequest(url);
        
        request.addOneParam("grant_type", "client_credentials");
        request.addOneParam("client_id", clientId);
        request.addOneParam("client_secret", secret);
        request.addOneParam("signature", signature);
        
        request.addOneParam("biz_center", bizCenter);
        request.addOneParam("usercode", userCode);
        
//        token = post(request, ResponseType.String);
    }
 
    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return null;
    }
}