Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only return register token against localhost, add descriptions to fields #413

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func serve(ctx context.Context) error {
// Add redis client to Handlers Config
so.Config.Handler.RedisClient = redisClient

// set dev flag
so.Config.Handler.IsDev = so.Config.Settings.Server.Dev

// add ready checks
so.AddServerOptions(
serveropts.WithReadyChecks(dbClient.Config, fgaClient, redisClient, dbClient.Job),
Expand Down
2 changes: 2 additions & 0 deletions internal/httpserve/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
type Handler struct {
// IsTest is a flag to determine if the application is running in test mode and will mock external calls
IsTest bool
// IsDev is a flag to determine if the application is running in development mode
IsDev bool
// DBClient to interact with the database
DBClient *ent.Client
// RedisClient to interact with redis
Expand Down
6 changes: 5 additions & 1 deletion internal/httpserve/handlers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ func (h *Handler) RegisterHandler(ctx echo.Context) error {
ID: meowuser.ID,
Email: meowuser.Email,
Message: "Welcome to Openlane!",
Token: meowtoken.Token,
}

// only return the token in development
if h.IsDev {
out.Token = meowtoken.Token
}

return h.Created(ctx, out)
Expand Down
2 changes: 1 addition & 1 deletion internal/httpserve/handlers/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (suite *HandlerTestSuite) TestVerifyHandler() {
email: "[email protected]",
tokenSet: true,
ttl: expiredTTL,
expectedMessage: "Token expired, a new token has been issued. Please try again",
expectedMessage: "Token expired, a new token has been issued. Please check your email and try again.",
expectedStatus: http.StatusCreated,
},
}
Expand Down
3 changes: 1 addition & 2 deletions internal/httpserve/handlers/verifyemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func (h *Handler) VerifyEmail(ctx echo.Context) error {
Reply: rout.Reply{Success: false},
ID: meowtoken.ID,
Email: user.Email,
Message: "Token expired, a new token has been issued. Please try again.",
Token: meowtoken.Token,
Message: "Token expired, a new token has been issued. Please check your email and try again.",
}

return h.Created(ctx, out)
Expand Down
23 changes: 22 additions & 1 deletion internal/httpserve/server/openapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"reflect"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"

Expand All @@ -18,7 +20,7 @@ func NewOpenAPISpec() (*openapi3.T, error) {
securityschemes := make(openapi3.SecuritySchemes)
examples := make(openapi3.Examples)

generator := openapi3gen.NewGenerator(openapi3gen.UseAllExportedFields())
generator := openapi3gen.NewGenerator(openapi3gen.UseAllExportedFields(), customizer)
for key, val := range openAPISchemas {
ref, err := generator.NewSchemaRefForValue(val, schemas)
if err != nil {
Expand Down Expand Up @@ -160,6 +162,25 @@ func NewOpenAPISpec() (*openapi3.T, error) {
}, nil
}

// customizer is a customizer function that allows for the modification of the generated schemas
// this is used to ignore fields that are not required in the OAS specification
// and to add additional metadata to the schema such as descriptions and examples
var customizer = openapi3gen.SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error {
if tag.Get("exclude") != "" && tag.Get("exclude") == "true" {
return &openapi3gen.ExcludeSchemaSentinel{}
}

if tag.Get("description") != "" {
schema.Description = tag.Get("description")
}

if tag.Get("example") != "" {
schema.Example = tag.Get("example")
}

return nil
})

// openAPISchemas is a mapping of types to auto generate schemas for - these specifically live under the OAS "schema" type so that we can simply make schemaRef's to them and not have to define them all individually in the OAS paths
var openAPISchemas = map[string]any{
"LoginRequest": &models.LoginRequest{},
Expand Down
Loading
Loading