Compare commits

..

No commits in common. "b03f9c9704d93e596b55a474ba3656f9ba8e61b9" and "1bbfa338d92b4122a658bb3487c98666aae4652a" have entirely different histories.

2 changed files with 60 additions and 60 deletions

View file

@ -13,32 +13,32 @@ import (
strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin" strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin"
) )
// PostSignInJSONBody defines parameters for PostSignIn. // PostAuthSignInJSONBody defines parameters for PostAuthSignIn.
type PostSignInJSONBody struct { type PostAuthSignInJSONBody struct {
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
Pass string `json:"pass"` Pass string `json:"pass"`
} }
// PostSignUpJSONBody defines parameters for PostSignUp. // PostAuthSignUpJSONBody defines parameters for PostAuthSignUp.
type PostSignUpJSONBody struct { type PostAuthSignUpJSONBody struct {
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
Pass string `json:"pass"` Pass string `json:"pass"`
} }
// PostSignInJSONRequestBody defines body for PostSignIn for application/json ContentType. // PostAuthSignInJSONRequestBody defines body for PostAuthSignIn for application/json ContentType.
type PostSignInJSONRequestBody PostSignInJSONBody type PostAuthSignInJSONRequestBody PostAuthSignInJSONBody
// PostSignUpJSONRequestBody defines body for PostSignUp for application/json ContentType. // PostAuthSignUpJSONRequestBody defines body for PostAuthSignUp for application/json ContentType.
type PostSignUpJSONRequestBody PostSignUpJSONBody type PostAuthSignUpJSONRequestBody PostAuthSignUpJSONBody
// ServerInterface represents all server handlers. // ServerInterface represents all server handlers.
type ServerInterface interface { type ServerInterface interface {
// Sign in a user and return JWT // Sign in a user and return JWT
// (POST /sign-in) // (POST /auth/sign-in)
PostSignIn(c *gin.Context) PostAuthSignIn(c *gin.Context)
// Sign up a new user // Sign up a new user
// (POST /sign-up) // (POST /auth/sign-up)
PostSignUp(c *gin.Context) PostAuthSignUp(c *gin.Context)
} }
// ServerInterfaceWrapper converts contexts to parameters. // ServerInterfaceWrapper converts contexts to parameters.
@ -50,8 +50,8 @@ type ServerInterfaceWrapper struct {
type MiddlewareFunc func(c *gin.Context) type MiddlewareFunc func(c *gin.Context)
// PostSignIn operation middleware // PostAuthSignIn operation middleware
func (siw *ServerInterfaceWrapper) PostSignIn(c *gin.Context) { func (siw *ServerInterfaceWrapper) PostAuthSignIn(c *gin.Context) {
for _, middleware := range siw.HandlerMiddlewares { for _, middleware := range siw.HandlerMiddlewares {
middleware(c) middleware(c)
@ -60,11 +60,11 @@ func (siw *ServerInterfaceWrapper) PostSignIn(c *gin.Context) {
} }
} }
siw.Handler.PostSignIn(c) siw.Handler.PostAuthSignIn(c)
} }
// PostSignUp operation middleware // PostAuthSignUp operation middleware
func (siw *ServerInterfaceWrapper) PostSignUp(c *gin.Context) { func (siw *ServerInterfaceWrapper) PostAuthSignUp(c *gin.Context) {
for _, middleware := range siw.HandlerMiddlewares { for _, middleware := range siw.HandlerMiddlewares {
middleware(c) middleware(c)
@ -73,7 +73,7 @@ func (siw *ServerInterfaceWrapper) PostSignUp(c *gin.Context) {
} }
} }
siw.Handler.PostSignUp(c) siw.Handler.PostAuthSignUp(c)
} }
// GinServerOptions provides options for the Gin server. // GinServerOptions provides options for the Gin server.
@ -103,54 +103,54 @@ func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options
ErrorHandler: errorHandler, ErrorHandler: errorHandler,
} }
router.POST(options.BaseURL+"/sign-in", wrapper.PostSignIn) router.POST(options.BaseURL+"/auth/sign-in", wrapper.PostAuthSignIn)
router.POST(options.BaseURL+"/sign-up", wrapper.PostSignUp) router.POST(options.BaseURL+"/auth/sign-up", wrapper.PostAuthSignUp)
} }
type PostSignInRequestObject struct { type PostAuthSignInRequestObject struct {
Body *PostSignInJSONRequestBody Body *PostAuthSignInJSONRequestBody
} }
type PostSignInResponseObject interface { type PostAuthSignInResponseObject interface {
VisitPostSignInResponse(w http.ResponseWriter) error VisitPostAuthSignInResponse(w http.ResponseWriter) error
} }
type PostSignIn200JSONResponse struct { type PostAuthSignIn200JSONResponse struct {
UserId int64 `json:"user_id"` UserId int64 `json:"user_id"`
UserName string `json:"user_name"` UserName string `json:"user_name"`
} }
func (response PostSignIn200JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error { func (response PostAuthSignIn200JSONResponse) VisitPostAuthSignInResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200) w.WriteHeader(200)
return json.NewEncoder(w).Encode(response) return json.NewEncoder(w).Encode(response)
} }
type PostSignIn401JSONResponse struct { type PostAuthSignIn401JSONResponse struct {
Error *string `json:"error,omitempty"` Error *string `json:"error,omitempty"`
} }
func (response PostSignIn401JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error { func (response PostAuthSignIn401JSONResponse) VisitPostAuthSignInResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(401) w.WriteHeader(401)
return json.NewEncoder(w).Encode(response) return json.NewEncoder(w).Encode(response)
} }
type PostSignUpRequestObject struct { type PostAuthSignUpRequestObject struct {
Body *PostSignUpJSONRequestBody Body *PostAuthSignUpJSONRequestBody
} }
type PostSignUpResponseObject interface { type PostAuthSignUpResponseObject interface {
VisitPostSignUpResponse(w http.ResponseWriter) error VisitPostAuthSignUpResponse(w http.ResponseWriter) error
} }
type PostSignUp200JSONResponse struct { type PostAuthSignUp200JSONResponse struct {
UserId int64 `json:"user_id"` UserId int64 `json:"user_id"`
} }
func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.ResponseWriter) error { func (response PostAuthSignUp200JSONResponse) VisitPostAuthSignUpResponse(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200) w.WriteHeader(200)
@ -160,11 +160,11 @@ func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.Respons
// StrictServerInterface represents all server handlers. // StrictServerInterface represents all server handlers.
type StrictServerInterface interface { type StrictServerInterface interface {
// Sign in a user and return JWT // Sign in a user and return JWT
// (POST /sign-in) // (POST /auth/sign-in)
PostSignIn(ctx context.Context, request PostSignInRequestObject) (PostSignInResponseObject, error) PostAuthSignIn(ctx context.Context, request PostAuthSignInRequestObject) (PostAuthSignInResponseObject, error)
// Sign up a new user // Sign up a new user
// (POST /sign-up) // (POST /auth/sign-up)
PostSignUp(ctx context.Context, request PostSignUpRequestObject) (PostSignUpResponseObject, error) PostAuthSignUp(ctx context.Context, request PostAuthSignUpRequestObject) (PostAuthSignUpResponseObject, error)
} }
type StrictHandlerFunc = strictgin.StrictGinHandlerFunc type StrictHandlerFunc = strictgin.StrictGinHandlerFunc
@ -179,11 +179,11 @@ type strictHandler struct {
middlewares []StrictMiddlewareFunc middlewares []StrictMiddlewareFunc
} }
// PostSignIn operation middleware // PostAuthSignIn operation middleware
func (sh *strictHandler) PostSignIn(ctx *gin.Context) { func (sh *strictHandler) PostAuthSignIn(ctx *gin.Context) {
var request PostSignInRequestObject var request PostAuthSignInRequestObject
var body PostSignInJSONRequestBody var body PostAuthSignInJSONRequestBody
if err := ctx.ShouldBindJSON(&body); err != nil { if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.Status(http.StatusBadRequest) ctx.Status(http.StatusBadRequest)
ctx.Error(err) ctx.Error(err)
@ -192,10 +192,10 @@ func (sh *strictHandler) PostSignIn(ctx *gin.Context) {
request.Body = &body request.Body = &body
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
return sh.ssi.PostSignIn(ctx, request.(PostSignInRequestObject)) return sh.ssi.PostAuthSignIn(ctx, request.(PostAuthSignInRequestObject))
} }
for _, middleware := range sh.middlewares { for _, middleware := range sh.middlewares {
handler = middleware(handler, "PostSignIn") handler = middleware(handler, "PostAuthSignIn")
} }
response, err := handler(ctx, request) response, err := handler(ctx, request)
@ -203,8 +203,8 @@ func (sh *strictHandler) PostSignIn(ctx *gin.Context) {
if err != nil { if err != nil {
ctx.Error(err) ctx.Error(err)
ctx.Status(http.StatusInternalServerError) ctx.Status(http.StatusInternalServerError)
} else if validResponse, ok := response.(PostSignInResponseObject); ok { } else if validResponse, ok := response.(PostAuthSignInResponseObject); ok {
if err := validResponse.VisitPostSignInResponse(ctx.Writer); err != nil { if err := validResponse.VisitPostAuthSignInResponse(ctx.Writer); err != nil {
ctx.Error(err) ctx.Error(err)
} }
} else if response != nil { } else if response != nil {
@ -212,11 +212,11 @@ func (sh *strictHandler) PostSignIn(ctx *gin.Context) {
} }
} }
// PostSignUp operation middleware // PostAuthSignUp operation middleware
func (sh *strictHandler) PostSignUp(ctx *gin.Context) { func (sh *strictHandler) PostAuthSignUp(ctx *gin.Context) {
var request PostSignUpRequestObject var request PostAuthSignUpRequestObject
var body PostSignUpJSONRequestBody var body PostAuthSignUpJSONRequestBody
if err := ctx.ShouldBindJSON(&body); err != nil { if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.Status(http.StatusBadRequest) ctx.Status(http.StatusBadRequest)
ctx.Error(err) ctx.Error(err)
@ -225,10 +225,10 @@ func (sh *strictHandler) PostSignUp(ctx *gin.Context) {
request.Body = &body request.Body = &body
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
return sh.ssi.PostSignUp(ctx, request.(PostSignUpRequestObject)) return sh.ssi.PostAuthSignUp(ctx, request.(PostAuthSignUpRequestObject))
} }
for _, middleware := range sh.middlewares { for _, middleware := range sh.middlewares {
handler = middleware(handler, "PostSignUp") handler = middleware(handler, "PostAuthSignUp")
} }
response, err := handler(ctx, request) response, err := handler(ctx, request)
@ -236,8 +236,8 @@ func (sh *strictHandler) PostSignUp(ctx *gin.Context) {
if err != nil { if err != nil {
ctx.Error(err) ctx.Error(err)
ctx.Status(http.StatusInternalServerError) ctx.Status(http.StatusInternalServerError)
} else if validResponse, ok := response.(PostSignUpResponseObject); ok { } else if validResponse, ok := response.(PostAuthSignUpResponseObject); ok {
if err := validResponse.VisitPostSignUpResponse(ctx.Writer); err != nil { if err := validResponse.VisitPostAuthSignUpResponse(ctx.Writer); err != nil {
ctx.Error(err) ctx.Error(err)
} }
} else if response != nil { } else if response != nil {

View file

@ -78,7 +78,7 @@ func (s Server) generateTokens(userID string) (accessToken string, refreshToken
return accessToken, refreshToken, csrfToken, nil return accessToken, refreshToken, csrfToken, nil
} }
func (s Server) PostSignUp(ctx context.Context, req auth.PostSignUpRequestObject) (auth.PostSignUpResponseObject, error) { func (s Server) PostAuthSignUp(ctx context.Context, req auth.PostAuthSignUpRequestObject) (auth.PostAuthSignUpResponseObject, error) {
passhash, err := HashPassword(req.Body.Pass) passhash, err := HashPassword(req.Body.Pass)
if err != nil { if err != nil {
log.Errorf("failed to hash password: %v", err) log.Errorf("failed to hash password: %v", err)
@ -94,17 +94,17 @@ func (s Server) PostSignUp(ctx context.Context, req auth.PostSignUpRequestObject
// TODO: check err and retyrn 400/500 // TODO: check err and retyrn 400/500
} }
return auth.PostSignUp200JSONResponse{ return auth.PostAuthSignUp200JSONResponse{
UserId: user_id, UserId: user_id,
}, nil }, nil
} }
func (s Server) PostSignIn(ctx context.Context, req auth.PostSignInRequestObject) (auth.PostSignInResponseObject, error) { func (s Server) PostAuthSignIn(ctx context.Context, req auth.PostAuthSignInRequestObject) (auth.PostAuthSignInResponseObject, error) {
ginCtx, ok := ctx.Value(gin.ContextKey).(*gin.Context) ginCtx, ok := ctx.Value(gin.ContextKey).(*gin.Context)
if !ok { if !ok {
log.Print("failed to get gin context") log.Print("failed to get gin context")
// TODO: change to 500 // TODO: change to 500
return auth.PostSignIn200JSONResponse{}, fmt.Errorf("failed to get gin.Context from context.Context") return auth.PostAuthSignIn200JSONResponse{}, fmt.Errorf("failed to get gin.Context from context.Context")
} }
user, err := s.db.GetUserByNickname(context.Background(), req.Body.Nickname) user, err := s.db.GetUserByNickname(context.Background(), req.Body.Nickname)
@ -120,7 +120,7 @@ func (s Server) PostSignIn(ctx context.Context, req auth.PostSignInRequestObject
} }
if !ok { if !ok {
err_msg := "invalid credentials" err_msg := "invalid credentials"
return auth.PostSignIn401JSONResponse{ return auth.PostAuthSignIn401JSONResponse{
Error: &err_msg, Error: &err_msg,
}, nil }, nil
} }
@ -137,7 +137,7 @@ func (s Server) PostSignIn(ctx context.Context, req auth.PostSignInRequestObject
ginCtx.SetCookie("refresh_token", refreshToken, 1209600, "/auth", "", false, true) ginCtx.SetCookie("refresh_token", refreshToken, 1209600, "/auth", "", false, true)
ginCtx.SetCookie("xsrf_token", csrfToken, 1209600, "/api", "", false, false) ginCtx.SetCookie("xsrf_token", csrfToken, 1209600, "/api", "", false, false)
result := auth.PostSignIn200JSONResponse{ result := auth.PostAuthSignIn200JSONResponse{
UserId: user.ID, UserId: user.ID,
UserName: user.Nickname, UserName: user.Nickname,
} }