This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rest: use UserSignup in request authentication (#180)
Authentication is performed by checking the existence of a valid UserSignup. UserSignup matching is performed on the JWT's `sub` field. Signed-off-by: Francesco Ilario <filario@redhat.com> Co-authored-by: Andy Sadler <ansadler@redhat.com>
- Loading branch information
Showing
15 changed files
with
166 additions
and
37 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,13 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: usersignup-reader | ||
rules: | ||
- apiGroups: | ||
- toolchain.dev.openshift.com | ||
resources: | ||
- usersignups | ||
verbs: | ||
- list | ||
- get | ||
- watch |
12 changes: 12 additions & 0 deletions
12
server/config/rbac/clusterrolebinding_usersignup_reader.yaml
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,12 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: rest-api-server:usersignup-reader | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: usersignup-reader | ||
subjects: | ||
- kind: ServiceAccount | ||
name: rest-api-server | ||
namespace: system |
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
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
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,82 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
|
||
ccontext "github.com/konflux-workspaces/workspaces/server/core/context" | ||
|
||
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
) | ||
|
||
type UserSignupMiddleware struct { | ||
cache cache.Cache | ||
requireUserSignup bool | ||
|
||
next http.Handler | ||
} | ||
|
||
func NewUserSignupMiddleware(next http.Handler, cache cache.Cache, requireUserSignup bool) *UserSignupMiddleware { | ||
return &UserSignupMiddleware{ | ||
cache: cache, | ||
requireUserSignup: requireUserSignup, | ||
|
||
next: next, | ||
} | ||
} | ||
|
||
func (m *UserSignupMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// retrieve User's JWT Sub | ||
u, ok := r.Context().Value(ccontext.UserSubKey).(string) | ||
if !ok { | ||
m.next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
// retrieve UserSignup for given sub | ||
us, err := m.lookupUserSignup(r.Context(), u) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if us == nil { | ||
w.WriteHeader(http.StatusForbidden) | ||
if _, err := w.Write([]byte("user needs to sign in")); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
// user is waiting for approval | ||
if us.Status.CompliantUsername == "" { | ||
w.WriteHeader(http.StatusForbidden) | ||
if _, err := w.Write([]byte("user is waiting for approval")); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
// TODO(@filariow): check if user is deactivated or banned | ||
|
||
// inject the userSignup.ComplaintUsername | ||
ctx := context.WithValue(r.Context(), ccontext.UserSignupComplaintNameKey, us.Status.CompliantUsername) | ||
m.next.ServeHTTP(w, r.WithContext(ctx)) | ||
} | ||
|
||
func (m *UserSignupMiddleware) lookupUserSignup(ctx context.Context, sub string) (*toolchainv1alpha1.UserSignup, error) { | ||
uu := toolchainv1alpha1.UserSignupList{} | ||
if err := m.cache.List(ctx, &uu); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, u := range uu.Items { | ||
if u.Spec.IdentityClaims.Sub == sub { | ||
return &u, nil | ||
} | ||
} | ||
|
||
return nil, 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