Compare commits
No commits in common. "337c22254864295ab210f8c1c2414ecc567efea4" and "7762e1de89fa0ec511b8d570c410a1d6f6357764" have entirely different histories.
337c222548
...
7762e1de89
@ -1,12 +1,12 @@
|
||||
package org.yobble.scala_monolith.api.endpoint.info
|
||||
package org.yobble.scala_monolith.api.endpoint
|
||||
|
||||
import io.circe.generic.auto.*
|
||||
import io.circe.generic.auto._
|
||||
import org.yobble.scala_monolith.api.response.{BaseResponse, ErrorResponse}
|
||||
import sttp.tapir._
|
||||
import sttp.tapir.generic.auto._
|
||||
import sttp.tapir.json.circe._
|
||||
import org.yobble.scala_monolith.api.util.ErrorExamples
|
||||
import sttp.model.StatusCode
|
||||
import sttp.tapir.*
|
||||
import sttp.tapir.generic.auto.*
|
||||
import sttp.tapir.json.circe.*
|
||||
|
||||
object ErrorsEndpoint {
|
||||
val errorsEndpoint: PublicEndpoint[Unit, ErrorResponse, BaseResponse, Any] =
|
||||
@ -1,12 +1,12 @@
|
||||
package org.yobble.scala_monolith.api.endpoint.info
|
||||
package org.yobble.scala_monolith.api.endpoint
|
||||
|
||||
import io.circe.generic.auto.*
|
||||
import sttp.tapir.*
|
||||
import sttp.tapir.json.circe.jsonBody
|
||||
import sttp.tapir.generic.auto.*
|
||||
import org.yobble.scala_monolith.api.response.{BaseResponse, ErrorResponse}
|
||||
import io.circe.generic.auto.*
|
||||
import org.yobble.scala_monolith.api.util.ErrorExamples
|
||||
import sttp.model.StatusCode
|
||||
import sttp.tapir.*
|
||||
import sttp.tapir.generic.auto.*
|
||||
import sttp.tapir.json.circe.jsonBody
|
||||
|
||||
object PingEndpoint {
|
||||
val pingEndpoint = endpoint
|
||||
@ -1,33 +0,0 @@
|
||||
package org.yobble.scala_monolith.api.endpoint.info
|
||||
|
||||
import io.circe.generic.auto.*
|
||||
import org.yobble.scala_monolith.api.response.{BaseResponse, ErrorResponse}
|
||||
import org.yobble.scala_monolith.api.util.ErrorExamples
|
||||
import sttp.model.StatusCode
|
||||
import sttp.tapir.*
|
||||
import sttp.tapir.generic.auto.*
|
||||
import sttp.tapir.json.circe.*
|
||||
|
||||
object TestErrorEndpoint {
|
||||
val testErrorEndpoint: PublicEndpoint[Int, ErrorResponse, Unit, Any] =
|
||||
endpoint.get
|
||||
.in("test-error" / path[Int]("code").description("The HTTP status code to return"))
|
||||
.tags(List("Info"))
|
||||
.name("Test-Error")
|
||||
.summary("Returns a response with the specified error code")
|
||||
.errorOut(
|
||||
oneOf[ErrorResponse](
|
||||
oneOfVariant(StatusCode.BadRequest, jsonBody[ErrorResponse].description("Bad Request").example(ErrorExamples.badRequest)),
|
||||
oneOfVariant(StatusCode.Unauthorized, jsonBody[ErrorResponse].description("Unauthorized").example(ErrorExamples.unauthorized)),
|
||||
oneOfVariant(StatusCode.Forbidden, jsonBody[ErrorResponse].description("Forbidden").example(ErrorExamples.forbidden)),
|
||||
oneOfVariant(StatusCode.NotFound, jsonBody[ErrorResponse].description("Not Found").example(ErrorExamples.notFound)),
|
||||
oneOfVariant(StatusCode.MethodNotAllowed, jsonBody[ErrorResponse].description("Method Not Allowed").example(ErrorExamples.notAllowed)),
|
||||
oneOfVariant(StatusCode.Conflict, jsonBody[ErrorResponse].description("Conflict").example(ErrorExamples.conflict)),
|
||||
oneOfVariant(StatusCode(418), jsonBody[ErrorResponse].description("I'm a teapot (In Development)").example(ErrorExamples.teapot)),
|
||||
oneOfVariant(StatusCode.UnprocessableEntity, jsonBody[ErrorResponse].description("Validation Error").example(ErrorExamples.validation)),
|
||||
oneOfVariant(StatusCode.InternalServerError, jsonBody[ErrorResponse].description("Internal Server Error").example(ErrorExamples.internal)),
|
||||
oneOfVariant(StatusCode.ServiceUnavailable, jsonBody[ErrorResponse].description("Service Unavailable").example(ErrorExamples.unavailable))
|
||||
)
|
||||
)
|
||||
.out(statusCode(StatusCode.Ok))
|
||||
}
|
||||
@ -11,6 +11,6 @@ object ErrorResponse {
|
||||
implicit val errorDetailEncoder: Encoder[ErrorDetail] = deriveEncoder[ErrorDetail]
|
||||
|
||||
// Encoder без поля code
|
||||
// implicit val errorResponseEncoder: Encoder[ErrorResponse] =
|
||||
// deriveEncoder[ErrorResponse].mapJsonObject(_.remove("code"))
|
||||
implicit val errorResponseEncoder: Encoder[ErrorResponse] =
|
||||
deriveEncoder[ErrorResponse].mapJsonObject(_.remove("code"))
|
||||
}
|
||||
|
||||
@ -4,12 +4,11 @@ import cats.effect.IO
|
||||
import doobie.Transactor
|
||||
import org.yobble.scala_monolith.api.endpoint.auth.AuthEndpoints
|
||||
import sttp.tapir.server.ServerEndpoint
|
||||
import org.yobble.scala_monolith.api.endpoint.info.{ErrorsEndpoint, PingEndpoint, TestErrorEndpoint}
|
||||
import org.yobble.scala_monolith.api.response.{BaseResponse, ErrorResponse}
|
||||
import org.yobble.scala_monolith.api.endpoint.PingEndpoint
|
||||
import org.yobble.scala_monolith.api.endpoint.ErrorsEndpoint
|
||||
import org.yobble.scala_monolith.api.response.BaseResponse
|
||||
import org.yobble.scala_monolith.repository.UserRepositoryImpl
|
||||
import org.yobble.scala_monolith.service.AuthService
|
||||
import sttp.model.StatusCode
|
||||
import org.yobble.scala_monolith.api.util.errorByStatus
|
||||
|
||||
object AllServerEndpoints {
|
||||
def all(transactor: Transactor[IO]): List[ServerEndpoint[Any, IO]] = {
|
||||
@ -20,12 +19,9 @@ object AllServerEndpoints {
|
||||
AuthEndpoints.loginEndpoint.serverLogic(authService.login)
|
||||
)
|
||||
|
||||
val otherEndpoints: List[ServerEndpoint[Any, IO]] = List(
|
||||
val otherEndpoints = List(
|
||||
PingEndpoint.pingEndpoint.serverLogicSuccess(_ => IO.pure(BaseResponse(message = "pong"))),
|
||||
ErrorsEndpoint.errorsEndpoint.serverLogicSuccess(_ => IO.pure(BaseResponse(message = "errors"))),
|
||||
TestErrorEndpoint.testErrorEndpoint.serverLogic(code => {
|
||||
IO.pure(Left(errorByStatus(StatusCode(code))))
|
||||
})
|
||||
ErrorsEndpoint.errorsEndpoint.serverLogicSuccess(_ => IO.pure(BaseResponse(message = "errors")))
|
||||
)
|
||||
|
||||
authEndpoints ++ otherEndpoints
|
||||
|
||||
@ -5,34 +5,34 @@ import sttp.model.StatusCode
|
||||
|
||||
object ErrorUtils {
|
||||
def badRequest(message: String = "Bad request"): ErrorResponse =
|
||||
ErrorResponse(code = 400, errors = List(ErrorDetail("Bad request", message)))
|
||||
ErrorResponse(code = 400, errors = List(ErrorDetail("general", message)))
|
||||
|
||||
def unauthorized(message: String = "Unauthorized"): ErrorResponse =
|
||||
ErrorResponse(code = 401, errors = List(ErrorDetail("Unauthorized", message)))
|
||||
ErrorResponse(code = 401, errors = List(ErrorDetail("login", message)))
|
||||
|
||||
def forbidden(message: String = "Forbidden"): ErrorResponse =
|
||||
ErrorResponse(code = 403, errors = List(ErrorDetail("Forbidden", message)))
|
||||
ErrorResponse(code = 403, errors = List(ErrorDetail("permission", message)))
|
||||
|
||||
def notFound(message: String = "Not found"): ErrorResponse =
|
||||
ErrorResponse(code = 404, errors = List(ErrorDetail("Not found", message)))
|
||||
ErrorResponse(code = 404, errors = List(ErrorDetail("resource", message)))
|
||||
|
||||
def methodNotAllowed(message: String = "Method not allowed"): ErrorResponse =
|
||||
ErrorResponse(code = 405, errors = List(ErrorDetail("Method not allowed", message)))
|
||||
ErrorResponse(code = 405, errors = List(ErrorDetail("request", message)))
|
||||
|
||||
def conflict(message: String = "Conflict"): ErrorResponse =
|
||||
ErrorResponse(code = 409, errors = List(ErrorDetail("Conflict", message)))
|
||||
ErrorResponse(code = 409, errors = List(ErrorDetail("conflict", message)))
|
||||
|
||||
def imATeapot(message: String = "This feature is under development"): ErrorResponse =
|
||||
ErrorResponse(code = 418, errors = List(ErrorDetail("This feature is under development", message)))
|
||||
ErrorResponse(code = 418, errors = List(ErrorDetail("debug", message)))
|
||||
|
||||
def validation(errors: List[(String, String)]): ErrorResponse =
|
||||
ErrorResponse(code = 422, errors = errors.map((ErrorDetail.apply _).tupled))
|
||||
|
||||
def internalServerError(message: String = "Internal Server Error"): ErrorResponse =
|
||||
ErrorResponse(code = 500, errors = List(ErrorDetail("Internal Server Error", message)))
|
||||
ErrorResponse(code = 500, errors = List(ErrorDetail("server", message)))
|
||||
|
||||
def serviceUnavailableError(message: String = "Service unavailable"): ErrorResponse =
|
||||
ErrorResponse(code = 503, errors = List(ErrorDetail("Service unavailable", message)))
|
||||
ErrorResponse(code = 503, errors = List(ErrorDetail("proxy", message)))
|
||||
}
|
||||
|
||||
object ErrorExamples {
|
||||
@ -40,37 +40,37 @@ object ErrorExamples {
|
||||
val badRequest: ErrorResponse =
|
||||
ErrorResponse(
|
||||
code = 400,
|
||||
errors = List(ErrorDetail("Bad request", "Bad request syntax or invalid parameters"))
|
||||
errors = List(ErrorDetail("general", "Bad request syntax or invalid parameters"))
|
||||
)
|
||||
|
||||
val unauthorized: ErrorResponse = ErrorResponse(
|
||||
code = 401,
|
||||
errors = List(ErrorDetail("Unauthorized", "Invalid login or password"))
|
||||
errors = List(ErrorDetail("login", "Invalid login or password"))
|
||||
)
|
||||
|
||||
val forbidden: ErrorResponse = ErrorResponse(
|
||||
code = 403,
|
||||
errors = List(ErrorDetail("Forbidden", "You don't have access to this resource"))
|
||||
errors = List(ErrorDetail("permission", "You don't have access to this resource"))
|
||||
)
|
||||
|
||||
val notFound: ErrorResponse = ErrorResponse(
|
||||
code = 404,
|
||||
errors = List(ErrorDetail("Not found", "Requested resource not found"))
|
||||
errors = List(ErrorDetail("resource", "Requested resource not found"))
|
||||
)
|
||||
|
||||
val notAllowed: ErrorResponse = ErrorResponse(
|
||||
code = 405,
|
||||
errors = List(ErrorDetail("Method not allowed", "Method not allowed on this endpoint"))
|
||||
errors = List(ErrorDetail("request", "Method not allowed on this endpoint"))
|
||||
)
|
||||
|
||||
val conflict: ErrorResponse = ErrorResponse(
|
||||
code = 409,
|
||||
errors = List(ErrorDetail("Conflict", "Resource already exists or conflict occurred"))
|
||||
errors = List(ErrorDetail("conflict", "Resource already exists or conflict occurred"))
|
||||
)
|
||||
|
||||
val teapot: ErrorResponse = ErrorResponse(
|
||||
code = 418,
|
||||
errors = List(ErrorDetail("This feature is under development", "This feature is under development"))
|
||||
errors = List(ErrorDetail("debug", "This feature is under development"))
|
||||
)
|
||||
|
||||
val validation: ErrorResponse = ErrorResponse(
|
||||
@ -80,12 +80,12 @@ object ErrorExamples {
|
||||
|
||||
val internal: ErrorResponse = ErrorResponse(
|
||||
code = 500,
|
||||
errors = List(ErrorDetail("Internal Server Error", "An unexpected error occurred. Please try again later."))
|
||||
errors = List(ErrorDetail("server", "An unexpected error occurred. Please try again later."))
|
||||
)
|
||||
|
||||
val unavailable: ErrorResponse = ErrorResponse(
|
||||
code = 503,
|
||||
errors = List(ErrorDetail("Service unavailable", "Service unavailable."))
|
||||
errors = List(ErrorDetail("proxy", "Service unavailable."))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ import doobie.Read
|
||||
import doobie.util.meta.Meta
|
||||
import java.util.UUID
|
||||
|
||||
case class User(id: UUID, login: String, passwordHash: String, isBlocked: Boolean, isDeleted: Boolean) derives Read
|
||||
case class User(id: UUID, login: String, passwordHash: String) derives Read
|
||||
|
||||
object User {
|
||||
implicit val uuidMeta: Meta[UUID] = Meta[String].timap(UUID.fromString)(_.toString)
|
||||
|
||||
@ -12,7 +12,7 @@ trait UserRepository {
|
||||
|
||||
class UserRepositoryImpl(transactor: Transactor[IO]) extends UserRepository {
|
||||
override def findByLogin(login: String): IO[Option[User]] = {
|
||||
sql"SELECT id, login, password_hash as passwordHash, is_blocked as isBlocked, is_deleted as isDeleted FROM users WHERE login = $login"
|
||||
sql"SELECT id, login, password_hash as passwordHash FROM users WHERE login = $login"
|
||||
.query[User]
|
||||
.option
|
||||
.transact(transactor)
|
||||
|
||||
@ -8,17 +8,11 @@ class AuthService(userRepository: UserRepository) {
|
||||
|
||||
def login(request: LoginRequest): IO[Either[String, LoginResponse]] = {
|
||||
userRepository.findByLogin(request.login).map {
|
||||
case Some(user) if user.passwordHash != request.password =>
|
||||
Left("Invalid login or password")
|
||||
case Some(user) if user.isBlocked =>
|
||||
Left("User account is disabled")
|
||||
case Some(user) if user.isDeleted =>
|
||||
Left("User account is deleted")
|
||||
case Some(user) =>
|
||||
case Some(user) if user.passwordHash == request.password =>
|
||||
// TODO: Implement proper password hashing (e.g., with bcrypt)
|
||||
// TODO: Implement real token generation
|
||||
Right(LoginResponse(accessToken = "fake-access-token", refreshToken = "fake-refresh-token"))
|
||||
case None =>
|
||||
case _ =>
|
||||
Left("Invalid login or password")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user