refactor: transitioned auth controller to use the new controller structure;
feat: setup DI for controllers; refactor: marked old utils and routes package parts as deprecated
This commit is contained in:
@@ -15,17 +15,17 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// @title Easywish client API
|
// @title Easywish client API
|
||||||
// @version 1.0
|
// @version 1.0
|
||||||
// @description Easy and feature-rich wishlist.
|
// @description Easy and feature-rich wishlist.
|
||||||
// @license.name GPL-3.0
|
// @license.name GPL-3.0
|
||||||
|
|
||||||
// @BasePath /api/
|
// @BasePath /api/
|
||||||
// @Schemes http
|
// @Schemes http
|
||||||
|
|
||||||
// @securityDefinitions.apikey JWT
|
// @securityDefinitions.apikey JWT
|
||||||
// @in header
|
// @in header
|
||||||
// @name Authorization
|
// @name Authorization
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@@ -46,7 +46,6 @@ import (
|
|||||||
"easywish/internal/database"
|
"easywish/internal/database"
|
||||||
"easywish/internal/logger"
|
"easywish/internal/logger"
|
||||||
redisclient "easywish/internal/redisClient"
|
redisclient "easywish/internal/redisClient"
|
||||||
"easywish/internal/routes"
|
|
||||||
"easywish/internal/services"
|
"easywish/internal/services"
|
||||||
"easywish/internal/validation"
|
"easywish/internal/validation"
|
||||||
|
|
||||||
@@ -74,7 +73,6 @@ func main() {
|
|||||||
validation.Module,
|
validation.Module,
|
||||||
|
|
||||||
controllers.Module,
|
controllers.Module,
|
||||||
routes.Module,
|
|
||||||
|
|
||||||
fx.Invoke(func(lc fx.Lifecycle, router *gin.Engine, syncLogger *logger.SyncLogger) {
|
fx.Invoke(func(lc fx.Lifecycle, router *gin.Engine, syncLogger *logger.SyncLogger) {
|
||||||
|
|
||||||
|
|||||||
@@ -38,257 +38,6 @@ const docTemplate = `{
|
|||||||
"responses": {}
|
"responses": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/auth/changePassword": {
|
|
||||||
"post": {
|
|
||||||
"security": [
|
|
||||||
{
|
|
||||||
"JWT": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Set new password using the old password",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.ChangePasswordRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Password successfully changed"
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Invalid old password"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/login": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Acquire tokens via login credentials (and 2FA code if needed)",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.LoginRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.LoginResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Invalid login credentials"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/passwordResetBegin": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Request password reset email",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.PasswordResetBeginRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Reset code sent to the email if it is attached to an account"
|
|
||||||
},
|
|
||||||
"429": {
|
|
||||||
"description": "Too many recent requests for this email"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/passwordResetComplete": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Complete password reset via email code",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.PasswordResetCompleteRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.PasswordResetCompleteResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Wrong verification code or username"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/refresh": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Receive new tokens via refresh token",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RefreshRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RefreshResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"401": {
|
|
||||||
"description": "Invalid refresh token"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/registrationBegin": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Register an account",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RegistrationBeginRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Account is created and awaiting verification"
|
|
||||||
},
|
|
||||||
"409": {
|
|
||||||
"description": "Username or email is already taken"
|
|
||||||
},
|
|
||||||
"429": {
|
|
||||||
"description": "Too many recent registration attempts for this email"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/registrationComplete": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Confirm with code, finish creating the account",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RegistrationCompleteRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RegistrationCompleteResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Invalid email or verification code"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/profile": {
|
"/profile": {
|
||||||
"patch": {
|
"patch": {
|
||||||
"security": [
|
"security": [
|
||||||
@@ -395,207 +144,6 @@ const docTemplate = `{
|
|||||||
],
|
],
|
||||||
"responses": {}
|
"responses": {}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"/service/health": {
|
|
||||||
"get": {
|
|
||||||
"description": "Used internally for checking service health",
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Service"
|
|
||||||
],
|
|
||||||
"summary": "Get health status",
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Says whether it's healthy or not",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/controllers.HealthStatus"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"definitions": {
|
|
||||||
"controllers.HealthStatus": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"healthy": {
|
|
||||||
"type": "boolean"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.ChangePasswordRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"old_password",
|
|
||||||
"password"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"old_password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"totp": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.LoginRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"password",
|
|
||||||
"username"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"password": {
|
|
||||||
"type": "string",
|
|
||||||
"maxLength": 100
|
|
||||||
},
|
|
||||||
"totp": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"username": {
|
|
||||||
"type": "string",
|
|
||||||
"maxLength": 20,
|
|
||||||
"minLength": 3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.LoginResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.PasswordResetBeginRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"email"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.PasswordResetCompleteRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"email",
|
|
||||||
"password",
|
|
||||||
"verification_code"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"log_out_sessions": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"verification_code": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.PasswordResetCompleteResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RefreshRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"refresh_token"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RefreshResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RegistrationBeginRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"email",
|
|
||||||
"password",
|
|
||||||
"username"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"username": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RegistrationCompleteRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"name",
|
|
||||||
"username",
|
|
||||||
"verification_code"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"birthday": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"username": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"verification_code": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RegistrationCompleteResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securityDefinitions": {
|
"securityDefinitions": {
|
||||||
|
|||||||
@@ -34,257 +34,6 @@
|
|||||||
"responses": {}
|
"responses": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/auth/changePassword": {
|
|
||||||
"post": {
|
|
||||||
"security": [
|
|
||||||
{
|
|
||||||
"JWT": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Set new password using the old password",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.ChangePasswordRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Password successfully changed"
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Invalid old password"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/login": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Acquire tokens via login credentials (and 2FA code if needed)",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.LoginRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.LoginResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Invalid login credentials"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/passwordResetBegin": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Request password reset email",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.PasswordResetBeginRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Reset code sent to the email if it is attached to an account"
|
|
||||||
},
|
|
||||||
"429": {
|
|
||||||
"description": "Too many recent requests for this email"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/passwordResetComplete": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Complete password reset via email code",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.PasswordResetCompleteRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.PasswordResetCompleteResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Wrong verification code or username"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/refresh": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Receive new tokens via refresh token",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RefreshRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RefreshResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"401": {
|
|
||||||
"description": "Invalid refresh token"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/registrationBegin": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Register an account",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RegistrationBeginRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Account is created and awaiting verification"
|
|
||||||
},
|
|
||||||
"409": {
|
|
||||||
"description": "Username or email is already taken"
|
|
||||||
},
|
|
||||||
"429": {
|
|
||||||
"description": "Too many recent registration attempts for this email"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/auth/registrationComplete": {
|
|
||||||
"post": {
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Auth"
|
|
||||||
],
|
|
||||||
"summary": "Confirm with code, finish creating the account",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"description": " ",
|
|
||||||
"name": "request",
|
|
||||||
"in": "body",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RegistrationCompleteRequest"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": " ",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/models.RegistrationCompleteResponse"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"403": {
|
|
||||||
"description": "Invalid email or verification code"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"/profile": {
|
"/profile": {
|
||||||
"patch": {
|
"patch": {
|
||||||
"security": [
|
"security": [
|
||||||
@@ -391,207 +140,6 @@
|
|||||||
],
|
],
|
||||||
"responses": {}
|
"responses": {}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"/service/health": {
|
|
||||||
"get": {
|
|
||||||
"description": "Used internally for checking service health",
|
|
||||||
"consumes": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"produces": [
|
|
||||||
"application/json"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
"Service"
|
|
||||||
],
|
|
||||||
"summary": "Get health status",
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Says whether it's healthy or not",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/controllers.HealthStatus"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"definitions": {
|
|
||||||
"controllers.HealthStatus": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"healthy": {
|
|
||||||
"type": "boolean"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.ChangePasswordRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"old_password",
|
|
||||||
"password"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"old_password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"totp": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.LoginRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"password",
|
|
||||||
"username"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"password": {
|
|
||||||
"type": "string",
|
|
||||||
"maxLength": 100
|
|
||||||
},
|
|
||||||
"totp": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"username": {
|
|
||||||
"type": "string",
|
|
||||||
"maxLength": 20,
|
|
||||||
"minLength": 3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.LoginResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.PasswordResetBeginRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"email"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.PasswordResetCompleteRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"email",
|
|
||||||
"password",
|
|
||||||
"verification_code"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"log_out_sessions": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"verification_code": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.PasswordResetCompleteResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RefreshRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"refresh_token"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RefreshResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RegistrationBeginRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"email",
|
|
||||||
"password",
|
|
||||||
"username"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"password": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"username": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RegistrationCompleteRequest": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"name",
|
|
||||||
"username",
|
|
||||||
"verification_code"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"birthday": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"username": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"verification_code": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.RegistrationCompleteResponse": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"access_token": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securityDefinitions": {
|
"securityDefinitions": {
|
||||||
|
|||||||
@@ -1,122 +1,4 @@
|
|||||||
basePath: /api/
|
basePath: /api/
|
||||||
definitions:
|
|
||||||
controllers.HealthStatus:
|
|
||||||
properties:
|
|
||||||
healthy:
|
|
||||||
type: boolean
|
|
||||||
type: object
|
|
||||||
models.ChangePasswordRequest:
|
|
||||||
properties:
|
|
||||||
old_password:
|
|
||||||
type: string
|
|
||||||
password:
|
|
||||||
type: string
|
|
||||||
totp:
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- old_password
|
|
||||||
- password
|
|
||||||
type: object
|
|
||||||
models.LoginRequest:
|
|
||||||
properties:
|
|
||||||
password:
|
|
||||||
maxLength: 100
|
|
||||||
type: string
|
|
||||||
totp:
|
|
||||||
type: string
|
|
||||||
username:
|
|
||||||
maxLength: 20
|
|
||||||
minLength: 3
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- password
|
|
||||||
- username
|
|
||||||
type: object
|
|
||||||
models.LoginResponse:
|
|
||||||
properties:
|
|
||||||
access_token:
|
|
||||||
type: string
|
|
||||||
refresh_token:
|
|
||||||
type: string
|
|
||||||
type: object
|
|
||||||
models.PasswordResetBeginRequest:
|
|
||||||
properties:
|
|
||||||
email:
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- email
|
|
||||||
type: object
|
|
||||||
models.PasswordResetCompleteRequest:
|
|
||||||
properties:
|
|
||||||
email:
|
|
||||||
type: string
|
|
||||||
log_out_sessions:
|
|
||||||
type: boolean
|
|
||||||
password:
|
|
||||||
type: string
|
|
||||||
verification_code:
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- email
|
|
||||||
- password
|
|
||||||
- verification_code
|
|
||||||
type: object
|
|
||||||
models.PasswordResetCompleteResponse:
|
|
||||||
properties:
|
|
||||||
access_token:
|
|
||||||
type: string
|
|
||||||
refresh_token:
|
|
||||||
type: string
|
|
||||||
type: object
|
|
||||||
models.RefreshRequest:
|
|
||||||
properties:
|
|
||||||
refresh_token:
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- refresh_token
|
|
||||||
type: object
|
|
||||||
models.RefreshResponse:
|
|
||||||
properties:
|
|
||||||
access_token:
|
|
||||||
type: string
|
|
||||||
refresh_token:
|
|
||||||
type: string
|
|
||||||
type: object
|
|
||||||
models.RegistrationBeginRequest:
|
|
||||||
properties:
|
|
||||||
email:
|
|
||||||
type: string
|
|
||||||
password:
|
|
||||||
type: string
|
|
||||||
username:
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- email
|
|
||||||
- password
|
|
||||||
- username
|
|
||||||
type: object
|
|
||||||
models.RegistrationCompleteRequest:
|
|
||||||
properties:
|
|
||||||
birthday:
|
|
||||||
type: string
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
username:
|
|
||||||
type: string
|
|
||||||
verification_code:
|
|
||||||
type: string
|
|
||||||
required:
|
|
||||||
- name
|
|
||||||
- username
|
|
||||||
- verification_code
|
|
||||||
type: object
|
|
||||||
models.RegistrationCompleteResponse:
|
|
||||||
properties:
|
|
||||||
access_token:
|
|
||||||
type: string
|
|
||||||
refresh_token:
|
|
||||||
type: string
|
|
||||||
type: object
|
|
||||||
info:
|
info:
|
||||||
contact: {}
|
contact: {}
|
||||||
description: Easy and feature-rich wishlist.
|
description: Easy and feature-rich wishlist.
|
||||||
@@ -137,165 +19,6 @@ paths:
|
|||||||
summary: Change account password
|
summary: Change account password
|
||||||
tags:
|
tags:
|
||||||
- Account
|
- Account
|
||||||
/auth/changePassword:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.ChangePasswordRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: Password successfully changed
|
|
||||||
"403":
|
|
||||||
description: Invalid old password
|
|
||||||
security:
|
|
||||||
- JWT: []
|
|
||||||
summary: Set new password using the old password
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/auth/login:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.LoginRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: ' '
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.LoginResponse'
|
|
||||||
"403":
|
|
||||||
description: Invalid login credentials
|
|
||||||
summary: Acquire tokens via login credentials (and 2FA code if needed)
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/auth/passwordResetBegin:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.PasswordResetBeginRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: Reset code sent to the email if it is attached to an account
|
|
||||||
"429":
|
|
||||||
description: Too many recent requests for this email
|
|
||||||
summary: Request password reset email
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/auth/passwordResetComplete:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.PasswordResetCompleteRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: ' '
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.PasswordResetCompleteResponse'
|
|
||||||
"403":
|
|
||||||
description: Wrong verification code or username
|
|
||||||
summary: Complete password reset via email code
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/auth/refresh:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.RefreshRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: ' '
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.RefreshResponse'
|
|
||||||
"401":
|
|
||||||
description: Invalid refresh token
|
|
||||||
summary: Receive new tokens via refresh token
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/auth/registrationBegin:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.RegistrationBeginRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: Account is created and awaiting verification
|
|
||||||
"409":
|
|
||||||
description: Username or email is already taken
|
|
||||||
"429":
|
|
||||||
description: Too many recent registration attempts for this email
|
|
||||||
summary: Register an account
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/auth/registrationComplete:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
parameters:
|
|
||||||
- description: ' '
|
|
||||||
in: body
|
|
||||||
name: request
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.RegistrationCompleteRequest'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: ' '
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/models.RegistrationCompleteResponse'
|
|
||||||
"403":
|
|
||||||
description: Invalid email or verification code
|
|
||||||
summary: Confirm with code, finish creating the account
|
|
||||||
tags:
|
|
||||||
- Auth
|
|
||||||
/profile:
|
/profile:
|
||||||
patch:
|
patch:
|
||||||
consumes:
|
consumes:
|
||||||
@@ -361,21 +84,6 @@ paths:
|
|||||||
summary: Update profile privacy settings
|
summary: Update profile privacy settings
|
||||||
tags:
|
tags:
|
||||||
- Profile
|
- Profile
|
||||||
/service/health:
|
|
||||||
get:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
description: Used internally for checking service health
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"200":
|
|
||||||
description: Says whether it's healthy or not
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/controllers.HealthStatus'
|
|
||||||
summary: Get health status
|
|
||||||
tags:
|
|
||||||
- Service
|
|
||||||
schemes:
|
schemes:
|
||||||
- http
|
- http
|
||||||
securityDefinitions:
|
securityDefinitions:
|
||||||
|
|||||||
@@ -23,12 +23,12 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @Summary Change account password
|
// @Summary Change account password
|
||||||
// @Tags Account
|
// @Tags Account
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security JWT
|
// @Security JWT
|
||||||
// @Router /account/changePassword [put]
|
// @Router /account/changePassword [put]
|
||||||
func ChangePassword(c *gin.Context) {
|
func ChangePassword(c *gin.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
errs "easywish/internal/errors"
|
errs "easywish/internal/errors"
|
||||||
"easywish/internal/middleware"
|
|
||||||
"easywish/internal/models"
|
"easywish/internal/models"
|
||||||
"easywish/internal/services"
|
"easywish/internal/services"
|
||||||
"easywish/internal/utils"
|
"easywish/internal/utils"
|
||||||
@@ -31,249 +30,258 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthController interface {
|
func NewAuthController(log *zap.Logger, auth services.AuthService) Controller {
|
||||||
RegistrationBegin(c *gin.Context)
|
return &controllerImpl{
|
||||||
RegistrationComplete(c *gin.Context)
|
Path: "/auth",
|
||||||
Login(c *gin.Context)
|
Middleware: []gin.HandlerFunc{},
|
||||||
Refresh(c *gin.Context)
|
Methods: []ControllerMethod{
|
||||||
PasswordResetBegin(c *gin.Context)
|
|
||||||
PasswordResetComplete(c *gin.Context)
|
// @Summary Register an account
|
||||||
ChangePassword(c *gin.Context)
|
// @Tags Auth
|
||||||
Router
|
// @Accept json
|
||||||
}
|
// @Produce json
|
||||||
|
// @Param request body models.RegistrationBeginRequest true " "
|
||||||
type authControllerImpl struct {
|
// @Success 200 "Account is created and awaiting verification"
|
||||||
log *zap.Logger
|
// @Failure 409 "Username or email is already taken"
|
||||||
auth services.AuthService
|
// @Failure 429 "Too many recent registration attempts for this email"
|
||||||
}
|
// @Router /auth/registrationBegin [post]
|
||||||
|
{
|
||||||
func NewAuthController(_log *zap.Logger, _auth services.AuthService) AuthController {
|
HttpMethod: POST,
|
||||||
return &authControllerImpl{log: _log, auth: _auth}
|
Path: "/registrationBegin",
|
||||||
}
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
// @Summary Acquire tokens via login credentials (and 2FA code if needed)
|
Function: func(c *gin.Context) {
|
||||||
// @Tags Auth
|
|
||||||
// @Accept json
|
request, err := GetRequest[models.RegistrationBeginRequest](c); if err != nil {
|
||||||
// @Produce json
|
return
|
||||||
// @Param request body models.LoginRequest true " "
|
}
|
||||||
// @Success 200 {object} models.LoginResponse " "
|
|
||||||
// @Failure 403 "Invalid login credentials"
|
_, err = auth.RegistrationBegin(request.Body); if err != nil {
|
||||||
// @Router /auth/login [post]
|
if errors.Is(err, errs.ErrUsernameTaken) || errors.Is(err, errs.ErrEmailTaken) {
|
||||||
func (a *authControllerImpl) Login(c *gin.Context) {
|
c.Status(http.StatusConflict)
|
||||||
request, ok := utils.GetRequest[models.LoginRequest](c)
|
} else {
|
||||||
if !ok {
|
c.Status(http.StatusInternalServerError)
|
||||||
c.Status(http.StatusBadRequest)
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := a.auth.Login(request.User, request.Body)
|
c.Status(http.StatusOK)
|
||||||
|
return
|
||||||
if err != nil {
|
},
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
},
|
||||||
c.Status(http.StatusForbidden)
|
|
||||||
} else {
|
// @Summary Confirm with code, finish creating the account
|
||||||
c.Status(http.StatusInternalServerError)
|
// @Tags Auth
|
||||||
}
|
// @Accept json
|
||||||
return
|
// @Produce json
|
||||||
}
|
// @Param request body models.RegistrationCompleteRequest true " "
|
||||||
|
// @Success 200 {object} models.RegistrationCompleteResponse " "
|
||||||
c.JSON(http.StatusOK, response)
|
// @Failure 403 "Invalid email or verification code"
|
||||||
}
|
// @Router /auth/registrationComplete [post]
|
||||||
|
{
|
||||||
// @Summary Request password reset email
|
HttpMethod: POST,
|
||||||
// @Tags Auth
|
Path: "/registrationComplete",
|
||||||
// @Accept json
|
Authorization: enums.GuestRole,
|
||||||
// @Produce json
|
Middleware: []gin.HandlerFunc{},
|
||||||
// @Param request body models.PasswordResetBeginRequest true " "
|
Function: func(c *gin.Context) {
|
||||||
// @Router /auth/passwordResetBegin [post]
|
|
||||||
// @Success 200 "Reset code sent to the email if it is attached to an account"
|
request, err := GetRequest[models.RegistrationCompleteRequest](c); if err != nil {
|
||||||
// @Failure 429 "Too many recent requests for this email"
|
return
|
||||||
func (a *authControllerImpl) PasswordResetBegin(c *gin.Context) {
|
}
|
||||||
request, ok := utils.GetRequest[models.PasswordResetBeginRequest](c)
|
|
||||||
if !ok {
|
response, err := auth.RegistrationComplete(request.User, request.Body)
|
||||||
c.Status(http.StatusBadRequest)
|
|
||||||
return
|
if err != nil {
|
||||||
}
|
if errors.Is(err, errs.ErrForbidden) {
|
||||||
|
c.Status(http.StatusForbidden)
|
||||||
response, err := a.auth.PasswordResetBegin(request.Body)
|
} else if errors.Is(err, errs.ErrUnauthorized) {
|
||||||
if err != nil {
|
c.Status(http.StatusUnauthorized)
|
||||||
if errors.Is(err, errs.ErrTooManyRequests) {
|
} else {
|
||||||
c.Status(http.StatusTooManyRequests)
|
c.Status(http.StatusInternalServerError)
|
||||||
} else {
|
}
|
||||||
c.Status(http.StatusInternalServerError)
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
c.JSON(http.StatusOK, response)
|
||||||
|
},
|
||||||
c.JSON(http.StatusOK, response)
|
},
|
||||||
}
|
|
||||||
|
// @Summary Acquire tokens via login credentials (and 2FA code if needed)
|
||||||
// @Summary Complete password reset via email code
|
// @Tags Auth
|
||||||
// @Tags Auth
|
// @Accept json
|
||||||
// @Accept json
|
// @Produce json
|
||||||
// @Produce json
|
// @Param request body models.LoginRequest true " "
|
||||||
// @Param request body models.PasswordResetCompleteRequest true " "
|
// @Success 200 {object} models.LoginResponse " "
|
||||||
// @Router /auth/passwordResetComplete [post]
|
// @Failure 403 "Invalid login credentials"
|
||||||
// @Success 200 {object} models.PasswordResetCompleteResponse " "
|
// @Router /auth/login [post]
|
||||||
// @Success 403 "Wrong verification code or username"
|
{
|
||||||
func (a *authControllerImpl) PasswordResetComplete(c *gin.Context) {
|
HttpMethod: POST,
|
||||||
|
Path: "/login",
|
||||||
request, ok := utils.GetRequest[models.PasswordResetCompleteRequest](c)
|
Authorization: enums.GuestRole,
|
||||||
if !ok {
|
Middleware: []gin.HandlerFunc{},
|
||||||
c.Status(http.StatusBadRequest)
|
Function: func(c *gin.Context) {
|
||||||
return
|
request, err := GetRequest[models.LoginRequest](c); if err != nil {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
response, err := a.auth.PasswordResetComplete(request.Body)
|
|
||||||
if err != nil {
|
response, err := auth.Login(request.User, request.Body)
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
|
||||||
c.Status(http.StatusForbidden)
|
if err != nil {
|
||||||
} else {
|
if errors.Is(err, errs.ErrForbidden) {
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusForbidden)
|
||||||
}
|
} else {
|
||||||
return
|
c.Status(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
c.JSON(http.StatusOK, response)
|
}
|
||||||
}
|
|
||||||
|
c.JSON(http.StatusOK, response)
|
||||||
|
},
|
||||||
// @Summary Receive new tokens via refresh token
|
},
|
||||||
// @Tags Auth
|
|
||||||
// @Accept json
|
// @Summary Receive new tokens via refresh token
|
||||||
// @Produce json
|
// @Tags Auth
|
||||||
// @Param request body models.RefreshRequest true " "
|
// @Accept json
|
||||||
// @Router /auth/refresh [post]
|
// @Produce json
|
||||||
// @Success 200 {object} models.RefreshResponse " "
|
// @Param request body models.RefreshRequest true " "
|
||||||
// @Failure 401 "Invalid refresh token"
|
// @Router /auth/refresh [post]
|
||||||
func (a *authControllerImpl) Refresh(c *gin.Context) {
|
// @Success 200 {object} models.RefreshResponse " "
|
||||||
|
// @Failure 401 "Invalid refresh token"
|
||||||
request, ok := utils.GetRequest[models.RefreshRequest](c)
|
{
|
||||||
if !ok {
|
HttpMethod: POST,
|
||||||
c.Status(http.StatusBadRequest)
|
Path: "/refresh",
|
||||||
return
|
Authorization: enums.GuestRole,
|
||||||
}
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: func(c *gin.Context) {
|
||||||
response, err := a.auth.Refresh(request.Body)
|
|
||||||
if err != nil {
|
request, err := GetRequest[models.RefreshRequest](c); if err != nil {
|
||||||
if utils.ErrorIsOneOf(
|
return
|
||||||
err,
|
}
|
||||||
errs.ErrTokenExpired,
|
|
||||||
errs.ErrTokenInvalid,
|
response, err := auth.Refresh(request.Body)
|
||||||
errs.ErrInvalidToken,
|
if err != nil {
|
||||||
errs.ErrWrongTokenType,
|
if utils.ErrorIsOneOf(
|
||||||
errs.ErrSessionNotFound,
|
err,
|
||||||
errs.ErrSessionTerminated,
|
errs.ErrTokenExpired,
|
||||||
) {
|
errs.ErrTokenInvalid,
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
errs.ErrInvalidToken,
|
||||||
} else {
|
errs.ErrWrongTokenType,
|
||||||
c.JSON(http.StatusInternalServerError, err.Error())
|
errs.ErrSessionNotFound,
|
||||||
}
|
errs.ErrSessionTerminated,
|
||||||
return
|
) {
|
||||||
}
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||||
|
} else {
|
||||||
c.JSON(http.StatusOK, response)
|
c.JSON(http.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
return
|
||||||
// @Summary Register an account
|
}
|
||||||
// @Tags Auth
|
|
||||||
// @Accept json
|
c.JSON(http.StatusOK, response)
|
||||||
// @Produce json
|
},
|
||||||
// @Param request body models.RegistrationBeginRequest true " "
|
},
|
||||||
// @Success 200 "Account is created and awaiting verification"
|
|
||||||
// @Failure 409 "Username or email is already taken"
|
// @Summary Request password reset email
|
||||||
// @Failure 429 "Too many recent registration attempts for this email"
|
// @Tags Auth
|
||||||
// @Router /auth/registrationBegin [post]
|
// @Accept json
|
||||||
func (a *authControllerImpl) RegistrationBegin(c *gin.Context) {
|
// @Produce json
|
||||||
|
// @Param request body models.PasswordResetBeginRequest true " "
|
||||||
request, ok := utils.GetRequest[models.RegistrationBeginRequest](c)
|
// @Router /auth/passwordResetBegin [post]
|
||||||
if !ok {
|
// @Success 200 "Reset code sent to the email if it is attached to an account"
|
||||||
c.Status(http.StatusBadRequest)
|
// @Failure 429 "Too many recent requests for this email"
|
||||||
return
|
{
|
||||||
}
|
HttpMethod: POST,
|
||||||
|
Path: "/passwordResetBegin",
|
||||||
_, err := a.auth.RegistrationBegin(request.Body)
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
if err != nil {
|
Function: func(c *gin.Context) {
|
||||||
if errors.Is(err, errs.ErrUsernameTaken) || errors.Is(err, errs.ErrEmailTaken) {
|
|
||||||
c.Status(http.StatusConflict)
|
request, err := GetRequest[models.PasswordResetBeginRequest](c); if err != nil {
|
||||||
} else {
|
return
|
||||||
c.Status(http.StatusInternalServerError)
|
}
|
||||||
}
|
|
||||||
return
|
response, err := auth.PasswordResetBegin(request.Body)
|
||||||
}
|
if err != nil {
|
||||||
|
if errors.Is(err, errs.ErrTooManyRequests) {
|
||||||
c.Status(http.StatusOK)
|
c.Status(http.StatusTooManyRequests)
|
||||||
return
|
} else {
|
||||||
}
|
c.Status(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
// @Summary Confirm with code, finish creating the account
|
return
|
||||||
// @Tags Auth
|
}
|
||||||
// @Accept json
|
|
||||||
// @Produce json
|
c.JSON(http.StatusOK, response)
|
||||||
// @Param request body models.RegistrationCompleteRequest true " "
|
},
|
||||||
// @Success 200 {object} models.RegistrationCompleteResponse " "
|
},
|
||||||
// @Failure 403 "Invalid email or verification code"
|
|
||||||
// @Router /auth/registrationComplete [post]
|
|
||||||
func (a *authControllerImpl) RegistrationComplete(c *gin.Context) {
|
// @Summary Complete password reset via email code
|
||||||
request, ok := utils.GetRequest[models.RegistrationCompleteRequest](c)
|
// @Tags Auth
|
||||||
if !ok {
|
// @Accept json
|
||||||
c.Status(http.StatusBadRequest)
|
// @Produce json
|
||||||
return
|
// @Param request body models.PasswordResetCompleteRequest true " "
|
||||||
}
|
// @Router /auth/passwordResetComplete [post]
|
||||||
|
// @Success 200 {object} models.PasswordResetCompleteResponse " "
|
||||||
response, err := a.auth.RegistrationComplete(request.User, request.Body)
|
// @Success 403 "Wrong verification code or username"
|
||||||
|
{
|
||||||
if err != nil {
|
HttpMethod: POST,
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
Path: "/passwordResetComplete",
|
||||||
c.Status(http.StatusForbidden)
|
Authorization: enums.GuestRole,
|
||||||
} else if errors.Is(err, errs.ErrUnauthorized) {
|
Middleware: []gin.HandlerFunc{},
|
||||||
c.Status(http.StatusUnauthorized)
|
Function: func(c *gin.Context) {
|
||||||
} else {
|
|
||||||
c.Status(http.StatusInternalServerError)
|
request, err := GetRequest[models.PasswordResetCompleteRequest](c); if err != nil {
|
||||||
}
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
response, err := auth.PasswordResetComplete(request.Body)
|
||||||
c.JSON(http.StatusOK, response)
|
if err != nil {
|
||||||
}
|
if errors.Is(err, errs.ErrForbidden) {
|
||||||
|
c.Status(http.StatusForbidden)
|
||||||
// @Summary Set new password using the old password
|
} else {
|
||||||
// @Tags Auth
|
c.Status(http.StatusInternalServerError)
|
||||||
// @Accept json
|
}
|
||||||
// @Produce json
|
return
|
||||||
// @Security JWT
|
}
|
||||||
// @Param request body models.ChangePasswordRequest true " "
|
|
||||||
// @Success 200 "Password successfully changed"
|
c.JSON(http.StatusOK, response)
|
||||||
// @Failure 403 "Invalid old password"
|
},
|
||||||
// @Router /auth/changePassword [post]
|
},
|
||||||
func (a *authControllerImpl) ChangePassword(c *gin.Context) {
|
|
||||||
request, ok := utils.GetRequest[models.ChangePasswordRequest](c)
|
// @Summary Set new password using the old password
|
||||||
if !ok {
|
// @Tags Auth
|
||||||
c.Status(http.StatusBadRequest)
|
// @Accept json
|
||||||
return
|
// @Produce json
|
||||||
}
|
// @Security JWT
|
||||||
|
// @Param request body models.ChangePasswordRequest true " "
|
||||||
response, err := a.auth.ChangePassword(request.Body, request.User)
|
// @Success 200 "Password successfully changed"
|
||||||
|
// @Failure 403 "Invalid old password"
|
||||||
if err != nil {
|
// @Router /auth/changePassword [post]
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
{
|
||||||
c.Status(http.StatusForbidden)
|
HttpMethod: POST,
|
||||||
} else {
|
Path: "/changePassword",
|
||||||
c.Status(http.StatusInternalServerError)
|
Authorization: enums.UserRole,
|
||||||
}
|
Middleware: []gin.HandlerFunc{},
|
||||||
return
|
Function: func(c *gin.Context) {
|
||||||
}
|
|
||||||
|
request, err := GetRequest[models.ChangePasswordRequest](c); if err != nil {
|
||||||
c.JSON(http.StatusOK, response)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authControllerImpl) RegisterRoutes(group *gin.RouterGroup) {
|
response, err := auth.ChangePassword(request.Body, request.User)
|
||||||
group.POST("/registrationBegin", middleware.RequestMiddleware[models.RegistrationBeginRequest](enums.GuestRole), a.RegistrationBegin)
|
|
||||||
group.POST("/registrationComplete", middleware.RequestMiddleware[models.RegistrationCompleteRequest](enums.GuestRole), a.RegistrationComplete)
|
if err != nil {
|
||||||
group.POST("/login", middleware.RequestMiddleware[models.LoginRequest](enums.GuestRole), a.Login)
|
if errors.Is(err, errs.ErrForbidden) {
|
||||||
group.POST("/refresh", middleware.RequestMiddleware[models.RefreshRequest](enums.GuestRole), a.Refresh)
|
c.Status(http.StatusForbidden)
|
||||||
group.POST("/passwordResetBegin", middleware.RequestMiddleware[models.PasswordResetBeginRequest](enums.GuestRole), a.PasswordResetBegin)
|
} else {
|
||||||
group.POST("/passwordResetComplete", middleware.RequestMiddleware[models.PasswordResetCompleteRequest](enums.GuestRole), a.PasswordResetComplete)
|
c.Status(http.StatusInternalServerError)
|
||||||
group.POST("/changePassword", middleware.RequestMiddleware[models.ChangePasswordRequest](enums.UserRole), a.ChangePassword)
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, response)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"easywish/internal/dto"
|
"easywish/internal/dto"
|
||||||
"easywish/internal/middleware"
|
|
||||||
"easywish/internal/services"
|
"easywish/internal/services"
|
||||||
"easywish/internal/utils/enums"
|
"easywish/internal/utils/enums"
|
||||||
"easywish/internal/validation"
|
"easywish/internal/validation"
|
||||||
@@ -48,39 +47,16 @@ type ControllerMethod struct {
|
|||||||
|
|
||||||
type controllerImpl struct {
|
type controllerImpl struct {
|
||||||
Path string
|
Path string
|
||||||
Authorization enums.Role
|
|
||||||
Middleware []gin.HandlerFunc
|
Middleware []gin.HandlerFunc
|
||||||
Methods []ControllerMethod
|
Methods []ControllerMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService) *gin.RouterGroup {
|
type Controller interface {
|
||||||
|
Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService) {
|
||||||
ctrlGroup := group.Group(ctrl.Path)
|
ctrlGroup := group.Group(ctrl.Path)
|
||||||
ctrlGroup.Use(middleware.AuthMiddleware(log, auth))
|
|
||||||
ctrlGroup.Use(gin.HandlerFunc(func(c *gin.Context) {
|
|
||||||
ip := c.ClientIP()
|
|
||||||
userAgent := c.Request.UserAgent()
|
|
||||||
sessionInfoFromCtx, ok := c.Get("session_info"); if !ok {
|
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid or missing session data"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionInfo := sessionInfoFromCtx.(dto.SessionInfo)
|
|
||||||
|
|
||||||
if sessionInfo.Role < ctrl.Authorization {
|
|
||||||
c.AbortWithStatusJSON(
|
|
||||||
http.StatusForbidden,
|
|
||||||
gin.H{"error": "Insufficient authorization for this controller"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Set("client_info", dto.ClientInfo{
|
|
||||||
SessionInfo: sessionInfo,
|
|
||||||
IP: ip,
|
|
||||||
UserAgent: userAgent,
|
|
||||||
})
|
|
||||||
|
|
||||||
c.Next()
|
|
||||||
}))
|
|
||||||
ctrlGroup.Use(ctrl.Middleware...)
|
ctrlGroup.Use(ctrl.Middleware...)
|
||||||
|
|
||||||
for _, method := range ctrl.Methods {
|
for _, method := range ctrl.Methods {
|
||||||
@@ -100,11 +76,6 @@ func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth
|
|||||||
}),
|
}),
|
||||||
method.Function)...,
|
method.Function)...,
|
||||||
)}
|
)}
|
||||||
return ctrlGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
type Controller interface {
|
|
||||||
Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRequest[ModelT any](c *gin.Context) (*dto.Request[ModelT], error) {
|
func GetRequest[ModelT any](c *gin.Context) (*dto.Request[ModelT], error) {
|
||||||
|
|||||||
@@ -39,53 +39,53 @@ func NewProfileController() ProfileController {
|
|||||||
return &profileControllerImpl{}
|
return &profileControllerImpl{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Get someone's profile details
|
// @Summary Get someone's profile details
|
||||||
// @Tags Profile
|
// @Tags Profile
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param username path string true "Username"
|
// @Param username path string true "Username"
|
||||||
// @Security JWT
|
// @Security JWT
|
||||||
// @Router /profile/{username} [get]
|
// @Router /profile/{username} [get]
|
||||||
func (p *profileControllerImpl) GetProfile(c *gin.Context) {
|
func (p *profileControllerImpl) GetProfile(c *gin.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Get own profile when authorized
|
// @Summary Get own profile when authorized
|
||||||
// @Tags Profile
|
// @Tags Profile
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security JWT
|
// @Security JWT
|
||||||
// @Router /profile/me [get]
|
// @Router /profile/me [get]
|
||||||
func (p *profileControllerImpl) GetOwnProfile(c *gin.Context) {
|
func (p *profileControllerImpl) GetOwnProfile(c *gin.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Update profile
|
// @Summary Update profile
|
||||||
// @Tags Profile
|
// @Tags Profile
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security JWT
|
// @Security JWT
|
||||||
// @Router /profile [patch]
|
// @Router /profile [patch]
|
||||||
func (p *profileControllerImpl) UpdateProfile(c *gin.Context) {
|
func (p *profileControllerImpl) UpdateProfile(c *gin.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Get profile privacy settings
|
// @Summary Get profile privacy settings
|
||||||
// @Tags Profile
|
// @Tags Profile
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security JWT
|
// @Security JWT
|
||||||
// @Router /profile/privacy [get]
|
// @Router /profile/privacy [get]
|
||||||
func (p *profileControllerImpl) GetPrivacySettings(c *gin.Context) {
|
func (p *profileControllerImpl) GetPrivacySettings(c *gin.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Update profile privacy settings
|
// @Summary Update profile privacy settings
|
||||||
// @Tags Profile
|
// @Tags Profile
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security JWT
|
// @Security JWT
|
||||||
// @Router /profile/privacy [patch]
|
// @Router /profile/privacy [patch]
|
||||||
func (p *profileControllerImpl) UpdatePrivacySettings(c *gin.Context) {
|
func (p *profileControllerImpl) UpdatePrivacySettings(c *gin.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,56 +1,56 @@
|
|||||||
// Copyright (c) 2025 Nikolai Papin
|
// Copyright (c) 2025 Nikolai Papin
|
||||||
//
|
//
|
||||||
// This file is part of Easywish
|
// This file is part of Easywish
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||||
// the GNU General Public License for more details.
|
// the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"easywish/internal/models"
|
||||||
|
"easywish/internal/utils/enums"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceController interface {
|
func NewServiceController() Controller {
|
||||||
HealthCheck(c *gin.Context)
|
return &controllerImpl{
|
||||||
Router
|
Path: "/service",
|
||||||
}
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Methods: []ControllerMethod{
|
||||||
|
|
||||||
type serviceControllerImpl struct{}
|
// Health godoc
|
||||||
|
// @Summary Get health status
|
||||||
|
// @Description Used internally for checking service health
|
||||||
|
// @Tags Service
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} HealthStatus "Says whether it's healthy or not"
|
||||||
|
// @Router /service/health [get]
|
||||||
|
{
|
||||||
|
HttpMethod: GET,
|
||||||
|
Path: "/health",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: func(c *gin.Context) {
|
||||||
|
|
||||||
func NewServiceController() ServiceController {
|
c.JSON(http.StatusOK, models.HealthStatusResponse{Healthy: true,})
|
||||||
return &serviceControllerImpl{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HealthCheck implements ServiceController.
|
},
|
||||||
// @Summary Get health status
|
},
|
||||||
// @Description Used internally for checking service health
|
|
||||||
// @Tags Service
|
|
||||||
// @Accept json
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200 {object} HealthStatus "Says whether it's healthy or not"
|
|
||||||
// @Router /service/health [get]
|
|
||||||
func (s *serviceControllerImpl) HealthCheck(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"healthy": true})
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterRoutes implements ServiceController.
|
},
|
||||||
func (s *serviceControllerImpl) RegisterRoutes(group *gin.RouterGroup) {
|
}
|
||||||
group.GET("/health", s.HealthCheck)
|
|
||||||
}
|
|
||||||
|
|
||||||
type HealthStatus struct {
|
|
||||||
Healthy bool `json:"healthy"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,72 @@
|
|||||||
// Copyright (c) 2025 Nikolai Papin
|
// Copyright (c) 2025 Nikolai Papin
|
||||||
//
|
//
|
||||||
// This file is part of Easywish
|
// This file is part of Easywish
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||||
// the GNU General Public License for more details.
|
// the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"easywish/internal/dto"
|
||||||
|
"easywish/internal/middleware"
|
||||||
|
"easywish/internal/services"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SetupControllersParams struct {
|
||||||
|
fx.In
|
||||||
|
Controllers []Controller `group:"controllers"`
|
||||||
|
Log *zap.Logger
|
||||||
|
Auth services.AuthService
|
||||||
|
Group *gin.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupControllers(p SetupControllersParams) {
|
||||||
|
|
||||||
|
apiGroup := p.Group.Group("/api")
|
||||||
|
apiGroup.Use(middleware.AuthMiddleware(p.Log, p.Auth))
|
||||||
|
apiGroup.Use(gin.HandlerFunc(func(c *gin.Context) {
|
||||||
|
ip := c.ClientIP()
|
||||||
|
userAgent := c.Request.UserAgent()
|
||||||
|
sessionInfoFromCtx, ok := c.Get("session_info"); if !ok {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid or missing session data"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionInfo := sessionInfoFromCtx.(dto.SessionInfo)
|
||||||
|
|
||||||
|
c.Set("client_info", dto.ClientInfo{
|
||||||
|
SessionInfo: sessionInfo,
|
||||||
|
IP: ip,
|
||||||
|
UserAgent: userAgent,
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}))
|
||||||
|
for _, ctrl := range p.Controllers {
|
||||||
|
ctrl.Setup(apiGroup, p.Log, p.Auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var Module = fx.Module("controllers",
|
var Module = fx.Module("controllers",
|
||||||
fx.Provide(
|
fx.Provide(
|
||||||
NewServiceController,
|
fx.Annotate(NewAuthController, fx.ResultTags(`group:"controllers"`)),
|
||||||
NewAuthController,
|
fx.Annotate(NewServiceController, fx.ResultTags(`group:"controllers"`)),
|
||||||
NewProfileController,
|
),
|
||||||
),
|
fx.Invoke(setupControllers),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
|
|
||||||
const requestKey = "request"
|
const requestKey = "request"
|
||||||
|
|
||||||
|
// Deprecated: no longer used, embedded into controllers.GetRequest instead
|
||||||
func ClientInfoFromContext(c *gin.Context) (*dto.ClientInfo, bool) {
|
func ClientInfoFromContext(c *gin.Context) (*dto.ClientInfo, bool) {
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
@@ -58,10 +59,12 @@ func ClientInfoFromContext(c *gin.Context) (*dto.ClientInfo, bool) {
|
|||||||
}, true
|
}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers.GetRequest
|
||||||
func RequestFromContext[T any](c *gin.Context) dto.Request[T] {
|
func RequestFromContext[T any](c *gin.Context) dto.Request[T] {
|
||||||
return c.Value(requestKey).(dto.Request[T])
|
return c.Value(requestKey).(dto.Request[T])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers.GetRequest
|
||||||
func RequestMiddleware[T any](role enums.Role) gin.HandlerFunc {
|
func RequestMiddleware[T any](role enums.Role) gin.HandlerFunc {
|
||||||
return gin.HandlerFunc(func(c *gin.Context) {
|
return gin.HandlerFunc(func(c *gin.Context) {
|
||||||
|
|
||||||
|
|||||||
22
backend/internal/models/service.go
Normal file
22
backend/internal/models/service.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// Copyright (c) 2025 Nikolai Papin
|
||||||
|
//
|
||||||
|
// This file is part of Easywish
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||||
|
// the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package models
|
||||||
|
|
||||||
|
type HealthStatusResponse struct {
|
||||||
|
Healthy bool `json:"healthy"`
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers
|
||||||
func NewRouter(engine *gin.Engine, log *zap.Logger, auth services.AuthService, groups []RouteGroup) *gin.Engine {
|
func NewRouter(engine *gin.Engine, log *zap.Logger, auth services.AuthService, groups []RouteGroup) *gin.Engine {
|
||||||
apiGroup := engine.Group("/api")
|
apiGroup := engine.Group("/api")
|
||||||
apiGroup.Use(middleware.AuthMiddleware(log, auth))
|
apiGroup.Use(middleware.AuthMiddleware(log, auth))
|
||||||
@@ -37,29 +38,30 @@ func NewRouter(engine *gin.Engine, log *zap.Logger, auth services.AuthService, g
|
|||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers
|
||||||
type RouteGroup struct {
|
type RouteGroup struct {
|
||||||
BasePath string
|
BasePath string
|
||||||
Middleware []gin.HandlerFunc
|
Middleware []gin.HandlerFunc
|
||||||
Router controllers.Router
|
Router controllers.Router
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouteGroups(
|
// func NewRouteGroups(
|
||||||
authController controllers.AuthController,
|
// authController controllers.AuthController,
|
||||||
serviceController controllers.ServiceController,
|
// serviceController controllers.ServiceController,
|
||||||
profileController controllers.ProfileController,
|
// profileController controllers.ProfileController,
|
||||||
) []RouteGroup {
|
// ) []RouteGroup {
|
||||||
return []RouteGroup{
|
// return []RouteGroup{
|
||||||
{
|
// {
|
||||||
BasePath: "/auth",
|
// BasePath: "/auth",
|
||||||
Router: authController,
|
// Router: authController,
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
BasePath: "/service",
|
// BasePath: "/service",
|
||||||
Router: serviceController,
|
// Router: serviceController,
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
BasePath: "/profile",
|
// BasePath: "/profile",
|
||||||
Router: profileController,
|
// Router: profileController,
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -21,9 +21,10 @@ import (
|
|||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers
|
||||||
var Module = fx.Module("routes",
|
var Module = fx.Module("routes",
|
||||||
fx.Provide(
|
// fx.Provide(
|
||||||
NewRouteGroups,
|
// NewRouteGroups,
|
||||||
),
|
// ),
|
||||||
fx.Invoke(NewRouter),
|
// fx.Invoke(NewRouter),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: use controllers.GetRequest method for the new controllers
|
||||||
func GetRequest[T any](c *gin.Context) (*dto.Request[T], bool) {
|
func GetRequest[T any](c *gin.Context) (*dto.Request[T], bool) {
|
||||||
|
|
||||||
req, ok := c.Get("request")
|
req, ok := c.Get("request")
|
||||||
|
|||||||
Reference in New Issue
Block a user