|
3 | 3 |
|
4 | 4 | import { |
5 | 5 | AtomicError, |
| 6 | + checkAuthenticationCookie, |
6 | 7 | Commit, |
7 | 8 | ErrorType, |
8 | 9 | parseCommit, |
9 | 10 | parseJsonADArray, |
10 | 11 | parseJsonADResource, |
11 | 12 | Resource, |
12 | 13 | serializeDeterministically, |
| 14 | + setCookieAuthentication, |
| 15 | + signRequest, |
13 | 16 | Store, |
14 | 17 | } from './index'; |
15 | 18 |
|
16 | 19 | /** Works both in node and the browser */ |
17 | 20 | import fetch from 'cross-fetch'; |
18 | | -import { signRequest } from './authentication'; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * One key-value pair per HTTP Header. Since we need to support both browsers |
@@ -55,8 +57,16 @@ export async function fetchResource( |
55 | 57 | // Sign the request if there is an agent present |
56 | 58 | const agent = store?.getAgent(); |
57 | 59 |
|
58 | | - if (agent) { |
59 | | - await signRequest(subject, agent, requestHeaders); |
| 60 | + if (agent && store) { |
| 61 | + // Cookies only work for same-origin requests right now |
| 62 | + // https://github.com/atomicdata-dev/atomic-data-browser/issues/253 |
| 63 | + if (subject.startsWith(window.location.origin)) { |
| 64 | + if (!checkAuthenticationCookie()) { |
| 65 | + setCookieAuthentication(store, agent); |
| 66 | + } |
| 67 | + } else { |
| 68 | + await signRequest(subject, agent, requestHeaders); |
| 69 | + } |
60 | 70 | } |
61 | 71 |
|
62 | 72 | let url = subject; |
@@ -88,10 +98,7 @@ export async function fetchResource( |
88 | 98 | ); |
89 | 99 | } |
90 | 100 | } else if (response.status === 401) { |
91 | | - throw new AtomicError( |
92 | | - `You don't have the rights to do view ${subject}. Are you signed in with the right Agent? More detailed error from server: ${body}`, |
93 | | - ErrorType.Unauthorized, |
94 | | - ); |
| 101 | + throw new AtomicError(body, ErrorType.Unauthorized); |
95 | 102 | } else if (response.status === 500) { |
96 | 103 | throw new AtomicError(body, ErrorType.Server); |
97 | 104 | } else if (response.status === 404) { |
@@ -194,12 +201,9 @@ export async function uploadFiles( |
194 | 201 | throw new AtomicError(`No agent present. Can't sign the upload request.`); |
195 | 202 | } |
196 | 203 |
|
197 | | - const signedHeaders = await signRequest(uploadURL.toString(), agent, {}); |
198 | | - |
199 | 204 | const options = { |
200 | 205 | method: 'POST', |
201 | 206 | body: formData, |
202 | | - headers: signedHeaders, |
203 | 207 | }; |
204 | 208 |
|
205 | 209 | const resp = await fetch(uploadURL.toString(), options); |
|
0 commit comments