File size: 5,274 Bytes
5c5b371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<%- include("partials/shared_header", { title: "Request User Token" }) %>

<style>
  #request-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 20px 0;
    width: 100%;
    gap: 10px;
  }

  #request-container button {
    flex: 1;
    width: 100%;
    max-width: 300px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
  }

  #refresh-token-input {
    width: 100%;
  }
</style>

<h1>Request User Token</h1>
<p>You can request a temporary user token to use this proxy. The token will be valid for <%= tokenLifetime %> hours.</p>
<% if (keyRequired) { %>
<div>
  <p>You need to supply the proxy password to request or refresh a token.</p>
  <div>
    <label for="proxy-key">Proxy password:</label>
    <input type="password" id="proxy-key" />
  </div>
</div>
<% } %>
<div id="request-container">
  <button id="request-token" onclick="requestChallenge('new')">Get a new token</button>
  <button id="refresh-token-toggle" onclick="switchSection('refresh')">Refresh an old token</button>
  <h6 id="existing-token-value" style="display: none">Existing token:</h6>
</div>
<div id="back-to-menu" style="display: none">
  <a href="#" onclick="switchSection('root')">« Back</a>
</div>
<div id="refresh-container" style="display: none">
  <div id="existing-token">
    <p>
      If you have an existing or expired token, enter it here to try to refresh it by completing a shorter verification.
    </p>
    <div>
      <label for="refresh-token-input">Existing token:</label>
      <input type="text" id="refresh-token-input" />
      <button id="refresh-token" onclick="requestChallenge('refresh')">Refresh</button>
    </div>
  </div>
</div>
<%- include("partials/user_challenge_widget") %>
<script>
  function switchSection(sectionId) {
    const backToMenu = document.getElementById("back-to-menu");
    const captchaSection = document.getElementById("captcha-container");
    const requestSection = document.getElementById("request-container");
    const refreshSection = document.getElementById("refresh-container");
    [backToMenu, captchaSection, requestSection, refreshSection].forEach((element) => (element.style.display = "none"));
    switch (sectionId) {
      case "root":
        requestSection.style.display = "flex";
        maybeLoadExistingToken();
        break;
      case "captcha":
        captchaSection.style.display = "block";
        backToMenu.style.display = "block";
        break;
      case "refresh":
        refreshSection.style.display = "block";
        backToMenu.style.display = "block";
        document.getElementById("refresh-token-input").focus();
        break;
    }
  }

  function requestChallenge(action) {
    const savedToken = localStorage.getItem("captcha-temp-token");
    const refreshInput = document.getElementById("refresh-token-input").value;
    if (savedToken && action === "new") {
      const confirmation = confirm(
        "It looks like you might already have an existing token. Are you sure you want to request a new one?"
      );
      if (!confirmation) {
        return;
      }
      localStorage.removeItem("captcha-temp-token");
      document.getElementById("existing-token").style.display = "none";
      document.getElementById("refresh-token").disabled = true;
    } else if (!refreshInput?.length && action === "refresh") {
      alert("You need to provide a token to refresh.");
      return;
    }

    const refreshToken = action === "refresh" ? refreshInput : undefined;
    const keyInput = document.getElementById("proxy-key");
    const proxyKey = (keyInput && keyInput.value) || undefined;
    if (!proxyKey?.length) {
      localStorage.removeItem("captcha-proxy-key");
    } else {
      localStorage.setItem("captcha-proxy-key", proxyKey);
    }

    fetch("/user/captcha/challenge", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ action, proxyKey, refreshToken, _csrf: "<%= csrfToken %>" }),
    })
      .then((response) => response.json())
      .then(function (data) {
        if (data.error) {
          throw new Error(data.error);
        }
        const { challenge, signature } = data;
        loadNewChallenge(challenge, signature);
        switchSection("captcha");
      })
      .catch(function (error) {
        console.error(error);
        alert(`Error getting verification - ${error.message}`);
      });
  }

  function maybeLoadExistingToken() {
    const existingToken = localStorage.getItem("captcha-temp-token");
    if (existingToken) {
      const data = JSON.parse(existingToken);
      const { token, expires } = data;
      const expiresDate = new Date(expires);
      document.getElementById(
        "existing-token-value"
      ).textContent = `User token: ${token} (valid until ${expiresDate.toLocaleString()})`;
      document.getElementById("existing-token-value").style.display = "block";
      document.getElementById("refresh-token-input").value = token;
    }

    const proxyKey = localStorage.getItem("captcha-proxy-key");
    if (proxyKey && document.getElementById("proxy-key")) {
      document.getElementById("proxy-key").value = proxyKey;
    }
  }

  switchSection("root");
</script>

<%- include("partials/user_footer") %>