Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access-control: cookie based access control #934

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions handlers/accesscontrol/access-control.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -63,7 +62,7 @@ func NewAccessControlHandlersCollection(cli config.Cli) *AccessControlHandlersCo
func (ac *AccessControlHandlersCollection) HandleUserNew(ctx context.Context, payload *misttriggers.UserNewPayload) (bool, error) {
playbackID := payload.StreamName[strings.Index(payload.StreamName, "+")+1:]

playbackAccessControlAllowed, err := ac.IsAuthorized(playbackID, payload.URL)
playbackAccessControlAllowed, err := ac.IsAuthorized(playbackID, payload)
if err != nil {
glog.Errorf("Unable to get playback access control info for playbackId=%v err=%s", playbackID, err.Error())
return false, err
Expand All @@ -78,11 +77,21 @@ func (ac *AccessControlHandlersCollection) HandleUserNew(ctx context.Context, pa
return false, nil
}

func (ac *AccessControlHandlersCollection) IsAuthorized(playbackID string, reqURL *url.URL) (bool, error) {
func (ac *AccessControlHandlersCollection) IsAuthorized(playbackID string, payload *misttriggers.UserNewPayload) (bool, error) {

acReq := PlaybackAccessControlRequest{Stream: playbackID, Type: "accessKey"}
cacheKey := ""
accessKey := reqURL.Query().Get("accessKey")
jwt := reqURL.Query().Get("jwt")
accessKey := payload.URL.Query().Get("accessKey")
jwt := payload.URL.Query().Get("jwt")

if accessKey == "" {
accessKey = payload.AccessKey
}

if jwt == "" {
jwt = payload.JWT
}

if accessKey != "" {
acReq.Type = "accessKey"
acReq.AccessKey = accessKey
Expand Down
11 changes: 11 additions & 0 deletions handlers/misttriggers/user_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type UserNewPayload struct {
URL *url.URL
FullURL string
SessionID string
Cookies []*http.Cookie
AccessKey string
JWT string
}

func ParseUserNewPayload(payload MistTriggerBody) (UserNewPayload, error) {
Expand All @@ -43,6 +46,14 @@ func ParseUserNewPayload(payload MistTriggerBody) (UserNewPayload, error) {

func (d *MistCallbackHandlersCollection) TriggerUserNew(ctx context.Context, w http.ResponseWriter, req *http.Request, body MistTriggerBody) {
payload, err := ParseUserNewPayload(body)
cookies := req.Cookies()
accessKey := req.Header.Get("X-Livepeer-Access-Key")
jwt := req.Header.Get("X-Livepeer-JWT")

payload.Cookies = cookies // would remove probably when everything's working
payload.AccessKey = accessKey
payload.JWT = jwt

if err != nil {
glog.Infof("Error parsing USER_NEW payload error=%q payload=%q", err, string(body))
w.WriteHeader(http.StatusBadRequest)
Expand Down
7 changes: 6 additions & 1 deletion middleware/gating.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"github.com/livepeer/catalyst-api/config"
catErrs "github.com/livepeer/catalyst-api/errors"
"github.com/livepeer/catalyst-api/handlers/accesscontrol"
"github.com/livepeer/catalyst-api/handlers/misttriggers"
"github.com/livepeer/catalyst-api/log"
"github.com/livepeer/catalyst-api/playback"
"github.com/livepeer/catalyst-api/requests"
Expand All @@ -31,7 +32,11 @@
accessKey := req.URL.Query().Get("accessKey")
jwt := req.URL.Query().Get("jwt")

playbackAccessControlAllowed, err := h.AccessControl.IsAuthorized(playbackID, req.URL)
payload := misttriggers.UserNewPayload{
URL: req.URL,
}

playbackAccessControlAllowed, err := h.AccessControl.IsAuthorized(playbackID, &payload)

Check warning on line 39 in middleware/gating.go

View check run for this annotation

Codecov / codecov/patch

middleware/gating.go#L35-L39

Added lines #L35 - L39 were not covered by tests
if err != nil {
log.LogError(requestID, "unable to get playback access control info", err, "playbackID", playbackID, "accessKey", accessKey, "jwt", jwt)
catErrs.WriteHTTPInternalServerError(w, "error authorizing playback request", nil)
Expand Down
Loading