Skip to content

Commit

Permalink
feat: read cookies in the new API auth module (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni Ben-tzur authored Jun 5, 2020
1 parent 9da1063 commit f604a28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions master/internal/grpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func RegisterHTTPProxy(e *echo.Echo, port int) error {
}
e.Any("/api/v1/*", func(c echo.Context) error {
request := c.Request()
if c.Request().Header.Get("Authorization") == "" {
if cookie, err := c.Cookie(cookieName); err == nil {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cookie.Value))
}
}
if _, ok := request.URL.Query()["pretty"]; ok {
request.Header.Set("Accept", jsonPretty)
}
Expand Down
5 changes: 3 additions & 2 deletions master/internal/grpc/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
// nolint:gosec // These are not potential hardcoded credentials.
gatewayTokenHeader = "grpcgateway-authorization"
userTokenHeader = "x-user-token"
cookieName = "auth"
)

var (
Expand Down Expand Up @@ -84,13 +85,13 @@ func userTokenResponse(_ context.Context, w http.ResponseWriter, resp proto.Mess
switch r := resp.(type) {
case *apiv1.LoginResponse:
http.SetCookie(w, &http.Cookie{
Name: "auth",
Name: cookieName,
Value: r.Token,
Expires: time.Now().Add(db.SessionDuration),
})
case *apiv1.LogoutResponse:
http.SetCookie(w, &http.Cookie{
Name: "auth",
Name: cookieName,
Value: "",
Expires: time.Unix(0, 0),
})
Expand Down

0 comments on commit f604a28

Please sign in to comment.