@meistrari/auth-core 1.13.2 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,7 +1,8 @@
1
1
  import * as better_auth_plugins from 'better-auth/plugins';
2
2
  import * as better_auth from 'better-auth';
3
- import { JWTPayload } from 'better-auth';
3
+ import { JWTPayload as JWTPayload$1 } from 'better-auth';
4
4
  export { APIError } from 'better-auth';
5
+ import { z } from 'zod';
5
6
  import * as better_auth_client from 'better-auth/client';
6
7
  import { BetterFetchOption } from 'better-auth/client';
7
8
  import * as jose from 'jose';
@@ -188,20 +189,33 @@ declare const memberAdditionalFields: {
188
189
  defaultValue: null;
189
190
  };
190
191
  };
191
- type JWTTokenPayload = JWTPayload & {
192
- email: string;
193
- user: {
194
- id: string;
195
- name: string;
196
- image: string | null | undefined;
197
- role: string | null;
198
- };
199
- workspace: {
200
- id: string;
201
- title: string;
202
- };
203
- sessionKey: string;
204
- };
192
+ declare const JWTPayloadUser: z.ZodObject<{
193
+ id: z.ZodString;
194
+ name: z.ZodString;
195
+ image: z.ZodOptional<z.ZodNullable<z.ZodString>>;
196
+ role: z.ZodNullable<z.ZodString>;
197
+ }, z.core.$strip>;
198
+ type JWTPayloadUser = z.infer<typeof JWTPayloadUser>;
199
+ declare const JWTPayloadWorkspace: z.ZodObject<{
200
+ id: z.ZodString;
201
+ title: z.ZodString;
202
+ }, z.core.$strip>;
203
+ type JWTPayloadWorkspace = z.infer<typeof JWTPayloadWorkspace>;
204
+ declare const JWTPayload: z.ZodObject<{
205
+ email: z.ZodString;
206
+ user: z.ZodObject<{
207
+ id: z.ZodString;
208
+ name: z.ZodString;
209
+ image: z.ZodOptional<z.ZodNullable<z.ZodString>>;
210
+ role: z.ZodNullable<z.ZodString>;
211
+ }, z.core.$strip>;
212
+ workspace: z.ZodObject<{
213
+ id: z.ZodString;
214
+ title: z.ZodString;
215
+ }, z.core.$strip>;
216
+ sessionKey: z.ZodString;
217
+ }, z.core.$strip>;
218
+ type JWTPayload = JWTPayload$1 & z.infer<typeof JWTPayload>;
205
219
 
206
220
  type ApiKeyMetadata = {
207
221
  user: {
@@ -3310,6 +3324,61 @@ declare function createAPIClient(apiUrl: string, fetchOptions?: BetterFetchOptio
3310
3324
  data: StartAuthorizationFlowResponse;
3311
3325
  error: null;
3312
3326
  }>;
3327
+ startDeviceAuthorizationFlow: (requesterApplicationId: string, targetApplicationId: string) => Promise<{
3328
+ data: null;
3329
+ error: {
3330
+ message?: string | undefined;
3331
+ status: number;
3332
+ statusText: string;
3333
+ };
3334
+ } | {
3335
+ data: DeviceAuthorizationResponse;
3336
+ error: null;
3337
+ }>;
3338
+ getDeviceAuthorizationContext: (userCode: string) => Promise<{
3339
+ data: null;
3340
+ error: {
3341
+ message?: string | undefined;
3342
+ status: number;
3343
+ statusText: string;
3344
+ };
3345
+ } | {
3346
+ data: DeviceAuthorizationContextResponse;
3347
+ error: null;
3348
+ }>;
3349
+ approveDeviceAuthorizationFlow: (userCode: string, organizationId: string) => Promise<{
3350
+ data: null;
3351
+ error: {
3352
+ message?: string | undefined;
3353
+ status: number;
3354
+ statusText: string;
3355
+ };
3356
+ } | {
3357
+ data: DeviceAuthorizationActionResponse;
3358
+ error: null;
3359
+ }>;
3360
+ denyDeviceAuthorizationFlow: (userCode: string) => Promise<{
3361
+ data: null;
3362
+ error: {
3363
+ message?: string | undefined;
3364
+ status: number;
3365
+ statusText: string;
3366
+ };
3367
+ } | {
3368
+ data: DeviceAuthorizationActionResponse;
3369
+ error: null;
3370
+ }>;
3371
+ exchangeDeviceCodeForTokens: (deviceCode: string) => Promise<{
3372
+ data: null;
3373
+ error: {
3374
+ message?: string | undefined;
3375
+ status: number;
3376
+ statusText: string;
3377
+ };
3378
+ } | {
3379
+ data: CompleteAuthorizationFlowResponse;
3380
+ error: null;
3381
+ }>;
3313
3382
  completeAuthorizationFlow: (code: string, codeVerifier: string) => Promise<{
3314
3383
  data: null;
3315
3384
  error: {
@@ -4346,6 +4415,32 @@ type WhoAmIResponse = {
4346
4415
  user: User;
4347
4416
  organization: FullOrganization;
4348
4417
  };
4418
+ type DeviceAuthorizationResponse = {
4419
+ device_code: string;
4420
+ user_code: string;
4421
+ verification_uri: string;
4422
+ verification_uri_complete: string;
4423
+ expires_in: number;
4424
+ interval: number;
4425
+ };
4426
+ type DeviceContextApplication = {
4427
+ id: string;
4428
+ name: string;
4429
+ description: string | null;
4430
+ };
4431
+ type DeviceAuthorizationContextResponse = {
4432
+ requester: DeviceContextApplication & {
4433
+ isVerified: boolean;
4434
+ };
4435
+ target: DeviceContextApplication;
4436
+ organizations: FullOrganization[];
4437
+ preselectedOrganizationId: string | null;
4438
+ status: 'pending' | 'approved' | 'denied';
4439
+ expiresIn: number;
4440
+ };
4441
+ type DeviceAuthorizationActionResponse = {
4442
+ success: boolean;
4443
+ };
4349
4444
 
4350
4445
  declare class BaseError extends Error {
4351
4446
  code: string;
@@ -4364,6 +4459,21 @@ declare class AuthorizationFlowError extends ApplicationError {
4364
4459
  declare class UserNotLoggedInError extends ApplicationError {
4365
4460
  constructor(message: string, options?: ErrorOptions);
4366
4461
  }
4462
+ declare class DeviceAuthorizationPendingError extends ApplicationError {
4463
+ constructor(options?: ErrorOptions);
4464
+ }
4465
+ declare class DeviceAuthorizationSlowDownError extends ApplicationError {
4466
+ constructor(options?: ErrorOptions);
4467
+ }
4468
+ declare class DeviceAccessDeniedError extends ApplicationError {
4469
+ constructor(options?: ErrorOptions);
4470
+ }
4471
+ declare class DeviceCodeExpiredError extends ApplicationError {
4472
+ constructor(options?: ErrorOptions);
4473
+ }
4474
+ declare class DeviceTransientServerError extends ApplicationError {
4475
+ constructor(options?: ErrorOptions);
4476
+ }
4367
4477
 
4368
4478
  /**
4369
4479
  * Service for managing applications and their candidate organizations.
@@ -4404,6 +4514,11 @@ declare class ApplicationService {
4404
4514
  * @param organizationId - The organization ID to start the authorization flow for
4405
4515
  */
4406
4516
  startAuthorizationFlow(applicationId: string, redirectUri: string, codeChallenge: string, organizationId: string): Promise<StartAuthorizationFlowResponse>;
4517
+ startDeviceAuthorizationFlow(requesterApplicationId: string, targetApplicationId: string): Promise<DeviceAuthorizationResponse>;
4518
+ getDeviceAuthorizationContext(userCode: string): Promise<DeviceAuthorizationContextResponse>;
4519
+ approveDeviceAuthorizationFlow(userCode: string, organizationId: string): Promise<DeviceAuthorizationActionResponse>;
4520
+ denyDeviceAuthorizationFlow(userCode: string): Promise<DeviceAuthorizationActionResponse>;
4521
+ exchangeDeviceCodeForTokens(deviceCode: string): Promise<CompleteAuthorizationFlowResponse>;
4407
4522
  /**
4408
4523
  * Completes an authorization flow for a specific application.
4409
4524
  *
@@ -5067,7 +5182,7 @@ declare function validateToken(token: string, apiUrl: string): Promise<boolean>;
5067
5182
  * console.log(payload.exp) // expiration timestamp
5068
5183
  * ```
5069
5184
  */
5070
- declare function extractTokenPayload(token: string): JWTTokenPayload;
5185
+ declare function extractTokenPayload(token: string): JWTPayload;
5071
5186
 
5072
- export { ApplicationError, AuthClient, AuthorizationFlowError, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, RefreshTokenExpiredError, Roles, UserNotLoggedInError, ac, createAPIClient, extractTokenPayload, isTokenExpired, memberAdditionalFields, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
5073
- export type { APIClient, ApiKey, ApiKeyMetadata, ApiKeyWithoutSecret, Application, CompleteAuthorizationFlowResponse, CreateApiKeyPayload, CreateTeamPayload, FullOrganization, Invitation, InviteUserToOrganizationOptions, JWTTokenPayload, ListCandidateOrganizationsResponse, ListMembersOptions, Member, ExtendedOrganization as Organization, RemoveUserFromOrganizationOptions, Role, Session, SignInWithEmailAndPasswordOptions, SignInWithSamlOptions, SocialSignInOptions, StartAuthorizationFlowResponse, Team, TeamMember, UpdateApiKeyPayload, UpdateMemberRoleOptions, UpdateOrganizationPayload, UpdateTeamPayload, User, WhoAmIResponse };
5187
+ export { ApplicationError, AuthClient, AuthorizationFlowError, DeviceAccessDeniedError, DeviceAuthorizationPendingError, DeviceAuthorizationSlowDownError, DeviceCodeExpiredError, DeviceTransientServerError, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, JWTPayload, JWTPayloadUser, JWTPayloadWorkspace, RefreshTokenExpiredError, Roles, UserNotLoggedInError, ac, createAPIClient, extractTokenPayload, isTokenExpired, memberAdditionalFields, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
5188
+ export type { APIClient, ApiKey, ApiKeyMetadata, ApiKeyWithoutSecret, Application, CompleteAuthorizationFlowResponse, CreateApiKeyPayload, CreateTeamPayload, DeviceAuthorizationActionResponse, DeviceAuthorizationContextResponse, DeviceAuthorizationResponse, FullOrganization, Invitation, InviteUserToOrganizationOptions, ListCandidateOrganizationsResponse, ListMembersOptions, Member, ExtendedOrganization as Organization, RemoveUserFromOrganizationOptions, Role, Session, SignInWithEmailAndPasswordOptions, SignInWithSamlOptions, SocialSignInOptions, StartAuthorizationFlowResponse, Team, TeamMember, UpdateApiKeyPayload, UpdateMemberRoleOptions, UpdateOrganizationPayload, UpdateTeamPayload, User, WhoAmIResponse };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import * as better_auth_plugins from 'better-auth/plugins';
2
2
  import * as better_auth from 'better-auth';
3
- import { JWTPayload } from 'better-auth';
3
+ import { JWTPayload as JWTPayload$1 } from 'better-auth';
4
4
  export { APIError } from 'better-auth';
5
+ import { z } from 'zod';
5
6
  import * as better_auth_client from 'better-auth/client';
6
7
  import { BetterFetchOption } from 'better-auth/client';
7
8
  import * as jose from 'jose';
@@ -188,20 +189,33 @@ declare const memberAdditionalFields: {
188
189
  defaultValue: null;
189
190
  };
190
191
  };
191
- type JWTTokenPayload = JWTPayload & {
192
- email: string;
193
- user: {
194
- id: string;
195
- name: string;
196
- image: string | null | undefined;
197
- role: string | null;
198
- };
199
- workspace: {
200
- id: string;
201
- title: string;
202
- };
203
- sessionKey: string;
204
- };
192
+ declare const JWTPayloadUser: z.ZodObject<{
193
+ id: z.ZodString;
194
+ name: z.ZodString;
195
+ image: z.ZodOptional<z.ZodNullable<z.ZodString>>;
196
+ role: z.ZodNullable<z.ZodString>;
197
+ }, z.core.$strip>;
198
+ type JWTPayloadUser = z.infer<typeof JWTPayloadUser>;
199
+ declare const JWTPayloadWorkspace: z.ZodObject<{
200
+ id: z.ZodString;
201
+ title: z.ZodString;
202
+ }, z.core.$strip>;
203
+ type JWTPayloadWorkspace = z.infer<typeof JWTPayloadWorkspace>;
204
+ declare const JWTPayload: z.ZodObject<{
205
+ email: z.ZodString;
206
+ user: z.ZodObject<{
207
+ id: z.ZodString;
208
+ name: z.ZodString;
209
+ image: z.ZodOptional<z.ZodNullable<z.ZodString>>;
210
+ role: z.ZodNullable<z.ZodString>;
211
+ }, z.core.$strip>;
212
+ workspace: z.ZodObject<{
213
+ id: z.ZodString;
214
+ title: z.ZodString;
215
+ }, z.core.$strip>;
216
+ sessionKey: z.ZodString;
217
+ }, z.core.$strip>;
218
+ type JWTPayload = JWTPayload$1 & z.infer<typeof JWTPayload>;
205
219
 
206
220
  type ApiKeyMetadata = {
207
221
  user: {
@@ -3310,6 +3324,61 @@ declare function createAPIClient(apiUrl: string, fetchOptions?: BetterFetchOptio
3310
3324
  data: StartAuthorizationFlowResponse;
3311
3325
  error: null;
3312
3326
  }>;
3327
+ startDeviceAuthorizationFlow: (requesterApplicationId: string, targetApplicationId: string) => Promise<{
3328
+ data: null;
3329
+ error: {
3330
+ message?: string | undefined;
3331
+ status: number;
3332
+ statusText: string;
3333
+ };
3334
+ } | {
3335
+ data: DeviceAuthorizationResponse;
3336
+ error: null;
3337
+ }>;
3338
+ getDeviceAuthorizationContext: (userCode: string) => Promise<{
3339
+ data: null;
3340
+ error: {
3341
+ message?: string | undefined;
3342
+ status: number;
3343
+ statusText: string;
3344
+ };
3345
+ } | {
3346
+ data: DeviceAuthorizationContextResponse;
3347
+ error: null;
3348
+ }>;
3349
+ approveDeviceAuthorizationFlow: (userCode: string, organizationId: string) => Promise<{
3350
+ data: null;
3351
+ error: {
3352
+ message?: string | undefined;
3353
+ status: number;
3354
+ statusText: string;
3355
+ };
3356
+ } | {
3357
+ data: DeviceAuthorizationActionResponse;
3358
+ error: null;
3359
+ }>;
3360
+ denyDeviceAuthorizationFlow: (userCode: string) => Promise<{
3361
+ data: null;
3362
+ error: {
3363
+ message?: string | undefined;
3364
+ status: number;
3365
+ statusText: string;
3366
+ };
3367
+ } | {
3368
+ data: DeviceAuthorizationActionResponse;
3369
+ error: null;
3370
+ }>;
3371
+ exchangeDeviceCodeForTokens: (deviceCode: string) => Promise<{
3372
+ data: null;
3373
+ error: {
3374
+ message?: string | undefined;
3375
+ status: number;
3376
+ statusText: string;
3377
+ };
3378
+ } | {
3379
+ data: CompleteAuthorizationFlowResponse;
3380
+ error: null;
3381
+ }>;
3313
3382
  completeAuthorizationFlow: (code: string, codeVerifier: string) => Promise<{
3314
3383
  data: null;
3315
3384
  error: {
@@ -4346,6 +4415,32 @@ type WhoAmIResponse = {
4346
4415
  user: User;
4347
4416
  organization: FullOrganization;
4348
4417
  };
4418
+ type DeviceAuthorizationResponse = {
4419
+ device_code: string;
4420
+ user_code: string;
4421
+ verification_uri: string;
4422
+ verification_uri_complete: string;
4423
+ expires_in: number;
4424
+ interval: number;
4425
+ };
4426
+ type DeviceContextApplication = {
4427
+ id: string;
4428
+ name: string;
4429
+ description: string | null;
4430
+ };
4431
+ type DeviceAuthorizationContextResponse = {
4432
+ requester: DeviceContextApplication & {
4433
+ isVerified: boolean;
4434
+ };
4435
+ target: DeviceContextApplication;
4436
+ organizations: FullOrganization[];
4437
+ preselectedOrganizationId: string | null;
4438
+ status: 'pending' | 'approved' | 'denied';
4439
+ expiresIn: number;
4440
+ };
4441
+ type DeviceAuthorizationActionResponse = {
4442
+ success: boolean;
4443
+ };
4349
4444
 
4350
4445
  declare class BaseError extends Error {
4351
4446
  code: string;
@@ -4364,6 +4459,21 @@ declare class AuthorizationFlowError extends ApplicationError {
4364
4459
  declare class UserNotLoggedInError extends ApplicationError {
4365
4460
  constructor(message: string, options?: ErrorOptions);
4366
4461
  }
4462
+ declare class DeviceAuthorizationPendingError extends ApplicationError {
4463
+ constructor(options?: ErrorOptions);
4464
+ }
4465
+ declare class DeviceAuthorizationSlowDownError extends ApplicationError {
4466
+ constructor(options?: ErrorOptions);
4467
+ }
4468
+ declare class DeviceAccessDeniedError extends ApplicationError {
4469
+ constructor(options?: ErrorOptions);
4470
+ }
4471
+ declare class DeviceCodeExpiredError extends ApplicationError {
4472
+ constructor(options?: ErrorOptions);
4473
+ }
4474
+ declare class DeviceTransientServerError extends ApplicationError {
4475
+ constructor(options?: ErrorOptions);
4476
+ }
4367
4477
 
4368
4478
  /**
4369
4479
  * Service for managing applications and their candidate organizations.
@@ -4404,6 +4514,11 @@ declare class ApplicationService {
4404
4514
  * @param organizationId - The organization ID to start the authorization flow for
4405
4515
  */
4406
4516
  startAuthorizationFlow(applicationId: string, redirectUri: string, codeChallenge: string, organizationId: string): Promise<StartAuthorizationFlowResponse>;
4517
+ startDeviceAuthorizationFlow(requesterApplicationId: string, targetApplicationId: string): Promise<DeviceAuthorizationResponse>;
4518
+ getDeviceAuthorizationContext(userCode: string): Promise<DeviceAuthorizationContextResponse>;
4519
+ approveDeviceAuthorizationFlow(userCode: string, organizationId: string): Promise<DeviceAuthorizationActionResponse>;
4520
+ denyDeviceAuthorizationFlow(userCode: string): Promise<DeviceAuthorizationActionResponse>;
4521
+ exchangeDeviceCodeForTokens(deviceCode: string): Promise<CompleteAuthorizationFlowResponse>;
4407
4522
  /**
4408
4523
  * Completes an authorization flow for a specific application.
4409
4524
  *
@@ -5067,7 +5182,7 @@ declare function validateToken(token: string, apiUrl: string): Promise<boolean>;
5067
5182
  * console.log(payload.exp) // expiration timestamp
5068
5183
  * ```
5069
5184
  */
5070
- declare function extractTokenPayload(token: string): JWTTokenPayload;
5185
+ declare function extractTokenPayload(token: string): JWTPayload;
5071
5186
 
5072
- export { ApplicationError, AuthClient, AuthorizationFlowError, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, RefreshTokenExpiredError, Roles, UserNotLoggedInError, ac, createAPIClient, extractTokenPayload, isTokenExpired, memberAdditionalFields, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
5073
- export type { APIClient, ApiKey, ApiKeyMetadata, ApiKeyWithoutSecret, Application, CompleteAuthorizationFlowResponse, CreateApiKeyPayload, CreateTeamPayload, FullOrganization, Invitation, InviteUserToOrganizationOptions, JWTTokenPayload, ListCandidateOrganizationsResponse, ListMembersOptions, Member, ExtendedOrganization as Organization, RemoveUserFromOrganizationOptions, Role, Session, SignInWithEmailAndPasswordOptions, SignInWithSamlOptions, SocialSignInOptions, StartAuthorizationFlowResponse, Team, TeamMember, UpdateApiKeyPayload, UpdateMemberRoleOptions, UpdateOrganizationPayload, UpdateTeamPayload, User, WhoAmIResponse };
5187
+ export { ApplicationError, AuthClient, AuthorizationFlowError, DeviceAccessDeniedError, DeviceAuthorizationPendingError, DeviceAuthorizationSlowDownError, DeviceCodeExpiredError, DeviceTransientServerError, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, JWTPayload, JWTPayloadUser, JWTPayloadWorkspace, RefreshTokenExpiredError, Roles, UserNotLoggedInError, ac, createAPIClient, extractTokenPayload, isTokenExpired, memberAdditionalFields, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
5188
+ export type { APIClient, ApiKey, ApiKeyMetadata, ApiKeyWithoutSecret, Application, CompleteAuthorizationFlowResponse, CreateApiKeyPayload, CreateTeamPayload, DeviceAuthorizationActionResponse, DeviceAuthorizationContextResponse, DeviceAuthorizationResponse, FullOrganization, Invitation, InviteUserToOrganizationOptions, ListCandidateOrganizationsResponse, ListMembersOptions, Member, ExtendedOrganization as Organization, RemoveUserFromOrganizationOptions, Role, Session, SignInWithEmailAndPasswordOptions, SignInWithSamlOptions, SocialSignInOptions, StartAuthorizationFlowResponse, Team, TeamMember, UpdateApiKeyPayload, UpdateMemberRoleOptions, UpdateOrganizationPayload, UpdateTeamPayload, User, WhoAmIResponse };
package/dist/index.mjs CHANGED
@@ -5,9 +5,10 @@ import { createAuthClient } from 'better-auth/client';
5
5
  import { organizationClient, inferOrgAdditionalFields, twoFactorClient, jwtClient, adminClient, inferAdditionalFields } from 'better-auth/client/plugins';
6
6
  import { createAccessControl } from 'better-auth/plugins/access';
7
7
  import { defaultStatements } from 'better-auth/plugins/organization/access';
8
+ import { z } from 'zod';
8
9
  export { APIError } from 'better-auth';
9
10
 
10
- const version = "1.13.2";
11
+ const version = "1.14.0";
11
12
 
12
13
  const statements = {
13
14
  ...defaultStatements,
@@ -60,7 +61,24 @@ const memberAdditionalFields = {
60
61
  defaultValue: null
61
62
  }
62
63
  };
64
+ const JWTPayloadUser = z.object({
65
+ id: z.string(),
66
+ name: z.string(),
67
+ image: z.string().nullable().optional(),
68
+ role: z.string().nullable()
69
+ });
70
+ const JWTPayloadWorkspace = z.object({
71
+ id: z.string(),
72
+ title: z.string()
73
+ });
74
+ const JWTPayload = z.object({
75
+ email: z.string(),
76
+ user: JWTPayloadUser,
77
+ workspace: JWTPayloadWorkspace,
78
+ sessionKey: z.string()
79
+ });
63
80
 
81
+ const DEVICE_CODE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
64
82
  function applicationsPluginClient() {
65
83
  return {
66
84
  id: "applications",
@@ -93,6 +111,49 @@ function applicationsPluginClient() {
93
111
  }
94
112
  });
95
113
  },
114
+ startDeviceAuthorizationFlow: async (requesterApplicationId, targetApplicationId) => {
115
+ return await $fetch("/applications/device/authorize", {
116
+ method: "POST",
117
+ body: {
118
+ requesterApplicationId,
119
+ targetApplicationId
120
+ }
121
+ });
122
+ },
123
+ getDeviceAuthorizationContext: async (userCode) => {
124
+ return await $fetch("/applications/device/context", {
125
+ method: "GET",
126
+ query: {
127
+ userCode
128
+ }
129
+ });
130
+ },
131
+ approveDeviceAuthorizationFlow: async (userCode, organizationId) => {
132
+ return await $fetch("/applications/device/approve", {
133
+ method: "POST",
134
+ body: {
135
+ userCode,
136
+ organizationId
137
+ }
138
+ });
139
+ },
140
+ denyDeviceAuthorizationFlow: async (userCode) => {
141
+ return await $fetch("/applications/device/deny", {
142
+ method: "POST",
143
+ body: {
144
+ userCode
145
+ }
146
+ });
147
+ },
148
+ exchangeDeviceCodeForTokens: async (deviceCode) => {
149
+ return await $fetch("/applications/token", {
150
+ method: "POST",
151
+ body: {
152
+ grantType: DEVICE_CODE_GRANT,
153
+ deviceCode
154
+ }
155
+ });
156
+ },
96
157
  completeAuthorizationFlow: async (code, codeVerifier) => {
97
158
  return await $fetch("/applications/token", {
98
159
  method: "POST",
@@ -224,7 +285,70 @@ class UserNotLoggedInError extends ApplicationError {
224
285
  this.code = "USER_NOT_LOGGED_IN";
225
286
  }
226
287
  }
288
+ class DeviceAuthorizationPendingError extends ApplicationError {
289
+ constructor(options) {
290
+ super("Authorization is still pending", options);
291
+ this.code = "AUTHORIZATION_PENDING";
292
+ }
293
+ }
294
+ class DeviceAuthorizationSlowDownError extends ApplicationError {
295
+ constructor(options) {
296
+ super("Polling too frequently. Slow down and retry later.", options);
297
+ this.code = "SLOW_DOWN";
298
+ }
299
+ }
300
+ class DeviceAccessDeniedError extends ApplicationError {
301
+ constructor(options) {
302
+ super("The user denied the device authorization request", options);
303
+ this.code = "ACCESS_DENIED";
304
+ }
305
+ }
306
+ class DeviceCodeExpiredError extends ApplicationError {
307
+ constructor(options) {
308
+ super("The device code expired or has already been consumed", options);
309
+ this.code = "EXPIRED_TOKEN";
310
+ }
311
+ }
312
+ class DeviceTransientServerError extends ApplicationError {
313
+ constructor(options) {
314
+ super("The authorization server returned a transient error. The exchange can be safely retried.", options);
315
+ this.code = "TRANSIENT_SERVER_ERROR";
316
+ }
317
+ }
227
318
 
319
+ function parseErrorCode(error) {
320
+ if (!error || typeof error !== "object") {
321
+ return null;
322
+ }
323
+ const candidateError = error;
324
+ return candidateError.code ?? candidateError.error?.code ?? null;
325
+ }
326
+ function parseErrorMessage(error) {
327
+ if (!error || typeof error !== "object") {
328
+ return "Failed to exchange device code for tokens";
329
+ }
330
+ const candidateError = error;
331
+ return candidateError.message ?? candidateError.error?.message ?? "Failed to exchange device code for tokens";
332
+ }
333
+ function throwDeviceGrantError(error) {
334
+ const code = parseErrorCode(error);
335
+ if (code === "authorization_pending") {
336
+ throw new DeviceAuthorizationPendingError({ cause: error });
337
+ }
338
+ if (code === "slow_down") {
339
+ throw new DeviceAuthorizationSlowDownError({ cause: error });
340
+ }
341
+ if (code === "access_denied") {
342
+ throw new DeviceAccessDeniedError({ cause: error });
343
+ }
344
+ if (code === "expired_token") {
345
+ throw new DeviceCodeExpiredError({ cause: error });
346
+ }
347
+ if (code === "temporarily_unavailable") {
348
+ throw new DeviceTransientServerError({ cause: error });
349
+ }
350
+ throw new ApplicationError(parseErrorMessage(error), { cause: error });
351
+ }
228
352
  class ApplicationService {
229
353
  /**
230
354
  * Creates a new ApplicationService instance.
@@ -265,6 +389,41 @@ class ApplicationService {
265
389
  }
266
390
  return response.data;
267
391
  }
392
+ async startDeviceAuthorizationFlow(requesterApplicationId, targetApplicationId) {
393
+ const response = await this.client.applications.startDeviceAuthorizationFlow(requesterApplicationId, targetApplicationId);
394
+ if (!response.data) {
395
+ throw new Error("No data returned from the API", { cause: response.error });
396
+ }
397
+ return response.data;
398
+ }
399
+ async getDeviceAuthorizationContext(userCode) {
400
+ const response = await this.client.applications.getDeviceAuthorizationContext(userCode);
401
+ if (!response.data) {
402
+ throw new Error("No data returned from the API", { cause: response.error });
403
+ }
404
+ return response.data;
405
+ }
406
+ async approveDeviceAuthorizationFlow(userCode, organizationId) {
407
+ const response = await this.client.applications.approveDeviceAuthorizationFlow(userCode, organizationId);
408
+ if (!response.data) {
409
+ throw new Error("No data returned from the API", { cause: response.error });
410
+ }
411
+ return response.data;
412
+ }
413
+ async denyDeviceAuthorizationFlow(userCode) {
414
+ const response = await this.client.applications.denyDeviceAuthorizationFlow(userCode);
415
+ if (!response.data) {
416
+ throw new Error("No data returned from the API", { cause: response.error });
417
+ }
418
+ return response.data;
419
+ }
420
+ async exchangeDeviceCodeForTokens(deviceCode) {
421
+ const response = await this.client.applications.exchangeDeviceCodeForTokens(deviceCode);
422
+ if (!response.data) {
423
+ throwDeviceGrantError(response.error);
424
+ }
425
+ return response.data;
426
+ }
268
427
  /**
269
428
  * Completes an authorization flow for a specific application.
270
429
  *
@@ -860,4 +1019,4 @@ function extractTokenPayload(token) {
860
1019
  return payload;
861
1020
  }
862
1021
 
863
- export { ApplicationError, AuthClient, AuthorizationFlowError, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, RefreshTokenExpiredError, Roles, UserNotLoggedInError, ac, extractTokenPayload, isTokenExpired, memberAdditionalFields, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
1022
+ export { ApplicationError, AuthClient, AuthorizationFlowError, DeviceAccessDeniedError, DeviceAuthorizationPendingError, DeviceAuthorizationSlowDownError, DeviceCodeExpiredError, DeviceTransientServerError, EmailRequired, InvalidCallbackURL, InvalidSocialProvider, JWTPayload, JWTPayloadUser, JWTPayloadWorkspace, RefreshTokenExpiredError, Roles, UserNotLoggedInError, ac, extractTokenPayload, isTokenExpired, memberAdditionalFields, organizationAdditionalFields, rolesAccessControl, userAdditionalFields, validateToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meistrari/auth-core",
3
- "version": "1.13.2",
3
+ "version": "1.14.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -20,7 +20,8 @@
20
20
  "@better-auth/api-key": "1.5.4",
21
21
  "@better-auth/sso": "1.5.4",
22
22
  "better-auth": "1.5.4",
23
- "jose": "6.1.3"
23
+ "jose": "6.1.3",
24
+ "zod": "4.3.6"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/node": "latest",