/**
  * @inheritdoc
  */
 public function getByAccessToken(AccessTokenEntity $entity)
 {
     $entity = new SessionEntity($this->server);
     $entity->setId('test');
     $entity->setOwner('test', 'test');
     return $entity;
 }
Example #2
0
 public function completeFlow($inputParams = null)
 {
     // Get the required params
     $clientId = $this->server->getRequest()->request->get('client_id', $this->server->getRequest()->getUser());
     if (is_null($clientId)) {
         throw new Exception\InvalidRequestException('client_id');
     }
     $clientSecret = $this->server->getRequest()->request->get('client_secret', $this->server->getRequest()->getPassword());
     if (is_null($clientSecret)) {
         throw new Exception\InvalidRequestException('client_secret');
     }
     // Validate client ID and client secret
     $client = $this->server->getClientStorage()->get($clientId, $clientSecret, null, $this->getIdentifier());
     if ($client instanceof ClientEntity === false) {
         $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidClientException();
     }
     $userId = call_user_func($this->getVerifyCredentialsCallback());
     if ($userId === false) {
         $this->server->getEventEmitter()->emit(new Event\UserAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidCredentialsException();
     }
     // Validate any scopes that are in the request
     $scopeParam = $this->server->getRequest()->request->get('scope', '');
     $scopes = $this->validateScopes($scopeParam, $client);
     // Create a new session
     $session = new SessionEntity($this->server);
     $session->setOwner('user', $userId);
     $session->associateClient($client);
     // Generate an access token
     $accessToken = new AccessTokenEntity($this->server);
     $accessToken->setId(SecureKey::generate());
     $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
     // Associate scopes with the session and access token
     foreach ($scopes as $scope) {
         $session->associateScope($scope);
     }
     foreach ($session->getScopes() as $scope) {
         $accessToken->associateScope($scope);
     }
     $this->server->getTokenType()->setSession($session);
     $this->server->getTokenType()->setParam('access_token', $accessToken->getId());
     $this->server->getTokenType()->setParam('expires_in', $this->getAccessTokenTTL());
     // Associate a refresh token if set
     if ($this->server->hasGrantType('refresh_token')) {
         $refreshToken = new RefreshTokenEntity($this->server);
         $refreshToken->setId(SecureKey::generate());
         $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
         $this->server->getTokenType()->setParam('refresh_token', $refreshToken->getId());
     }
     // Save everything
     $session->save();
     $accessToken->setSession($session);
     $accessToken->save();
     if ($this->server->hasGrantType('refresh_token')) {
         $refreshToken->setAccessToken($accessToken);
         $refreshToken->save();
     }
     return $this->server->getTokenType()->generateResponse();
 }
Example #3
0
 /**
  * Complete the password grant.
  *
  * @return array
  *
  * @throws
  */
 public function completeFlow()
 {
     $client = $this->getClient();
     $userId = $this->getUserId($this->server->getRequest(), $this->getVerifyCredentialsCallback());
     if ($userId === false) {
         $this->server->getEventEmitter()->emit(new UserAuthenticationFailedEvent($this->server->getRequest()));
         throw new InvalidCredentialsException();
     }
     // Create a new session
     $session = new SessionEntity($this->server);
     $session->setOwner('user', $userId);
     $session->associateClient($client);
     // Generate an access token
     $accessToken = new AccessTokenEntity($this->server);
     $accessToken->setId(SecureKey::generate());
     $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
     $this->server->getTokenType()->setSession($session);
     $this->server->getTokenType()->setParam('access_token', $accessToken->getId());
     $this->server->getTokenType()->setParam('expires_in', $this->getAccessTokenTTL());
     // Save everything
     $session->save();
     $accessToken->setSession($session);
     $accessToken->save();
     // Associate a refresh token if set
     if ($this->server->hasGrantType('refresh_token')) {
         $refreshToken = new RefreshTokenEntity($this->server);
         $refreshToken->setId(SecureKey::generate());
         $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
         $this->server->getTokenType()->setParam('refresh_token', $refreshToken->getId());
         $refreshToken->setAccessToken($accessToken);
         $refreshToken->save();
     }
     return $this->server->getTokenType()->generateResponse();
 }
 /**
  * @param Session $session
  *
  * @return SessionEntity
  */
 protected function createEntity(Session $session)
 {
     $entity = new SessionEntity($this->server);
     $entity->setId($session->getKey());
     $entity->setOwner($session->ownerType, $session->ownerId);
     return $entity;
 }
Example #5
0
 /**
  * Complete the client credentials grant
  *
  * @return array
  *
  * @throws
  */
 public function completeFlow()
 {
     $selfClient = app('selfClient');
     // Get the required params
     if (is_null($selfClient)) {
         throw new Exception\InvalidClientException();
     }
     // Validate client ID and client secret
     $client = $this->server->getClientStorage()->get($selfClient->id, $selfClient->secret, null, $this->getIdentifier());
     if ($client instanceof ClientEntity === false) {
         $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidClientException();
     }
     // Create a new session
     $session = new SessionEntity($this->server);
     $session->setOwner('client', $client->getId());
     $session->associateClient($client);
     // Generate an access token
     $accessToken = new AccessTokenEntity($this->server);
     $accessToken->setId(SecureKey::generate());
     $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
     foreach ($session->getScopes() as $scope) {
         $accessToken->associateScope($scope);
     }
     // Save everything
     $session->save();
     $accessToken->setSession($session);
     $accessToken->save();
     $oauthClient = new GenericProvider(['clientId' => $selfClient->id, 'clientSecret' => $selfClient->secret, 'redirectUri' => null, 'urlAuthorize' => null, 'urlAccessToken' => null, 'urlResourceOwnerDetails' => null]);
     $accessToken = new AccessToken(['access_token' => $accessToken->getId(), 'expires' => $accessToken->getExpireTime()]);
     return function ($method, $url, $options = []) use($oauthClient, $accessToken) {
         return $oauthClient->getAuthenticatedRequest($method, $url, $accessToken, $options);
     };
 }
 /**
  * {@inheritdoc}
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $result = M('oauth_sessions')->field('oauth_sessions.id, oauth_sessions.owner_type, oauth_sessions.owner_id, oauth_sessions.client_id, oauth_sessions.client_redirect_uri')->join('LEFT JOIN oauth_auth_codes ON oauth_auth_codes.session_id = oauth_sessions.id')->where(array('oauth_auth_codes.auth_code' => $authCode->getId()))->select();
     if (count($result) === 1) {
         $session = new SessionEntity($this->server);
         $session->setId($result[0]['id']);
         $session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
         return $session;
     }
     return;
 }
 /**
  * {@inheritdoc}
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $result = DB::table('oauth_sessions')->select(['oauth_sessions.id', 'oauth_sessions.owner_type', 'oauth_sessions.owner_id', 'oauth_sessions.client_id', 'oauth_sessions.client_redirect_uri'])->join('oauth_auth_codes', 'oauth_auth_codes.session_id', '=', 'oauth_sessions.id')->where('oauth_auth_codes.auth_code', $authCode->getId())->get();
     if (count($result) === 1) {
         $session = new SessionEntity($this->server);
         $session->setId($result[0]->id);
         $session->setOwner($result[0]->owner_type, $result[0]->owner_id);
         return $session;
     }
     return null;
 }
Example #8
0
 public function getByAccessToken(AccessTokenEntity $accessToken)
 {
     $accessToken = AccessToken::where('token', '=', $accessToken->getId())->first();
     if ($accessToken) {
         $_session = $accessToken->session;
         $session = new SessionEntity($this->server);
         $session->setId($_session->id);
         $session->setOwner($_session->owner_type, $_session->owner_id);
         return $session;
     }
 }
 /**
  * Get a session from an auth code
  * @param  \League\OAuth2\Server\Entity\AuthCodeEntity $authCode The auth code
  * @return \League\OAuth2\Server\Entity\SessionEntity
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $result = $this->db->fetchAll("SELECT s.id, s.owner_type, s.owner_id, s.client_id, s.client_redirect_uri FROM oauth_sessions s " . "JOIN oauth_auth_codes c ON c.session_id = s.id " . "WHERE c.auth_code = ?", Db::FETCH_ASSOC, [$authCode->getId()]);
     if (count($result) === 1) {
         $session = new SessionEntity($this->server);
         $session->setId($result[0]['id']);
         $session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
         return $session;
     }
     return NULL;
 }
 /**
  * {@inheritdoc}
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $result = $this->getConnection()->table('oauth_sessions')->select(['oauth_sessions.id', 'oauth_sessions.owner_type', 'oauth_sessions.owner_id', 'oauth_sessions.client_id', 'oauth_sessions.client_redirect_uri'])->join('oauth_auth_codes', 'oauth_auth_codes.session_id', '=', 'oauth_sessions.id')->where('oauth_auth_codes.auth_code', $authCode->getId())->first();
     if (!is_null($result)) {
         $session = new SessionEntity($this->server);
         $session->setId($result->id);
         $session->setOwner($result->owner_type, $result->owner_id);
         return $session;
     }
     return;
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $result = app('db')->table('oauth_session')->select(['oauth_session.id', 'oauth_session.owner_type', 'oauth_session.owner_id', 'oauth_session.client_id', 'oauth_session.client_redirect_uri'])->join('oauth_auth_code', 'oauth_auth_code.session_id', '=', 'oauth_session.id')->where('oauth_auth_code.auth_code', $authCode->getId())->first();
     if (is_object($result)) {
         $session = new SessionEntity($this->server);
         $session->setId($result->id);
         $session->setOwner($result->owner_type, $result->owner_id);
         return $session;
     }
     return;
 }
Example #12
0
 public function getByAuthCode(AuthCodeEntity $auth_code)
 {
     $sql = 'SELECT oauth_sessions.id, oauth_sessions.owner_type,' . ' oauth_sessions.owner_id, oauth_sessions.client_id,' . ' oauth_sessions.client_redirect_uri' . ' FROM oauth_sessions' . ' INNER JOIN oauth_authorization_codes' . ' ON oauth_authorization_codes.session_id = oauth_sessions.id' . ' WHERE oauth_authorization_codes.authorization_code = ?;';
     $results = $this->db->fetch($sql, [$auth_code->getId()]);
     if (count($results) === 1) {
         $session = new SessionEntity($this->server);
         $session->setId($result[0]['id']);
         $session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
         return $session;
     }
     return null;
 }
Example #13
0
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     // $result = Sessions::getOauthSessionByAuthCode($authCode->getId());
     // if (!empty($result)) {
     $session = new SessionEntity($this->server);
     //?
     $session->setId(1);
     $session->setOwner('client', 'testclient');
     return $session;
     // }
     // return;
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 public function getByAuthCode(AuthCodeEntity $authCodeEntity)
 {
     /** @var AuthCode $authCode */
     $authCode = $this->dm->getRepository(AuthCode::class)->find($authCodeEntity->getId());
     if ($authCode === null) {
         return null;
     }
     $session = $authCode->getSession();
     $sessionEntity = new SessionEntity($this->server);
     $sessionEntity->setId($session->getId());
     $sessionEntity->setOwner($session->getOwnerType(), $session->getOwnerId());
     return $sessionEntity;
 }
 /**
  * {@inheritdoc}
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $query = $this->db->createQueryBuilder()->select('s.id', 's.owner_type', 's.owner_id')->from('oauth_sessions', 's')->join('s', 'oauth_auth_codes', 'ac', 's.id = ac.session_id')->where('ac.auth_code = :authCode');
     $query->createNamedParameter($authCode->getId(), \PDO::PARAM_STR, ':authCode');
     $stmt = $query->execute();
     $result = $stmt->fetchAll();
     if (count($result) === 1) {
         $session = new SessionEntity($this->server);
         $session->setId($result[0]['id']);
         $session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
         return $session;
     }
 }
 /**
  * {@inheritdoc}
  *
  * @param \League\OAuth2\Server\Entity\AuthCodeEntity $authCode Auth code
  * @return \League\OAuth2\Server\Entity\SessionEntity
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $this->loadModel('OAuthServer.Sessions');
     $result = $this->Sessions->find()->matching('AuthCodes', function ($q) use($authCode) {
         return $q->where(['code' => $authCode->getId()]);
     })->first();
     if ($result) {
         $session = new SessionEntity($this->server);
         $session->setId($result->id);
         $session->setOwner($result->owner_model, $result->owner_id);
         return $session;
     }
 }
 public function testPutAndGetSessionByAccessToken()
 {
     $server = m::mock(AbstractServer::class);
     $server->shouldReceive('getEventEmitter->emit')->once();
     $session = new SessionEntity($server);
     $session->setOwner('owner', 1);
     $session->setId('id');
     $accessToken = new AccessTokenEntity($server);
     $accessToken->setId('my_token');
     $accessToken->setExpireTime(1);
     $this->cache->putSessionByAccessToken($accessToken, $session);
     $session = $this->cache->getSessionByAccessToken('my_token');
     $this->assertSame(['id' => 'id', 'owner_type' => 'owner', 'owner_id' => 1], $session);
 }
Example #18
0
 /**
  * Get a session from an auth code
  *
  * @param  \League\OAuth2\Server\Entity\AuthCodeEntity $authCode The auth code
  *
  * @return SessionEntity
  * @throws OAuthException
  */
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $querySessions = new Query();
     $session = null;
     $sessionResult = $querySessions->select(['{{%oauth_sessions}}.id as id', '{{%oauth_sessions}}.owner_type as owner_type', '{{%oauth_sessions}}.owner_id as owner_id', '{{%oauth_sessions}}.client_id as client_id', '{{%oauth_sessions}}.client_redirect_uri as redirect_uri'])->from('{{%oauth_sessions}}')->innerJoin('oauth_auth_codes', '{{%oauth_auth_codes}}.session_id={{%oauth_sessions}}.id')->where(['{{%oauth_auth_codes}}.auth_code' => $authCode->getId()])->one();
     if ($sessionResult) {
         $session = new SessionEntity($this->getServer());
         $session->setId($sessionResult['id']);
         $session->setOwner($sessionResult['owner_type'], $sessionResult['owner_id']);
         if (!$session->save()) {
             throw new OAuthException(json_encode($session->errors));
         }
     } else {
         throw new OAuthException(json_encode($sessionResult));
     }
     return $session;
 }
 public function getByAuthCode(AuthCodeEntity $authCode)
 {
     $rawData = $this->redis->get("auth_code:{$authCode->getId()}");
     if (!$rawData) {
         return null;
     }
     $data = json_decode($rawData, true);
     $sessionId = $data['session_id'];
     $rawData = $this->redis->get("session:{$data["session_id"]}");
     if (!$rawData) {
         return null;
     }
     $data = json_decode($rawData, true);
     $session = new SessionEntity($this->server);
     $session->setId($sessionId);
     $session->setOwner($data['owner_type'], $data['owner_id']);
     return $session;
 }
Example #20
0
 /**
  * Parse a new authorize request
  *
  * @param string $type       The session owner's type
  * @param string $typeId     The session owner's ID
  * @param array  $authParams The authorize request $_GET parameters
  *
  * @return string An authorisation code
  */
 public function newAuthorizeRequest($type, $typeId, $authParams = [])
 {
     // Create a new session
     $session = new SessionEntity($this->server);
     $session->setOwner($type, $typeId);
     $session->associateClient($authParams['client']);
     // Create a new auth code
     $authCode = new AuthCodeEntity($this->server);
     $authCode->setId(SecureKey::generate());
     $authCode->setRedirectUri($authParams['redirect_uri']);
     $authCode->setExpireTime(time() + $this->authTokenTTL);
     foreach ($authParams['scopes'] as $scope) {
         $authCode->associateScope($scope);
         $session->associateScope($scope);
     }
     $session->save();
     $authCode->setSession($session);
     $authCode->save();
     return $authCode->generateRedirectUri($authParams['state']);
 }
 /**
  * @param ResourceServer $resourceServer
  * @param EmitterInterface $emitter
  * @param string $ownerId
  */
 private function resourceServerWillReturnOwnerId(ResourceServer $resourceServer, EmitterInterface $emitter, $ownerId)
 {
     $resourceServer->getEventEmitter()->willReturn($emitter);
     $sessionEntity = new SessionEntity($resourceServer->getWrappedObject());
     $sessionEntity->setOwner('user', $ownerId);
     $accessTokenEntity = new AccessTokenEntity($resourceServer->getWrappedObject());
     $accessTokenEntity->setSession($sessionEntity);
     $resourceServer->getAccessToken()->willReturn($accessTokenEntity);
 }
 /**
  * Generate the redirect URI for the Implicit grant
  * @param $ownerType
  * @param $ownerId
  * @param $params
  * @return string
  * @throws Exception\InvalidClientException
  * @throws Exception\InvalidRequestException
  */
 public function getRedirectUri($ownerType, $ownerId, $params)
 {
     // Get required params
     if (!isset($params['client']) || $params['client'] instanceof ClientEntity === false) {
         $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
         throw new Exception\InvalidClientException();
     }
     $client = $params['client'];
     if (!isset($params['redirect_uri']) || is_null($params['redirect_uri'])) {
         throw new Exception\InvalidRequestException('redirect_uri');
     }
     $redirectUri = $params['redirect_uri'];
     // Create a new session
     $session = new SessionEntity($this->server);
     $session->setOwner($ownerType, $ownerId);
     $session->associateClient($client);
     // Generate the access token
     $accessToken = new AccessTokenEntity($this->server);
     $accessToken->setId(SecureKey::generate());
     $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
     if (isset($params['scopes'])) {
         foreach ($params['scopes'] as $implicitScope) {
             $session->associateScope($implicitScope);
         }
         foreach ($session->getScopes() as $scope) {
             $accessToken->associateScope($scope);
         }
     }
     $this->server->getTokenType()->setSession($session);
     $this->server->getTokenType()->setParam('access_token', $accessToken->getId());
     $this->server->getTokenType()->setParam('expires_in', $this->getAccessTokenTTL());
     // Save all the things
     $session->save();
     $accessToken->setSession($session);
     $accessToken->save();
     $token = $this->server->getTokenType()->generateResponse();
     if (isset($params['state']) && $params['state']) {
         $token['state'] = $params['state'];
     }
     return $params['redirect_uri'] . '#' . join('&', array_map(function ($v, $k) {
         return $k . '=' . $v;
     }, $token, array_keys($token)));
 }