From 6e7ff522251832491855fe2f2c1a750b167bf898 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 4 Jul 2025 12:08:03 +0000 Subject: [PATCH 1/2] Initial plan From 6bd94c6d902c39b63f1cb4e9f3e088b84ad7e3b7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 4 Jul 2025 12:18:19 +0000 Subject: [PATCH 2/2] Complete OAuth template implementation for PHP Co-authored-by: somashaker23 <113500400+somashaker23@users.noreply.github.com> --- .env.example | 18 ++++ .gitignore | 26 +++++ README.md | 147 +++++++++++++++++++++++++- composer.json | 16 +++ src/config/oauth.php | 28 +++++ src/public/oauth-demo.html | 187 ++++++++++++++++++++++++++++++++++ src/public/oauth/callback.php | 90 ++++++++++++++++ src/public/oauth/login.php | 55 ++++++++++ src/public/oauth/logout.php | 22 ++++ src/public/oauth/user.php | 34 +++++++ 10 files changed, 621 insertions(+), 2 deletions(-) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/config/oauth.php create mode 100644 src/public/oauth-demo.html create mode 100644 src/public/oauth/callback.php create mode 100644 src/public/oauth/login.php create mode 100644 src/public/oauth/logout.php create mode 100644 src/public/oauth/user.php diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d05e915 --- /dev/null +++ b/.env.example @@ -0,0 +1,18 @@ +# OAuth Configuration Environment Variables +# Copy this file to .env and update with your actual values + +# Google OAuth Configuration +GOOGLE_CLIENT_ID=your_google_client_id_here +GOOGLE_CLIENT_SECRET=your_google_client_secret_here +GOOGLE_REDIRECT_URI=http://localhost:8080/oauth/callback.php + +# Application Configuration +APP_URL=http://localhost:8080 +APP_NAME="PHP API Seed with OAuth" + +# Database Configuration (if needed) +DB_HOST=localhost +DB_PORT=3306 +DB_NAME=appdb +DB_USER=root +DB_PASSWORD=rootpassword \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7034387 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# Dependencies +/vendor/ +composer.lock + +# Environment variables +.env + +# IDE files +.vscode/ +.idea/ +*.swp +*.swo + +# OS files +.DS_Store +Thumbs.db + +# Log files +*.log + +# Cache files +cache/ +tmp/ + +# Docker +docker-compose.override.yml \ No newline at end of file diff --git a/README.md b/README.md index 057fed8..386dd25 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,15 @@ -# PHP Seed Project in Docker +# PHP Seed Project with OAuth Template -This is a simple PHP project designed to run in a **Docker** container. It uses **PHP-FPM**, **MySQL** (with **phpMyAdmin** for database management), and Docker Compose to set up the environment. +This is a simple PHP project designed to run in a **Docker** container with **Google OAuth integration**. It uses **PHP-FPM**, **MySQL** (with **phpMyAdmin** for database management), and Docker Compose to set up the environment. + +## Features + +- ✅ Simple PHP API structure +- 🔐 Google OAuth integration template +- 🐳 Docker containerization +- 🗄️ MySQL database with phpMyAdmin +- 📝 Ready-to-use OAuth endpoints +- 🎨 Demo frontend for testing OAuth flow ## Prerequisites @@ -8,6 +17,7 @@ Before starting, make sure you have the following installed: - **Docker**: [Install Docker](https://docs.docker.com/get-docker/) - **Docker Compose**: [Install Docker Compose](https://docs.docker.com/compose/install/) +- **Composer**: [Install Composer](https://getcomposer.org/download/) ## Getting Started @@ -16,3 +26,136 @@ Before starting, make sure you have the following installed: ```bash git clone git@github.com:somashaker23/php-api-seed.git cd php-api-seed +``` + +### 2. Install Dependencies: + +```bash +composer install +``` + +### 3. Set Up OAuth Configuration: + +#### 3.1 Create Google OAuth Credentials: + +1. Go to [Google Cloud Console](https://console.cloud.google.com/) +2. Create a new project or select an existing one +3. Enable the **Google+ API** and **Google OAuth2 API** +4. Go to **Credentials** → **Create Credentials** → **OAuth 2.0 Client ID** +5. Configure the consent screen if prompted +6. Set application type to **Web application** +7. Add authorized redirect URI: `http://localhost:8080/oauth/callback.php` +8. Copy the **Client ID** and **Client Secret** + +#### 3.2 Update Configuration: + +Edit `src/config/oauth.php` and replace: +- `YOUR_GOOGLE_CLIENT_ID` with your actual Client ID +- `YOUR_GOOGLE_CLIENT_SECRET` with your actual Client Secret + +```php +'client_id' => 'your-actual-client-id-here', +'client_secret' => 'your-actual-client-secret-here', +``` + +### 4. Start the Application: + +#### Using Docker: + +```bash +docker compose up --build +``` + +#### Using PHP built-in server: + +```bash +php -S localhost:8080 -t src/public +``` + +## Usage + +### OAuth Demo + +Visit `http://localhost:8080/oauth-demo.html` to see the OAuth integration in action: + +- **Demo Page**: `http://localhost:8080/oauth-demo.html` +- **Login**: `http://localhost:8080/oauth/login.php` +- **User Profile**: `http://localhost:8080/oauth/user.php` +- **Logout**: `http://localhost:8080/oauth/logout.php` + +### API Endpoints + +- **Main API**: `http://localhost:8080/` - Returns basic API response +- **OAuth Login**: `http://localhost:8080/oauth/login.php` - Redirects to Google OAuth +- **OAuth Callback**: `http://localhost:8080/oauth/callback.php` - Handles OAuth callback +- **User Profile**: `http://localhost:8080/oauth/user.php` - Returns authenticated user info +- **Logout**: `http://localhost:8080/oauth/logout.php` - Clears session + +### Example OAuth Flow + +1. **Login**: Visit `/oauth/login.php` +2. **Authenticate**: User logs in with Google +3. **Callback**: Google redirects to `/oauth/callback.php` +4. **Profile**: Access user data via `/oauth/user.php` +5. **Logout**: Clear session with `/oauth/logout.php` + +## Development + +### File Structure + +``` +src/ +├── config/ +│ └── oauth.php # OAuth configuration +├── oauth/ +│ ├── login.php # OAuth login endpoint +│ ├── callback.php # OAuth callback handler +│ ├── user.php # User profile endpoint +│ └── logout.php # Logout endpoint +└── public/ + ├── index.php # Main API endpoint + └── oauth-demo.html # Demo frontend +``` + +### Adding OAuth to Your Project + +1. **Include the OAuth files** in your project +2. **Update configuration** in `src/config/oauth.php` +3. **Start OAuth flow** by redirecting to `/oauth/login.php` +4. **Handle user data** from the session after authentication + +### Session Management + +User data is stored in PHP sessions after successful authentication: + +```php +$_SESSION['user'] = [ + 'id' => $userInfo->getId(), + 'email' => $userInfo->getEmail(), + 'name' => $userInfo->getName(), + 'picture' => $userInfo->getPicture(), + 'verified_email' => $userInfo->getVerifiedEmail() +]; +``` + +## Docker Services + +- **PHP Application**: `http://localhost:8080` +- **phpMyAdmin**: `http://localhost:3000` +- **MySQL**: `localhost:3306` + +## Troubleshooting + +### Common Issues + +1. **"OAuth not configured"** - Update `src/config/oauth.php` with your Google OAuth credentials +2. **"Invalid redirect URI"** - Make sure the redirect URI in Google Console matches exactly +3. **"Access denied"** - Check if the Google+ API is enabled in your Google Cloud project +4. **Composer dependencies missing** - Run `composer install` + +### Security Notes + +- Never commit real OAuth credentials to version control +- Use environment variables for production deployments +- Always validate the `state` parameter in OAuth callbacks +- Implement proper session management for production use diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..40a4369 --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "somashaker23/php-api-seed", + "description": "PHP API Seed Project with OAuth Template", + "type": "project", + "require": { + "php": "^8.0", + "google/apiclient": "^2.15" + }, + "autoload": { + "psr-4": { + "App\\": "src/" + } + }, + "minimum-stability": "stable", + "prefer-stable": true +} \ No newline at end of file diff --git a/src/config/oauth.php b/src/config/oauth.php new file mode 100644 index 0000000..5c45fba --- /dev/null +++ b/src/config/oauth.php @@ -0,0 +1,28 @@ + [ + // Get these from Google Cloud Console: https://console.cloud.google.com/ + 'client_id' => 'YOUR_GOOGLE_CLIENT_ID', + 'client_secret' => 'YOUR_GOOGLE_CLIENT_SECRET', + 'redirect_uri' => 'http://localhost:8080/oauth/callback.php', + + // OAuth scopes - what permissions to request + 'scopes' => [ + 'openid', + 'email', + 'profile' + ], + + // OAuth endpoints + 'authorization_base_url' => 'https://accounts.google.com/o/oauth2/v2/auth', + 'token_url' => 'https://oauth2.googleapis.com/token', + 'userinfo_url' => 'https://www.googleapis.com/oauth2/v2/userinfo' + ] +]; \ No newline at end of file diff --git a/src/public/oauth-demo.html b/src/public/oauth-demo.html new file mode 100644 index 0000000..abf16c9 --- /dev/null +++ b/src/public/oauth-demo.html @@ -0,0 +1,187 @@ + + +
+ + +This demo shows Google OAuth integration for PHP developers.
+ + + +Click the button below to authenticate with Google OAuth:
+ 🔐 Login with Google +Test the OAuth API endpoints:
+ + + + +http://localhost:8080/oauth/callback.php as authorized redirect URIsrc/config/oauth.php with your credentialscomposer install to install dependencies