Skip to content

Commit 303e470

Browse files
committed
refactor: remove auto refreshing of tokens which didn't work anyway
1 parent 5371a56 commit 303e470

File tree

3 files changed

+28
-58
lines changed

3 files changed

+28
-58
lines changed

src/AuthenticatedAdapter.ts

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,61 @@
11
import { AxiosRequestConfig } from 'axios';
22
import { Adapter } from './types/adapters';
33

4-
export class AuthenticatedAdapter {
5-
protected authenticated = false;
6-
mageId?: string;
4+
const replaceMageIdInURL = (url: string, mageId: string) => url.replace(/\|MAGE_ID\|/, mageId);
5+
const addHeaders = (config: AxiosRequestConfig | undefined, headers: Record<string, string>) => ({ ...config, headers: { ...config?.headers, ...headers } });
76

7+
export class AuthenticatedAdapter {
88
constructor(
99
protected readonly baseAdapter: Adapter,
1010
protected readonly credentials: {
1111
appSecret: string;
1212
appId: string;
13-
autoRefresh?: boolean;
14-
tokenTTL?: number;
1513
}
16-
) {}
17-
18-
protected replaceMageIdInURL(url: string): string {
19-
return url.replace(/\|MAGE_ID\|/, this.mageId as string);
20-
}
14+
) { }
2115

2216
async get<T>(url: string, config?: AxiosRequestConfig): Promise<T> {
23-
await this.authenticate();
17+
const [mageId, headers] = await this.authenticate();
2418

25-
return this.baseAdapter.get(this.replaceMageIdInURL(url), config);
19+
return this.baseAdapter.get(replaceMageIdInURL(url, mageId), addHeaders(config, headers));
2620
}
2721

2822
async post<T>(url: string, body: unknown, config?: AxiosRequestConfig): Promise<T> {
29-
await this.authenticate();
23+
const [mageId, headers] = await this.authenticate();
3024

31-
return this.baseAdapter.post(this.replaceMageIdInURL(url), body, config);
25+
return this.baseAdapter.post(replaceMageIdInURL(url, mageId), body, addHeaders(config, headers));
3226
}
3327

3428
async put<T>(url: string, body: unknown, config?: AxiosRequestConfig): Promise<T> {
35-
await this.authenticate();
29+
const [mageId, headers] = await this.authenticate();
3630

37-
return this.baseAdapter.put(this.replaceMageIdInURL(url), body, config);
31+
return this.baseAdapter.put(replaceMageIdInURL(url, mageId), body, addHeaders(config, headers));
3832
}
3933

4034
async delete<T>(url: string, config?: AxiosRequestConfig): Promise<T> {
41-
await this.authenticate();
35+
const [mageId, headers] = await this.authenticate();
4236

43-
return this.baseAdapter.delete(this.replaceMageIdInURL(url), config);
37+
return this.baseAdapter.delete(replaceMageIdInURL(url, mageId), addHeaders(config, headers));
4438
}
4539

4640
async getMageId(): Promise<string> {
47-
await this.authenticate();
41+
const [mageId] = await this.authenticate();
4842

49-
return this.mageId as string;
43+
return mageId;
5044
}
5145

5246
/**
5347
* Authenticate with the API. You can find the App ID and secret with the next link.
5448
* @see https://developer.magento.com/account/apikeys
5549
* @see https://devdocs.magento.com/marketplace/eqp/v1/auth.html#session-token
5650
*/
57-
protected async authenticate(): Promise<void> {
58-
this.mageId = undefined;
59-
60-
const { expires_in, mage_id, ust } = await this.baseAdapter.post<{
51+
protected async authenticate() {
52+
const { mage_id, ust } = await this.baseAdapter.post<{
6153
mage_id: string;
6254
ust: string;
6355
expires_in: number;
6456
}>(
6557
'/app/session/token',
66-
{ grant_type: 'session', expires_in: this.credentials.tokenTTL ?? 7200 },
58+
{ grant_type: 'session', expires_in: 360 },
6759
{
6860
auth: {
6961
username: this.credentials.appId,
@@ -72,19 +64,6 @@ export class AuthenticatedAdapter {
7264
}
7365
);
7466

75-
this.mageId = mage_id;
76-
77-
this.baseAdapter.setHeader('Authorization', `Bearer ${ust}`);
78-
79-
if (this.credentials.autoRefresh) {
80-
// Re-run this function 5 seconds before the token expires
81-
setTimeout(async () => {
82-
this.authenticated = false;
83-
84-
await this.authenticate();
85-
}, expires_in * 1000 - 5);
86-
}
87-
88-
this.authenticated = true;
67+
return [mage_id, { authorization: `Bearer ${ust}` }] as const;
8968
}
9069
}

src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ export class EQP {
2323
readonly reportService: ReportService;
2424
readonly packageService: PackageService;
2525

26-
/** The authenticated user's Magento ID */
27-
get mageId(): string | undefined {
28-
return this.adapter.mageId;
26+
/**
27+
* Fetch the user's Mage ID
28+
* @see https://developer.adobe.com/commerce/marketplace/guides/eqp/v1/auth/#authentication-and-authorization-flow
29+
*/
30+
getMageId() {
31+
return this.adapter.getMageId();
2932
}
3033

3134
constructor(options: EQPOptions) {
@@ -35,9 +38,7 @@ export class EQP {
3538
options.adapter ?? new AxiosAdapter(`https://commercedeveloper${options.environment === 'sandbox' ? '-sandbox' : ''}-api.adobe.com/rest/v1`),
3639
{
3740
appId: options.appId,
38-
appSecret: options.appSecret,
39-
autoRefresh: options.autoRefresh ?? false,
40-
tokenTTL: options.expiresIn
41+
appSecret: options.appSecret
4142
}
4243
);
4344

tests/index.test.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,10 @@ describe('EQP', () => {
2222
test('get mageId', async () => {
2323
mockAuth(mock.mockAdapter);
2424

25-
mock.mockAdapter
26-
.onGet(
27-
'/users/MAGE_ID',
28-
undefined,
29-
expect.objectContaining({
30-
Authorization: expect.stringMatching('Bearer TOKEN')
31-
})
32-
)
33-
.reply(200, sampleUser);
34-
3525
// Already tested in tests/services/UserService.test.ts
36-
await subject.userService.getUser(false);
26+
const mageId = await subject.getMageId();
3727

38-
expect(subject.mageId).toBeDefined();
39-
expect(subject.mageId).toEqual(sampleUser.mage_id);
28+
expect(mageId).toBeDefined();
29+
expect(mageId).toEqual(sampleUser.mage_id);
4030
});
4131
});

0 commit comments

Comments
 (0)