-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAPI SecurityMiddleware facade (#30)
- Loading branch information
Showing
8 changed files
with
99 additions
and
40 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
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,3 @@ | ||
package rest_test | ||
|
||
import _ "github.com/bool64/dev" // Include CI/Dev scripts to project. |
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,57 @@ | ||
package nethttp_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/swaggest/openapi-go/openapi3" | ||
"github.com/swaggest/rest/chirouter" | ||
"github.com/swaggest/rest/nethttp" | ||
"github.com/swaggest/rest/openapi" | ||
"github.com/swaggest/usecase" | ||
) | ||
|
||
func ExampleSecurityMiddleware() { | ||
// Create router. | ||
r := chirouter.NewWrapper(chi.NewRouter()) | ||
|
||
// Init API documentation schema. | ||
apiSchema := &openapi.Collector{} | ||
|
||
// Setup middlewares (non-documentary middlewares omitted for brevity). | ||
r.Use( | ||
nethttp.OpenAPIMiddleware(apiSchema), // Documentation collector. | ||
) | ||
|
||
// Configure an actual security middleware. | ||
serviceTokenAuth := func(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
if req.Header.Get("Authorization") != "<secret>" { | ||
http.Error(w, "Authentication failed.", http.StatusUnauthorized) | ||
|
||
return | ||
} | ||
|
||
h.ServeHTTP(w, req) | ||
}) | ||
} | ||
|
||
// Configure documentation middleware to describe actual security middleware. | ||
serviceTokenDoc := nethttp.SecurityMiddleware(apiSchema, "serviceToken", openapi3.SecurityScheme{ | ||
APIKeySecurityScheme: &openapi3.APIKeySecurityScheme{ | ||
Name: "Authorization", | ||
In: openapi3.APIKeySecuritySchemeInHeader, | ||
}, | ||
}) | ||
|
||
u := usecase.NewIOI(nil, nil, func(ctx context.Context, input, output interface{}) error { | ||
// Do something. | ||
return nil | ||
}) | ||
|
||
// Add use case handler to router with security middleware. | ||
r. | ||
With(serviceTokenAuth, serviceTokenDoc). // Apply a pair of middlewares: actual security and documentation. | ||
Method(http.MethodGet, "/foo", nethttp.NewHandler(u)) | ||
} |
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 was deleted.
Oops, something went wrong.