Skip to content

Commit 69e896c

Browse files
authored
[Video] Properly get the service auth aud from the session (#5025)
1 parent 91fe416 commit 69e896c

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/lib/strings/url-helpers.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,21 @@ export function shortLinkToHref(url: string): string {
339339
return url
340340
}
341341
}
342+
343+
export function getHostnameFromUrl(url: string): string | null {
344+
let urlp
345+
try {
346+
urlp = new URL(url)
347+
} catch (e) {
348+
return null
349+
}
350+
return urlp.hostname
351+
}
352+
353+
export function getServiceAuthAudFromUrl(url: string): string | null {
354+
const hostname = getHostnameFromUrl(url)
355+
if (!hostname) {
356+
return null
357+
}
358+
return `did:web:${hostname}`
359+
}

src/state/queries/video/video-upload.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {cancelable} from '#/lib/async/cancelable'
77
import {CompressedVideo} from '#/lib/media/video/compress'
88
import {createVideoEndpointUrl} from '#/state/queries/video/util'
99
import {useAgent, useSession} from '#/state/session'
10+
import {getServiceAuthAudFromUrl} from 'lib/strings/url-helpers'
1011

1112
export const useUploadVideoMutation = ({
1213
onSuccess,
@@ -30,14 +31,18 @@ export const useUploadVideoMutation = ({
3031
name: `${nanoid(12)}.mp4`, // @TODO what are we limiting this to?
3132
})
3233

33-
// a logged-in agent should have this set, but we'll check just in case
34-
if (!agent.pdsUrl) {
34+
if (!currentAccount?.service) {
35+
throw new Error('User is not logged in')
36+
}
37+
38+
const serviceAuthAud = getServiceAuthAudFromUrl(currentAccount.service)
39+
if (!serviceAuthAud) {
3540
throw new Error('Agent does not have a PDS URL')
3641
}
3742

3843
const {data: serviceAuth} = await agent.com.atproto.server.getServiceAuth(
3944
{
40-
aud: `did:web:${agent.pdsUrl.hostname}`,
45+
aud: serviceAuthAud,
4146
lxm: 'com.atproto.repo.uploadBlob',
4247
},
4348
)

src/state/queries/video/video-upload.web.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {cancelable} from '#/lib/async/cancelable'
66
import {CompressedVideo} from '#/lib/media/video/compress'
77
import {createVideoEndpointUrl} from '#/state/queries/video/util'
88
import {useAgent, useSession} from '#/state/session'
9+
import {getServiceAuthAudFromUrl} from 'lib/strings/url-helpers'
910

1011
export const useUploadVideoMutation = ({
1112
onSuccess,
@@ -29,14 +30,18 @@ export const useUploadVideoMutation = ({
2930
name: `${nanoid(12)}.mp4`, // @TODO: make sure it's always mp4'
3031
})
3132

32-
// a logged-in agent should have this set, but we'll check just in case
33-
if (!agent.pdsUrl) {
33+
if (!currentAccount?.service) {
34+
throw new Error('User is not logged in')
35+
}
36+
37+
const serviceAuthAud = getServiceAuthAudFromUrl(currentAccount.service)
38+
if (!serviceAuthAud) {
3439
throw new Error('Agent does not have a PDS URL')
3540
}
3641

3742
const {data: serviceAuth} = await agent.com.atproto.server.getServiceAuth(
3843
{
39-
aud: `did:web:${agent.pdsUrl.hostname}`,
44+
aud: serviceAuthAud,
4045
lxm: 'com.atproto.repo.uploadBlob',
4146
},
4247
)

0 commit comments

Comments
 (0)