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

Make auth token configurable (no longer hardcoded) #59

Merged
merged 1 commit into from
Oct 11, 2022
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
10 changes: 5 additions & 5 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"github.com/livepeer/catalyst-api/middleware"
)

func ListenAndServe(apiPort, mistPort, mistHttpPort int) error {
func ListenAndServe(apiPort, mistPort, mistHttpPort int, apiToken string) error {
mc := &clients.MistClient{
ApiUrl: fmt.Sprintf("http://localhost:%d/api2", mistPort),
HttpReqUrl: fmt.Sprintf("http://localhost:%d", mistHttpPort),
TriggerCallback: fmt.Sprintf("http://localhost:%d/api/mist/trigger", apiPort),
}

listen := fmt.Sprintf("0.0.0.0:%d", apiPort)
router := NewCatalystAPIRouter(mc)
router := NewCatalystAPIRouter(mc, apiToken)

_ = config.Logger.Log(
"msg", "Starting Catalyst API",
Expand All @@ -30,7 +30,7 @@ func ListenAndServe(apiPort, mistPort, mistHttpPort int) error {
return http.ListenAndServe(listen, router)
}

func NewCatalystAPIRouter(mc *clients.MistClient) *httprouter.Router {
func NewCatalystAPIRouter(mc *clients.MistClient, apiToken string) *httprouter.Router {
router := httprouter.New()
withLogging := middleware.LogRequest()
withAuth := middleware.IsAuthorized
Expand All @@ -42,8 +42,8 @@ func NewCatalystAPIRouter(mc *clients.MistClient) *httprouter.Router {
router.GET("/ok", withLogging(catalystApiHandlers.Ok()))

// Public Catalyst API
router.POST("/api/vod", withLogging(withAuth(catalystApiHandlers.UploadVOD())))
router.POST("/api/transcode/file", withLogging(withAuth(catalystApiHandlers.TranscodeSegment())))
router.POST("/api/vod", withLogging(withAuth(apiToken, catalystApiHandlers.UploadVOD())))
router.POST("/api/transcode/file", withLogging(withAuth(apiToken, catalystApiHandlers.TranscodeSegment())))

// Endpoint to receive "Triggers" (callbacks) from Mist
router.POST("/api/mist/trigger", withLogging(mistCallbackHandlers.Trigger()))
Expand Down
2 changes: 1 addition & 1 deletion api/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestInitServer(t *testing.T) {
require := require.New(t)
router := NewCatalystAPIRouter(nil)
router := NewCatalystAPIRouter(nil, "IAmAuthorized")

handle, _, _ := router.Lookup("GET", "/ok")
require.NotNil(handle)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func main() {
port := flag.Int("port", 4949, "Port to listen on")
mistPort := flag.Int("mist-port", 4242, "Port to listen on")
mistHttpPort := flag.Int("mist-http-port", 8080, "Port to listen on")
apiToken := flag.String("api-token", "IAmAuthorized", "Auth header value for API access")
flag.StringVar(&config.RecordingCallback, "recording", "http://recording.livepeer.com/recording/status", "Callback URL for recording start&stop events")
mistJson := flag.Bool("j", false, "Print application info as JSON. Used by Mist to present flags in its UI.")
flag.Parse()
Expand All @@ -22,7 +23,7 @@ func main() {
return
}

if err := api.ListenAndServe(*port, *mistPort, *mistHttpPort); err != nil {
if err := api.ListenAndServe(*port, *mistPort, *mistHttpPort, *apiToken); err != nil {
log.Fatal(err)
}
}
6 changes: 2 additions & 4 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/livepeer/catalyst-api/errors"
)

var testToken = "IAmAuthorized"

func IsAuthorized(next httprouter.Handle) httprouter.Handle {
func IsAuthorized(apiToken string, next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
authHeader := r.Header.Get("Authorization")

Expand All @@ -21,7 +19,7 @@ func IsAuthorized(next httprouter.Handle) httprouter.Handle {

token := strings.TrimPrefix(authHeader, "Bearer ")

if token != testToken {
if token != apiToken {
errors.WriteHTTPUnauthorized(w, "Invalid Token", nil)
return
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNoAuthHeader(t *testing.T) {
req, _ := http.NewRequest("GET", "/ok", nil)
rr := httptest.NewRecorder()
catalystApiHandlers := handlers.CatalystAPIHandlersCollection{}
router.GET("/ok", IsAuthorized(catalystApiHandlers.Ok()))
router.GET("/ok", IsAuthorized("IAmAuthorized", catalystApiHandlers.Ok()))
router.ServeHTTP(rr, req)

require.Equal(rr.Code, 401, "should return 401")
Expand All @@ -33,7 +33,7 @@ func TestWrongKey(t *testing.T) {

rr := httptest.NewRecorder()
catalystApiHandlers := handlers.CatalystAPIHandlersCollection{}
router.GET("/ok", IsAuthorized(catalystApiHandlers.Ok()))
router.GET("/ok", IsAuthorized("IAmAuthorized", catalystApiHandlers.Ok()))
router.ServeHTTP(rr, req)

require.Equal(rr.Code, 401, "should return 401")
Expand Down