-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- The CF CLI requires a token endpoint during log streaming - This endpoint provides a token with a valid expiration, which allows the CLI to proceed. The token is not used for actual authentication so it only needs to be in a parsable format. - Eventually the CLI will be updated to skip this call but for now we need it. [#1294] Co-authored-by: Matt Royal <mroyal@vmware.com> Co-authored-by: Julian Hjortshoj <hjortshojj@vmware.com>
- Loading branch information
1 parent
77c5e7e
commit 6e1eca3
Showing
8 changed files
with
126 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"code.cloudfoundry.org/korifi/api/authorization" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/golang-jwt/jwt" | ||
"github.com/gorilla/mux" | ||
"net/http" | ||
"net/url" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
const ( | ||
OAuthTokenPath = "/oauth/token" | ||
) | ||
|
||
type OAuthTokenHandler struct { | ||
handlerWrapper *AuthAwareHandlerFuncWrapper | ||
apiBaseURL url.URL | ||
} | ||
|
||
func NewOAuthToken(apiBaseURL url.URL) *OAuthTokenHandler { | ||
return &OAuthTokenHandler{ | ||
handlerWrapper: NewUnauthenticatedHandlerFuncWrapper(ctrl.Log.WithName("OAuthTokenHandler")), //NewAuthAwareHandlerFuncWrapper(ctrl.Log.WithName("OAuthTokenHandler")), | ||
apiBaseURL: apiBaseURL, | ||
} | ||
} | ||
|
||
func (h *OAuthTokenHandler) oauthTokenHandler(ctx context.Context, logger logr.Logger, authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) { | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour).Unix(), | ||
}) | ||
|
||
tokenString, err := token.SignedString([]byte("not-a-real-secret")) | ||
if err != nil { | ||
// we don't expect to hit this ever, given that the string above is hard coded. | ||
panic(err.Error()) | ||
} | ||
|
||
return NewHandlerResponse(http.StatusOK).WithBody(map[string]string{ | ||
"token_type": "bearer", | ||
"access_token": tokenString, | ||
}), nil | ||
} | ||
|
||
func (h *OAuthTokenHandler) RegisterRoutes(router *mux.Router) { | ||
router.Path(OAuthTokenPath).Methods("POST").HandlerFunc(h.handlerWrapper.Wrap(h.oauthTokenHandler)) | ||
} |
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,58 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"code.cloudfoundry.org/korifi/api/authorization" | ||
apis "code.cloudfoundry.org/korifi/api/handlers" | ||
|
||
"github.com/SermoDigital/jose/jws" | ||
"github.com/go-http-utils/headers" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("OAuthToken", func() { | ||
const oauthTokenBase = "/oauth/token" | ||
|
||
var ( | ||
OAuthTokenHandler *apis.OAuthTokenHandler | ||
requestMethod string | ||
requestPath string | ||
) | ||
|
||
BeforeEach(func() { | ||
requestPath = oauthTokenBase | ||
requestMethod = http.MethodPost | ||
ctx = authorization.NewContext(ctx, &authorization.Info{Token: "the-token"}) | ||
OAuthTokenHandler = apis.NewOAuthToken(*serverURL) | ||
OAuthTokenHandler.RegisterRoutes(router) | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
req, err := http.NewRequestWithContext(ctx, requestMethod, requestPath, nil) | ||
req.Header.Add(headers.Authorization, authHeader) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
router.ServeHTTP(rr, req) | ||
}) | ||
|
||
Describe("POST OAuthToken", func() { | ||
It("returns 201 with appropriate success JSON", func() { | ||
Expect(rr).To(HaveHTTPStatus(http.StatusOK)) | ||
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json")) | ||
jsonBody := map[string]string{} | ||
Expect(json.NewDecoder(rr.Body).Decode(&jsonBody)).To(Succeed()) | ||
Expect(jsonBody).To(HaveKeyWithValue("token_type", "bearer")) | ||
Expect(jsonBody).To(HaveKeyWithValue("access_token", Not(BeEmpty()))) | ||
|
||
tokenout, err := jws.ParseJWT([]byte(jsonBody["access_token"])) | ||
Expect(err).NotTo(HaveOccurred()) | ||
expiration, ok := tokenout.Claims().Expiration() | ||
Expect(ok).To(BeTrue()) | ||
Expect(expiration.Unix()).To(BeNumerically(">", time.Now().Add(time.Minute*59).Unix())) | ||
}) | ||
}) | ||
}) |
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