diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e257593 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea +/.git \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dc4a467 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# Start from the official Go image +FROM golang:1.22-alpine + +# Set the working directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies +RUN go mod download + +# Copy the source code into the container +COPY . . + +# Build the application +RUN go build -o main . + +# Expose the port the app runs on +EXPOSE 8080 + +# Command to run the executable +CMD ["./main"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..51ffe72 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# OAuth Mock Server + +This is a simple OAuth 2.0 mock server implemented in Go. It's designed to simulate an OAuth 2.0 provider for testing purposes, especially in CI/CD environments. + +## Features + +- Simulates OAuth 2.0 authorization flow +- Provides JWT access tokens +- Returns static user information +- Easy to configure via environment variables +- Lightweight and easy to deploy + +## Prerequisites + +- Go 1.16 or higher +- Docker (optional, for containerized deployment) + +## Configuration + +The server can be configured using the following environment variables: + +- `PORT`: The port on which the server will run (default: 8080) +- `CLIENT_ID`: The client ID to use (default: "test-client") +- `CLIENT_SECRET`: The client secret to use (default: "test-secret") + +## Running the Server + +### Locally + +1. Clone the repository: + ``` + git clone https://github.com/yourusername/oauth-mock-server.git + cd oauth-mock-server + ``` + +2. Run the server: + ``` + go run main.go + ``` + +### Using Docker + +1. Build the Docker image: + ``` + docker build -t oauth-mock-server . + ``` + +2. Run the container: + ``` + docker run -p 8080:8080 -e CLIENT_ID=my-client -e CLIENT_SECRET=my-secret oauth-mock-server + ``` + +## Usage + +1. Initiate the OAuth flow by sending a GET request to `/authorize` with the following parameters: + - `client_id`: Your client ID + - `redirect_uri`: Your callback URL + - `state`: A random state value for security + +2. The server will display a consent screen and automatically approve after 2 seconds, redirecting to your `redirect_uri` with an authorization code. + +3. Exchange the authorization code for an access token by sending a POST request to `/token` with the following parameters: + - `grant_type`: "authorization_code" + - `code`: The authorization code received in step 2 + - `client_id`: Your client ID + - `client_secret`: Your client secret + +4. Use the received access token to make requests to the `/userinfo` endpoint by including it in the Authorization header: + ``` + Authorization: Bearer + ``` + +## License + +This project is licensed under the MIT License. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..38ef184 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/shaharia-lab/oauth-mock-server + +go 1.22 + +require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/google/uuid v1.6.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..24693e9 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=