-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openapi): add OpenAPI spec generation (#7)
* Add openapi package * Update usage string * Add OpenAPI spec function for blocking endpoint * Add OpenAPI subcommand * Add OpenAPI spec for check endpoint * Update check HTTP method OpenAPI spec * Add security scheme * Add 401 responses to OpenAPI specs * Add error handling to fix SAST
- Loading branch information
Showing
7 changed files
with
156 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/divergentcodes/jwt-block/internal/core" | ||
"github.com/divergentcodes/jwt-block/web" | ||
) | ||
|
||
var openapiCmd = &cobra.Command{ | ||
Use: "openapi", | ||
Short: "Generate OpenAPI specs for jwt-block", | ||
Long: "Generate OpenAPI specs for jwt-block", | ||
Run: openapi, | ||
} | ||
|
||
func init() { | ||
logger := core.GetLogger() | ||
|
||
openapiCmd.PersistentFlags().String("format", "yaml", "format for OpenAPI specs (yaml or json)") | ||
err := viper.BindPFlag("format", openapiCmd.PersistentFlags().Lookup("format")) | ||
if err != nil { | ||
logger.Fatalw(err.Error()) | ||
} | ||
|
||
rootCmd.AddCommand(openapiCmd) | ||
} | ||
|
||
func openapi(cmd *cobra.Command, args []string) { | ||
logger := core.GetLogger() | ||
|
||
format := viper.GetString("format") | ||
schema, err := web.GenerateOpenAPI(format) | ||
if err != nil { | ||
logger.Fatalw(err.Error()) | ||
} | ||
|
||
fmt.Print(schema) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package web | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/divergentcodes/jwt-block/internal/core" | ||
"github.com/swaggest/openapi-go/openapi3" | ||
) | ||
|
||
// Generate the OpenAPI spec for the service. | ||
func GenerateOpenAPI(format string) (string, error) { | ||
reflector := openapi3.Reflector{} | ||
|
||
// Basic info. | ||
reflector.Spec = &openapi3.Spec{ | ||
Openapi: "3.0.3", | ||
} | ||
reflector.Spec.Info. | ||
WithTitle("JWT Block"). | ||
WithVersion(core.Version). | ||
WithDescription("API of the JWT Block service") | ||
|
||
// Base URL. | ||
server := openapi3.Server{ | ||
URL: "http://jwtblock.localhost", | ||
} | ||
reflector.Spec.Servers = append(reflector.Spec.Servers, server) | ||
|
||
// Declare security scheme. | ||
securityName := "bearerToken" | ||
reflector.Spec.SetHTTPBearerTokenSecurity(securityName, "JWT", "Access token") | ||
reflector.Spec.WithSecurity(map[string][]string{securityName: {}}) | ||
|
||
// Endpoints. | ||
blockGenerateOpenAPI(&reflector) | ||
checkGenerateOpenAPI(&reflector) | ||
|
||
// Dump the schema. | ||
var schema []byte | ||
var err error | ||
if format == "json" { | ||
schema, err = reflector.Spec.MarshalJSON() | ||
} else if format == "yaml" { | ||
schema, err = reflector.Spec.MarshalYAML() | ||
} else { | ||
return "", fmt.Errorf("invalid OpenAPI output format: %s", format) | ||
} | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(schema), nil | ||
} |