Пример #1
0
 /**
  *	User Login
  *	Based on simple form.
  *
  *	@param	string	$payload
  *
  *	@return mixed
  */
 public static function login($payload)
 {
     # Add payload to GET
     $_GET = (array) $payload;
     # Validate client
     $server = Oauth2Verifier::getInstance()->getServer();
     $request = Oauth2Verifier::getInstance()->getRequest();
     $response = new Response();
     $client = self::validateClient($server, $request, $response);
     if (!$client || $client->getRedirectUri() != $payload->redirect_uri) {
         throw new \Cloudoki\InvalidParameterException('Invalid client id or redirect uri');
     }
     # Validate user
     if (!empty($payload->email)) {
         $user = User::email($payload->email)->first();
     } else {
         throw new \Cloudoki\InvalidParameterException('Invalid e-mail.');
     }
     if (isset($user) && !$user || !$user->checkPassword($payload->password)) {
         throw new \Cloudoki\InvalidParameterException('Invalid password or e-mail.');
     }
     # Validate Authorization
     $authorization = $user->oauth2authorizations()->where('client_id', $client->getClientId())->first();
     if (!$authorization) {
         $sessiontoken = Oauth2AccessToken::create(['access_token' => Oauth2AccessToken::generateAccessToken(), 'client_id' => $client->getClientId(), 'user_id' => $user->getId(), 'expires' => new Carbon('+ 2 minute', Config::get('app.timezone'))]);
         return ['view' => 'approve', 'session_token' => $sessiontoken->getToken(), 'user' => $user->schema('basic'), 'client' => $client->schema('basic')];
     }
     # Or return validated
     $accesstoken = $user->oauth2accesstokens()->validWithClient($client)->first() ?: Oauth2AccessToken::create(['access_token' => Oauth2AccessToken::generateAccessToken(), 'client_id' => $client->getClientId(), 'user_id' => $user->getId(), 'expires' => Carbon::now(new DateTimeZone(Config::get('app.timezone')))->addYear()]);
     return ['uri' => $client->getRedirectUri() . '?access_token=' . $accesstoken->getToken()];
 }
Пример #2
0
 /**
  *	Valid access
  *	Make sure the user has a valid access token.
  *
  *	@return boolean
  */
 public static function validAccess()
 {
     return Oauth2Verifier::isValid();
 }