public function afterAuth() { $this->errorMessage = array(); if (!isset($_REQUEST['code'])) { $this->errorMessage[] = "This isn't redirected from the providers site."; return false; } $tokenID = $this->decodeIDToken($_REQUEST['code']); if ($tokenID === false || strlen($tokenID["username"]) < 1 || strlen($tokenID["email"]) < 1) { $this->errorMessage[] = "Nothing to get from the authenticating server. tokenID=" . var_export($tokenID, true); return false; } $this->userInfo = array("username" => $tokenID["username"], "realname" => $tokenID["realname"], "email" => $tokenID["email"]); $dbProxy = new DB_Proxy(); $dbProxy->initialize(null, null, null, false); $dbProxy->dbSettings->setLDAPExpiringSeconds(3600 * 24); $credential = $dbProxy->generateCredential(30); $param = array("username" => $tokenID["username"], "hashedpasswd" => $credential, "realname" => $tokenID["realname"], "email" => $tokenID["email"]); $this->isCreate = $dbProxy->dbClass->authSupportOAuthUserHandling($param); if ($this->debugMode) { $this->errorMessage[] = "authSupportOAuthUserHandling sends " . var_export($param, true) . ", returns {$this->isCreate}."; $this->errorMessage = array_merge($this->errorMessage, $dbProxy->logger->getDebugMessages()); } $this->errorMessage = array_merge($this->errorMessage, $dbProxy->logger->getErrorMessages()); $oAuthStoring = isset($_COOKIE["_im_oauth_storing"]) ? $_COOKIE["_im_oauth_storing"] : ""; $oAuthStoring = $oAuthStoring == 'session-storage' ? "true" : "false"; $oAuthRealm = isset($_COOKIE["_im_oauth_realm"]) ? $_COOKIE["_im_oauth_realm"] : ""; $this->jsCode = ''; $this->jsCode .= 'function setAnyStore(key, val) {'; $this->jsCode .= "var isSession = {$oAuthStoring}, realm = '{$oAuthRealm}';"; $this->jsCode .= 'var d, isFinish = false, ex = 3600, authKey;'; $this->jsCode .= 'd = new Date();d.setTime(d.getTime() + ex * 1000);'; $this->jsCode .= 'authKey = key + ((realm.length > 0) ? ("_" + realm) : "");'; $this->jsCode .= 'try {if (isSession){sessionStorage.setItem(authKey, val);isFinish = true;}}'; $this->jsCode .= 'catch(ex){}'; $this->jsCode .= 'if (!isFinish) {document.cookie = authKey + "=" + encodeURIComponent(val)'; $this->jsCode .= '+ ";path=/;" + "max-age=" + ex + ";expires=" + d.toUTCString() + ";"'; $this->jsCode .= '+ ((document.URL.substring(0, 8) == "https://") ? "secure;" : "")}}'; $this->jsCode .= "setAnyStore('_im_username', '" . $tokenID["username"] . "');"; $this->jsCode .= "setAnyStore('_im_credential', '" . $credential . "');"; $this->jsCode .= "setAnyStore('_im_openidtoken', '" . $this->id_token . "');"; if (count($this->errorMessage) < 1 && !(!$this->doRedirect && $this->isCreate)) { $this->jsCode .= "location.href = '" . $_COOKIE["_im_oauth_backurl"] . "';"; return true; } return true; }