|
| 1 | +package com.shuzijun.leetcode.plugin.window; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONArray; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.intellij.openapi.application.ApplicationManager; |
| 6 | +import com.intellij.openapi.progress.ProgressIndicator; |
| 7 | +import com.intellij.openapi.progress.ProgressManager; |
| 8 | +import com.intellij.openapi.progress.Task; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.shuzijun.leetcode.plugin.manager.ViewManager; |
| 11 | +import com.shuzijun.leetcode.plugin.model.Config; |
| 12 | +import com.shuzijun.leetcode.plugin.setting.PersistentConfig; |
| 13 | +import com.shuzijun.leetcode.plugin.utils.*; |
| 14 | +import org.apache.commons.lang.StringUtils; |
| 15 | +import org.apache.http.HttpEntity; |
| 16 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | + |
| 19 | +import javax.swing.*; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.net.HttpCookie; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author shuzijun |
| 26 | + */ |
| 27 | +public class HttpLogin { |
| 28 | + public static boolean ajaxLogin(Config config, JTree tree, Project project) { |
| 29 | + |
| 30 | + if (URLUtils.leetcode.equals(URLUtils.getLeetcodeHost())) { |
| 31 | + return Boolean.FALSE; |
| 32 | + } |
| 33 | + |
| 34 | + if (StringUtils.isBlank(PersistentConfig.getInstance().getPassword())) { |
| 35 | + return Boolean.FALSE; |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + HttpEntity ent = MultipartEntityBuilder.create() |
| 40 | + .addTextBody("csrfmiddlewaretoken", HttpRequestUtils.getToken()) |
| 41 | + .addTextBody("login", config.getLoginName()) |
| 42 | + .addTextBody("password", PersistentConfig.getInstance().getPassword()) |
| 43 | + .addTextBody("next", "/problems") |
| 44 | + .build(); |
| 45 | + HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeLogin(), ent.getContentType().getValue()); |
| 46 | + httpRequest.setBody(IOUtils.toString(ent.getContent(), "UTF-8")); |
| 47 | + httpRequest.addHeader("x-requested-with", "XMLHttpRequest"); |
| 48 | + httpRequest.addHeader("accept", "*/*"); |
| 49 | + HttpResponse response = HttpRequestUtils.executePost(httpRequest); |
| 50 | + |
| 51 | + if (response == null) { |
| 52 | + MessageUtils.getInstance(project).showWarnMsg("warning", PropertiesUtils.getInfo("request.failed")); |
| 53 | + return Boolean.FALSE; |
| 54 | + } |
| 55 | + |
| 56 | + String body = response.getBody(); |
| 57 | + |
| 58 | + if ((response.getStatusCode() == 200 || response.getStatusCode() == 302)) { |
| 59 | + if (StringUtils.isNotBlank(body) && body.startsWith("{")) { |
| 60 | + JSONObject jsonObject = JSONObject.parseObject(body); |
| 61 | + JSONArray jsonArray = jsonObject.getJSONObject("form").getJSONArray("errors"); |
| 62 | + if (jsonArray.isEmpty()) { |
| 63 | + MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("login.success")); |
| 64 | + examineEmail(project); |
| 65 | + ViewManager.loadServiceData(tree, project); |
| 66 | + return Boolean.TRUE; |
| 67 | + } else { |
| 68 | + MessageUtils.getInstance(project).showInfoMsg("info", StringUtils.join(jsonArray, ",")); |
| 69 | + return Boolean.FALSE; |
| 70 | + } |
| 71 | + } else if (StringUtils.isBlank(body)) { |
| 72 | + MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("login.success")); |
| 73 | + examineEmail(project); |
| 74 | + ViewManager.loadServiceData(tree, project); |
| 75 | + return Boolean.TRUE; |
| 76 | + } else { |
| 77 | + HttpRequestUtils.resetHttpclient(); |
| 78 | + MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("login.unknown")); |
| 79 | + SentryUtils.submitErrorReport(null, String.format("login.unknown:\nStatusCode:%s\nbody:%s", response.getStatusCode(), body)); |
| 80 | + return Boolean.FALSE; |
| 81 | + } |
| 82 | + } else if (response.getStatusCode() == 400) { |
| 83 | + JSONObject jsonObject = JSONObject.parseObject(body); |
| 84 | + MessageUtils.getInstance(project).showInfoMsg("info", StringUtils.join(jsonObject.getJSONObject("form").getJSONArray("errors"), ",")); |
| 85 | + return Boolean.FALSE; |
| 86 | + } else { |
| 87 | + HttpRequestUtils.resetHttpclient(); |
| 88 | + MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("login.unknown")); |
| 89 | + SentryUtils.submitErrorReport(null, String.format("login.unknown:\nStatusCode:%s\nbody:%s", response.getStatusCode(), body)); |
| 90 | + return Boolean.FALSE; |
| 91 | + } |
| 92 | + } catch (Exception e) { |
| 93 | + LogUtils.LOG.error("登陆错误", e); |
| 94 | + MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("login.failed")); |
| 95 | + return Boolean.FALSE; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static void examineEmail(Project project) { |
| 100 | + ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { |
| 101 | + @Override |
| 102 | + public void run() { |
| 103 | + HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(), "application/json"); |
| 104 | + try { |
| 105 | + httpRequest.setBody("{\"operationName\":\"user\",\"variables\":{},\"query\":\"query user {\\n user {\\n socialAccounts\\n username\\n emails {\\n email\\n primary\\n verified\\n __typename\\n }\\n phone\\n profile {\\n rewardStats\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}"); |
| 106 | + httpRequest.addHeader("Accept", "application/json"); |
| 107 | + HttpResponse response = HttpRequestUtils.executePost(httpRequest); |
| 108 | + if (response != null && response.getStatusCode() == 200) { |
| 109 | + |
| 110 | + String body = response.getBody(); |
| 111 | + |
| 112 | + JSONArray jsonArray = JSONObject.parseObject(body).getJSONObject("data").getJSONObject("user").getJSONArray("emails"); |
| 113 | + if (jsonArray != null && jsonArray.size() > 0) { |
| 114 | + for (int i = 0; i < jsonArray.size(); i++) { |
| 115 | + JSONObject object = jsonArray.getJSONObject(i); |
| 116 | + if (object.getBoolean("verified")) { |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + MessageUtils.getInstance(project).showWarnMsg("info", PropertiesUtils.getInfo("user.email")); |
| 123 | + } |
| 124 | + } catch (Exception i) { |
| 125 | + LogUtils.LOG.error("验证邮箱错误"); |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + public static void loginSuccess(JTree tree, Project project, List<HttpCookie> cookieList) { |
| 132 | + ProgressManager.getInstance().run(new Task.Backgroundable(project, "leetcode.loginSuccess", false) { |
| 133 | + @Override |
| 134 | + public void run(@NotNull ProgressIndicator progressIndicator) { |
| 135 | + Config config = PersistentConfig.getInstance().getInitConfig(); |
| 136 | + config.addCookie(config.getUrl() + config.getLoginName(), CookieUtils.httpCookieToJSONString(cookieList)); |
| 137 | + PersistentConfig.getInstance().setInitConfig(config); |
| 138 | + MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("login.success")); |
| 139 | + ViewManager.loadServiceData(tree, project); |
| 140 | + examineEmail(project); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + public static boolean isSupportedJcef() { |
| 146 | + try { |
| 147 | + Class<?> JBCefAppClass = Class.forName("com.intellij.ui.jcef.JBCefApp"); |
| 148 | + Method method = JBCefAppClass.getMethod("isSupported"); |
| 149 | + boolean supported = (boolean) method.invoke(null); |
| 150 | + |
| 151 | + Config config = PersistentConfig.getInstance().getInitConfig(); |
| 152 | + return config.getJcef() && supported; |
| 153 | + } catch (Throwable e) { |
| 154 | + return Boolean.FALSE; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments