@docyrus/signin 0.4.1 → 0.6.1
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/README.md +111 -0
- package/dist/{constants-CKGcqYM4.d.ts → constants-piCFw8QN.d.ts} +41 -2
- package/dist/core-index.d.ts +5 -3
- package/dist/core-index.js +179 -2
- package/dist/core-index.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +207 -2
- package/dist/index.js.map +1 -1
- package/dist/react-native/index.d.ts +6 -4
- package/dist/react-native/index.js +207 -2
- package/dist/react-native/index.js.map +1 -1
- package/dist/{types-CmNZOfNS.d.ts → types-CxRNvqus.d.ts} +58 -2
- package/dist/{use-docyrus-client--oJPgA_i.d.ts → use-docyrus-client-wHDwM9iQ.d.ts} +2 -2
- package/dist/vue/index.d.ts +13 -3
- package/dist/vue/index.js +86 -3
- package/dist/vue/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OAuth2TokenStorage, CryptoProvider, RestApiClient
|
|
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 {
|
|
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,
|
|
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
|
|
package/dist/vue/index.d.ts
CHANGED
|
@@ -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 {
|
|
5
|
-
export { D as DocyrusAuthConfig } from '../types-
|
|
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,19 @@ 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
|
+
const maybeUser = response;
|
|
607
|
+
this.user = maybeUser && typeof maybeUser === "object" && "id" in maybeUser && "email" in maybeUser ? maybeUser : response.data;
|
|
608
|
+
} catch {
|
|
609
|
+
this.user = null;
|
|
610
|
+
}
|
|
611
|
+
this.notify();
|
|
535
612
|
}
|
|
536
613
|
/**
|
|
537
614
|
* Create a RestApiClient with a custom TokenManager that proactively
|
|
@@ -671,7 +748,8 @@ var DocyrusAuthProvider = defineComponent({
|
|
|
671
748
|
allowedHostOrigins: { type: Array, default: void 0 },
|
|
672
749
|
storageKeyPrefix: { type: String, default: void 0 },
|
|
673
750
|
tokenStorage: { type: Object, default: void 0 },
|
|
674
|
-
forceMode: { type: String, default: void 0 }
|
|
751
|
+
forceMode: { type: String, default: void 0 },
|
|
752
|
+
initialTokens: { type: Object, default: void 0 }
|
|
675
753
|
},
|
|
676
754
|
setup(props, { slots }) {
|
|
677
755
|
const status = ref("loading");
|
|
@@ -683,6 +761,9 @@ var DocyrusAuthProvider = defineComponent({
|
|
|
683
761
|
const signIn = () => {
|
|
684
762
|
manager?.signIn();
|
|
685
763
|
};
|
|
764
|
+
const signInWithTokens = async (seedTokens) => {
|
|
765
|
+
await manager?.signInWithTokens(seedTokens);
|
|
766
|
+
};
|
|
686
767
|
const signOut = async () => {
|
|
687
768
|
await manager?.signOut();
|
|
688
769
|
};
|
|
@@ -706,6 +787,7 @@ var DocyrusAuthProvider = defineComponent({
|
|
|
706
787
|
return error.value;
|
|
707
788
|
},
|
|
708
789
|
signIn,
|
|
790
|
+
signInWithTokens,
|
|
709
791
|
getAuthorizationUrl,
|
|
710
792
|
signOut
|
|
711
793
|
};
|
|
@@ -720,7 +802,8 @@ var DocyrusAuthProvider = defineComponent({
|
|
|
720
802
|
allowedHostOrigins: props.allowedHostOrigins,
|
|
721
803
|
storageKeyPrefix: props.storageKeyPrefix,
|
|
722
804
|
tokenStorage: props.tokenStorage,
|
|
723
|
-
forceMode: props.forceMode
|
|
805
|
+
forceMode: props.forceMode,
|
|
806
|
+
initialTokens: props.initialTokens
|
|
724
807
|
};
|
|
725
808
|
manager = new AuthManager(config);
|
|
726
809
|
mode.value = manager.getMode();
|