-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds an HTTP handler that authenticates users based on the JWT token included in the `Authorization` HTTP header. Related: https://issues.redhat.com/browse/MGMT-16123 Signed-off-by: Juan Hernandez <[email protected]>
- Loading branch information
Showing
17 changed files
with
2,915 additions
and
72 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,46 @@ | ||
/* | ||
Copyright 2023 Red Hat Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
compliance with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
implied. See the License for the specific language governing permissions and limitations under the | ||
License. | ||
*/ | ||
|
||
// This file contains functions that extract information from the context. | ||
|
||
package authentication | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
) | ||
|
||
// contextKey is the type used to store the authentication information in the context. | ||
type contextKey int | ||
|
||
// tokenContextKey is the key used to store the authenticated JWT in the context. | ||
const tokenContextKey contextKey = iota | ||
|
||
// TokenIntoContext creates a new context containing the given token. | ||
func ContextWithToken(parent context.Context, token *jwt.Token) context.Context { | ||
return context.WithValue(parent, tokenContextKey, token) | ||
} | ||
|
||
// TokenFromContext extracts the JSON web token of the user from the context. It panics if there | ||
// is no such token. | ||
func TokenFromContext(ctx context.Context) *jwt.Token { | ||
token := ctx.Value(tokenContextKey) | ||
switch token := token.(type) { | ||
case *jwt.Token: | ||
return token | ||
default: | ||
panic("failed to get token from context") | ||
} | ||
} |
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,55 @@ | ||
/* | ||
Copyright (c) 2019 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// This file contains tests for the functions that extract authentication and authorization | ||
// information from contexts. | ||
|
||
package authentication | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2/dsl/core" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/openshift-kni/oran-o2ims/internal/testing" | ||
) | ||
|
||
var _ = Describe("Add token to context", func() { | ||
It("Adds the token", func() { | ||
token := MakeTokenObject(nil) | ||
ctx := ContextWithToken(context.TODO(), token) | ||
extracted := ctx.Value(tokenContextKey) | ||
Expect(extracted).To(BeIdenticalTo(token)) | ||
}) | ||
}) | ||
|
||
var _ = Describe("Get token from context", func() { | ||
It("Succeeds if there is a token", func() { | ||
token := MakeTokenObject(nil) | ||
ctx := context.WithValue(context.TODO(), tokenContextKey, token) | ||
extracted := TokenFromContext(ctx) | ||
Expect(extracted).ToNot(BeNil()) | ||
Expect(extracted.Raw).To(Equal(token.Raw)) | ||
}) | ||
|
||
It("Panics if there is no token", func() { | ||
ctx := context.TODO() | ||
Expect(func() { | ||
TokenFromContext(ctx) | ||
}).To(PanicWith("failed to get token from context")) | ||
}) | ||
}) |
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,43 @@ | ||
/* | ||
Copyright 2023 Red Hat Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
compliance with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
implied. See the License for the specific language governing permissions and limitations under the | ||
License. | ||
*/ | ||
|
||
package authentication | ||
|
||
import "github.com/spf13/pflag" | ||
|
||
// AddFlags adds the flags related to authentication to the given flag set. | ||
func AddFlags(set *pflag.FlagSet) { | ||
_ = set.StringArray( | ||
aclFileFlagName, | ||
[]string{}, | ||
"ACL file.", | ||
) | ||
_ = set.StringArray( | ||
jwksFileFlagName, | ||
[]string{}, | ||
"File containing the JSON web key set.", | ||
) | ||
_ = set.StringArray( | ||
jwksURLFlagName, | ||
[]string{}, | ||
"URL containing the JSON web key set.", | ||
) | ||
} | ||
|
||
// Names of the flags: | ||
const ( | ||
aclFileFlagName = "authn-acl-file" | ||
jwksFileFlagName = "authn-jwks-file" | ||
jwksURLFlagName = "authn-jwks-url" | ||
) |
Oops, something went wrong.