gAuthCraft is a light microservice and flexible auth system for Google Oauth2 that works with gRPC and easily deploy with Docker and Jenkins.
This is a solution for both http side and gRPC side requests. Http server will listen to requests from users to handle them auth and gRPC server will handle internal requests and only return token validation and decoded data. So you can handle auth with Google provider easily. Also you can connect any app with any language to the gRPC and use it.
- User send HTTP request to HTTP server
- HTTP server get the request and check the
gAuth
token - If token is correct redirect user to the
SUCCESS_AUTH_REDIRECT_PATH
that is in env file. - If token is incorrect/expired will delete the
gAuth
token, then redirect user to Google 2auth server. After auth in Google it will send a request to your server. Then it will make a new token for user and redirect user to theSUCCESS_AUTH_REDIRECT_PATH
. - The server will save token as
gAuth
in cookies. - Default algorithm for signature is
HS256
but you can change it by passing algorithm totokenMaker
function. - This repo uses linksmith package to make URL easily.
gRPC server designed to work with application internal connection. You can connect it with any language based on auth.proto
file and verify user token with it.
Jenkins pipeline automates the build, test, and deployment process of application using Docker Compose. It includes the following stages:
- Build: Builds the Docker images for all services defined in
docker-compose.yml
. - Run Tests: Runs the test suite inside the
test
direction to verify the app's functionality. - Deploy: Deploys the application by starting all services in detached mode.
This pipeline ensures smooth CI/CD by automating the entire flow from building to deploying the application.
This repo use docker and Jenkins for deployment. So you have to have them in your system. For docker installation use this: Docker installation and for Jenkins you can use this docs: Jenkins on Docker. I used docker for easy deployment and jenkins for testing and deployment control for further development.
git clone https://github.com/yahya-aghdam/gAuthCraft.git
npm i
You can get your Google API key from Google API & Service: Google Oauth2
# Main URL
MAIN_URL=http://localhost
# api PATH
API_PATH=/api/g_auth
HTTP_PORT=3000
# Proto
PROTO_URL=0.0.0.0
PROTO_PORT=50051
# JWT secret key. It's better to be safe random 256bit string
JWT_SECRET=your_secret_key
COOKIE_DOMAIN=localhost
# Google
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-cient-secret
# Redirect path
SUCCESS_AUTH_REDIRECT_PATH=http://localhost:3000/api/g_auth/successful_auth
As you can see in package.json
file you can use these commands for development, test or deployment in the app
"scripts": {
"dev": "nodemon src/index.ts",
"test": "jest --verbose",
"build": "rm -rf ./dist && tsc",
"start": "npm run build && node dist/index.js",
"makeProto": "protoc --plugin=protoc-gen-ts_proto=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=. ./src/protos/auth.proto --ts_proto_opt=outputServices=grpc-js,esModuleInterop=true",
"docker:dev": "docker compose -f docker-compose.dev.yml up",
"docker:build": "docker compose up --build -d"
}
Scripts explanation:
- Run app without docker as development. It will refresh after any changes:
npm run dev
- Test app logic with
jest
based on files at./test
direction:
npm test
- Build app locally without docker:
npm run build
- Run app locally without docker:
npm run start
- If you made any changes in
auth.proto
file you have to make newauth.ts
file to handle and use it inauth.service.proto.ts
file. So you have to haveprotoc
in your system. To install Protocol Buffer Compiler on your system you can download last release and install it on your system: Download last protoc release
npm run makeProto
- If you want to develop app as docker container you can use this command to dockerize an run the app. You can see changes after any save without need to restart the app. If you close the command or terminal, app will be stop.
npm run docker:dev
- If you want to deploy app and run it in background.
npm run docker:build
This project is licensed under the MIT License - see the LICENSE file for details.