Skip to content

Commit

Permalink
fix: 1. anthropic client using common getHeaders; 2. always using `Au…
Browse files Browse the repository at this point in the history
…thorization` header send access code
  • Loading branch information
lloydzhou committed Jul 1, 2024
1 parent d65ddea commit 37e2517
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
3 changes: 2 additions & 1 deletion app/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export function getHeaders() {
accessStore.enabledAccessControl() &&
validString(accessStore.accessCode)
) {
headers[authHeader] = makeBearer(
// access_code must send with header named `Authorization`, will using in auth middleware.
headers['Authorization'] = makeBearer(
ACCESS_CODE_PREFIX + accessStore.accessCode,
);
}
Expand Down
27 changes: 2 additions & 25 deletions app/client/platforms/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ export class ClaudeApi implements LLMApi {
headers: {
...getHeaders(), // get common headers
"anthropic-version": accessStore.anthropicApiVersion,
Authorization: getAuthKey(accessStore.anthropicApiKey),
// do not send `anthropicApiKey` in browser!!!
// Authorization: getAuthKey(accessStore.anthropicApiKey),
},
};

Expand Down Expand Up @@ -387,27 +388,3 @@ function trimEnd(s: string, end = " ") {

return s;
}

function bearer(value: string) {
return `Bearer ${value.trim()}`;
}

function getAuthKey(apiKey = "") {
const accessStore = useAccessStore.getState();
const isApp = !!getClientConfig()?.isApp;
let authKey = "";

if (apiKey) {
// use user's api key first
authKey = bearer(apiKey);
} else if (
accessStore.enabledAccessControl() &&
!isApp &&
!!accessStore.accessCode
) {
// or use access code
authKey = bearer(ACCESS_CODE_PREFIX + accessStore.accessCode);
}

return authKey;
}

0 comments on commit 37e2517

Please sign in to comment.