Skip to content

tpushkarsingh/auth-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth Server

This is the Authorization Server for the KitchenSink application. It provides authentication and token generation using Spring Authorization Server with OAuth2 and PKCE.

🚀 Features

  • OAuth2 Authorization Code Flow with PKCE
  • JWT-based authentication
  • User authentication with MongoDB
  • Role-based access control (Admin, Client)
  • CORS support for frontend integration
  • Docker support

🛠️ Tech Stack

  • Java 21
  • Spring Boot 3.x
  • Spring Security OAuth2 Authorization Server
  • MongoDB
  • Docker

🏗️ Project Structure

/auth-server
│── src/main/java/com/mongo/auth_server
│   ├── config/               # Security & Authorization Configuration
│   ├── controller/           # User API Controllers
│   ├── model/                # User Entity
│   ├── repository/           # MongoDB Repositories
│   ├── service/              # Authentication Services
│── src/main/resources
│   ├── application.properties  # Application configuration
│── Dockerfile                 # Docker setup
│── pom.xml                     # Maven dependencies

📌 Prerequisites

  • Java 21
  • Maven 3+
  • MongoDB running on Docker
  • Docker (if running in containers)

⚙️ Setup & Configuration

1️⃣ Update MongoDB Connection

Ensure MongoDB is running and update application.properties with the correct database URL:

spring.data.mongodb.uri=mongodb://admin:adminpassword@localhost:27017/auth-db?authSource=admin

2️⃣ Build & Run Locally

# Clone the repository
git clone <repo-url>
cd auth-server

# Build the project
mvn clean install

# Run the server
mvn spring-boot:run

3️⃣ Run with Docker

# Build Docker Image
docker build -t auth-server .

# Run Container
docker run -p 9000:9000 --name auth-server auth-server

4️⃣ Run with Docker Compose

If using docker-compose.yml, start all services together:

docker-compose up -d

4️⃣ Run locally in IDE

Run the AuthServerApplication.java file which will start the project on port 9000

🔑 API Endpoints

User Authentication & Token Generation

Method Endpoint Description
GET /login Login Page
POST /oauth2/token Generate Access Token
GET /oauth2/authorize Authorization Endpoint
GET /api/user/role Fetch User Role
GET /logout Logout & Redirect

🔐 Testing Authorization Flow

1️⃣ Get Authorization Code

Visit the following URL in your browser:

http://localhost:9000/oauth2/authorize?response_type=code&client_id=public-client-react-app&redirect_uri=http://localhost:3000/login/oauth2/code/public-client-react-app&scope=openid

After logging in, copy the code from the redirect URL.

2️⃣ Exchange Code for Access Token

Run the following curl command:

curl --location 'http://localhost:9000/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_id=public-client-react-app' \
--data-urlencode 'redirect_uri=http://localhost:3000/login/oauth2/code/public-client-react-app' \
--data-urlencode 'code=<YOUR_AUTHORIZATION_CODE>'

This will return an access token that can be used for authentication.

🛑 Stopping the Server

Locally

Press CTRL + C

Docker

docker stop auth-server
docker rm auth-server

Docker Compose

docker-compose down

📜 License

This project is licensed under the Apache License 2.0.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published