Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,26 @@ The documentation can be founded at the path `/docs/swagger.yaml` or accessing t
- `internal`: Directory to contain application code that should not be exposed to external packages.
- `core`: Directory that contains the application's core business logic.
- `thumb`: Directory contains definition of the entity's heights, interfaces, repository and service of the entity Thumb.
- `users`: Directory contains definition of the entity's heights, interfaces, repository and service of the entity User.
- `adapters`: Directory to contain external services that will interact with the application core.
- `db`: Directory contains the implementation of the repositories.
- `rest`: Directory that contains the definition of the application's controllers and handlers for manipulating data provided by the controller
- `domainerrors`: Directory that contains the definition of the application's domain errors.

## Basic Authentication

This project uses basic authentication to protect the endpoints.

Example:

| User | Password |
|-------|----------|
| test | test |

```curl
curl 'http://localhost:8080/login' -H 'Authorization: Basic dGVzdDp0ZXN0'
```

## Creating new users

Use the Swagger for access the endpoint `/user` and create a new user.
90 changes: 89 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,95 @@ const docTemplate = `{
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
"paths": {
"/login": {
"get": {
"description": "Authenticates a user using Basic Authentication and returns user information.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Auth"
],
"summary": "User Login",
"parameters": [
{
"type": "string",
"description": "Basic Authentication credentials (username:password)",
"name": "Authorization",
"in": "header",
"required": true
}
],
"responses": {
"200": {
"description": "Successful login",
"schema": {}
},
"401": {
"description": "Unauthorized",
"schema": {}
}
}
}
},
"/user": {
"post": {
"description": "Creates a new user with the provided nickname and password.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"users"
],
"summary": "Create a new user",
"parameters": [
{
"description": "User creation request body",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handler.CreateUserRequest"
}
}
],
"responses": {
"201": {
"description": "User created",
"schema": {}
},
"400": {
"description": "Bad request",
"schema": {}
}
}
}
}
},
"definitions": {
"handler.CreateUserRequest": {
"type": "object",
"required": [
"nickname",
"password"
],
"properties": {
"nickname": {
"type": "string"
},
"password": {
"type": "string"
}
}
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
Expand Down
90 changes: 89 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,93 @@
},
"host": "localhost:8080",
"basePath": "/",
"paths": {}
"paths": {
"/login": {
"get": {
"description": "Authenticates a user using Basic Authentication and returns user information.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Auth"
],
"summary": "User Login",
"parameters": [
{
"type": "string",
"description": "Basic Authentication credentials (username:password)",
"name": "Authorization",
"in": "header",
"required": true
}
],
"responses": {
"200": {
"description": "Successful login",
"schema": {}
},
"401": {
"description": "Unauthorized",
"schema": {}
}
}
}
},
"/user": {
"post": {
"description": "Creates a new user with the provided nickname and password.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"users"
],
"summary": "Create a new user",
"parameters": [
{
"description": "User creation request body",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handler.CreateUserRequest"
}
}
],
"responses": {
"201": {
"description": "User created",
"schema": {}
},
"400": {
"description": "Bad request",
"schema": {}
}
}
}
}
},
"definitions": {
"handler.CreateUserRequest": {
"type": "object",
"required": [
"nickname",
"password"
],
"properties": {
"nickname": {
"type": "string"
},
"password": {
"type": "string"
}
}
}
}
}
61 changes: 60 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
basePath: /
definitions:
handler.CreateUserRequest:
properties:
nickname:
type: string
password:
type: string
required:
- nickname
- password
type: object
host: localhost:8080
info:
contact: {}
description: Hackathon
title: Thumb processor worker
version: 0.1.0
paths: {}
paths:
/login:
get:
consumes:
- application/json
description: Authenticates a user using Basic Authentication and returns user
information.
parameters:
- description: Basic Authentication credentials (username:password)
in: header
name: Authorization
required: true
type: string
produces:
- application/json
responses:
"200":
description: Successful login
schema: {}
"401":
description: Unauthorized
schema: {}
summary: User Login
tags:
- Auth
/user:
post:
consumes:
- application/json
description: Creates a new user with the provided nickname and password.
parameters:
- description: User creation request body
in: body
name: request
required: true
schema:
$ref: '#/definitions/handler.CreateUserRequest'
produces:
- application/json
responses:
"201":
description: User created
schema: {}
"400":
description: Bad request
schema: {}
summary: Create a new user
tags:
- users
swagger: "2.0"
6 changes: 6 additions & 0 deletions internal/adapters/db/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package db

type User struct {
Nickname string
Password string
}
33 changes: 33 additions & 0 deletions internal/adapters/rest/handler/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package handler

import (
"github.com/gin-gonic/gin"
"net/http"
)

// RegisterLoginHandlers registers the login handler with the given Gin router group.
//
// @Summary User Login
// @Description Authenticates a user using Basic Authentication and returns user information.
// @Tags Auth
// @Accept json
// @Produce json
// @Param Authorization header string true "Basic Authentication credentials (username:password)"
// @Success 200 {object} interface{} "Successful login"
// @Failure 401 {object} interface{} "Unauthorized"
// @Router /login [get]
func RegisterLoginHandlers(authorizedGroup *gin.RouterGroup) {
authorizedGroup.GET("login", func(c *gin.Context) {
user, _, ok := c.Request.BasicAuth()

if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}

c.JSON(http.StatusOK, gin.H{
"nickname": user,
"status": "logged",
})
})
}
Loading
Loading