um mal licht ins dunkle zu bringen. Ich arbeite gerade an einem LogIn Modul für OpenID -> respective für Wargaming OpenID.
Da mir kaum hier einer helfen konnte, liegt als Modul Grundlage die Beta von dustiii für twitter-api-anmeldung vor.
In der nutzt er um den user zu linken oder eine Verbindung zu Ilch herzustellen den AuthProider.
Nun Bekomm ich ja über OpenID keinen festen Token zurück, sondern folgende wehrte.
account_id, acces_token (welcher sich bei jeder anmeldung ändert) und dazu noch expired_at.
Somit gibt es ja auch im ILch das Modul unter user für Authtoken. Nur wie bekomm ich da einen Link zum angemelden Benutzer hin?
Ich kann euch gern die Auth.php geben, welche momentan funktioniert, aber leider mit authprovider.
Hoffe ihr könnt mir helfen, aus dem chaos ordnung zu schaffen.
<?php namespace Modules\wargamingauth\Controllers; use Ilch\Controller\Frontend; use Modules\wargamingauth\Libs\wargamingOAuth; use Modules\wargamingauth\Mappers\DbLog; use Modules\User\Mappers\AuthProvider; use Modules\User\Mappers\AuthToken; use Modules\User\Mappers\User as UserMapper; use Modules\User\Mappers\Group; use Modules\User\Models\AuthProviderUser; use Modules\User\Models\User; use Modules\User\Service\Password as PasswordService; use Ilch\Validation; class Auth extends Frontend { /** * @var DbLog instance */ protected $dbLog; /** * Renders the register form. */ public function registAction() { if (! array_dot($_SESSION, 'wargamingauth.login') || array_dot($_SESSION, 'wargamingauth.login.expires') < time()) { $this->addMessage('registExpired', 'danger'); $this->redirect(['module' => 'user', 'controller' => 'regist', 'action' => 'index']); } $oauth = array_dot($_SESSION, 'wargamingauth.login'); $this->getView()->set('rules', $this->getConfig()->get('regist_rules')); $this->getView()->set('user', $oauth); } /** * Saves the new user to the database. */ public function saveAction() { if (!$this->getRequest()->isPost()) { $this->addMessage('badRequest'); $this->redirect('/'); } if (! array_dot($_SESSION, 'wargamingauth.login') || array_dot($_SESSION, 'wargamingauth.login.expires') < time()) { $this->addMessage('badRequest'); $this->redirect(['module' => 'user', 'controller' => 'login', 'action' => 'index']); } $input = [ 'userName' => trim($this->getRequest()->getPost('userName')), 'email' => trim($this->getRequest()->getPost('email')), ]; $validation = Validation::create($input, [ 'userName' => 'required|unique:users,name', 'email' => 'required|email|unique:users,email', ]); if ($validation->isValid()) { // register user $registMapper = new UserMapper(); $groupMapper = new Group(); $userGroup = $groupMapper->getGroupById(2); $currentDate = new \Ilch\Date(); $user = (new User()) ->setName($input['userName']) ->setPassword((new PasswordService())->hash(PasswordService::generateSecurePassword(32))) ->setEmail($input['email']) ->setDateCreated($currentDate->format('Y-m-d H:i:s', true)) ->addGroup($userGroup) ->setDateConfirmed($currentDate->format('Y-m-d H:i:s', true)); $userId = $registMapper->save($user); $oauth = array_dot($_SESSION, 'wargamingauth.login'); $authProviderUser = (new AuthProviderUser()) ->setIdentifier($oauth['user_id']) ->setProvider('wargaming') ->setOauthToken($oauth['oauth_token']) ->setOauthTokenSecret($oauth['oauth_token_secret']) ->setScreenName($oauth['screen_name']) ->setUserId($userId); $link = (new AuthProvider())->linkProviderWithUser($authProviderUser); if ($link === true) { $_SESSION['user_id'] = $userId; $this->addMessage('wargamingauth.linksuccess'); $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'index']); } $this->addMessage('wargamingauth.linkfailed', 'danger'); $this->redirect('/'); } $this->addMessage($validation->getErrorBag()->getErrorMessages(), 'danger', true); $this->redirect() ->withInput() ->withErrors($validation->getErrorBag()) ->to(['action' => 'regist']); } public function unlinkAction() { if (loggedIn()) { if ($this->getRequest()->isPost()) { $authProvider = new AuthProvider(); $res = $authProvider->unlinkUser('wargaming', currentUser()->getId()); if ($res > 0) { $this->addMessage('wargamingauth.unlinkedsuccessfully'); $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } $this->addMessage('wargamingauth.couldnotunlink', 'danger'); $this->redirect('/'); } $this->addMessage('wargamingauth.badrequest', 'danger'); $this->redirect('/'); } $this->addMessage('wargamingauth.notauthenticated', 'danger'); $this->redirect('/'); } /** * Initialize authentication. */ public function indexAction() { $callbackUrl = $this->getLayout()->getUrl([ 'module' => 'wargamingauth', 'controller' => 'auth', 'action' => 'callback', ]); $auth = new wargamingOAuth( $this->getConfig()->get('wargamingauth_consumer_key'), 'popup', 1, null, $callbackUrl ); try { $auth->obtainTokens(); // var_dump($auth->getAuthenticationEndpoint()); // die(); $this->redirect($auth->getAuthenticationEndpoint()); // wargaming openid } catch (\Exception $e) { $this->addMessage('wargamingauth.authenticationfailure', 'danger'); if (loggedIn()) { $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } $this->redirect(['module' => 'user', 'controller' => 'login', 'action' => 'index']); } } /** * Callback action. */ public function callbackAction() { //var_dump($_GET); //die(); $auth = new wargamingOAuth( $this->getConfig()->get('wargamingauth_consumer_key') ); try { //var_dump($this->getRequest()); //die(); $auth->handleCallback($this->getRequest()); // var_dump($auth->getToken() . '<br>' . $auth->getAccount_id() . '<br>' . $auth->getExpires_at() . '<br>'); // var_dump($_GET['nickname']); // die(); // $auth->convertTokens(); $wargamingUser = array( 'user_id' => $auth->getAccount_id(), 'oauth_token' => $auth->getToken(), 'screen_name' => $_GET['nickname'], 'oauth_token_user' => null ); $authProvider = new AuthProvider(); $existingLink = $authProvider->providerAccountIsLinked('wargaming', $wargamingUser['user_id']); if (loggedIn()) { if ($authProvider->hasProviderLinked('wargaming', currentUser()->getId())) { $this->dbLog()->info( "User " . currentUser()->getName() . " had provider already linked.", [ 'userId' => currentUser()->getId(), 'userName' => currentUser()->getName(), 'wargamingAccount' => $wargamingUser ] ); $this->addMessage('providerAlreadyLinked', 'danger'); $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } if ($existingLink === true) { $this->dbLog()->info( "User " . currentUser()->getName() . " tried to link an already linked wargaming account.", [ 'userId' => currentUser()->getId(), 'userName' => currentUser()->getName(), 'wargamingAccount' => $wargamingUser ] ); $this->addMessage('accountAlreadyLinkedToDifferentUser', 'danger'); $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } $authProviderUser = (new AuthProviderUser()) ->setIdentifier($wargamingUser['user_id']) ->setProvider('wargaming') ->setOauthToken($wargamingUser['oauth_token']) ->setOauthTokenSecret($wargamingUser['oauth_token_user']) ->setScreenName($wargamingUser['screen_name']) ->setUserId(currentUser()->getId()); $link = $authProvider->linkProviderWithUser($authProviderUser); if ($link === true) { $this->dbLog()->info( "User " . currentUser()->getName() . " has linked a wargaming account.", [ 'userId' => currentUser()->getId(), 'userName' => currentUser()->getName(), 'wargamingAccount' => $wargamingUser ] ); $this->addMessage('linkSuccess'); $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } $this->dbLog()->error( "User " . currentUser()->getName() . " could not link his wargaming account.", [ 'userId' => currentUser()->getId(), 'userName' => currentUser()->getName(), 'wargamingAccount' => $wargamingUser ] ); $this->addMessage('linkFailed', 'danger'); $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } if ($existingLink === true) { $userId = $authProvider->getUserIdByProvider('wargaming', $wargamingUser['user_id']); if (is_null($userId)) { $this->addMessage('couldNotFindRequestedUser'); $this->redirect(['module' => 'user', 'controller' => 'login', 'action' => 'index']); } $_SESSION['user_id'] = $userId; $this->addMessage('loginSuccess'); $this->redirect('/'); } if ($existingLink === false && ! loggedIn() && ! $this->getConfig()->get('regist_accept')) { $this->addMessage('wargamingauth.messages.registrationNotAllowed', 'danger'); $this->redirect(['module' => 'user', 'controller' => 'login', 'action' => 'index']); } array_dot_set($_SESSION, 'wargamingauth.login', $wargamingUser); array_dot_set($_SESSION, 'wargamingauth.login.expires', strtotime('+5 minutes')); $this->redirect(['action' => 'regist']); } catch (\Exception $e) { $this->addMessage('wargamingauth.authenticationfailure', 'danger'); if (loggedIn()) { $this->redirect(['module' => 'user', 'controller' => 'panel', 'action' => 'providers']); } else { $this->redirect(['module' => 'user', 'controller' => 'login', 'action' => 'index']); } } } /** * @return DbLog */ protected function dbLog() { if ($this->dbLog instanceof DbLog) { return $this->dbLog; } return $this->dbLog = new DbLog(); } }
verwendete ilch Version: 2.1.x
betroffene Homepage: www.r1sing.de