Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fsobh/auth


https://github.com/fsobh/auth

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

Authentication service

- Supports **HTTP/2** and **gRPC** protocols.
- PostgreSQL database with **sqlc** for type-safe queries.
- Redis queue for background tasks.


db diagram

## ๐Ÿ”— Table of Contents

- [๐Ÿ“ Overview](#-overview)
- [๐Ÿ‘พ Features](#-features)
- [๐Ÿ“ Project Structure](#-project-structure)
- [๐Ÿ“‚ Project Index](#-project-index)
- [๐Ÿš€ Getting Started](#-getting-started)
- [โ˜‘๏ธ Prerequisites](#-prerequisites)
- [โš™๏ธ Installation](#-installation)
- [๐Ÿค– Usage](#๐Ÿค–-usage)
- [๐Ÿงช Testing](#๐Ÿงช-testing)
- [๐Ÿ“Œ Project Roadmap](#-project-roadmap)
- [๐Ÿ”ฐ Contributing](#-contributing)
- [๐ŸŽ— License](#-license)
- [๐Ÿ™Œ Acknowledgments](#-acknowledgments)

---

## ๐Ÿ“ Overview

A Golang authentication service API, supporting both HTTP/2 and gRPC protocols.
The service is designed to provide user authentication and authorization features,
including user registration, login, and email verification.
The service is built
using a PostgreSQL database with sqlc for type-safe queries and a Redis queue for background tasks.

---

## ๐Ÿ‘พ Features

- **User Registration**: Register a new user with a unique email, username, and password.
- **Email Verification**: Sends a verification email to a user's email address. **User creation will rollback if verification email fails to send (using transactional queries with callbacks)**
- **User Login**: Authenticate a user with a valid email and password.
- **User Update**: Update a user's profile information, including their email, username, and password.

---

## ๐Ÿ“ Project Structure

```sh
โ””โ”€โ”€ auth/
โ”œโ”€โ”€ db
โ”‚ โ”œโ”€โ”€ migration
โ”‚ โ”œโ”€โ”€ query
โ”‚ โ””โ”€โ”€ sqlc
โ”œโ”€โ”€ doc
โ”‚ โ”œโ”€โ”€ db.dbml
โ”‚ โ”œโ”€โ”€ statik
โ”‚ โ””โ”€โ”€ swagger
โ”œโ”€โ”€ gapi
โ”‚ โ”œโ”€โ”€ authorization.go
โ”‚ โ”œโ”€โ”€ converter.go
โ”‚ โ”œโ”€โ”€ error.go
โ”‚ โ”œโ”€โ”€ logger.go
โ”‚ โ”œโ”€โ”€ metadata.go
โ”‚ โ”œโ”€โ”€ rpc_create_user.go
โ”‚ โ”œโ”€โ”€ rpc_login_user.go
โ”‚ โ”œโ”€โ”€ rpc_update_user.go
โ”‚ โ”œโ”€โ”€ rpc_verify_email.go
โ”‚ โ””โ”€โ”€ server.go
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ go.sum
โ”œโ”€โ”€ main.go
โ”œโ”€โ”€ makefile
โ”œโ”€โ”€ pb
โ”‚ โ”œโ”€โ”€ rpc_create_user.pb.go
โ”‚ โ”œโ”€โ”€ rpc_login_user.pb.go
โ”‚ โ”œโ”€โ”€ rpc_update_user.pb.go
โ”‚ โ”œโ”€โ”€ rpc_verify_email.pb.go
โ”‚ โ”œโ”€โ”€ service_auth.pb.go
โ”‚ โ”œโ”€โ”€ service_auth.pb.gw.go
โ”‚ โ”œโ”€โ”€ service_auth_grpc.pb.go
โ”‚ โ””โ”€โ”€ user.pb.go
โ”œโ”€โ”€ proto
โ”‚ โ”œโ”€โ”€ google
โ”‚ โ”œโ”€โ”€ protoc-gen-openapiv2
โ”‚ โ”œโ”€โ”€ rpc_create_user.proto
โ”‚ โ”œโ”€โ”€ rpc_login_user.proto
โ”‚ โ”œโ”€โ”€ rpc_update_user.proto
โ”‚ โ”œโ”€โ”€ rpc_verify_email.proto
โ”‚ โ”œโ”€โ”€ service_auth.proto
โ”‚ โ””โ”€โ”€ user.proto
โ”œโ”€โ”€ sqlc.yaml
โ”œโ”€โ”€ util
โ”‚ โ”œโ”€โ”€ config.go
โ”‚ โ”œโ”€โ”€ password.go
โ”‚ โ””โ”€โ”€ random.go
โ”œโ”€โ”€ val
โ”‚ โ””โ”€โ”€ validator.go
โ””โ”€โ”€ worker
โ”œโ”€โ”€ distributor.go
โ”œโ”€โ”€ logger.go
โ”œโ”€โ”€ processor.go
โ””โ”€โ”€ task_send_verify_email.go
```

### ๐Ÿ“‚ Project Index

AUTH/

__root__




makefile
โฏ makefile with commands for local development


main.go
โฏ Main entry point for API service


go.mod
โฏ Go mod file


go.sum
โฏ Go sum file


sqlc.yaml
โฏ Sqlc V2 configuration file





worker



processor.go
โฏ Task processor for Redis queue - picks up tasks from the queue to process and run them


task_send_verify_email.go
โฏ Redis queue task for sending out the verification email to new users


logger.go
โฏ Logger for the Redis queue


distributor.go
โฏ Redis task distributor interface - distributes the tasks into appropriate queues





proto



user.proto
โฏ proto buff specification for `User` object serialization


rpc_update_user.proto
โฏ proto buff specification for `Update user` request / response serialization


rpc_create_user.proto
โฏ proto buff specification for `Create user` request / response serialization


service_auth.proto
โฏ proto buff specification for API service


rpc_verify_email.proto
โฏ proto buff specification for `Verify email` request / response serialization


rpc_login_user.proto
โฏ proto buff specification for `Login user` request / response serialization



protoc-gen-openapiv2


options



openapiv2.proto
โฏ REPLACE-ME


annotations.proto
โฏ REPLACE-ME







google


api



httpbody.proto
โฏ REPLACE-ME


field_behavior.proto
โฏ REPLACE-ME


http.proto
โฏ REPLACE-ME


annotations.proto
โฏ REPLACE-ME









doc



db.dbml
โฏ Database markup version of the database schema



statik



statik.go
โฏ Serves static swagger specification page





swagger



swagger-initializer.js
โฏ cloned from swagger-ui


swagger-ui-standalone-preset.js
โฏ cloned from swagger-ui


auth.swagger.json
โฏ cloned from swagger-ui


swagger-ui-es-bundle.js
โฏ cloned from swagger-ui


swagger-ui-bundle.js
โฏ cloned from swagger-ui


index.css
โฏ cloned from swagger-ui


swagger-ui-es-bundle-core.js
โฏ cloned from swagger-ui


swagger-ui.js
โฏ cloned from swagger-ui


swagger-ui.css
โฏ cloned from swagger-ui


index.html
โฏ cloned from swagger-ui


oauth2-redirect.html
โฏ cloned from swagger-ui







gapi



metadata.go
โฏ Helper to capture metadata of gRPC gateway requests


logger.go
โฏ Logger file for gRPC & HTTP requests


authorization.go
โฏ Validates token format for incoming authorized calls


rpc_update_user.go
โฏ Function that is called to update a user record when the api endpoint is invoked


rpc_verify_email.go
โฏ Function that is called to verify a new users email when the api endpoint is invoked


error.go
โฏ Helper to handle generic errors (invalid request params, unauthorized calls)


rpc_login_user.go
โฏ Function that is called to log a user into their account when the api endpoint is invoked


converter.go
โฏ Helper to sanitize User database objects from sensitive data (like password)


rpc_create_user.go
โฏ Function that is called to create a new user account when the api endpoint is invoked


server.go
โฏ Initializes a new Server to run





val



validator.go
โฏ Helper to validate string format for email, names, passwords, etc...





pb



user.pb.go
โฏ Code generated by protoc-gen-go


service_auth_grpc.pb.go
โฏ Code generated by protoc-gen-go


rpc_login_user.pb.go
โฏ Code generated by protoc-gen-go


rpc_update_user.pb.go
โฏ Code generated by protoc-gen-go


rpc_create_user.pb.go
โฏ Code generated by protoc-gen-go


service_auth.pb.go
โฏ Code generated by protoc-gen-go


service_auth.pb.gw.go
โฏ Code generated by protoc-gen-go


rpc_verify_email.pb.go
โฏ Code generated by protoc-gen-go





util



password.go
โฏ Helper to hash & compare passwords


config.go
โฏ Server configs read from app.env


random.go
โฏ Random generator utility





db


sqlc



models.go
โฏ Code generated by sqlc


db.go
โฏ Code generated by sqlc


verify_email.sql.go
โฏ Code generated by sqlc


user.sql.go
โฏ Code generated by sqlc


querier.go
โฏ Code generated by sqlc


tx_verify_email.go
โฏ Transactional write to the database to update user records across tables when email is verified


store.go
โฏ SQLStore provides all functions to execute db queries and transactions. Also used to Mock DB for tests


tx_create_user.go
โฏ Transactional write that creates a user in the database. This transaction has a callback that executes only when the database write is successful. We use this call back to send out the verification email upon sign up


sessions.sql.go
โฏ Code generated by sqlc





query



user.sql
โฏ Queries & annotations pertaining to users for sqlc to generate application database code


sessions.sql
โฏ Queries & annotations pertaining to sessions for sqlc to generate application database code


verify_email.sql
โฏ Queries & annotations pertaining to verifying emails for sqlc to generate application database code





migration



000001_init_schema.up.sql
โฏ Initial database schema for migrate up to use


000001_init_schema.down.sql
โฏ Initial drop database schema for migrate down to use





---
## ๐Ÿš€ Getting Started

### โ˜‘๏ธ Prerequisites

Before getting started with auth, ensure your runtime environment meets the following requirements:

- **Programming Language:** Go
- **Package Manager:** Go modules
- **Containerization:** Docker

### โš™๏ธ Installation

Install auth using the following methods:

1. Clone the auth repository:
```sh
โฏ git clone https://github.com/fsobh/auth
```

2. Navigate to the project directory:
```sh
โฏ cd auth
```

3. Install the project dependencies:

**Using `go modules`** ย  [](https://golang.org/)

```sh
โฏ go mod tidy
```

### ๐Ÿค– Usage
**Steps 1 and 2 are essential for the service to run**

1. **Update `app.env` with your environment variables**

| Name | Description
|:------------------:| :---:
| PASETO_PRIVATE_KEY | Hexadecimal private key (Paseto V2)
| PASETO_PUBLIC_KEY | Hexadecimal public key (Paseto V2)
| SES_FROM_EMAIL | Verified AWS SES email address
2. **Update the `.env` file with your AWS account credentials**

| Name | Description
|:------------------:| :---:
| AWS_ACCESS_KEY_ID | AWS IAM access key
| AWS_SECRET_ACCESS_KEY | AWS IAM secret key
| AWS_DEFAULT_REGION | AWS region

3. Run the following command to start the service:
```sh
โฏ docker compose up
```

[//]: # (### ๐Ÿงช Testing)

[//]: # (Run the test suite using the following command:)

[//]: # (**Using `go modules`** ย  [](https://golang.org/))

[//]: # ()
[//]: # (```sh)

[//]: # (โฏ go test ./...)

[//]: # (```)

---
## ๐Ÿ“Œ Project Roadmap

- [X] **`Task 1`**: Authentication + sessions.
- [ ] **`Task 2`**: Add RBAC.
- [ ] **`Task 3`**: Add OAuth2 support.
- [ ] **`Task 4`**: Add unit tests.
- [ ] **`Task 5`**: Add Github actions to auto deploy.

---

## ๐Ÿ”ฐ Contributing

- **๐Ÿ’ฌ [Join the Discussions](https://github.com/fsobh/auth/discussions)**: Share your insights, provide feedback, or ask questions.
- **๐Ÿ› [Report Issues](https://github.com/fsobh/auth/issues)**: Submit bugs found or log feature requests for the `auth` project.
- **๐Ÿ’ก [Submit Pull Requests](https://github.com/fsobh/auth/blob/main/CONTRIBUTING.md)**: Review open PRs, and submit your own PRs.

Contributing Guidelines

1. **Fork the Repository**: Start by forking the project repository to your github account.
2. **Clone Locally**: Clone the forked repository to your local machine using a git client.
```sh
git clone https://github.com/fsobh/auth
```
3. **Create a New Branch**: Always work on a new branch, giving it a descriptive name.
```sh
git checkout -b new-feature-x
```
4. **Make Your Changes**: Develop and test your changes locally.
5. **Commit Your Changes**: Commit with a clear message describing your updates.
```sh
git commit -m 'Implemented new feature x.'
```
6. **Push to github**: Push the changes to your forked repository.
```sh
git push origin new-feature-x
```
7. **Submit a Pull Request**: Create a PR against the original project repository. Clearly describe the changes and their motivations.
8. **Review**: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!

Contributor Graph






---

## ๐ŸŽ— License
- This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.

---

## ๐Ÿ™Œ Acknowledgments

- [Zero log](https://github.com/rs/zerolog)
- [sqlc](https://github.com/sqlc-dev/sqlc-gen-go)
- [proto buf](https://github.com/golang/protobuf)
- [mockdb](https://pkg.go.dev/github.com/tmaiaroto/discfg/storage/mockdb)
- [statik](https://github.com/rakyll/statik)
- [asynq](https://github.com/hibiken/asynq)

---