jbilcke-hf HF staff commited on
Commit
278d122
·
1 Parent(s): 12506f0

auth is working

Browse files
src/index.mts CHANGED
@@ -11,6 +11,7 @@ import { main } from "./main.mts"
11
  import { completedFilesDirFilePath } from "./config.mts"
12
  import { deleteTask } from "./scheduler/deleteTask.mts"
13
  import { getPendingTasks } from "./scheduler/getPendingTasks.mts"
 
14
 
15
  main()
16
 
@@ -22,15 +23,15 @@ app.use(express.json())
22
  app.post("/", async (req, res) => {
23
  const request = req.body as VideoSequenceRequest
24
 
25
- const token = `${request.token || ""}`
26
- if (token !== process.env.VC_SECRET_ACCESS_TOKEN) {
27
- console.log("couldn't find access token in the query")
28
  res.status(401)
29
  res.write(JSON.stringify({ error: "invalid token" }))
30
  res.end()
31
  return
32
  }
33
 
 
34
  let task: VideoTask = null
35
 
36
  console.log(`creating task from request..`)
@@ -60,6 +61,15 @@ app.post("/", async (req, res) => {
60
 
61
  // get all pending tasks
62
  app.get("/", async (req, res) => {
 
 
 
 
 
 
 
 
 
63
  try {
64
  const tasks = await getPendingTasks()
65
  res.status(200)
@@ -74,6 +84,15 @@ app.get("/", async (req, res) => {
74
  })
75
 
76
  app.get("/:id", async (req, res) => {
 
 
 
 
 
 
 
 
 
77
  try {
78
  const task = await getTask(req.params.id)
79
  res.status(200)
@@ -88,6 +107,16 @@ app.get("/:id", async (req, res) => {
88
  })
89
 
90
  app.delete("/:id", async (req, res) => {
 
 
 
 
 
 
 
 
 
 
91
  let task: VideoTask = null
92
  try {
93
  task = await getTask(req.params.id)
@@ -112,6 +141,16 @@ app.delete("/:id", async (req, res) => {
112
  })
113
 
114
  app.get("/video/:id\.mp4", async (req, res) => {
 
 
 
 
 
 
 
 
 
 
115
  if (!req.params.id) {
116
  res.status(400)
117
  res.write(JSON.stringify({ error: "please provide a valid video id" }))
 
11
  import { completedFilesDirFilePath } from "./config.mts"
12
  import { deleteTask } from "./scheduler/deleteTask.mts"
13
  import { getPendingTasks } from "./scheduler/getPendingTasks.mts"
14
+ import { hasValidAuthorization } from "./utils/hasValidAuthorization.mts"
15
 
16
  main()
17
 
 
23
  app.post("/", async (req, res) => {
24
  const request = req.body as VideoSequenceRequest
25
 
26
+ if (!hasValidAuthorization(req.headers)) {
27
+ console.log("Invalid authorization")
 
28
  res.status(401)
29
  res.write(JSON.stringify({ error: "invalid token" }))
30
  res.end()
31
  return
32
  }
33
 
34
+
35
  let task: VideoTask = null
36
 
37
  console.log(`creating task from request..`)
 
61
 
62
  // get all pending tasks
63
  app.get("/", async (req, res) => {
64
+
65
+ if (!hasValidAuthorization(req.headers)) {
66
+ console.log("Invalid authorization")
67
+ res.status(401)
68
+ res.write(JSON.stringify({ error: "invalid token" }))
69
+ res.end()
70
+ return
71
+ }
72
+
73
  try {
74
  const tasks = await getPendingTasks()
75
  res.status(200)
 
84
  })
85
 
86
  app.get("/:id", async (req, res) => {
87
+
88
+ if (!hasValidAuthorization(req.headers)) {
89
+ console.log("Invalid authorization")
90
+ res.status(401)
91
+ res.write(JSON.stringify({ error: "invalid token" }))
92
+ res.end()
93
+ return
94
+ }
95
+
96
  try {
97
  const task = await getTask(req.params.id)
98
  res.status(200)
 
107
  })
108
 
109
  app.delete("/:id", async (req, res) => {
110
+
111
+ if (!hasValidAuthorization(req.headers)) {
112
+ console.log("Invalid authorization")
113
+ res.status(401)
114
+ res.write(JSON.stringify({ error: "invalid token" }))
115
+ res.end()
116
+ return
117
+ }
118
+
119
+
120
  let task: VideoTask = null
121
  try {
122
  task = await getTask(req.params.id)
 
141
  })
142
 
143
  app.get("/video/:id\.mp4", async (req, res) => {
144
+
145
+ if (!hasValidAuthorization(req.headers)) {
146
+ console.log("Invalid authorization")
147
+ res.status(401)
148
+ res.write(JSON.stringify({ error: "invalid token" }))
149
+ res.end()
150
+ return
151
+ }
152
+
153
+
154
  if (!req.params.id) {
155
  res.status(400)
156
  res.write(JSON.stringify({ error: "please provide a valid video id" }))
src/utils/hasValidAuthorization.mts ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import { IncomingHttpHeaders } from "node:http"
2
+
3
+ export const hasValidAuthorization = (headers: IncomingHttpHeaders) => {
4
+ const [_, token] = `${headers.authorization || ""}`.split(" ")
5
+ if (typeof token === "string" && token.trim() === process.env.VC_SECRET_ACCESS_TOKEN.trim()) {
6
+ return true
7
+ }
8
+
9
+ return false
10
+ }