From c8fbd64650b6181710e0f649b81780d8311754d8 Mon Sep 17 00:00:00 2001 From: Natan Evandro Date: Fri, 15 Sep 2017 08:42:55 -0300 Subject: [PATCH] Teste --- .gitignore | 1 + .htaccess | 9 + README.md | 54 ++-- bd/bd.sql | 39 +++ composer.json | 22 ++ composer.lock | 772 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.php | 211 ++++++++++++++ 7 files changed, 1082 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 .htaccess create mode 100644 bd/bd.sql create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 index.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a725465a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ \ No newline at end of file diff --git a/.htaccess b/.htaccess new file mode 100644 index 00000000..d1b713a6 --- /dev/null +++ b/.htaccess @@ -0,0 +1,9 @@ + + Options -MultiViews + + RewriteEngine On + #RewriteBase /path/to/app + RewriteCond %{REQUEST_FILENAME} !-d + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^ index.php [QSA,L] + \ No newline at end of file diff --git a/README.md b/README.md index 8fb16649..1af621d0 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,36 @@ -# A tarefa -Sua tarefa consiste em desenvolver uma API RESTful para manipular um determinado recurso. Deverá ser utilizado o framework Silex. +#README -# Requisitos -A escolha do recurso deverá ser feita pelo desenvolvedor, atendendo apenas os requisitos mínimos abaixo: +-Necessario dar um composer install em todas dependencias -* Deverá conter um ID -* Deverá conter pelo menos quatro propriedades (exemplos: nome, email, etc.) -* Deverá conter campos que armazenem as datas de criação e alteração do recurso +-Configurar banco de dados conforme seu ambiente. Utilieza o .sql que está na pasta BD. -A API deverá atender às seguintes exigências: +Modo de teste: -* Listagem de todos os recursos -* Busca de um recurso pelo ID -* Criação de um novo recurso -* Alteração de um recurso existente -* Exclusão de um recurso -* Aceitar e retornar apenas JSON -* Deverá possuir algum método de autenticação para utilização de seus endpoints +-Feito apenas o back-end. Necessario usar o postman para testar. -# Ferramentas -* PHP -* Banco de dados MySQL -* Framework Silex +-Usuario Natan já criado. Use o mesmo para testar. Vá na aba Body, utileze "raw" -> JSON(application/json) +Cole o seguinte json no campo abaixo: (Que é um usuario já existente no bd). -# Fluxo de desenvolvimento -1. Faça um fork deste repositório -2. Crie uma nova branch e nomeie-a com seu usuário do Github -3. Quando o desenvolvimento estiver concluído, faça um pull request + { -# Padrões de nomenclatura -1. Código fonte, nome do banco de dados, tabelas e campos devem estar em inglês + "id" : 1, -**Inclua no seu commit todos os arquivos necessários para que possamos testar o código.** + "name" : "Natan", + + "username" : "souzanatan", + + "password" : "natanpassword", + + "address" : "Rua Schnaider" + + } + +-Com ele, será obrigado gerar um token indo para: + + METHOD POST -> http://localhost/teste/generate-token (necessario configurar conforme seu ambiente) + +-Ele verificará se existe o mesmo existe no banco de dados para gerar token. (Sistema de login). + +-Gerado o token, vá até a aba HEADERS, acrescendo uma nova key chamada "AuthorizationKey", e o value você coloca "Bearer TokenQueVocêGerouVemAqui". Deixe ele habilitado. (Como se você tivesse logado e entrasse no sistema). + +-Agora você está autenticado para utilizar os endpoints. \ No newline at end of file diff --git a/bd/bd.sql b/bd/bd.sql new file mode 100644 index 00000000..5f22884e --- /dev/null +++ b/bd/bd.sql @@ -0,0 +1,39 @@ +/* +SQLyog Community v12.2.4 (64 bit) +MySQL - 5.6.35-81.0-log : Database - xavico +********************************************************************* +*/ + + +/*!40101 SET NAMES utf8 */; + +/*!40101 SET SQL_MODE=''*/; + +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +/*Table structure for table `users` */ + +DROP TABLE IF EXISTS `users`; + +CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) COLLATE latin1_general_ci NOT NULL, + `username` varchar(255) COLLATE latin1_general_ci NOT NULL, + `password` varchar(255) COLLATE latin1_general_ci NOT NULL, + `address` varchar(255) COLLATE latin1_general_ci NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; + +/*Data for the table `users` */ + +insert into `users`(`id`,`name`,`username`,`password`,`address`) values + +(1,'Natan','souzanatan','$2y$10$r97vqslfCT/D8TN6Eed8QuwuqeXuzF63oHQlvEVJRistobx1T8QN6','Rua Schnaider'); + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..fd520e56 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "repositories": [ + { + "type": "composer", + "url": "https://packagist.org" + }, + { "packagist": false } + ], + "name": "cod4b/teste-esc", + "description": "Teste", + "require": { + "silex/silex": "^2.2", + "twig/twig": "^1.34", + "lcobucci/jwt": "^3.2" + }, + "authors": [ + { + "name": "Natan", + "email": "natanevandro@gmail.com" + } + ] +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000..655fb02b --- /dev/null +++ b/composer.lock @@ -0,0 +1,772 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "8466fe44665bd79d5b0ebab34b002584", + "content-hash": "ac283f995db018d1b07a3c74b606defc", + "packages": [ + { + "name": "lcobucci/jwt", + "version": "3.2.2", + "source": { + "type": "git", + "url": "https://github.com/lcobucci/jwt.git", + "reference": "0b5930be73582369e10c4d4bb7a12bac927a203c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/lcobucci/jwt/zipball/0b5930be73582369e10c4d4bb7a12bac927a203c", + "reference": "0b5930be73582369e10c4d4bb7a12bac927a203c", + "shasum": "" + }, + "require": { + "ext-openssl": "*", + "php": ">=5.5" + }, + "require-dev": { + "mdanter/ecc": "~0.3.1", + "mikey179/vfsstream": "~1.5", + "phpmd/phpmd": "~2.2", + "phpunit/php-invoker": "~1.1", + "phpunit/phpunit": "~4.5", + "squizlabs/php_codesniffer": "~2.3" + }, + "suggest": { + "mdanter/ecc": "Required to use Elliptic Curves based algorithms." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1-dev" + } + }, + "autoload": { + "psr-4": { + "Lcobucci\\JWT\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Luís Otávio Cobucci Oblonczyk", + "email": "lcobucci@gmail.com", + "role": "Developer" + } + ], + "description": "A simple library to work with JSON Web Token and JSON Web Signature", + "keywords": [ + "JWS", + "jwt" + ], + "time": "2017-09-01 08:23:26" + }, + { + "name": "pimple/pimple", + "version": "v3.2.2", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Pimple.git", + "reference": "4d45fb62d96418396ec58ba76e6f065bca16e10a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Pimple/zipball/4d45fb62d96418396ec58ba76e6f065bca16e10a", + "reference": "4d45fb62d96418396ec58ba76e6f065bca16e10a", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "psr/container": "^1.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Pimple": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Pimple, a simple Dependency Injection Container", + "homepage": "http://pimple.sensiolabs.org", + "keywords": [ + "container", + "dependency injection" + ], + "time": "2017-07-23 07:32:15" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14 16:28:37" + }, + { + "name": "psr/log", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", + "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2016-10-10 12:19:37" + }, + { + "name": "silex/silex", + "version": "v2.2.0", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Silex.git", + "reference": "ec7d5b5334465414952d4b2e935e73bd085dbbbb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Silex/zipball/ec7d5b5334465414952d4b2e935e73bd085dbbbb", + "reference": "ec7d5b5334465414952d4b2e935e73bd085dbbbb", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "pimple/pimple": "~3.0", + "symfony/event-dispatcher": "~2.8|^3.0", + "symfony/http-foundation": "~2.8|^3.0", + "symfony/http-kernel": "~2.8|^3.0", + "symfony/routing": "~2.8|^3.0" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35 || >= 5.0, <5.4.3" + }, + "replace": { + "silex/api": "self.version", + "silex/providers": "self.version" + }, + "require-dev": { + "doctrine/dbal": "~2.2", + "monolog/monolog": "^1.4.1", + "swiftmailer/swiftmailer": "~5", + "symfony/asset": "~2.8|^3.0", + "symfony/browser-kit": "~2.8|^3.0", + "symfony/config": "~2.8|^3.0", + "symfony/css-selector": "~2.8|^3.0", + "symfony/debug": "~2.8|^3.0", + "symfony/doctrine-bridge": "~2.8|^3.0", + "symfony/dom-crawler": "~2.8|^3.0", + "symfony/expression-language": "~2.8|^3.0", + "symfony/finder": "~2.8|^3.0", + "symfony/form": "~2.8|^3.0", + "symfony/intl": "~2.8|^3.0", + "symfony/monolog-bridge": "~2.8|^3.0", + "symfony/options-resolver": "~2.8|^3.0", + "symfony/phpunit-bridge": "^3.2", + "symfony/process": "~2.8|^3.0", + "symfony/security": "~2.8|^3.0", + "symfony/serializer": "~2.8|^3.0", + "symfony/translation": "~2.8|^3.0", + "symfony/twig-bridge": "~2.8|^3.0", + "symfony/validator": "~2.8|^3.0", + "symfony/var-dumper": "~2.8|^3.0", + "symfony/web-link": "^3.3", + "twig/twig": "~1.28|~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Silex\\": "src/Silex" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + } + ], + "description": "The PHP micro-framework based on the Symfony Components", + "homepage": "http://silex.sensiolabs.org", + "keywords": [ + "microframework" + ], + "time": "2017-07-23 07:40:14" + }, + { + "name": "symfony/debug", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/debug.git", + "reference": "8beb24eec70b345c313640962df933499373a944" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/debug/zipball/8beb24eec70b345c313640962df933499373a944", + "reference": "8beb24eec70b345c313640962df933499373a944", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "psr/log": "~1.0" + }, + "conflict": { + "symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2" + }, + "require-dev": { + "symfony/http-kernel": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Debug\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Debug Component", + "homepage": "https://symfony.com", + "time": "2017-09-01 13:23:39" + }, + { + "name": "symfony/event-dispatcher", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/event-dispatcher.git", + "reference": "54ca9520a00386f83bca145819ad3b619aaa2485" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/54ca9520a00386f83bca145819ad3b619aaa2485", + "reference": "54ca9520a00386f83bca145819ad3b619aaa2485", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "conflict": { + "symfony/dependency-injection": "<3.3" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", + "symfony/expression-language": "~2.8|~3.0", + "symfony/stopwatch": "~2.8|~3.0" + }, + "suggest": { + "symfony/dependency-injection": "", + "symfony/http-kernel": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\EventDispatcher\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony EventDispatcher Component", + "homepage": "https://symfony.com", + "time": "2017-07-29 21:54:42" + }, + { + "name": "symfony/http-foundation", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-foundation.git", + "reference": "2cdc7de1921d1a1c805a13dc05e44a2cd58f5ad3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/2cdc7de1921d1a1c805a13dc05e44a2cd58f5ad3", + "reference": "2cdc7de1921d1a1c805a13dc05e44a2cd58f5ad3", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.1" + }, + "require-dev": { + "symfony/expression-language": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony HttpFoundation Component", + "homepage": "https://symfony.com", + "time": "2017-09-06 17:07:39" + }, + { + "name": "symfony/http-kernel", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-kernel.git", + "reference": "70f5bb3cdd737624249953b61023411e26be5db7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-kernel/zipball/70f5bb3cdd737624249953b61023411e26be5db7", + "reference": "70f5bb3cdd737624249953b61023411e26be5db7", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "psr/log": "~1.0", + "symfony/debug": "~2.8|~3.0", + "symfony/event-dispatcher": "~2.8|~3.0", + "symfony/http-foundation": "~3.3" + }, + "conflict": { + "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.3", + "symfony/var-dumper": "<3.3", + "twig/twig": "<1.34|<2.4,>=2" + }, + "require-dev": { + "psr/cache": "~1.0", + "symfony/browser-kit": "~2.8|~3.0", + "symfony/class-loader": "~2.8|~3.0", + "symfony/config": "~2.8|~3.0", + "symfony/console": "~2.8|~3.0", + "symfony/css-selector": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", + "symfony/dom-crawler": "~2.8|~3.0", + "symfony/expression-language": "~2.8|~3.0", + "symfony/finder": "~2.8|~3.0", + "symfony/process": "~2.8|~3.0", + "symfony/routing": "~2.8|~3.0", + "symfony/stopwatch": "~2.8|~3.0", + "symfony/templating": "~2.8|~3.0", + "symfony/translation": "~2.8|~3.0", + "symfony/var-dumper": "~3.3" + }, + "suggest": { + "symfony/browser-kit": "", + "symfony/class-loader": "", + "symfony/config": "", + "symfony/console": "", + "symfony/dependency-injection": "", + "symfony/finder": "", + "symfony/var-dumper": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpKernel\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony HttpKernel Component", + "homepage": "https://symfony.com", + "time": "2017-09-11 16:13:23" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.5.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2017-06-14 15:44:48" + }, + { + "name": "symfony/routing", + "version": "v3.3.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/routing.git", + "reference": "970326dcd04522e1cd1fe128abaee54c225e27f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/routing/zipball/970326dcd04522e1cd1fe128abaee54c225e27f9", + "reference": "970326dcd04522e1cd1fe128abaee54c225e27f9", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "conflict": { + "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.3", + "symfony/yaml": "<3.3" + }, + "require-dev": { + "doctrine/annotations": "~1.0", + "doctrine/common": "~2.2", + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", + "symfony/expression-language": "~2.8|~3.0", + "symfony/http-foundation": "~2.8|~3.0", + "symfony/yaml": "~3.3" + }, + "suggest": { + "doctrine/annotations": "For using the annotation loader", + "symfony/config": "For using the all-in-one router or any loader", + "symfony/dependency-injection": "For loading routes from a service", + "symfony/expression-language": "For using expression matching", + "symfony/http-foundation": "For using a Symfony Request object", + "symfony/yaml": "For using the YAML loader" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Routing\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Routing Component", + "homepage": "https://symfony.com", + "keywords": [ + "router", + "routing", + "uri", + "url" + ], + "time": "2017-07-29 21:54:42" + }, + { + "name": "twig/twig", + "version": "v1.34.4", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "f878bab48edb66ad9c6ed626bf817f60c6c096ee" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/f878bab48edb66ad9c6ed626bf817f60c6c096ee", + "reference": "f878bab48edb66ad9c6ed626bf817f60c6c096ee", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "psr/container": "^1.0", + "symfony/debug": "~2.7", + "symfony/phpunit-bridge": "~3.3@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.34-dev" + } + }, + "autoload": { + "psr-0": { + "Twig_": "lib/" + }, + "psr-4": { + "Twig\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + }, + { + "name": "Twig Team", + "homepage": "http://twig.sensiolabs.org/contributors", + "role": "Contributors" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "http://twig.sensiolabs.org", + "keywords": [ + "templating" + ], + "time": "2017-07-04 13:19:31" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/index.php b/index.php new file mode 100644 index 00000000..025538ab --- /dev/null +++ b/index.php @@ -0,0 +1,211 @@ + ['/testeESC/generate-token'], + 'jwt' => ['secret' => 'AS109238ALSKFD'] +]; + +$app->before(function (Request $request, $app) { + $uri = $request->getRequestUri(); + $allowedURIs = $app['security']['allowedURIs']; + + + if (in_array($uri, $allowedURIs)) { + return; + } + if (!$request->headers->has('authorizationkey')) { + return new Response('', 401, ['WWW-Authenticate' => 'Bearer']); + } + + /* @var string[] $tokens */ + $tokens = array_filter(explode(' ', $request->headers->get('authorizationkey'), 2)); + + if (count($tokens) !== 2) { + return new Response('', 401, ['WWW-Authenticate' => 'Bearer']); + } + + if ('Bearer' !== ucfirst($tokens[0])) { + return new Response('', 401, ['WWW-Authenticate' => 'Bearer']); + } + + /* @var string $tokenString */ + $tokenString = $tokens[1]; + $tokenParser = new Parser(); + + try { + $token = $tokenParser->parse($tokenString); + } catch (\InvalidArgumentException $e) { + return new Response('', 403); + } + + $tokenSigner = new Sha256(); + $jwtKey = $app['security']['jwt']['secret']; + $didVerifyAndValidate = ( + $token->validate(new ValidationData()) && + $token->verify($tokenSigner, $jwtKey) + ); + + if (!$didVerifyAndValidate) { + return new Response('', 401); + } +}); + +$app->post('/generate-token', function () use ($app) { + $data = json_decode(file_get_contents('php://input'), true); + $db = $app['connection']; + + $sql = "SELECT * FROM users where username='".$data['username']."'"; + $stmt = $db->query($sql); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$user) { + return new Response(400); + } + + if (! password_verify($data['password'], $user['password'])) { + return new Response(400); + } + + $builder = new Builder(); + + $builder->setIssuedAt(time()); + $builder->setNotBefore(time()); + $builder->set('userId', $user['id']); + + $tokenSigner = new Sha256(); + $jwtKey = $app['security']['jwt']['secret']; + + $builder->sign($tokenSigner, $jwtKey); + + return (string) $builder->getToken(); + }); + +$app->get('/users', function(Request $request) use ($app){ + + $sql = 'SELECT * FROM users'; + + try { + /* @var $db PDO */ + $db = $app['connection']; + $stmt = $db->query($sql); + $return = $stmt->fetchAll(PDO::FETCH_OBJ); + + return new JsonResponse($return); + + } catch(\PDOException $e) { + return new Response(400, ['message' => $e->getMessage()]); + }; + +}); + +$app->get('users/{id}', function($id) use ($app){ + + $sql = "SELECT * FROM users where id='".$id."'"; + + try { + /* @var $db PDO */ + $db = $app['connection']; + $stmt = $db->query($sql); + $return = $stmt->fetch(PDO::FETCH_OBJ); + + return new JsonResponse($return); + + } catch(\PDOException $e) { + return new Response(400, ['message' => $e->getMessage()]); + } + +}); + +$app->post('/users', function(Request $request) use ($app){ + $data = json_decode(file_get_contents('php://input'), true); + $hash = password_hash($data['password'], PASSWORD_DEFAULT); + $sql = 'INSERT INTO users(name, username, password, address)VALUES(?, ?, ?, ?)'; + + try { + /* @var $db PDO */ + $db = $app['connection']; + $stmt = $db->prepare($sql); + $paramOrder = 1; + + $stmt->bindParam($paramOrder++, $data['name']); + $stmt->bindParam($paramOrder++, $data['username']); + $stmt->bindParam($paramOrder++, $hash); + $stmt->bindParam($paramOrder++, $data['address']); + + $stmt->execute(); + + return new Response(); + + } catch(\PDOException $e) { + return new Response(400, ['message' => $e->getMessage()]); + } + +}); + +$app->put('/users', function(Request $request) use ($app){ + + $data = json_decode(file_get_contents('php://input'), true); + $hash = password_hash($data['password'], PASSWORD_DEFAULT); + $sql = "UPDATE users SET name= ?, username = ?, password = ?, address = ? WHERE id = ?"; + + try { + /* @var $db PDO */ + $db = $app['connection']; + $stmt = $db->prepare($sql); + $paramOrder = 1; + + $stmt->bindParam($paramOrder++, $data['name']); + $stmt->bindParam($paramOrder++, $data['username']); + $stmt->bindParam($paramOrder++, $hash); + $stmt->bindParam($paramOrder++, $data['address']); + $stmt->bindParam($paramOrder++, $data['id']); + + $stmt->execute(); + + return new Response(); + + } catch(\PDOException $e) { + return new Response(400, ['message' => $e->getMessage()]); + } +}); + +$app->delete('/users/{id}', function($id) use ($app){ + $sql = 'DELETE FROM users WHERE id = ?'; + + try { + /* @var $db PDO */ + $db = $app['connection']; + $stmt = $db->prepare($sql); + $paramOrder = 1; + + $stmt->bindParam($paramOrder++, $id); + + $stmt->execute(); + + return new Response(); + + } catch (\PDOException $e) { + return new Response(400, ['message' => $e->getMessage()]);; + } +}); + +$app->run(); \ No newline at end of file