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

66
LINES

< > BotCompany Repo | #1011958 // standaloneWebAuth

JavaX fragment (include)

static L<S> standaloneWebAuth_authorizedCookies;
static Lock standaloneWebAuth_lock = lock();

// make sure that pw = params.get("pw")
// returns pair(response, authorized)
static Pair<S, Bool> standaloneWebAuth(S url, S cookie, S pw) {
  lock standaloneWebAuth_lock;
  
  if (empty(cookie)) ret pair(null, false);

  S realPW = standaloneWebAuth_realPW();
  
  // No PW set?
  
  if (empty(realPW)) {
    print("No password set by admin - please put password in: " + standaloneWebAuth_realPWFile().getAbsolutePath());
    ret pair(null, false);
  }
    
  if (nempty(pw)) {
    if (eqic(pw, "logout")) {
      standaloneWebAuth_authorizedCookies().remove(cookie);
      ret pair("OK, logged out.", false);
    }
    if (neq(pw, realPW)) {
      logQuotedWithDate("bad-login-attempts.txt", print("XXX - Alert, bad PW entered on web - XXX - IP: " + clientIP());
      ret pair("Bad PW, bugger", false);
    }
    
    standaloneWebAuth_authorizedCookies.add(cookie);
    save("standaloneWebAuth_authorizedCookies");
    ret pair(null, true);
  }
  
  bool in = contains(standaloneWebAuth_authorizedCookies(), cookie);
  if (in) ret pair(null, true);
    
  ret pair(null, false);
}

static S standaloneWebAuth_showForm(S url, S cookie) {
  bool in = nempty(cookie) && contains(standaloneWebAuth_authorizedCookies(), cookie);
  S formContents = tag("p", "Enter deh pass word: " + htag("input", "", "type", "password", "name", "pw"));
  formContents += tag("input", "", "type", "submit", "value", "Submit!");
  ret p((in ? "You're in." : "You're not in.") + " " + (nempty(cookie) ? "Have cookie." : "No cookie."))
    + tag("form", formContents, "method", "POST", "action", url)
    + tag("form", hhidden("pw", "logout") + hsubmit("Log out"), 
      "method", "POST", "action", url);
}

static File standaloneWebAuth_realPWFile() {
  ret secretProgramFile("master-pw");
}

static S standaloneWebAuth_realPW() {
  ret trim(loadTextFile(standaloneWebAuth_realPWFile()));
}

static L<S> standaloneWebAuth_authorizedCookies() {
  lock standaloneWebAuth_lock;
  if (standaloneWebAuth_authorizedCookies == null) {
    standaloneWebAuth_authorizedCookies = new L;
    load("standaloneWebAuth_authorizedCookies");
  }
  ret standaloneWebAuth_authorizedCookies;
}

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: 359 / 408
Version history: 14 change(s)
Referenced in: [show references]