Skip to content

Commit

Permalink
feat(auth): Move /login, /auth and /auth/refresh route to auth MS (#24)
Browse files Browse the repository at this point in the history
Remove /login from user MS
Add /login in auth MS
Add kafka communication between the two MS for login
Add documentation generation to auth MS
Remove /auth/refresh from user MS
Add /auth/refresh in auth MS
Add kafka communication between the two MS for refresh token
Remove /auth from user MS
Add /auth in auth MS
  • Loading branch information
victorneuret authored Oct 21, 2020
1 parent 7d3a9b4 commit 5c7d0cb
Show file tree
Hide file tree
Showing 68 changed files with 1,646 additions and 1,127 deletions.
2 changes: 0 additions & 2 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ services:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_HOST_NAME: ${MY_IP}
KAFKA_CREATE_TOPICS: "register:1:1,register-response:1:1"

# User Service
user:
Expand All @@ -45,7 +44,6 @@ services:
dockerfile: Dockerfile-dev
depends_on:
- mongodb
- redis
- broker
env_file: .env-dev
environment:
Expand Down
5 changes: 5 additions & 0 deletions microservices/auth/app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
install_swagger:
go get -u github.com/go-swagger/go-swagger/cmd/swagger

swagger:
swagger generate spec -o ./swagger.yml -i swagger-auth.yml --scan-models
27 changes: 0 additions & 27 deletions microservices/auth/app/data/kafkaRegisterResponse.go

This file was deleted.

35 changes: 35 additions & 0 deletions microservices/auth/app/data/kafkaUserLogin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package data

import (
"encoding/json"
)

// KafkaUserLoginMessage is the JSON struct sent to the user MS using the kafka topic `login`.
type KafkaUserLoginMessage struct {
Data KafkaUserLoginData `json:"data"`
}

// KafkaUserLoginData contain the data to be sent to the user MS using the kafka topic `login`.
type KafkaUserLoginData struct {
Login string `json:"login"`
Password string `json:"password"`
}

// CreateLoginMessage return a JSON of KafkaUserLoginMessage from an UserLogin.
func CreateLoginMessage(user UserLogin) ([]byte, error) {
// Create message struct
message := KafkaUserLoginMessage{
Data: KafkaUserLoginData{
Login: user.Login,
Password: user.Password,
},
}

// Marshal message
messageJSON, err := json.Marshal(message)
if err != nil {
return nil, err
}

return messageJSON, nil
}
2 changes: 1 addition & 1 deletion microservices/auth/app/data/kafkaUserRegister.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type KafkaUserRegisterData struct {
Password string `json:"password"`
}

// CreateRegisterMessage return a JSON of KafkaUserRegisterMessage from an id (UUID) and an UserRegister.
// CreateRegisterMessage return a JSON of KafkaUserRegisterMessage from an UserRegister.
func CreateRegisterMessage(user UserRegister) ([]byte, error) {
// Create message struct
message := KafkaUserRegisterMessage{
Expand Down
34 changes: 34 additions & 0 deletions microservices/auth/app/data/kafkaUserResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package data

import (
"encoding/json"
"log"

"github.com/gofiber/fiber/v2"
)

// KafkaUserResponseMessage is the success answer expected from the register-response and login-response topic.
type KafkaUserResponseMessage struct {
Data struct {
Code int `json:"code"`
Content KafkaUser `json:"content"`
} `json:"data"`
}

// KafkaUser is the data send by kafka for user info.
// We don't use data.User since we need to get the ID of the user that we don't want to return in the route JSON.
type KafkaUser struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
}

// UnmarshalUserResponse unmarshal the kafka message into a KafkaUserResponseMessage.
func UnmarshalUserResponse(message []byte) (*KafkaUser, error) {
var messageStruct KafkaUserResponseMessage
if err := json.Unmarshal(message, &messageStruct); err != nil {
log.Println(err)
return nil, NewHTTPErrorInfo(fiber.StatusInternalServerError, err.Error())
}
return &messageStruct.Data.Content, nil
}
14 changes: 14 additions & 0 deletions microservices/auth/app/data/userLogin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package data

// UserLogin is the body parameter given to login a new user.
// swagger:model
type UserLogin struct {
// The email or the username of the user
// required: true
// example: john@provider.net
Login string `json:"login" validate:"required"`
// The password of the user
// required: true
// example: leHAiOjE1OTgzNz
Password string `json:"password" validate:"required"`
}
2 changes: 1 addition & 1 deletion microservices/auth/app/errorHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func errorHandler(ctx *fiber.Ctx, err error) error {
}

// Return default message for 500+ error
if code >= fiber.StatusInternalServerError {
if code >= fiber.StatusInternalServerError || code == fiber.StatusUnauthorized {
return ctx.SendStatus(code)
}
// Send custom message if set or message contained in the errorInfo
Expand Down
10 changes: 5 additions & 5 deletions microservices/auth/app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ go 1.15
require (
github.com/alexandr-io/berrors v1.2.7
github.com/confluentinc/confluent-kafka-go v1.4.2
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fatih/structtag v1.2.0
github.com/go-openapi/runtime v0.19.22
github.com/go-redis/redis/v8 v8.2.1
github.com/gofiber/fiber/v2 v2.0.6
github.com/gofiber/fiber/v2 v2.1.0
github.com/gofiber/jwt/v2 v2.0.1
github.com/google/uuid v1.1.2
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5
github.com/valyala/fasthttp v1.16.0
golang.org/x/crypto v0.0.0-20190617133340-57b3e21c3d56
golang.org/x/text v0.3.3 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0
)
Loading

0 comments on commit 5c7d0cb

Please sign in to comment.