-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define and implement an OpenAPI spec in the backend (#141)
* Define and use an OpenAPI spec Signed-off-by: Ali Ok <[email protected]> * Add new codegen to update and verify scripts Signed-off-by: Ali Ok <[email protected]> * Update codegen Signed-off-by: Ali Ok <[email protected]> * goimports Signed-off-by: Ali Ok <[email protected]> * Fix URLs in tests and the plugin Signed-off-by: Ali Ok <[email protected]> * Make linter happier Signed-off-by: Ali Ok <[email protected]> * Fix scripts Signed-off-by: Ali Ok <[email protected]> --------- Signed-off-by: Ali Ok <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,029 additions
and
179 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,50 @@ | ||
package eventmesh | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type authTokenKey struct{} | ||
|
||
const ( | ||
AuthTokenHeader = "Authorization" | ||
BearerPrefix = "Bearer " | ||
) | ||
|
||
func AuthTokenMiddleware() mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
authTokenStr := r.Header.Get(AuthTokenHeader) | ||
if authTokenStr != "" && strings.HasPrefix(authTokenStr, BearerPrefix) { | ||
authTokenStr = strings.TrimPrefix(authTokenStr, BearerPrefix) | ||
} | ||
|
||
// set the token in the context | ||
if authTokenStr == "" || strings.TrimSpace(authTokenStr) == "" { | ||
http.Error(w, "missing auth token. set the 'Authorization: Bearer YOUR_KEY' header", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
ctx := WithAuthToken(r.Context(), authTokenStr) | ||
|
||
// Call the handler | ||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} | ||
} | ||
|
||
func WithAuthToken(ctx context.Context, authToken string) context.Context { | ||
return context.WithValue(ctx, authTokenKey{}, authToken) | ||
} | ||
|
||
func GetAuthToken(ctx context.Context) (string, bool) { | ||
token, ok := ctx.Value(authTokenKey{}).(string) | ||
if token == "" { | ||
return "", false | ||
} | ||
return token, ok | ||
} |
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
Oops, something went wrong.