コード例 #1
0
ファイル: ClientCredentials.php プロジェクト: uqiauto/fusio
    protected function generate(Credentials $credentials, $scope)
    {
        $sql = 'SELECT id,
				       userId
			      FROM fusio_app
			     WHERE appKey = :app_key
			       AND appSecret = :app_secret
			       AND status = :status';
        $app = $this->connection->fetchAssoc($sql, array('app_key' => $credentials->getClientId(), 'app_secret' => $credentials->getClientSecret(), 'status' => App::STATUS_ACTIVE));
        if (!empty($app)) {
            // validate scopes
            $scopes = $this->getValidScopes($app['id'], $scope);
            if (empty($scopes)) {
                throw new ServerErrorException('No valid scope given');
            }
            // generate access token
            $expires = new \DateTime();
            $expires->add(new \DateInterval('PT6H'));
            $now = new \DateTime();
            $accessToken = TokenGenerator::generateToken();
            $this->connection->insert('fusio_app_token', ['appId' => $app['id'], 'userId' => $app['userId'], 'status' => AppToken::STATUS_ACTIVE, 'token' => $accessToken, 'scope' => implode(',', $scopes), 'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', 'expire' => $expires->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), 'date' => $now->format($this->connection->getDatabasePlatform()->getDateTimeFormatString())]);
            $token = new AccessToken();
            $token->setAccessToken($accessToken);
            $token->setTokenType('bearer');
            $token->setExpiresIn($expires->getTimestamp());
            $token->setScope(implode(',', $scopes));
            return $token;
        } else {
            throw new ServerErrorException('Unknown user');
        }
    }
コード例 #2
0
ファイル: ApiExecutor.php プロジェクト: apioo/fusio-impl
 /**
  * @return string
  */
 protected function getAccessToken()
 {
     if (empty($this->accessToken)) {
         // insert access token
         $token = TokenGenerator::generateToken();
         $expire = new DateTime('+30 minute');
         $now = new DateTime();
         $this->connection->insert('fusio_app_token', ['appId' => 1, 'userId' => 1, 'status' => 1, 'token' => $token, 'scope' => 'backend', 'ip' => '127.0.0.1', 'expire' => $expire->format('Y-m-d H:i:s'), 'date' => $now->format('Y-m-d H:i:s')]);
         return $this->accessToken = $token;
     } else {
         return $this->accessToken;
     }
 }
コード例 #3
0
 public function testGenerateToken()
 {
     $this->assertEquals(80, strlen(TokenGenerator::generateToken()));
 }
コード例 #4
0
ファイル: App.php プロジェクト: apioo/fusio-impl
 public function generateAccessToken($appId, $userId, array $scopes, $ip, DateInterval $expire)
 {
     if (empty($scopes)) {
         throw new StatusCode\BadRequestException('No scopes provided');
     }
     $expires = new \DateTime();
     $expires->add($expire);
     $now = new \DateTime();
     // generate access token
     $accessToken = TokenGenerator::generateToken();
     $this->appTokenTable->create(['appId' => $appId, 'userId' => $userId, 'status' => Table\App\Token::STATUS_ACTIVE, 'token' => $accessToken, 'scope' => implode(',', $scopes), 'ip' => $ip, 'expire' => $expires, 'date' => $now]);
     $token = new AccessToken();
     $token->setAccessToken($accessToken);
     $token->setTokenType('bearer');
     $token->setExpiresIn($expires->getTimestamp());
     $token->setScope(implode(',', $scopes));
     return $token;
 }