Skip to content

Commit 996818e

Browse files
CourtcircuitsTristan Radulescu
and
Tristan Radulescu
authored
fix(GIST-25): added /user/me route
* wip(GIST-25): merged auth into user package bc cycle import * chore(GIST-25): just added just * wip(GIST-25): added tests on retrieve user info * docs(GIST-25): added user/me route to api doc --------- Co-authored-by: Tristan Radulescu <[email protected]>
1 parent 801ca26 commit 996818e

26 files changed

+273
-81
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
tmp
33
.env
4+
api

README.md

+19-14
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ Check the [API documentation](http://localhost:4000) for more information (for n
1414
- Air
1515
- docker compose
1616
- migrate
17+
- Just
1718

1819
### Onboarding script
1920

2021
```bash
2122
docker compose up -d
22-
migrate -path=migrations -database "postgresql://postgres:[email protected]:5432/gists?sslmode=disable" -verbose up
23-
air
23+
just migrate
24+
just dev
2425
```
2526

2627
## Installation
@@ -52,17 +53,21 @@ MAIL_SMTP="<REDACTED>"
5253
MAIL_PASSWORD="<REDACTED>"
5354
SMTP_PORT="<REDACTED>"
5455
SMTP_HOST="<REDACTED>"
56+
APP_KEY="<REDACTED>"
5557
```
5658

57-
4. Run the server
59+
4. Run the server in development mode
5860

5961
```bash
62+
just dev
63+
# or
6064
air
6165
```
6266

6367
## Configuration
6468

65-
All the configuration is done through env variables :
69+
All the configuration is done through env variables :
70+
6671
- `PORT` : the port on which your web server runs
6772
- `PG_USER` : the postgres user
6873
- `PG_PASSWORD` : the postgres password
@@ -75,13 +80,20 @@ All the configuration is done through env variables :
7580
- `GOOGLE_SECRET` : your google client secret for OAUTH2
7681
- `GITHUB_KEY` : your github client key for OAUTH2
7782
- `GITHUB_SECRET` : your github client secret for OAUTH2
83+
- `MAIL_SMTP` : your smtp server
84+
- `MAIL_PASSWORD` : your smtp password
85+
- `SMTP_PORT` : your smtp port
86+
- `SMTP_HOST` : your smtp host
87+
- `APP_KEY` : your app key, which is a random string that is used to encrypt access tokens
7888

7989
## Tests
8090

8191
To run tests, execute:
8292

8393
```bash
84-
go test .
94+
just test <your-test-name>
95+
# or to run all tests
96+
just test-all
8597
```
8698

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

97109
To run the existing migrations locally :
98110

99-
### With bash
100-
101111
```bash
112+
just migrate
113+
# or
102114
migrate -path=migrations -database "postgresql://postgres:[email protected]:5432/gists?sslmode=disable" -verbose up
103115
```
104-
105-
### With go
106-
107-
```bash
108-
go build main.go
109-
./main migrate
110-
```

docs/openapi.json

+1-1
Large diffs are not rendered by default.

gists/router.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gists
22

33
import (
4-
"github.com/gistapp/api/server"
4+
"github.com/gistapp/api/user"
55
"github.com/gofiber/fiber/v2"
66
)
77

@@ -10,7 +10,7 @@ type GistRouter struct {
1010
}
1111

1212
func (r *GistRouter) SubscribeRoutes(app *fiber.Router) {
13-
gists_router := (*app).Group("/gists", server.AuthNeededMiddleware)
13+
gists_router := (*app).Group("/gists", user.AuthNeededMiddleware)
1414

1515
gists_router.Post("/", r.Controller.Save())
1616
gists_router.Patch("/:id/name", r.Controller.UpdateName())

justfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
build:
2+
go build -o api -v
3+
4+
test-all:
5+
go test ./tests/ -v
6+
7+
test TEST:
8+
go test ./tests/{{TEST}} -v
9+
10+
migrate: build
11+
./api migrate
12+
13+
dev:
14+
air

main.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/gistapp/api/auth"
87
"github.com/gistapp/api/gists"
98
"github.com/gistapp/api/organizations"
109
"github.com/gistapp/api/server"
1110
"github.com/gistapp/api/storage"
11+
"github.com/gistapp/api/user"
1212
"github.com/gistapp/api/utils"
1313
"github.com/gofiber/fiber/v2/log"
1414
)
@@ -37,19 +37,23 @@ func main() {
3737
Controller: gists.GistController,
3838
}
3939

40-
authRouter := auth.AuthRouter{
41-
Controller: &auth.AuthControllerImpl{
42-
AuthService: &auth.AuthService,
40+
authRouter := user.AuthRouter{
41+
Controller: &user.AuthControllerImpl{
42+
AuthService: &user.AuthService,
4343
},
4444
}
4545

46+
userRouter := user.UserRouter{
47+
Controller: &user.UserControllerImpl{},
48+
}
49+
4650
orgRouter := organizations.OrganizationRouter{
4751
Controller: organizations.OrganizationControllerImpl{},
4852
}
4953

50-
auth.AuthService.RegisterProviders() //register goth providers for authentication
54+
user.AuthService.RegisterProviders() //register goth providers for authentication
5155

5256
// Start the server
53-
s.Setup(&gistRouter, &authRouter, &orgRouter)
57+
s.Setup(&gistRouter, &authRouter, &orgRouter, &userRouter)
5458
s.Ignite()
5559
}

organizations/router.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package organizations
22

33
import (
4-
"github.com/gistapp/api/server"
4+
"github.com/gistapp/api/user"
55
"github.com/gofiber/fiber/v2"
66
)
77

@@ -10,7 +10,7 @@ type OrganizationRouter struct {
1010
}
1111

1212
func (r *OrganizationRouter) SubscribeRoutes(app *fiber.Router) {
13-
organizations_router := (*app).Group("/orgs", server.AuthNeededMiddleware)
13+
organizations_router := (*app).Group("/orgs", user.AuthNeededMiddleware)
1414

1515
organizations_router.Post("/", r.Controller.Save())
1616
organizations_router.Get("/", r.Controller.GetAsMember())

server/middleware.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package server
22

33
import (
4-
"github.com/gistapp/api/auth"
4+
"github.com/gistapp/api/user"
55
"github.com/gofiber/fiber/v2"
66
)
77

@@ -21,7 +21,7 @@ func AuthNeededMiddleware(ctx *fiber.Ctx) error {
2121
})
2222
}
2323
raw_token := string(ctx.Request().Header.Peek("Authorization")[7:])
24-
claims, err := auth.AuthService.IsAuthenticated(raw_token)
24+
claims, err := user.AuthService.IsAuthenticated(raw_token)
2525
if err != nil {
2626
return ctx.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
2727
"error": "Unauthorized",

tests/gists_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"os"
77
"testing"
88

9-
"github.com/gistapp/api/auth"
109
"github.com/gistapp/api/gists"
1110
"github.com/gistapp/api/organizations"
1211
"github.com/gistapp/api/server"
1312
"github.com/gistapp/api/storage"
1413
"github.com/gistapp/api/tests/mock"
14+
"github.com/gistapp/api/user"
1515
"github.com/gistapp/api/utils"
1616
"github.com/gofiber/fiber/v2"
1717
)
@@ -34,7 +34,7 @@ func InitServerGists() *fiber.App {
3434
Controller: gists.GistController,
3535
}
3636

37-
auth_router := auth.AuthRouter{
37+
auth_router := user.AuthRouter{
3838
Controller: &mock.MockAuthController{
3939
//needs GetUser to fit the auth interface
4040
AuthService: &mock.MockAuthService{},
@@ -55,7 +55,7 @@ func TestCreateGists(t *testing.T) {
5555
app := InitServerGists()
5656
authToken := GetAuthToken(t, app)
5757

58-
body, req := utils.MakeRequest(t, app, "/gists", map[string]string{
58+
body, req := utils.MakeRequest("POST", t, app, "/gists", map[string]string{
5959
"name": "Test Gist",
6060
"content": "Test content",
6161
}, map[string]string{
@@ -73,7 +73,7 @@ func TestCreateGists(t *testing.T) {
7373
t.Run("Create a new organization gist", func(t *testing.T) {
7474
app := InitServerGists()
7575
auth_token := GetAuthToken(t, app)
76-
claims, _ := auth.AuthService.IsAuthenticated(auth_token)
76+
claims, _ := user.AuthService.IsAuthenticated(auth_token)
7777

7878
org_mod := organizations.OrganizationSQL{
7979
Name: sql.NullString{
@@ -93,7 +93,7 @@ func TestCreateGists(t *testing.T) {
9393
"org_id": org.ID,
9494
}
9595

96-
body, req := utils.MakeRequest(t, app, "/gists", payload, map[string]string{
96+
body, req := utils.MakeRequest("POST", t, app, "/gists", payload, map[string]string{
9797
"Authorization": fmt.Sprintf("Bearer %s", auth_token),
9898
})
9999

tests/mock/auth_controller.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package mock
22

33
import (
4-
"github.com/gistapp/api/auth"
4+
"github.com/gistapp/api/user"
55
"github.com/gofiber/fiber/v2"
66
)
77

8-
type MockAuthController struct{
9-
AuthService auth.IAuthService
8+
type MockAuthController struct {
9+
AuthService user.IAuthService
1010
}
1111

1212
func (a *MockAuthController) Callback() fiber.Handler {
@@ -23,7 +23,7 @@ func (a *MockAuthController) Authenticate() fiber.Handler {
2323

2424
func (a *MockAuthController) LocalAuth() fiber.Handler {
2525
return func(c *fiber.Ctx) error {
26-
e := new(auth.AuthLocalValidator)
26+
e := new(user.AuthLocalValidator)
2727
if err := c.BodyParser(e); err != nil {
2828
return c.Status(400).SendString("Request must be valid JSON with field email as text")
2929
}
@@ -39,7 +39,7 @@ func (a *MockAuthController) LocalAuth() fiber.Handler {
3939

4040
func (a *MockAuthController) VerifyAuthToken() fiber.Handler {
4141
return func(c *fiber.Ctx) error {
42-
e := new(auth.AuthLocalVerificationValidator)
42+
e := new(user.AuthLocalVerificationValidator)
4343

4444
if err := c.BodyParser(e); err != nil {
4545
return c.Status(400).SendString("Request must be valid JSON with fields token and email as text")
@@ -61,4 +61,4 @@ func (a *MockAuthController) VerifyAuthToken() fiber.Handler {
6161
c.Cookie(token_cookie)
6262
return c.Status(200).JSON(fiber.Map{"message": "You are now logged in"})
6363
}
64-
}
64+
}

tests/mock/auth_service.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"strings"
88

9-
"github.com/gistapp/api/auth"
109
"github.com/gistapp/api/user"
1110
"github.com/gistapp/api/utils"
1211
"github.com/gofiber/fiber/v2"
@@ -20,12 +19,12 @@ func (m *MockAuthService) Authenticate(c *fiber.Ctx) error {
2019
return nil
2120
}
2221

23-
func (m *MockAuthService) LocalAuth(email string) (auth.TokenSQL, error) {
22+
func (m *MockAuthService) LocalAuth(email string) (user.TokenSQL, error) {
2423
token_val := utils.GenToken(6)
25-
token_model := auth.TokenSQL{
24+
token_model := user.TokenSQL{
2625
Keyword: sql.NullString{String: email, Valid: true},
2726
Value: sql.NullString{String: token_val, Valid: true},
28-
Type: sql.NullString{String: string(auth.LocalAuth), Valid: true},
27+
Type: sql.NullString{String: string(user.LocalAuth), Valid: true},
2928
}
3029

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

3736
func (m *MockAuthService) VerifyLocalAuthToken(token string, email string) (string, error) {
38-
token_model := auth.TokenSQL{
37+
token_model := user.TokenSQL{
3938
Value: sql.NullString{String: token, Valid: true},
4039
Keyword: sql.NullString{String: email, Valid: true},
41-
Type: sql.NullString{String: string(auth.LocalAuth), Valid: true},
40+
Type: sql.NullString{String: string(user.LocalAuth), Valid: true},
4241
}
4342
token_data, err := token_model.Get()
4443
if err != nil {
@@ -80,8 +79,8 @@ func (m *MockAuthService) Callback(c *fiber.Ctx) (string, error) {
8079
return "", nil
8180
}
8281

83-
func (a *MockAuthService) GetUser(auth_user goth.User) (*user.User, *auth.AuthIdentity, error) {
84-
return auth.AuthService.GetUser(auth_user)
82+
func (a *MockAuthService) GetUser(auth_user goth.User) (*user.User, *user.AuthIdentity, error) {
83+
return user.AuthService.GetUser(auth_user)
8584
}
8685

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

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

117-
func (a *MockAuthService) IsAuthenticated(token string) (*auth.JWTClaim, error) {
116+
func (a *MockAuthService) IsAuthenticated(token string) (*user.JWTClaim, error) {
118117
claims, err := utils.VerifyJWT(token)
119118

120119
if err != nil {
121120
return nil, err
122121
}
123122

124-
jwtClaim := new(auth.JWTClaim)
123+
jwtClaim := new(user.JWTClaim)
125124
jwtClaim.Pub = claims["pub"].(string)
126125
jwtClaim.Email = claims["email"].(string)
127126

tests/organization_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ import (
77
"strconv"
88
"testing"
99

10-
"github.com/gistapp/api/auth"
1110
"github.com/gistapp/api/gists"
1211
"github.com/gistapp/api/organizations"
1312
"github.com/gistapp/api/server"
1413
"github.com/gistapp/api/storage"
1514
"github.com/gistapp/api/tests/mock"
15+
"github.com/gistapp/api/user"
1616
"github.com/gistapp/api/utils"
1717
"github.com/gofiber/fiber/v2"
1818
)
1919

20-
var endpoint = "http://localhost:4000"
21-
2220
func InitServerOrgs() *fiber.App {
2321
// Check for command-line arguments
2422
if len(os.Args) > 1 && os.Args[1] == "migrate" {
@@ -37,7 +35,7 @@ func InitServerOrgs() *fiber.App {
3735
Controller: gists.GistController,
3836
}
3937

40-
auth_router := auth.AuthRouter{
38+
auth_router := user.AuthRouter{
4139
Controller: &mock.MockAuthController{
4240
AuthService: &mock.MockAuthService{},
4341
},
@@ -70,7 +68,7 @@ func TestCreateOrganization(t *testing.T) {
7068
}
7169
fmt.Println(org_payload)
7270
//
73-
body, _ := utils.MakeRequest(t, app, "/orgs", org_payload, map[string]string{
71+
body, _ := utils.MakeRequest("POST", t, app, "/orgs", org_payload, map[string]string{
7472
"Authorization": "Bearer " + auth_token,
7573
})
7674
//

0 commit comments

Comments
 (0)