void ESP8266WebServerEx::handleLogin(const char* login, size_t loginLength, const char* homePath) {
  
  hasLogin = true;
  
  String session = GetSessionId();
  long tag = -1;
  bool validSession = false;

  if ( session.length()==32 ) validSession = g_Session.isValid(session,&tag);

  // if everything is OK and user already logged in
  if ( validSession && tag>=0 && tag<10 ) {
    redirect(homePath);
    return;
  }

  // if user never login before, just display a form with challenge string
  long challenge = random(11,0x7fffffff);
  if ( !hasArg("userid") || !hasArg("hashpw") ) {
    challengeString = createChallengeString(challenge);
    SetSessionId(g_Session.Create(challenge));
    sendEx(200, "text/html", login, loginLength, &loginForm);
    return;
  }  

  // user already provided userId and password
  String userId = arg("userid");
  String hashPw = arg("hashpw");

  challengeString = createChallengeString(tag);
  if ( userId==adminUser ) {
    if ( checkPassword(adminUser,g_ModuleSettings.GetLoginPassword(true).c_str(),challengeString,hashPw) ) {
      g_Session.SetTag(session,ADMIN_USER_ID);
      redirect(homePath);
      return;  
    }
  } else if ( userId==normalUser) {
    if ( checkPassword(normalUser,g_ModuleSettings.GetLoginPassword(false).c_str(),challengeString,hashPw) ) {
      g_Session.SetTag(session,USER_USER_ID);
      redirect(homePath);
      return;
    }
  }

  // show the login form with invalid userid/password here
  challengeString = createChallengeString(challenge);
  SetSessionId(g_Session.Create(challenge));
  sendEx(200, "text/html", login, loginLength, &loginFormWithError);
}
already_AddRefed<Promise>
MediaKeySession::Load(const nsAString& aSessionId, ErrorResult& aRv)
{
    nsRefPtr<Promise> promise(mKeys->MakePromise(aRv));
    if (aRv.Failed()) {
        return nullptr;
    }

    if (aSessionId.IsEmpty()) {
        promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
        // "The sessionId parameter is empty."
        return promise.forget();
    }

    if (!mUninitialized) {
        promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
        return promise.forget();
    }

    mUninitialized = false;

    // We now know the sessionId being loaded into this session. Remove the
    // session from its owning MediaKey's set of sessions awaiting a sessionId.
    nsRefPtr<MediaKeySession> session(mKeys->GetPendingSession(Token()));
    MOZ_ASSERT(session == this, "Session should be awaiting id on its own token");

    // Associate with the known sessionId.
    SetSessionId(aSessionId);

    mKeys->GetCDMProxy()->LoadSession(mKeys->StorePromise(promise), aSessionId);

    return promise.forget();
}
bool ESP8266WebServerEx::VerifyCookie(const char* mime) {
  if ( !hasLogin || strcmp(mime,"text/html")!=0 ) return true;

  String session = GetSessionId();
  long tag = -1;
  if ( session.length()==32 && g_Session.isValid(session,&tag) ) {
    if ( tag>0 && tag<10 ) {
      SetSessionId(session);
      return true;
    }
  }
  redirect("/"); 
  return false;    
}
Example #4
0
already_AddRefed<Promise>
MediaKeySession::Load(const nsAString& aSessionId, ErrorResult& aRv)
{
  nsRefPtr<DetailedPromise> promise(MakePromise(aRv,
    NS_LITERAL_CSTRING("MediaKeySession.load")));
  if (aRv.Failed()) {
    return nullptr;
  }

  if (aSessionId.IsEmpty()) {
    promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR,
                         NS_LITERAL_CSTRING("Trying to load a session with empty session ID"));
    // "The sessionId parameter is empty."
    EME_LOG("MediaKeySession[%p,''] Load() failed, no sessionId", this);
    return promise.forget();
  }

  if (!mUninitialized) {
    promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR,
                         NS_LITERAL_CSTRING("Session is already initialized in MediaKeySession.load()"));
    EME_LOG("MediaKeySession[%p,'%s'] Load() failed, uninitialized",
      this, NS_ConvertUTF16toUTF8(aSessionId).get());
    return promise.forget();
  }

  mUninitialized = false;

  // We now know the sessionId being loaded into this session. Remove the
  // session from its owning MediaKey's set of sessions awaiting a sessionId.
  nsRefPtr<MediaKeySession> session(mKeys->GetPendingSession(Token()));
  MOZ_ASSERT(session == this, "Session should be awaiting id on its own token");

  // Associate with the known sessionId.
  SetSessionId(aSessionId);

  PromiseId pid = mKeys->StorePromise(promise);
  mKeys->GetCDMProxy()->LoadSession(pid, aSessionId);

  EME_LOG("MediaKeySession[%p,'%s'] Load() sent to CDM, promiseId=%d",
    this, NS_ConvertUTF16toUTF8(mSessionId).get(), pid);

  return promise.forget();
}
/** Handle LBS request for MO-LR
@param aSessionId The session ID supplied by LBS.
*/
void CSuplMolrFsmSession::MoLrReq(const TLbsNetSessionId& aSessionId, const TLbsNetPosRequestOptionsBase& aOptions)
	{
	SUPLLOG(ELogP1, "CSuplMolrFsmSession::MoLrReq() Begin\n");

	// Initialise the new procedure
	InitialiseProcedure();
	
	// Store the supplied ID information
	SetSessionId(aSessionId);

	// Store MOLR Options needed for SUPL START
	//
	if (KErrNone != StoreLocationRequest(aOptions))
		CancelMachine(CSuplFsmSessionBase::ECancelClosing, CSuplFsmSessionBase::EReasonNone);

	// Perform a state transition
	PerformTransition();
	SUPLLOG(ELogP1, "CSuplMolrFsmSession::MoLrReq() End\n");
	}
already_AddRefed<Promise>
MediaKeySession::Load(const nsAString& aSessionId, ErrorResult& aRv)
{
  RefPtr<DetailedPromise> promise(MakePromise(aRv,
    NS_LITERAL_CSTRING("MediaKeySession.load")));
  if (aRv.Failed()) {
    return nullptr;
  }

  // 1. If this object is closed, return a promise rejected with an InvalidStateError.
  if (IsClosed()) {
    promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
                         NS_LITERAL_CSTRING("Session is closed in MediaKeySession.load()"));
    EME_LOG("MediaKeySession[%p,'%s'] Load() failed, closed",
      this, NS_ConvertUTF16toUTF8(aSessionId).get());
    return promise.forget();
  }

  // 2.If this object's uninitialized value is false, return a promise rejected
  // with an InvalidStateError.
  if (!mUninitialized) {
    promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
                         NS_LITERAL_CSTRING("Session is already initialized in MediaKeySession.load()"));
    EME_LOG("MediaKeySession[%p,'%s'] Load() failed, uninitialized",
      this, NS_ConvertUTF16toUTF8(aSessionId).get());
    return promise.forget();
  }

  // 3.Let this object's uninitialized value be false.
  mUninitialized = false;

  // 4. If sessionId is the empty string, return a promise rejected with a newly created TypeError.
  if (aSessionId.IsEmpty()) {
    promise->MaybeReject(NS_ERROR_DOM_TYPE_ERR,
                         NS_LITERAL_CSTRING("Trying to load a session with empty session ID"));
    // "The sessionId parameter is empty."
    EME_LOG("MediaKeySession[%p,''] Load() failed, no sessionId", this);
    return promise.forget();
  }

  // 5. If the result of running the Is persistent session type? algorithm
  // on this object's session type is false, return a promise rejected with
  // a newly created TypeError.
  if (mSessionType == MediaKeySessionType::Temporary) {
    promise->MaybeReject(NS_ERROR_DOM_TYPE_ERR,
                         NS_LITERAL_CSTRING("Trying to load() into a non-persistent session"));
    EME_LOG("MediaKeySession[%p,''] Load() failed, can't load in a non-persistent session", this);
    return promise.forget();
  }

  // Note: We don't support persistent sessions in any keysystem, so all calls
  // to Load() should reject with a TypeError in the preceding check. Omitting
  // implementing the rest of the specified MediaKeySession::Load() algorithm.

  // We now know the sessionId being loaded into this session. Remove the
  // session from its owning MediaKey's set of sessions awaiting a sessionId.
  RefPtr<MediaKeySession> session(mKeys->GetPendingSession(Token()));
  MOZ_ASSERT(session == this, "Session should be awaiting id on its own token");

  // Associate with the known sessionId.
  SetSessionId(aSessionId);

  PromiseId pid = mKeys->StorePromise(promise);
  mKeys->GetCDMProxy()->LoadSession(pid, aSessionId);

  EME_LOG("MediaKeySession[%p,'%s'] Load() sent to CDM, promiseId=%d",
    this, NS_ConvertUTF16toUTF8(mSessionId).get(), pid);

  return promise.forget();
}