@docyrus/signin 0.4.1 → 0.6.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.
@@ -1,4 +1,4 @@
1
- import { OAuth2TokenStorage, CryptoProvider, RestApiClient, OAuth2Tokens } from '@docyrus/api-client';
1
+ import { OAuth2TokenStorage, OAuth2Tokens, CryptoProvider, RestApiClient } from '@docyrus/api-client';
2
2
 
3
3
  /** Authentication mode detected at runtime */
4
4
  type AuthMode = 'standalone' | 'iframe' | 'react-native';
@@ -31,6 +31,12 @@ interface DocyrusAuthConfig {
31
31
  tokenStorage?: OAuth2TokenStorage;
32
32
  /** Override auto-detection: force a specific auth mode */
33
33
  forceMode?: AuthMode;
34
+ /**
35
+ * Bootstrap an authenticated session from tokens obtained elsewhere
36
+ * (for example, a prior device flow). When provided, these tokens
37
+ * replace any currently stored standalone/react-native session.
38
+ */
39
+ initialTokens?: OAuth2Tokens;
34
40
  /** Enable SSR: sync access token to a cookie readable by server components. Default: false */
35
41
  ssr?: boolean;
36
42
  /** Cookie name for SSR token sync. Default: 'docyrus-token' */
@@ -52,12 +58,22 @@ interface DocyrusAuthContextValue {
52
58
  client: RestApiClient | null;
53
59
  /** Current tokens (null when unauthenticated) */
54
60
  tokens: OAuth2Tokens | null;
61
+ /** Current user data (null when not authenticated or not yet fetched) */
62
+ user: DocyrusUser | null;
55
63
  /** Initiate sign-in (only relevant in standalone mode) */
56
64
  signIn: () => void;
65
+ /** Bootstrap an authenticated session from existing tokens */
66
+ signInWithTokens: (tokens: OAuth2Tokens) => Promise<void>;
57
67
  /** Get the OAuth2 authorization URL without navigating. For Electron apps */
58
68
  getAuthorizationUrl: () => Promise<string | null>;
59
69
  /** Sign out and clear tokens */
60
70
  signOut: () => Promise<void>;
71
+ /** Check if the current user has a specific role (by slug or uid) */
72
+ hasRole: (role: string | string[] | null | undefined) => boolean;
73
+ /** Check if the current user has permission for an operation on a data source */
74
+ hasPermission: (operation: string, dataSourceId?: string | null) => boolean;
75
+ /** Re-fetch the current user from the API */
76
+ refreshUser: () => Promise<void>;
61
77
  /** Any error that occurred during auth */
62
78
  error: Error | null;
63
79
  }
@@ -71,5 +87,45 @@ interface HostSignInMessage {
71
87
  interface TokenRefreshRequestMessage {
72
88
  type: 'token-refresh-request';
73
89
  }
90
+ /** A role object as returned by GET /v1/users/me */
91
+ interface DocyrusRole {
92
+ uid: string;
93
+ slug: string;
94
+ rules: DocyrusAclRule[];
95
+ }
96
+ /** An ACL rule — maps a data source to its allowed operations */
97
+ interface DocyrusAclRule {
98
+ dataSourceId: string;
99
+ allowedOperations: string[];
100
+ }
101
+ /** The user object as returned by GET /v1/users/me */
102
+ interface DocyrusUser {
103
+ id: string;
104
+ email: string;
105
+ firstname?: string | null;
106
+ lastname?: string | null;
107
+ photo?: string | null;
108
+ primaryRole: DocyrusRole | null;
109
+ roles: DocyrusRole[];
110
+ aclRules: DocyrusAclRule[];
111
+ tenant?: {
112
+ id: string;
113
+ name: string;
114
+ no: number;
115
+ logo_url?: string | null;
116
+ } | null;
117
+ [key: string]: unknown;
118
+ }
119
+ /** ACL operation identifier */
120
+ type AclOperation = 'view' | 'create' | 'edit' | 'delete' | 'create_bulk' | 'delete_bulk' | 'export' | 'export_bulk' | 'import' | 'print' | 'print_bulk';
121
+ /** Configuration for permission checking behavior */
122
+ interface PermissionConfig {
123
+ /**
124
+ * Data sources that are always permitted for certain operations,
125
+ * keyed by data source UUID to an array of allowed operations.
126
+ * Defaults to the standard Docyrus system data sources.
127
+ */
128
+ alwaysPermittedDataSources?: Record<string, AclOperation[]>;
129
+ }
74
130
 
75
- export type { AuthMode as A, DocyrusAuthConfig as D, HostSignInMessage as H, OpenAuthSessionFn as O, TokenRefreshRequestMessage as T, AuthSessionResult as a, AuthStatus as b, DocyrusAuthContextValue as c };
131
+ export type { AclOperation as A, DocyrusAuthConfig as D, HostSignInMessage as H, OpenAuthSessionFn as O, PermissionConfig as P, TokenRefreshRequestMessage as T, AuthMode as a, AuthSessionResult as b, AuthStatus as c, DocyrusAclRule as d, DocyrusAuthContextValue as e, DocyrusRole as f, DocyrusUser as g };
@@ -1,7 +1,7 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as react from 'react';
3
3
  import { ReactNode } from 'react';
4
- import { D as DocyrusAuthConfig, c as DocyrusAuthContextValue } from './types-CmNZOfNS.js';
4
+ import { D as DocyrusAuthConfig, e as DocyrusAuthContextValue } from './types-CxRNvqus.js';
5
5
  import { RestApiClient } from '@docyrus/api-client';
6
6
 
7
7
  interface DocyrusAuthProviderProps extends DocyrusAuthConfig {
@@ -15,7 +15,7 @@ declare const DocyrusAuthContext: react.Context<DocyrusAuthContextValue | null>;
15
15
  * Hook to access Docyrus auth state.
16
16
  * Must be used within a <DocyrusAuthProvider>.
17
17
  *
18
- * Returns: { status, mode, tokens, signIn, signOut, client, error }
18
+ * Returns: { status, mode, tokens, signIn, signInWithTokens, signOut, client, error }
19
19
  */
20
20
  declare function useDocyrusAuth(): DocyrusAuthContextValue;
21
21
 
@@ -1,8 +1,8 @@
1
1
  import * as vue from 'vue';
2
2
  import { PropType } from 'vue';
3
3
  import { OAuth2TokenStorage, OAuth2Tokens, RestApiClient } from '@docyrus/api-client';
4
- import { A as AuthMode, b as AuthStatus } from '../types-CmNZOfNS.js';
5
- export { D as DocyrusAuthConfig } from '../types-CmNZOfNS.js';
4
+ import { a as AuthMode, c as AuthStatus } from '../types-CxRNvqus.js';
5
+ export { D as DocyrusAuthConfig } from '../types-CxRNvqus.js';
6
6
 
7
7
  interface DocyrusAuthState {
8
8
  status: AuthStatus;
@@ -11,6 +11,7 @@ interface DocyrusAuthState {
11
11
  client: RestApiClient | null;
12
12
  error: Error | null;
13
13
  signIn: () => void;
14
+ signInWithTokens: (tokens: OAuth2Tokens) => Promise<void>;
14
15
  getAuthorizationUrl: () => Promise<string | null>;
15
16
  signOut: () => Promise<void>;
16
17
  }
@@ -18,7 +19,7 @@ interface DocyrusAuthState {
18
19
  * Vue composable to access Docyrus auth state.
19
20
  * Must be used within a `<DocyrusAuthProvider>`.
20
21
  *
21
- * Returns: { status, mode, tokens, signIn, signOut, client, error }
22
+ * Returns: { status, mode, tokens, signIn, signInWithTokens, signOut, client, error }
22
23
  */
23
24
  declare function getDocyrusAuth(): DocyrusAuthState;
24
25
  /** @deprecated Use `getDocyrusAuth()` instead. */
@@ -83,6 +84,10 @@ declare const DocyrusAuthProvider: vue.DefineComponent<vue.ExtractPropTypes<{
83
84
  type: PropType<AuthMode>;
84
85
  default: undefined;
85
86
  };
87
+ initialTokens: {
88
+ type: PropType<OAuth2Tokens>;
89
+ default: undefined;
90
+ };
86
91
  }>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
87
92
  [key: string]: any;
88
93
  }>[] | undefined, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
@@ -122,6 +127,10 @@ declare const DocyrusAuthProvider: vue.DefineComponent<vue.ExtractPropTypes<{
122
127
  type: PropType<AuthMode>;
123
128
  default: undefined;
124
129
  };
130
+ initialTokens: {
131
+ type: PropType<OAuth2Tokens>;
132
+ default: undefined;
133
+ };
125
134
  }>> & Readonly<{}>, {
126
135
  clientId: string;
127
136
  apiUrl: string;
@@ -132,6 +141,7 @@ declare const DocyrusAuthProvider: vue.DefineComponent<vue.ExtractPropTypes<{
132
141
  storageKeyPrefix: string;
133
142
  tokenStorage: OAuth2TokenStorage;
134
143
  forceMode: AuthMode;
144
+ initialTokens: OAuth2Tokens;
135
145
  }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
136
146
  /**
137
147
  * "Sign in with Docyrus" button component.
package/dist/vue/index.js CHANGED
@@ -125,6 +125,10 @@ var StandaloneOAuth2Auth = class {
125
125
  async getStoredTokens() {
126
126
  return this.oauth2Client.getTokens();
127
127
  }
128
+ /** Persist externally obtained tokens for immediate authenticated use. */
129
+ async setTokens(tokens) {
130
+ await Promise.resolve(this.storage.setTokens(tokens));
131
+ }
128
132
  /** Check if the stored token is expired (with 60s buffer). */
129
133
  async isTokenExpired() {
130
134
  return this.oauth2Client.isTokenExpired();
@@ -295,6 +299,10 @@ var ReactNativeOAuth2Auth = class {
295
299
  async getStoredTokens() {
296
300
  return this.oauth2Client.getTokens();
297
301
  }
302
+ /** Persist externally obtained tokens for immediate authenticated use. */
303
+ async setTokens(tokens) {
304
+ await Promise.resolve(this.storage.setTokens(tokens));
305
+ }
298
306
  /** Check if the stored token is expired (with 60s buffer). */
299
307
  async isTokenExpired() {
300
308
  return this.oauth2Client.isTokenExpired();
@@ -340,6 +348,7 @@ var AuthManager = class {
340
348
  tokens = null;
341
349
  error = null;
342
350
  client = null;
351
+ user = null;
343
352
  standaloneAuth = null;
344
353
  iframeAuth = null;
345
354
  reactNativeAuth = null;
@@ -365,6 +374,13 @@ var AuthManager = class {
365
374
  getError() {
366
375
  return this.error;
367
376
  }
377
+ getUser() {
378
+ return this.user;
379
+ }
380
+ /** Re-fetch the current user from the API. No-op if not authenticated. */
381
+ async refreshUser() {
382
+ await this.fetchUser();
383
+ }
368
384
  subscribe(listener) {
369
385
  this.listeners.add(listener);
370
386
  return () => this.listeners.delete(listener);
@@ -374,13 +390,22 @@ var AuthManager = class {
374
390
  listener({
375
391
  status: this.status,
376
392
  tokens: this.tokens,
377
- error: this.error
393
+ error: this.error,
394
+ user: this.user
378
395
  });
379
396
  }
380
397
  }
398
+ getInitialTokens() {
399
+ return this.config.initialTokens ?? null;
400
+ }
381
401
  /** Initialize the auth manager. Must be called once on mount. */
382
402
  async initialize() {
383
403
  try {
404
+ if (this.mode === "iframe" && this.getInitialTokens()) {
405
+ throw new Error(
406
+ "initialTokens is not supported in iframe mode. Use the host postMessage signin flow or force standalone mode."
407
+ );
408
+ }
384
409
  if (this.mode === "react-native") {
385
410
  await this.initializeReactNativeMode();
386
411
  } else if (this.mode === "iframe") {
@@ -413,6 +438,12 @@ var AuthManager = class {
413
438
  }
414
439
  return;
415
440
  }
441
+ const initialTokens = this.getInitialTokens();
442
+ if (initialTokens) {
443
+ await this.standaloneAuth.setTokens(initialTokens);
444
+ this.setAuthenticated(initialTokens);
445
+ return;
446
+ }
416
447
  const stored = await this.standaloneAuth.getStoredTokens();
417
448
  if (stored) {
418
449
  const isExpired = await this.standaloneAuth.isTokenExpired();
@@ -450,6 +481,12 @@ var AuthManager = class {
450
481
  */
451
482
  async initializeReactNativeMode() {
452
483
  this.reactNativeAuth = new ReactNativeOAuth2Auth(this.config);
484
+ const initialTokens = this.getInitialTokens();
485
+ if (initialTokens) {
486
+ await this.reactNativeAuth.setTokens(initialTokens);
487
+ this.setAuthenticated(initialTokens);
488
+ return;
489
+ }
453
490
  const stored = await this.reactNativeAuth.getStoredTokens();
454
491
  if (stored) {
455
492
  const isExpired = await this.reactNativeAuth.isTokenExpired();
@@ -484,6 +521,30 @@ var AuthManager = class {
484
521
  if (this.mode !== "standalone" || !this.standaloneAuth) return;
485
522
  await this.standaloneAuth.initiateLogin();
486
523
  }
524
+ /**
525
+ * Bootstrap an authenticated session from tokens obtained outside the
526
+ * browser redirect flow, such as a prior device authorization flow.
527
+ */
528
+ async signInWithTokens(tokens) {
529
+ if (this.mode === "iframe") {
530
+ throw new Error(
531
+ "signInWithTokens() is not supported in iframe mode. Use the host postMessage signin flow or force standalone mode."
532
+ );
533
+ }
534
+ if (this.mode === "react-native") {
535
+ if (!this.reactNativeAuth) {
536
+ this.reactNativeAuth = new ReactNativeOAuth2Auth(this.config);
537
+ }
538
+ await this.reactNativeAuth.setTokens(tokens);
539
+ this.setAuthenticated(tokens);
540
+ return;
541
+ }
542
+ if (!this.standaloneAuth) {
543
+ this.standaloneAuth = new StandaloneOAuth2Auth(this.config);
544
+ }
545
+ await this.standaloneAuth.setTokens(tokens);
546
+ this.setAuthenticated(tokens);
547
+ }
487
548
  /**
488
549
  * Get the OAuth2 authorization URL without navigating.
489
550
  * Use this in Electron apps to open the URL in an external browser.
@@ -514,6 +575,7 @@ var AuthManager = class {
514
575
  }
515
576
  this.tokens = null;
516
577
  this.client = null;
578
+ this.user = null;
517
579
  this.status = "unauthenticated";
518
580
  this.error = null;
519
581
  this.notify();
@@ -521,6 +583,8 @@ var AuthManager = class {
521
583
  /**
522
584
  * Called when valid tokens are received (either mode).
523
585
  * Creates/updates the RestApiClient and schedules token refresh.
586
+ * Fires a user fetch in the background — the first notify() fires with user: null,
587
+ * and a second notify() fires once the user is fetched.
524
588
  */
525
589
  setAuthenticated(tokens) {
526
590
  this.tokens = tokens;
@@ -532,6 +596,18 @@ var AuthManager = class {
532
596
  this.createClient();
533
597
  this.scheduleTokenRefresh(tokens);
534
598
  this.notify();
599
+ this.fetchUser();
600
+ }
601
+ /** Fetch the current user from /v1/users/me. Silently sets user to null on failure. */
602
+ async fetchUser() {
603
+ if (!this.client) return;
604
+ try {
605
+ const response = await this.client.get("/v1/users/me");
606
+ this.user = response.data;
607
+ } catch {
608
+ this.user = null;
609
+ }
610
+ this.notify();
535
611
  }
536
612
  /**
537
613
  * Create a RestApiClient with a custom TokenManager that proactively
@@ -671,7 +747,8 @@ var DocyrusAuthProvider = defineComponent({
671
747
  allowedHostOrigins: { type: Array, default: void 0 },
672
748
  storageKeyPrefix: { type: String, default: void 0 },
673
749
  tokenStorage: { type: Object, default: void 0 },
674
- forceMode: { type: String, default: void 0 }
750
+ forceMode: { type: String, default: void 0 },
751
+ initialTokens: { type: Object, default: void 0 }
675
752
  },
676
753
  setup(props, { slots }) {
677
754
  const status = ref("loading");
@@ -683,6 +760,9 @@ var DocyrusAuthProvider = defineComponent({
683
760
  const signIn = () => {
684
761
  manager?.signIn();
685
762
  };
763
+ const signInWithTokens = async (seedTokens) => {
764
+ await manager?.signInWithTokens(seedTokens);
765
+ };
686
766
  const signOut = async () => {
687
767
  await manager?.signOut();
688
768
  };
@@ -706,6 +786,7 @@ var DocyrusAuthProvider = defineComponent({
706
786
  return error.value;
707
787
  },
708
788
  signIn,
789
+ signInWithTokens,
709
790
  getAuthorizationUrl,
710
791
  signOut
711
792
  };
@@ -720,7 +801,8 @@ var DocyrusAuthProvider = defineComponent({
720
801
  allowedHostOrigins: props.allowedHostOrigins,
721
802
  storageKeyPrefix: props.storageKeyPrefix,
722
803
  tokenStorage: props.tokenStorage,
723
- forceMode: props.forceMode
804
+ forceMode: props.forceMode,
805
+ initialTokens: props.initialTokens
724
806
  };
725
807
  manager = new AuthManager(config);
726
808
  mode.value = manager.getMode();