Skip to content

Commit b4cbff2

Browse files
authored
Merge pull request #8 from yepcode/feature/YEP-2933
YEP-2933 Implement cached access token endpoint for yepcode run
2 parents 8d93ad9 + 8cbf313 commit b4cbff2

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/api/yepcodeApi.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class YepCodeApi {
5959
private teamId?: string;
6060
private accessToken?: string;
6161
private timeout: number;
62+
private apiToken?: string;
6263

6364
constructor(config: YepCodeApiConfig = {}) {
6465
if (typeof fetch !== "function") {
@@ -73,9 +74,6 @@ export class YepCodeApi {
7374
...envConfig,
7475
...config,
7576
};
76-
if (!finalConfig.authUrl) {
77-
finalConfig.authUrl = `${finalConfig.apiHost}/auth/realms/yepcode/protocol/openid-connect/token`;
78-
}
7977

8078
if (
8179
!finalConfig.accessToken &&
@@ -122,13 +120,14 @@ export class YepCodeApi {
122120
this.apiHost = finalConfig.apiHost;
123121
this.clientId = finalConfig.clientId;
124122
this.clientSecret = finalConfig.clientSecret;
125-
this.authUrl = finalConfig.authUrl;
123+
this.apiToken = finalConfig.apiToken;
126124
this.teamId = finalConfig.teamId;
127125
this.accessToken = finalConfig.accessToken;
128126
this.timeout = finalConfig.timeout;
129127
if (!this.teamId) {
130128
this.initTeamId();
131129
}
130+
this.authUrl = finalConfig.authUrl ?? this.getAuthURL();
132131
}
133132

134133
getTeamId(): string {
@@ -163,8 +162,12 @@ export class YepCodeApi {
163162
return `${this.apiHost}/api/${this.teamId}/rest`;
164163
}
165164

165+
private getAuthURL(): string {
166+
return `${this.getBaseURL()}/auth/token`;
167+
}
168+
166169
private async getAccessToken(): Promise<string> {
167-
if (!this.clientId || !this.clientSecret) {
170+
if (!this.apiToken && (!this.clientId || !this.clientSecret)) {
168171
throw new Error(
169172
"AccessToken has expired. Provide a new one or enable automatic refreshing by providing an apiToken or clientId and clientSecret."
170173
);
@@ -174,12 +177,12 @@ export class YepCodeApi {
174177
const response = await fetch(this.authUrl, {
175178
method: "POST",
176179
headers: {
177-
authorization: `Basic ${Buffer.from(
178-
`${this.clientId}:${this.clientSecret}`
179-
).toString("base64")}`,
180-
"Content-Type": "application/x-www-form-urlencoded",
180+
"x-api-token":
181+
this.apiToken ??
182+
`sk-${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString(
183+
"base64"
184+
)}`,
181185
},
182-
body: "grant_type=client_credentials",
183186
});
184187

185188
if (!response.ok) {

0 commit comments

Comments
 (0)