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
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
package foundation.token;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.json.JSONObject;
 
import com.nimbusds.jose.JOSEObjectType;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.JWSVerifier;
import com.nimbusds.jose.Payload;
import com.nimbusds.jose.crypto.MACSigner;
import com.nimbusds.jose.crypto.MACVerifier;
 
import foundation.server.config.Configer;
import foundation.util.ContentBuilder;
import foundation.util.Util;
 
 
@SuppressWarnings("deprecation")
public class UserToken {
 
    private static int OverMinute = 20;
    private static JWSHeader header;
    private static String secretKey;
 
    private String userId;
    private String onlineCode;
    private long createTime;
    private long expireTime;
    private String encrypted;
    private boolean valid;
    private boolean empty;
    
    static {
        OverMinute = Integer.parseInt(Configer.getString("tokenOverTime"));
        header = new JWSHeader(JWSAlgorithm.HS256, JOSEObjectType.JWT, null, null, null, null, null, null, null, null, null, null, null);
        secretKey = "kwq8a_B6WMqHOrEi-gFR5rRPmPL7qoShZJn0VFfXpXc1Yfw6BfVrliAP9C4";//Configer.getString("tokenSecretKey", "foundation");
    }
 
    public static UserToken getInstance(String token) {
        String encrypted = token;
        
        UserToken result = new UserToken();
        
        if (Util.isEmpty(encrypted)) {
            return result;
        }
        
        try {
            result.encrypted = encrypted;
            result.parse();            
        }
        catch (Exception e) {
            result.userId = token;
        }
        
        return result;
    }
    
    public static UserToken getInstance(String userId, String onlineCode) {
        UserToken result = new UserToken();
        
        result.userId = userId;
        result.onlineCode = onlineCode;
        result.createTime = (new Date()).getTime();
        result.expireTime = result.createTime + 1000 * 60 * OverMinute;
        result.compile();
        
        return result;
    }
    
    private UserToken() {
 
    }
    
    private void parse() throws Exception {
        if (Util.isEmpty(encrypted)) {
            valid = false;
        }
        
        JWSObject jwsObject = JWSObject.parse(encrypted);
        Payload payload = jwsObject.getPayload();
        JWSVerifier verifier = new MACVerifier(secretKey.getBytes());
        
        if(!jwsObject.verify(verifier)) {
            throw new Exception("invalid token");
        }
         
        String value = payload.toString();
        JSONObject json = new JSONObject(value);
        
        if (json.has("userId")) {
            userId = json.getString("userId");
        }
        
        if (json.has("onlineCode")) {
            onlineCode = json.getString("onlineCode");
        }
        
        if (json.has("expiretime")) {
            expireTime = json.getLong("expiretime");
        }
        
        if (json.has("createtime")) {
            createTime = json.getLong("createtime");
        }
    }
    
    private void compile() {
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("userId", userId);
            dataMap.put("onlineCode", String.valueOf(createTime));
//            dataMap.put("createtime", createTime);
            dataMap.put("expiretime", expireTime);
            
            //1. 
            JSONObject jsonObject = new JSONObject(dataMap);
            byte[] bytes = jsonObject.toString().getBytes();
            Payload payload = new Payload(bytes);  
            
            //2. 
            JWSObject jwsObject = new JWSObject(header, payload);
            jwsObject.sign(new MACSigner(secretKey.getBytes()));
            
            //3. 
            encrypted = jwsObject.serialize();
            valid = true;            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public boolean validate() {
        if (Util.isEmpty(userId)) {
            return false;
        }
        
        long curTime = new Date().getTime();
        
        if (curTime > expireTime && expireTime != 0) {
            return false;
        }
        
        return true;
    }
 
    public String getContent() {
        return encrypted;
    }
 
    public boolean isValid() {
        return valid;
    }
 
    public boolean isEmpty() {
        return empty;
    }
 
    public String getUserId() {
        return userId;
    }
 
    public String getOnlineCode() {
        return onlineCode;
    }
 
    @Override
    public String toString() {
        ContentBuilder result = new ContentBuilder(", ");
        result.append("userId=" + userId);
        result.append("onlineCode=" + onlineCode);
        result.append("createTime=" + createTime);
        result.append("expireTime=" + expireTime);
        result.append("secretKey=" + secretKey);
        result.append("encrypted=" + encrypted);        
 
        return result.toString();
    }
    
    public static void main(String[] args) {
        UserToken userToken = UserToken.getInstance("admin", "1234567890");
        System.out.println(userToken.getContent());
    }
 
}