Skip to content

Commit 63592fa

Browse files
committed
Add basic authentication
1 parent 30dd58c commit 63592fa

File tree

15 files changed

+751
-10
lines changed

15 files changed

+751
-10
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,26 @@ The documentation can be founded at the path `/docs/swagger.yaml` or accessing t
9999
- `internal`: Directory to contain application code that should not be exposed to external packages.
100100
- `core`: Directory that contains the application's core business logic.
101101
- `thumb`: Directory contains definition of the entity's heights, interfaces, repository and service of the entity Thumb.
102+
- `users`: Directory contains definition of the entity's heights, interfaces, repository and service of the entity User.
102103
- `adapters`: Directory to contain external services that will interact with the application core.
103104
- `db`: Directory contains the implementation of the repositories.
104105
- `rest`: Directory that contains the definition of the application's controllers and handlers for manipulating data provided by the controller
105106
- `domainerrors`: Directory that contains the definition of the application's domain errors.
107+
108+
## Basic Authentication
109+
110+
This project uses basic authentication to protect the endpoints.
111+
112+
Example:
113+
114+
| User | Password |
115+
|-------|----------|
116+
| test | test |
117+
118+
```curl
119+
curl 'http://localhost:8080/login' -H 'Authorization: Basic dGVzdDp0ZXN0'
120+
```
121+
122+
## Creating new users
123+
124+
Use the Swagger for access the endpoint `/user` and create a new user.

docs/docs.go

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,95 @@ const docTemplate = `{
1414
},
1515
"host": "{{.Host}}",
1616
"basePath": "{{.BasePath}}",
17-
"paths": {}
17+
"paths": {
18+
"/login": {
19+
"get": {
20+
"description": "Authenticates a user using Basic Authentication and returns user information.",
21+
"consumes": [
22+
"application/json"
23+
],
24+
"produces": [
25+
"application/json"
26+
],
27+
"tags": [
28+
"Auth"
29+
],
30+
"summary": "User Login",
31+
"parameters": [
32+
{
33+
"type": "string",
34+
"description": "Basic Authentication credentials (username:password)",
35+
"name": "Authorization",
36+
"in": "header",
37+
"required": true
38+
}
39+
],
40+
"responses": {
41+
"200": {
42+
"description": "Successful login",
43+
"schema": {}
44+
},
45+
"401": {
46+
"description": "Unauthorized",
47+
"schema": {}
48+
}
49+
}
50+
}
51+
},
52+
"/user": {
53+
"post": {
54+
"description": "Creates a new user with the provided nickname and password.",
55+
"consumes": [
56+
"application/json"
57+
],
58+
"produces": [
59+
"application/json"
60+
],
61+
"tags": [
62+
"users"
63+
],
64+
"summary": "Create a new user",
65+
"parameters": [
66+
{
67+
"description": "User creation request body",
68+
"name": "request",
69+
"in": "body",
70+
"required": true,
71+
"schema": {
72+
"$ref": "#/definitions/handler.CreateUserRequest"
73+
}
74+
}
75+
],
76+
"responses": {
77+
"201": {
78+
"description": "User created",
79+
"schema": {}
80+
},
81+
"400": {
82+
"description": "Bad request",
83+
"schema": {}
84+
}
85+
}
86+
}
87+
}
88+
},
89+
"definitions": {
90+
"handler.CreateUserRequest": {
91+
"type": "object",
92+
"required": [
93+
"nickname",
94+
"password"
95+
],
96+
"properties": {
97+
"nickname": {
98+
"type": "string"
99+
},
100+
"password": {
101+
"type": "string"
102+
}
103+
}
104+
}
105+
}
18106
}`
19107

20108
// SwaggerInfo holds exported Swagger Info so clients can modify it

docs/swagger.json

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,93 @@
88
},
99
"host": "localhost:8080",
1010
"basePath": "/",
11-
"paths": {}
11+
"paths": {
12+
"/login": {
13+
"get": {
14+
"description": "Authenticates a user using Basic Authentication and returns user information.",
15+
"consumes": [
16+
"application/json"
17+
],
18+
"produces": [
19+
"application/json"
20+
],
21+
"tags": [
22+
"Auth"
23+
],
24+
"summary": "User Login",
25+
"parameters": [
26+
{
27+
"type": "string",
28+
"description": "Basic Authentication credentials (username:password)",
29+
"name": "Authorization",
30+
"in": "header",
31+
"required": true
32+
}
33+
],
34+
"responses": {
35+
"200": {
36+
"description": "Successful login",
37+
"schema": {}
38+
},
39+
"401": {
40+
"description": "Unauthorized",
41+
"schema": {}
42+
}
43+
}
44+
}
45+
},
46+
"/user": {
47+
"post": {
48+
"description": "Creates a new user with the provided nickname and password.",
49+
"consumes": [
50+
"application/json"
51+
],
52+
"produces": [
53+
"application/json"
54+
],
55+
"tags": [
56+
"users"
57+
],
58+
"summary": "Create a new user",
59+
"parameters": [
60+
{
61+
"description": "User creation request body",
62+
"name": "request",
63+
"in": "body",
64+
"required": true,
65+
"schema": {
66+
"$ref": "#/definitions/handler.CreateUserRequest"
67+
}
68+
}
69+
],
70+
"responses": {
71+
"201": {
72+
"description": "User created",
73+
"schema": {}
74+
},
75+
"400": {
76+
"description": "Bad request",
77+
"schema": {}
78+
}
79+
}
80+
}
81+
}
82+
},
83+
"definitions": {
84+
"handler.CreateUserRequest": {
85+
"type": "object",
86+
"required": [
87+
"nickname",
88+
"password"
89+
],
90+
"properties": {
91+
"nickname": {
92+
"type": "string"
93+
},
94+
"password": {
95+
"type": "string"
96+
}
97+
}
98+
}
99+
}
12100
}

docs/swagger.yaml

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,68 @@
11
basePath: /
2+
definitions:
3+
handler.CreateUserRequest:
4+
properties:
5+
nickname:
6+
type: string
7+
password:
8+
type: string
9+
required:
10+
- nickname
11+
- password
12+
type: object
213
host: localhost:8080
314
info:
415
contact: {}
516
description: Hackathon
617
title: Thumb processor worker
718
version: 0.1.0
8-
paths: {}
19+
paths:
20+
/login:
21+
get:
22+
consumes:
23+
- application/json
24+
description: Authenticates a user using Basic Authentication and returns user
25+
information.
26+
parameters:
27+
- description: Basic Authentication credentials (username:password)
28+
in: header
29+
name: Authorization
30+
required: true
31+
type: string
32+
produces:
33+
- application/json
34+
responses:
35+
"200":
36+
description: Successful login
37+
schema: {}
38+
"401":
39+
description: Unauthorized
40+
schema: {}
41+
summary: User Login
42+
tags:
43+
- Auth
44+
/user:
45+
post:
46+
consumes:
47+
- application/json
48+
description: Creates a new user with the provided nickname and password.
49+
parameters:
50+
- description: User creation request body
51+
in: body
52+
name: request
53+
required: true
54+
schema:
55+
$ref: '#/definitions/handler.CreateUserRequest'
56+
produces:
57+
- application/json
58+
responses:
59+
"201":
60+
description: User created
61+
schema: {}
62+
"400":
63+
description: Bad request
64+
schema: {}
65+
summary: Create a new user
66+
tags:
67+
- users
968
swagger: "2.0"

internal/adapters/db/user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package db
2+
3+
type User struct {
4+
Nickname string
5+
Password string
6+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package handler
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"net/http"
6+
)
7+
8+
// RegisterLoginHandlers registers the login handler with the given Gin router group.
9+
//
10+
// @Summary User Login
11+
// @Description Authenticates a user using Basic Authentication and returns user information.
12+
// @Tags Auth
13+
// @Accept json
14+
// @Produce json
15+
// @Param Authorization header string true "Basic Authentication credentials (username:password)"
16+
// @Success 200 {object} interface{} "Successful login"
17+
// @Failure 401 {object} interface{} "Unauthorized"
18+
// @Router /login [get]
19+
func RegisterLoginHandlers(authorizedGroup *gin.RouterGroup) {
20+
authorizedGroup.GET("login", func(c *gin.Context) {
21+
user, _, ok := c.Request.BasicAuth()
22+
23+
if !ok {
24+
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
25+
return
26+
}
27+
28+
c.JSON(http.StatusOK, gin.H{
29+
"nickname": user,
30+
"status": "logged",
31+
})
32+
})
33+
}

0 commit comments

Comments
 (0)