Skip to content

Commit

Permalink
update: repository
Browse files Browse the repository at this point in the history
  • Loading branch information
FromSi committed Nov 5, 2024
1 parent fff8bca commit 41c5114
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 53 deletions.
4 changes: 2 additions & 2 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ paths:
required:
- uuid
- userUUID
- agent
- userAgent
- ip
- expiredAt
- createdAt
Expand All @@ -344,7 +344,7 @@ paths:
type: string
format: uuid
example: '09d0ce56-33ab-4fce-90bb-d2b4d6d844ba'
agent:
userAgent:
type: string
format: text
example: 'Mozilla/5.0 (X11; Linux x86_64; rv:121.0)'
Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type DevicesRequestBody struct{}
func NewDevicesRequestBody(context *gin.Context) (*DevicesRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody DevicesRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type LoginRequestBody struct {
func NewLoginRequestBody(context *gin.Context) (*LoginRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody LoginRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/http/requests/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type LogoutRequestBody struct{}
func NewLogoutRequestBody(context *gin.Context) (*LogoutRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody LogoutRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
println(err.Error())
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/logout_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type LogoutAllRequestBody struct{}
func NewLogoutAllRequestBody(context *gin.Context) (*LogoutAllRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody LogoutAllRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/logout_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type LogoutDeviceRequestBody struct {
func NewLogoutDeviceRequestBody(context *gin.Context) (*LogoutDeviceRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody LogoutDeviceRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/password_reset_with_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type PasswordResetWithOldRequestBody struct {
func NewPasswordResetWithOldRequestBody(context *gin.Context) (*PasswordResetWithOldRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody PasswordResetWithOldRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/password_reset_with_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type PasswordResetWithTokenRequestBody struct {
func NewPasswordResetWithTokenRequestBody(context *gin.Context) (*PasswordResetWithTokenRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody PasswordResetWithTokenRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type RefreshRequestBody struct {
func NewRefreshRequestBody(context *gin.Context) (*RefreshRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody RefreshRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type RegisterRequestBody struct {
func NewRegisterRequestBody(context *gin.Context) (*RegisterRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody RegisterRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/http/requests/send_reset_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type SendResetTokenRequestBody struct {
func NewSendResetTokenRequestBody(context *gin.Context) (*SendResetTokenRequestBody, *responses.ErrorBadRequestResponse) {
var requestBody SendResetTokenRequestBody

if err := context.ShouldBindJSON(&requestBody); err != nil {
if err := context.ShouldBindJSON(&requestBody); err != nil && err.Error() != "EOF" {
return nil, responses.NewErrorBadRequestResponseByError(err)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/http/responses/success_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type SuccessDevicesResponse struct {
type SuccessDevicesResponseData struct {
UUID string `json:"uuid"`
UserUUID string `json:"userUUID"`
Agent string `json:"agent"`
UserAgent string `json:"userAgent"`
Ip string `json:"ip"`
ExpiredAt int `json:"expiredAt"`
CreatedAt int `json:"createdAt"`
Expand All @@ -23,7 +23,7 @@ func NewSuccessDevicesResponse(devices []repositories.Device) *SuccessDevicesRes
data[i] = SuccessDevicesResponseData{
UUID: device.GetUUID(),
UserUUID: device.GetUserUUID(),
Agent: device.GetUserAgent(),
UserAgent: device.GetUserAgent(),
Ip: device.GetIp(),
ExpiredAt: device.GetExpiredAt(),
CreatedAt: device.GetCreatedAt(),
Expand Down
2 changes: 1 addition & 1 deletion internal/http/responses/success_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_NewSuccessDevicesResponse(t *testing.T) {

assert.Nil(t, err)

expected := `{"data":[{"uuid":"1","userUUID":"2","agent":"3","ip":"4","expiredAt":6,"createdAt":7,"updatedAt":8}]}`
expected := `{"data":[{"uuid":"1","userUUID":"2","userAgent":"3","ip":"4","expiredAt":6,"createdAt":7,"updatedAt":8}]}`

assert.Equal(t, string(responseToJson), expected)
}
22 changes: 15 additions & 7 deletions internal/http/routes/devices.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package routes

import (
"fmt"
"github.com/fromsi/jwt-oauth-sso/internal/configs"
"github.com/fromsi/jwt-oauth-sso/internal/http/requests"
"github.com/fromsi/jwt-oauth-sso/internal/http/responses"
"github.com/fromsi/jwt-oauth-sso/internal/repositories"
"github.com/gin-gonic/gin"
"net/http"
)

type DevicesRoute struct {
config configs.TokenConfig
config configs.TokenConfig
deviceRepository repositories.DeviceRepository
}

func NewDevicesRoute(config configs.TokenConfig) *DevicesRoute {
func NewDevicesRoute(
config configs.TokenConfig,
deviceRepository repositories.DeviceRepository,
) *DevicesRoute {
return &DevicesRoute{
config: config,
config: config,
deviceRepository: deviceRepository,
}
}

Expand All @@ -27,7 +33,7 @@ func (receiver DevicesRoute) Pattern() string {
}

func (receiver DevicesRoute) Handle(context *gin.Context) {
_, err := requests.NewBearerAuthRequestHeader(context, receiver.config)
headers, err := requests.NewBearerAuthRequestHeader(context, receiver.config)

if err != nil {
context.Status(http.StatusUnauthorized)
Expand All @@ -43,7 +49,9 @@ func (receiver DevicesRoute) Handle(context *gin.Context) {
return
}

fmt.Println(map[string]any{})
devices := receiver.deviceRepository.GetDevicesByUserUUID(headers.AccessToken.Subject)

context.JSON(http.StatusOK, map[string]string{"message": "Hello"})
response := responses.NewSuccessDevicesResponse(devices)

context.JSON(http.StatusOK, response)
}
26 changes: 19 additions & 7 deletions internal/http/routes/logout.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package routes

import (
"fmt"
"github.com/fromsi/jwt-oauth-sso/internal/configs"
"github.com/fromsi/jwt-oauth-sso/internal/http/requests"
"github.com/fromsi/jwt-oauth-sso/internal/http/responses"
"github.com/fromsi/jwt-oauth-sso/internal/repositories"
"github.com/gin-gonic/gin"
"net/http"
)

type LogoutRoute struct {
config configs.TokenConfig
config configs.TokenConfig
deviceRepository repositories.DeviceRepository
}

func NewLogoutRoute(config configs.TokenConfig) *LogoutRoute {
func NewLogoutRoute(
config configs.TokenConfig,
deviceRepository repositories.DeviceRepository,
) *LogoutRoute {
return &LogoutRoute{
config: config,
config: config,
deviceRepository: deviceRepository,
}
}

Expand All @@ -27,7 +33,7 @@ func (receiver LogoutRoute) Pattern() string {
}

func (receiver LogoutRoute) Handle(context *gin.Context) {
_, err := requests.NewBearerAuthRequestHeader(context, receiver.config)
headers, err := requests.NewBearerAuthRequestHeader(context, receiver.config)

if err != nil {
context.Status(http.StatusUnauthorized)
Expand All @@ -43,7 +49,13 @@ func (receiver LogoutRoute) Handle(context *gin.Context) {
return
}

fmt.Println(map[string]any{})
err = receiver.deviceRepository.DeleteDeviceByUUID(headers.AccessToken.DeviceUUID)

context.Status(http.StatusContinue)
if err != nil {
context.JSON(http.StatusInternalServerError, responses.NewErrorInternalServerResponse(err))

return
}

context.Status(http.StatusAccepted)
}
26 changes: 19 additions & 7 deletions internal/http/routes/logout_all.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package routes

import (
"fmt"
"github.com/fromsi/jwt-oauth-sso/internal/configs"
"github.com/fromsi/jwt-oauth-sso/internal/http/requests"
"github.com/fromsi/jwt-oauth-sso/internal/http/responses"
"github.com/fromsi/jwt-oauth-sso/internal/repositories"
"github.com/gin-gonic/gin"
"net/http"
)

type LogoutAllRoute struct {
config configs.TokenConfig
config configs.TokenConfig
deviceRepository repositories.DeviceRepository
}

func NewLogoutAllRoute(config configs.TokenConfig) *LogoutAllRoute {
func NewLogoutAllRoute(
config configs.TokenConfig,
deviceRepository repositories.DeviceRepository,
) *LogoutAllRoute {
return &LogoutAllRoute{
config: config,
config: config,
deviceRepository: deviceRepository,
}
}

Expand All @@ -27,7 +33,7 @@ func (receiver LogoutAllRoute) Pattern() string {
}

func (receiver LogoutAllRoute) Handle(context *gin.Context) {
_, err := requests.NewBearerAuthRequestHeader(context, receiver.config)
headers, err := requests.NewBearerAuthRequestHeader(context, receiver.config)

if err != nil {
context.Status(http.StatusUnauthorized)
Expand All @@ -43,7 +49,13 @@ func (receiver LogoutAllRoute) Handle(context *gin.Context) {
return
}

fmt.Println(map[string]any{})
err = receiver.deviceRepository.DeleteAllDevicesByUserUUID(headers.AccessToken.Subject)

context.Status(http.StatusContinue)
if err != nil {
context.JSON(http.StatusInternalServerError, responses.NewErrorInternalServerResponse(err))

return
}

context.Status(http.StatusAccepted)
}
28 changes: 19 additions & 9 deletions internal/http/routes/logout_device.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package routes

import (
"fmt"
"github.com/fromsi/jwt-oauth-sso/internal/configs"
"github.com/fromsi/jwt-oauth-sso/internal/http/requests"
"github.com/fromsi/jwt-oauth-sso/internal/http/responses"
"github.com/fromsi/jwt-oauth-sso/internal/repositories"
"github.com/gin-gonic/gin"
"net/http"
)

type LogoutDeviceRoute struct {
config configs.TokenConfig
config configs.TokenConfig
deviceRepository repositories.DeviceRepository
}

func NewLogoutDeviceRoute(config configs.TokenConfig) *LogoutDeviceRoute {
func NewLogoutDeviceRoute(
config configs.TokenConfig,
deviceRepository repositories.DeviceRepository,
) *LogoutDeviceRoute {
return &LogoutDeviceRoute{
config: config,
config: config,
deviceRepository: deviceRepository,
}
}

Expand All @@ -27,7 +33,7 @@ func (receiver LogoutDeviceRoute) Pattern() string {
}

func (receiver LogoutDeviceRoute) Handle(context *gin.Context) {
_, err := requests.NewBearerAuthRequestHeader(context, receiver.config)
headers, err := requests.NewBearerAuthRequestHeader(context, receiver.config)

if err != nil {
context.Status(http.StatusUnauthorized)
Expand All @@ -43,9 +49,13 @@ func (receiver LogoutDeviceRoute) Handle(context *gin.Context) {
return
}

fmt.Println(map[string]any{
"device_uuid": request.Body.DeviceUUID,
})
err = receiver.deviceRepository.DeleteDeviceByUUIDAndUserUUID(request.Body.DeviceUUID, headers.AccessToken.Subject)

context.Status(http.StatusContinue)
if err != nil {
context.JSON(http.StatusInternalServerError, responses.NewErrorInternalServerResponse(err))

return
}

context.Status(http.StatusAccepted)
}
8 changes: 0 additions & 8 deletions internal/http/routes/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ func (receiver RegisterRoute) Handle(context *gin.Context) {
return
}

err = receiver.deviceRepository.CreateDevice(device)

if err != nil {
context.JSON(http.StatusInternalServerError, responses.NewErrorInternalServerResponse(err))

return
}

response, err := responses.NewSuccessRegisterResponse(receiver.config, device)

if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions internal/mocks/repositories/mock_device_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 41c5114

Please sign in to comment.