Skip to content

Commit a3a88f2

Browse files
added create token function for m2m auth
1 parent 4cb2254 commit a3a88f2

File tree

9 files changed

+101
-4
lines changed

9 files changed

+101
-4
lines changed

.changeset/dull-melons-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/dev-cli': patch
3+
---
4+
5+
Add support for pnpm and workspace dependencies

.changeset/nine-pumas-type.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@clerk/backend": minor
3+
---
4+
5+
feat(backend): Introduce Machine-to-machine Auth
6+
7+
Adding backend function to create machine tokens
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { MachineToken } from '../resources';
2+
import { AbstractAPI } from './AbstractApi';
3+
4+
interface MachineTokensClaims {
5+
[k: string]: unknown;
6+
}
7+
8+
type CreateMachineTokensParams = {
9+
machineId: string;
10+
claims?: MachineTokensClaims;
11+
expiresInSeconds?: number;
12+
allowedClockSkew?: number;
13+
};
14+
15+
const basePath = '/machine_tokens';
16+
export class MachineTokensAPI extends AbstractAPI {
17+
public async create(params: CreateMachineTokensParams) {
18+
return this.request<MachineToken>({
19+
method: 'POST',
20+
path: basePath,
21+
bodyParams: params,
22+
});
23+
}
24+
}

packages/backend/src/api/endpoints/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './ClientApi';
55
export * from './DomainApi';
66
export * from './EmailAddressApi';
77
export * from './InvitationApi';
8+
export * from './MachineTokensApi';
89
export * from './OrganizationApi';
910
export * from './PhoneNumberApi';
1011
export * from './RedirectUrlApi';

packages/backend/src/api/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DomainAPI,
66
EmailAddressAPI,
77
InvitationAPI,
8+
MachineTokensAPI,
89
OrganizationAPI,
910
PhoneNumberAPI,
1011
RedirectUrlAPI,
@@ -31,6 +32,7 @@ export function createBackendApiClient(options: CreateBackendApiOptions) {
3132
clients: new ClientAPI(request),
3233
emailAddresses: new EmailAddressAPI(request),
3334
invitations: new InvitationAPI(request),
35+
machineTokens: new MachineTokensAPI(request),
3436
organizations: new OrganizationAPI(request),
3537
phoneNumbers: new PhoneNumberAPI(request),
3638
redirectUrls: new RedirectUrlAPI(request),

packages/backend/src/api/resources/JSON.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const ObjectType = {
1616
FacebookAccount: 'facebook_account',
1717
GoogleAccount: 'google_account',
1818
Invitation: 'invitation',
19+
MachineToken: 'machine_token',
1920
OauthAccessToken: 'oauth_access_token',
2021
Organization: 'organization',
2122
OrganizationInvitation: 'organization_invitation',
@@ -113,6 +114,11 @@ export interface ExternalAccountJSON extends ClerkResourceJSON {
113114
verification: VerificationJSON | null;
114115
}
115116

117+
export interface MachineTokenJSON {
118+
object: typeof ObjectType.MachineToken;
119+
jwt: string;
120+
}
121+
116122
export interface SamlAccountJSON extends ClerkResourceJSON {
117123
object: typeof ObjectType.SamlAccount;
118124
provider: string;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { MachineTokenJSON } from './JSON';
2+
3+
export class MachineToken {
4+
constructor(readonly token: string) {}
5+
6+
static fromJSON(data: MachineTokenJSON): MachineToken {
7+
return new MachineToken(data.jwt);
8+
}
9+
}

packages/backend/src/api/resources/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export * from './ExternalAccount';
1919
export * from './IdentificationLink';
2020
export * from './Invitation';
2121
export * from './JSON';
22+
export * from './MachineToken';
2223
export * from './OauthAccessToken';
2324
export * from './Organization';
2425
export * from './OrganizationInvitation';

packages/dev-cli/src/commands/setup.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,49 @@ async function mergeEnvFiles(filename, config) {
113113
);
114114
}
115115

116+
/**
117+
* Checks if the current working directory contains a lockfile that can be imported with `pnpm import`
118+
* @returns {boolean}
119+
*/
120+
function hasSupportedLockfile() {
121+
const SUPPORTED_LOCKFILES = ['package-lock.json', 'yarn.lock'];
122+
for (const lockfile of SUPPORTED_LOCKFILES) {
123+
if (existsSync(join(process.cwd(), lockfile))) {
124+
return true;
125+
}
126+
}
127+
128+
return false;
129+
}
130+
131+
/**
132+
* Imports a compatible lockfile with `pnpm import` if present.
133+
* @returns {Promise<void>}
134+
*/
135+
async function importPackageLock() {
136+
return new Promise((resolve, reject) => {
137+
if (!hasSupportedLockfile()) {
138+
resolve();
139+
}
140+
console.log('Supported non-pnpm lockfile detected, importing with `pnpm import`...');
141+
142+
const child = spawn('pnpm', ['import'], {
143+
stdio: 'inherit',
144+
env: {
145+
...process.env,
146+
},
147+
});
148+
149+
child.on('close', code => {
150+
if (code !== 0) {
151+
reject();
152+
return;
153+
}
154+
resolve();
155+
});
156+
});
157+
}
158+
116159
/**
117160
* Installs the monorepo versions of the Clerk dependencies listed in the `package.json` file of the cwd.
118161
* @returns {Promise<void>}
@@ -128,15 +171,13 @@ async function linkDependencies() {
128171
.filter(dep => dep in clerkPackages)
129172
.map(clerkDep => clerkPackages[clerkDep]);
130173

131-
const args = ['install', '--no-audit', '--no-fund', ...dependenciesToBeInstalled];
174+
const args = ['add', ...dependenciesToBeInstalled];
132175

133176
return new Promise((resolve, reject) => {
134-
const child = spawn('npm', args, {
177+
const child = spawn('pnpm', args, {
135178
stdio: 'inherit',
136179
env: {
137180
...process.env,
138-
ADBLOCK: '1',
139-
DISABLE_OPENCOLLECTIVE: '1',
140181
},
141182
});
142183

@@ -160,6 +201,7 @@ async function linkDependencies() {
160201
export async function setup({ js = true, skipInstall = false }) {
161202
if (!skipInstall) {
162203
console.log('Installing monorepo versions of Clerk packages from package.json...');
204+
await importPackageLock();
163205
await linkDependencies();
164206
}
165207

0 commit comments

Comments
 (0)