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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | <?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