Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

66
LINES

< > BotCompany Repo | #1011958 // standaloneWebAuth

JavaX fragment (include)

1  
static L<S> standaloneWebAuth_authorizedCookies;
2  
static Lock standaloneWebAuth_lock = lock();
3  
4  
// make sure that pw = params.get("pw")
5  
// returns pair(response, authorized)
6  
static Pair<S, Bool> standaloneWebAuth(S url, S cookie, S pw) {
7  
  lock standaloneWebAuth_lock;
8  
  
9  
  if (empty(cookie)) ret pair(null, false);
10  
11  
  S realPW = standaloneWebAuth_realPW();
12  
  
13  
  // No PW set?
14  
  
15  
  if (empty(realPW)) {
16  
    print("No password set by admin - please put password in: " + standaloneWebAuth_realPWFile().getAbsolutePath());
17  
    ret pair(null, false);
18  
  }
19  
    
20  
  if (nempty(pw)) {
21  
    if (eqic(pw, "logout")) {
22  
      standaloneWebAuth_authorizedCookies().remove(cookie);
23  
      ret pair("OK, logged out.", false);
24  
    }
25  
    if (neq(pw, realPW)) {
26  
      logQuotedWithDate("bad-login-attempts.txt", print("XXX - Alert, bad PW entered on web - XXX - IP: " + clientIP());
27  
      ret pair("Bad PW, bugger", false);
28  
    }
29  
    
30  
    standaloneWebAuth_authorizedCookies.add(cookie);
31  
    save("standaloneWebAuth_authorizedCookies");
32  
    ret pair(null, true);
33  
  }
34  
  
35  
  bool in = contains(standaloneWebAuth_authorizedCookies(), cookie);
36  
  if (in) ret pair(null, true);
37  
    
38  
  ret pair(null, false);
39  
}
40  
41  
static S standaloneWebAuth_showForm(S url, S cookie) {
42  
  bool in = nempty(cookie) && contains(standaloneWebAuth_authorizedCookies(), cookie);
43  
  S formContents = tag("p", "Enter deh pass word: " + htag("input", "", "type", "password", "name", "pw"));
44  
  formContents += tag("input", "", "type", "submit", "value", "Submit!");
45  
  ret p((in ? "You're in." : "You're not in.") + " " + (nempty(cookie) ? "Have cookie." : "No cookie."))
46  
    + tag("form", formContents, "method", "POST", "action", url)
47  
    + tag("form", hhidden("pw", "logout") + hsubmit("Log out"), 
48  
      "method", "POST", "action", url);
49  
}
50  
51  
static File standaloneWebAuth_realPWFile() {
52  
  ret secretProgramFile("master-pw");
53  
}
54  
55  
static S standaloneWebAuth_realPW() {
56  
  ret trim(loadTextFile(standaloneWebAuth_realPWFile()));
57  
}
58  
59  
static L<S> standaloneWebAuth_authorizedCookies() {
60  
  lock standaloneWebAuth_lock;
61  
  if (standaloneWebAuth_authorizedCookies == null) {
62  
    standaloneWebAuth_authorizedCookies = new L;
63  
    load("standaloneWebAuth_authorizedCookies");
64  
  }
65  
  ret standaloneWebAuth_authorizedCookies;
66  
}

Author comment

Began life as a copy of #1002590

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011958
Snippet name: standaloneWebAuth
Eternal ID of this version: #1011958/15
Text MD5: f927115560ed050859bee90a74b08699
Author: stefan
Category: eleu
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-06 15:38:52
Source code size: 2316 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 361 / 410
Version history: 14 change(s)
Referenced in: [show references]