Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ ormconfig.json
# Local env files #
###################
.local.env
.local.env.*
.local.env.*

# idea #
##########
.idea/
33 changes: 33 additions & 0 deletions @core/authentication-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
10 changes: 10 additions & 0 deletions @core/authentication-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM maven:3.8.3-openjdk-17 as build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package

FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=build /app/target/authentication-service-0.0.1-SNAPSHOT.jar /app
ENTRYPOINT ["java","-jar","/app/authentication-service-0.0.1-SNAPSHOT.jar"]
167 changes: 167 additions & 0 deletions @core/authentication-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# JWT Token-Based Authentication Service
This README provides information about a JWT token-based authentication service.

### Features:
1. One-time refresh token / refresh token rotation
(using the refresh token multiple times will invalidate the entire refresh token family.)
2. Access token
3. User registration
4. User Login

### Tech Stacks:

1. Spring Boot 3.2.0
2. Java 17
3. Spring Security
4. PostgreSQL for user information
5. Redis for refresh token

## How to Run
### 1. Set up Secret_Key environment
**Replace the Secret key below in production.**

```bash
export SECRET_KEY='404E635266556A586E3272357538782F413F4428472B4B6250645367566B5970'
```

### 2. Docker Compose Up

To run the service, use Docker:

```bash
docker-compose up
```



## APIs

### 1. Register

Register a new user with the service,
and retrieve an accessToken and a refreshToken.

- Curl Command:

```bash
curl -X POST http://localhost:8080/api/v1/auth/register \
-H 'Content-Type: application/json' \
-d '{"firstname": "fname", "lastname": "lname", "loginEmail": "test@test.com", "password": "1234"}'
```

- Typical Response:

```json
{
"refreshToken": "new token",
"accessToken": "new token",
"message": "Succeed."
}
```

### 2. Authenticate with Email address and Password

Authenticate a user with Email address and Password,
and retrieve an access token and a refresh token.

- Curl Command:

```bash
curl -X POST http://localhost:8080/api/v1/auth/authenticate \
-H 'Content-Type: application/json' \
-d '{"loginEmail": "test@test.com", "password": "1234"}'
```

- Typical Response:

```json
{
"refreshToken": "new token",
"accessToken": "new token",
"message": "Succeed."
}
```

### 3. Get Access Token

Get a new AccessToken using a one time RefreshToken,
it won't revoke the old AccessToken, but will revoke the one time RefreshToken.

- Curl Command:

```bash
curl -X POST http://localhost:8080/api/v1/auth/get-access-token \
-H 'Content-Type: application/json' \
-d '{"refreshToken": "refresh-token"}'
```

- Typical Response:

```json
{
"refreshToken": "new token",
"accessToken": "new token",
"message": "Succeed."
}
```

```json
{
"refreshToken": null,
"accessToken": null,
"message": "The refresh token has exceeded the usage limit."
}
```

### 4. Revoke Refresh Token

Revoke a RefreshToken by sending the token you want to revoke.

- Curl Command:

```bash
curl -X POST http://localhost:8080/api/v1/auth/revoke-refresh-token \
-H 'Content-Type: application/json' \
-d '{"refreshToken": "token"}'
```

- Typical Response:

```json
{
"refreshToken": "null",
"accessToken": "null",
"message": "Revoked."
}
```

### 5. PingPong (Authenticated Endpoint)

Check the service status. Requires the user to be logged in.

- **Endpoint:** `GET http://localhost:8080/api/v1/ping`

- Curl Command:

```bash
curl -X GET http://localhost:8080/api/v1/ping \
-H 'Authorization: Bearer [Your JWT Token]'
```

- Typical Response:

```
Pong
```



## Security Notes
Ensure that your application secrets and database credentials are securely managed and not hard-coded or exposed in your code or Docker configuration. Use environment variables or secure secret management solutions.



## To-Do List

1. **Read the Secret Key from the Environment:** Modify the application to securely read the `SECRET_KEY` used for JWT token generation and verification from the environment variables. This approach enhances security by avoiding hard-coding sensitive information.
2. **Respond with Refreshing Token:** Implement functionality to issue a refreshing token alongside the access token during the authentication process. This token can be used by clients to obtain a new access token when the current one expires, without requiring user credentials again.
33 changes: 33 additions & 0 deletions @core/authentication-service/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3.1'

services:
some-postgres:
image: postgres
environment:
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_DB: flowio-authentication
ports:
- 5432:5432
networks:
- flowio-auth-net

redis:
image: redis:latest
ports:
- 6379:6379
networks:
- flowio-auth-net

authentication-service:
build: .
ports:
- 8080:8080
networks:
- flowio-auth-net
environment:
- SECRET_KEY=${SECRET_KEY}



networks:
flowio-auth-net:
Loading