/** * @param Session $session * @param Message $msg */ public function onMessage(Session $session, Message $msg) { if (!$session->isAuthenticated()) { if ($msg instanceof HelloMessage) { $this->manager->debug("got hello"); // send welcome message if ($this->sessions->contains($session)) { $this->manager->error("Connection tried to rejoin realm when it is already joined to the realm."); $session->sendMessage(ErrorMessage::createErrorMessageFromMessage($msg)); // TODO should shut down session here } else { $this->sessions->attach($session); $session->setRealm($this); $session->setState(Session::STATE_UP); // this should probably be after authentication if ($this->getAuthenticationManager() !== null) { $this->getAuthenticationManager()->onAuthenticationMessage($this, $session, $msg); } else { $session->setAuthenticated(true); $session->setAuthenticationDetails(AuthenticationDetails::createAnonymous()); // the broker and dealer should give us this information $roles = array("broker" => new \stdClass(), "dealer" => new \stdClass()); $session->sendMessage(new WelcomeMessage($session->getSessionId(), array("roles" => $roles))); } } } else { if ($msg instanceof AuthenticateMessage) { if ($this->getAuthenticationManager() !== null) { $this->getAuthenticationManager()->onAuthenticationMessage($this, $session, $msg); } else { // TODO: should shut down here probably $this->manager->error("Authenticate sent to realm without auth manager."); } } else { $this->manager->error("Unhandled message sent to unauthenticated realm: " . $msg->getMsgCode()); $session->sendMessage(new AbortMessage(new \stdClass(), "wamp.error.not_authorized")); $session->shutdown(); } } } else { $handled = false; /* @var $role AbstractRole */ foreach ($this->roles as $role) { if ($role->handlesMessage($msg)) { $role->onMessage($session, $msg); $handled = true; break; } } if (!$handled) { $this->manager->warning("Unhandled message sent to \"{$this->getRealmName()}\": {$msg->getSerializedMessage()}"); } } }
/** * Handles all messages for authentication (Hello and Authenticate) * This is called by the Realm to handle authentication * * @param \Thruway\Realm $realm * @param \Thruway\Session $session * @param \Thruway\Message\Message $msg * @throws \Exception */ public function onAuthenticationMessage(Realm $realm, Session $session, Message $msg) { if ($session->isAuthenticated()) { throw new \Exception("Message sent to authentication manager for already authenticated session."); } // trusted transports do not need any authentication if ($session->getTransport()->isTrusted()) { $authDetails = new AuthenticationDetails(); $authDetails->setAuthMethod('internalClient'); $authDetails->setAuthId('internal'); // set the authid if the hello has one if ($msg instanceof HelloMessage) { $details = $msg->getDetails(); if (isset($details)) { if (isset($details['authid'])) { $authDetails->setAuthId($details['authid']); } } } $authDetails->addAuthRole("authenticated_user"); $authDetails->addAuthRole("admin"); $session->setAuthenticationDetails($authDetails); $session->setAuthenticated(true); $session->sendMessage(new WelcomeMessage($session->getSessionId(), ['authid' => $authDetails->getAuthId(), 'authmethod' => $authDetails->getAuthMethod(), 'authrole' => $authDetails->getAuthRole(), 'authroles' => $authDetails->getAuthRoles()])); return; } if (!$this->readyToAuthenticate()) { $session->abort(new \stdClass(), 'thruway.authenticator.not_ready'); return; } if ($msg instanceof HelloMessage) { if ($session->getAuthenticationDetails() !== null) { // Todo: probably shouldn't be so dramatic here throw new \Exception("Hello message sent to authentication manager when there is already authentication details attached."); } $this->handleHelloMessage($realm, $session, $msg); } else { if ($msg instanceof AuthenticateMessage) { $this->handleAuthenticateMessage($realm, $session, $msg); } else { throw new \Exception("Invalid message type sent to AuthenticationManager."); } } }
/** * Handle process received message * * @param \Thruway\Session $session * @param \Thruway\Message\Message $msg */ public function onMessage(Session $session, Message $msg) { if ($msg instanceof GoodByeMessage) { Logger::info($this, "Received a GoodBye, so shutting the session down"); $session->sendMessage(new GoodbyeMessage(new \stdClass(), "wamp.error.goodbye_and_out")); $session->shutdown(); } elseif ($session->isAuthenticated()) { $this->processAuthenticated($session, $msg); } elseif ($msg instanceof AbortMessage) { $this->processAbort($session, $msg); } elseif ($msg instanceof HelloMessage) { $this->processHello($session, $msg); } elseif ($msg instanceof AuthenticateMessage) { $this->processAuthenticate($session, $msg); } else { Logger::error($this, "Unhandled message sent to unauthenticated realm: " . $msg->getMsgCode()); $session->abort(new \stdClass(), "wamp.error.not_authorized"); } }