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

feat(GIST-22): add organizations #8

Merged
merged 11 commits into from
Aug 18, 2024
34 changes: 34 additions & 0 deletions .github/workflows/mr-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Continuous testing
on:
pull_request:
branches:
- main

jobs:
test:
name: Integration tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: gists
POSTGRES_USER: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- name: Install Go toolchain
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version: 1.23.0
- name: Run migrations
run: go build -o main && chmod +x ./main && PORT="4000" PG_USER="postgres" PG_PASSWORD="postgres" PG_PORT="5432" PG_HOST="0.0.0.0" PG_DATABASE="gists" GOOGLE_KEY="" GOOGLE_SECRET="" GITHUB_KEY="" GITHUB_SECRET="" PUBLIC_URL="http://localhost:4000" APP_KEY="DUMP_APP_KEY_FOR_TEST" SMTP_HOST="" MAIL_SMTP="" MAIL_PASSWORD="" SMTP_PORT="" FRONTEND_URL="http://localhost:3000" ./main migrate
- name: Run tests
run: PORT="4000" PG_USER="postgres" PG_PASSWORD="postgres" PG_PORT="5432" PG_HOST="0.0.0.0" PG_DATABASE="gists" GOOGLE_KEY="" GOOGLE_SECRET="" GITHUB_KEY="" GITHUB_SECRET="" PUBLIC_URL="http://localhost:4000" APP_KEY="DUMP_APP_KEY_FOR_TEST" SMTP_HOST="" MAIL_SMTP="" MAIL_PASSWORD="" SMTP_PORT="" FRONTEND_URL="http://localhost:3000" go test ./tests/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ansible.python.interpreterPath": "/bin/python3"
}
19 changes: 14 additions & 5 deletions auth/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import (
"github.com/gofiber/fiber/v2"
)

type AuthControllerImpl struct{}
type IAuthController interface {
Callback() fiber.Handler
Authenticate() fiber.Handler
LocalAuth() fiber.Handler
VerifyAuthToken() fiber.Handler
}

type AuthControllerImpl struct{
AuthService IAuthService
}

type AuthLocalValidator struct {
Email string `json:"email"`
Expand All @@ -18,7 +27,7 @@ type AuthLocalVerificationValidator struct {

func (a *AuthControllerImpl) Callback() fiber.Handler {
return func(c *fiber.Ctx) error {
token, err := AuthService.Callback(c)
token, err := a.AuthService.Callback(c)
if err != nil {
return c.Status(400).SendString(err.Error())
}
Expand All @@ -33,7 +42,7 @@ func (a *AuthControllerImpl) Callback() fiber.Handler {

func (a *AuthControllerImpl) Authenticate() fiber.Handler {
return func(c *fiber.Ctx) error {
return AuthService.Authenticate(c)
return a.AuthService.Authenticate(c)
}
}

Expand All @@ -44,7 +53,7 @@ func (a *AuthControllerImpl) LocalAuth() fiber.Handler {
return c.Status(400).SendString("Request must be valid JSON with field email as text")
}

if err := AuthService.LocalAuth(e.Email); err != nil {
if _, err := a.AuthService.LocalAuth(e.Email); err != nil {
return c.Status(400).SendString(err.Error())
}

Expand All @@ -63,7 +72,7 @@ func (a *AuthControllerImpl) VerifyAuthToken() fiber.Handler {
token := e.Token
email := e.Email

jwt_token, err := AuthService.VerifyLocalAuthToken(token, email)
jwt_token, err := a.AuthService.VerifyLocalAuthToken(token, email)

if err != nil {
return c.Status(400).SendString(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion auth/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package auth
import "github.com/gofiber/fiber/v2"

type AuthRouter struct {
Controller AuthControllerImpl
Controller IAuthController
}

func (r *AuthRouter) SubscribeRoutes(app *fiber.Router) {
Expand Down
17 changes: 14 additions & 3 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import (
"github.com/shareed2k/goth_fiber"
)

type IAuthService interface {
Authenticate(c *fiber.Ctx) error
LocalAuth(email string) (TokenSQL, error)
VerifyLocalAuthToken(token string, email string) (string, error)
Callback(c *fiber.Ctx) (string, error)
GetUser(auth_user goth.User) (*user.User, *AuthIdentity, error)
Register(auth_user goth.User) (*user.User, error)
RegisterProviders()
IsAuthenticated(token string) (*JWTClaim, error)
}

type AuthServiceImpl struct{}

func (a *AuthServiceImpl) Authenticate(c *fiber.Ctx) error {
Expand All @@ -28,7 +39,7 @@ func (a *AuthServiceImpl) Authenticate(c *fiber.Ctx) error {
}

// generates a token and sends it to the user by email
func (a *AuthServiceImpl) LocalAuth(email string) error {
func (a *AuthServiceImpl) LocalAuth(email string) (TokenSQL, error) {
token_val := utils.GenToken(6)
token_model := TokenSQL{
Keyword: sql.NullString{String: email, Valid: true},
Expand All @@ -39,12 +50,12 @@ func (a *AuthServiceImpl) LocalAuth(email string) error {
_, err := token_model.Save()

if err != nil {
return err
return token_model, err
}

err = utils.SendEmail("Gistapp: Local Auth", "Your token is: "+token_val, email)

return err
return token_model, err
}

// verifies the token and finishes the registration
Expand Down
Loading
Loading