-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
29 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,39 @@ | ||
package reverseproxy | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/osstotalsoft/bifrost/abstraction" | ||
"net/http" | ||
) | ||
|
||
//ClearCorsHeaders deletes cors headers from upstream service | ||
func ClearCorsHeaders(response *http.Response) error { | ||
//hack when upstream service has cors enabled; cors will be handled by the gateway | ||
response.Header.Del("Access-Control-Allow-Origin") | ||
response.Header.Del("Access-Control-Allow-Credentials") | ||
response.Header.Del("Access-Control-Allow-Methods") | ||
response.Header.Del("Access-Control-Allow-Headers") | ||
return nil | ||
} | ||
|
||
//AddUserIdToHeader puts userId claim to request header | ||
func AddUserIdToHeader(req *http.Request) error { | ||
claims, err := getClaims(req.Context()) | ||
if err == nil { | ||
if sub, ok := claims["sub"]; ok { | ||
req.Header.Add(abstraction.HttpUserIdHeader, sub.(string)) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
//getClaims get the claims map stored in the context | ||
func getClaims(context context.Context) (map[string]interface{}, error) { | ||
claims, ok := context.Value(abstraction.ContextClaimsKey).(map[string]interface{}) | ||
if !ok { | ||
return nil, errors.New("claims not present or not authenticated") | ||
} | ||
|
||
return claims, nil | ||
} |
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