Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip(GIST-25): merged auth into user package bc cycle import #9

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore(GIST-25): just added just
Tristan Radulescu committed Aug 20, 2024
commit 55e036838451a2fa5053567a497d18e821050556
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
tmp
.env
api
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -14,13 +14,14 @@ Check the [API documentation](http://localhost:4000) for more information (for n
- Air
- docker compose
- migrate
- Just

### Onboarding script

```bash
docker compose up -d
migrate -path=migrations -database "postgresql://postgres:[email protected]:5432/gists?sslmode=disable" -verbose up
air
just migrate
just dev
```

## Installation
@@ -52,17 +53,21 @@ MAIL_SMTP="<REDACTED>"
MAIL_PASSWORD="<REDACTED>"
SMTP_PORT="<REDACTED>"
SMTP_HOST="<REDACTED>"
APP_KEY="<REDACTED>"
```

4. Run the server
4. Run the server in development mode

```bash
just dev
# or
air
```

## Configuration

All the configuration is done through env variables :
All the configuration is done through env variables :

- `PORT` : the port on which your web server runs
- `PG_USER` : the postgres user
- `PG_PASSWORD` : the postgres password
@@ -75,13 +80,20 @@ All the configuration is done through env variables :
- `GOOGLE_SECRET` : your google client secret for OAUTH2
- `GITHUB_KEY` : your github client key for OAUTH2
- `GITHUB_SECRET` : your github client secret for OAUTH2
- `MAIL_SMTP` : your smtp server
- `MAIL_PASSWORD` : your smtp password
- `SMTP_PORT` : your smtp port
- `SMTP_HOST` : your smtp host
- `APP_KEY` : your app key, which is a random string that is used to encrypt access tokens

## Tests

To run tests, execute:

```bash
go test .
just test <your-test-name>
# or to run all tests
just test-all
```

## Migrations
@@ -96,15 +108,8 @@ migrate create -ext=sql -dir=migrations -seq init

To run the existing migrations locally :

### With bash

```bash
just migrate
# or
migrate -path=migrations -database "postgresql://postgres:[email protected]:5432/gists?sslmode=disable" -verbose up
```

### With go

```bash
go build main.go
./main migrate
```
14 changes: 14 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
build:
go build -o api -v

test-all:
go test ./tests/ -v

test TEST:
go test ./tests/{{TEST}} -v

migrate: build
./api migrate

dev:
air
20 changes: 10 additions & 10 deletions tests/mock/auth_service.go
Original file line number Diff line number Diff line change
@@ -19,12 +19,12 @@ func (m *MockAuthService) Authenticate(c *fiber.Ctx) error {
return nil
}

func (m *MockAuthService) LocalAuth(email string) (auth.TokenSQL, error) {
func (m *MockAuthService) LocalAuth(email string) (user.TokenSQL, error) {
token_val := utils.GenToken(6)
token_model := auth.TokenSQL{
token_model := user.TokenSQL{
Keyword: sql.NullString{String: email, Valid: true},
Value: sql.NullString{String: token_val, Valid: true},
Type: sql.NullString{String: string(auth.LocalAuth), Valid: true},
Type: sql.NullString{String: string(user.LocalAuth), Valid: true},
}

_, err := token_model.Save()
@@ -34,10 +34,10 @@ func (m *MockAuthService) LocalAuth(email string) (auth.TokenSQL, error) {
}

func (m *MockAuthService) VerifyLocalAuthToken(token string, email string) (string, error) {
token_model := auth.TokenSQL{
token_model := user.TokenSQL{
Value: sql.NullString{String: token, Valid: true},
Keyword: sql.NullString{String: email, Valid: true},
Type: sql.NullString{String: string(auth.LocalAuth), Valid: true},
Type: sql.NullString{String: string(user.LocalAuth), Valid: true},
}
token_data, err := token_model.Get()
if err != nil {
@@ -79,8 +79,8 @@ func (m *MockAuthService) Callback(c *fiber.Ctx) (string, error) {
return "", nil
}

func (a *MockAuthService) GetUser(auth_user goth.User) (*user.User, *auth.AuthIdentity, error) {
return auth.AuthService.GetUser(auth_user)
func (a *MockAuthService) GetUser(auth_user goth.User) (*user.User, *user.AuthIdentity, error) {
return user.AuthService.GetUser(auth_user)
}

func (m *MockAuthService) Register(auth_user goth.User) (*user.User, error) {
@@ -102,7 +102,7 @@ func (m *MockAuthService) Register(auth_user goth.User) (*user.User, error) {
return nil, err
}

auth_identity_model := auth.AuthIdentitySQL{
auth_identity_model := user.AuthIdentitySQL{
Data: sql.NullString{String: string(data), Valid: true},
Type: sql.NullString{String: auth_user.Provider, Valid: true},
OwnerID: sql.NullString{String: user_data.ID, Valid: true},
@@ -113,14 +113,14 @@ func (m *MockAuthService) Register(auth_user goth.User) (*user.User, error) {
return user_data, err
}

func (a *MockAuthService) IsAuthenticated(token string) (*auth.JWTClaim, error) {
func (a *MockAuthService) IsAuthenticated(token string) (*user.JWTClaim, error) {
claims, err := utils.VerifyJWT(token)

if err != nil {
return nil, err
}

jwtClaim := new(auth.JWTClaim)
jwtClaim := new(user.JWTClaim)
jwtClaim.Pub = claims["pub"].(string)
jwtClaim.Email = claims["email"].(string)