nuxt-auther 1.0.1 → 1.0.3

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.
Files changed (92) hide show
  1. package/dist/module.cjs +5 -0
  2. package/dist/module.d.mts +3 -0
  3. package/dist/module.d.ts +3 -0
  4. package/dist/module.json +8 -0
  5. package/dist/module.mjs +1007 -0
  6. package/dist/runtime/composables.d.ts +2 -0
  7. package/dist/runtime/composables.mjs +2 -0
  8. package/dist/runtime/core/auth.d.ts +53 -0
  9. package/dist/runtime/core/auth.mjs +387 -0
  10. package/dist/runtime/core/index.d.ts +3 -0
  11. package/dist/runtime/core/index.mjs +3 -0
  12. package/dist/runtime/core/middleware.d.ts +2 -0
  13. package/dist/runtime/core/middleware.mjs +41 -0
  14. package/dist/runtime/core/storage.d.ts +41 -0
  15. package/dist/runtime/core/storage.mjs +277 -0
  16. package/dist/runtime/inc/configuration-document-request-error.d.ts +3 -0
  17. package/dist/runtime/inc/configuration-document-request-error.mjs +6 -0
  18. package/dist/runtime/inc/configuration-document.d.ts +26 -0
  19. package/dist/runtime/inc/configuration-document.mjs +94 -0
  20. package/dist/runtime/inc/default-properties.d.ts +123 -0
  21. package/dist/runtime/inc/default-properties.mjs +132 -0
  22. package/dist/runtime/inc/expired-auth-session-error.d.ts +3 -0
  23. package/dist/runtime/inc/expired-auth-session-error.mjs +6 -0
  24. package/dist/runtime/inc/id-token.d.ts +15 -0
  25. package/dist/runtime/inc/id-token.mjs +81 -0
  26. package/dist/runtime/inc/index.d.ts +10 -0
  27. package/dist/runtime/inc/index.mjs +10 -0
  28. package/dist/runtime/inc/refresh-controller.d.ts +9 -0
  29. package/dist/runtime/inc/refresh-controller.mjs +29 -0
  30. package/dist/runtime/inc/refresh-token.d.ts +14 -0
  31. package/dist/runtime/inc/refresh-token.mjs +75 -0
  32. package/dist/runtime/inc/request-handler.d.ts +17 -0
  33. package/dist/runtime/inc/request-handler.mjs +93 -0
  34. package/dist/runtime/inc/token-status.d.ts +12 -0
  35. package/dist/runtime/inc/token-status.mjs +39 -0
  36. package/dist/runtime/inc/token.d.ts +14 -0
  37. package/dist/runtime/inc/token.mjs +83 -0
  38. package/dist/runtime/index.d.ts +3 -0
  39. package/dist/runtime/index.mjs +3 -0
  40. package/dist/runtime/providers/auth0.d.ts +7 -0
  41. package/dist/runtime/providers/auth0.mjs +15 -0
  42. package/dist/runtime/providers/discord.d.ts +6 -0
  43. package/dist/runtime/providers/discord.mjs +18 -0
  44. package/dist/runtime/providers/facebook.d.ts +6 -0
  45. package/dist/runtime/providers/facebook.mjs +13 -0
  46. package/dist/runtime/providers/github.d.ts +6 -0
  47. package/dist/runtime/providers/github.mjs +15 -0
  48. package/dist/runtime/providers/google.d.ts +6 -0
  49. package/dist/runtime/providers/google.mjs +13 -0
  50. package/dist/runtime/providers/index.d.ts +8 -0
  51. package/dist/runtime/providers/index.mjs +8 -0
  52. package/dist/runtime/providers/laravel-jwt.d.ts +7 -0
  53. package/dist/runtime/providers/laravel-jwt.mjs +47 -0
  54. package/dist/runtime/providers/laravel-passport.d.ts +12 -0
  55. package/dist/runtime/providers/laravel-passport.mjs +69 -0
  56. package/dist/runtime/providers/laravel-sanctum.d.ts +6 -0
  57. package/dist/runtime/providers/laravel-sanctum.mjs +52 -0
  58. package/dist/runtime/schemes/auth0.d.ts +4 -0
  59. package/dist/runtime/schemes/auth0.mjs +13 -0
  60. package/dist/runtime/schemes/base.d.ts +8 -0
  61. package/dist/runtime/schemes/base.mjs +11 -0
  62. package/dist/runtime/schemes/cookie.d.ts +23 -0
  63. package/dist/runtime/schemes/cookie.mjs +111 -0
  64. package/dist/runtime/schemes/index.d.ts +8 -0
  65. package/dist/runtime/schemes/index.mjs +8 -0
  66. package/dist/runtime/schemes/laravel-jwt.d.ts +5 -0
  67. package/dist/runtime/schemes/laravel-jwt.mjs +6 -0
  68. package/dist/runtime/schemes/local.d.ts +38 -0
  69. package/dist/runtime/schemes/local.mjs +160 -0
  70. package/dist/runtime/schemes/oauth2.d.ts +61 -0
  71. package/dist/runtime/schemes/oauth2.mjs +374 -0
  72. package/dist/runtime/schemes/openIDConnect.d.ts +24 -0
  73. package/dist/runtime/schemes/openIDConnect.mjs +190 -0
  74. package/dist/runtime/schemes/refresh.d.ts +26 -0
  75. package/dist/runtime/schemes/refresh.mjs +141 -0
  76. package/dist/runtime/token-nitro.d.ts +2 -0
  77. package/dist/runtime/token-nitro.mjs +9 -0
  78. package/dist/types/index.d.ts +42 -0
  79. package/dist/types/openIDConnectConfigurationDocument.d.ts +31 -0
  80. package/dist/types/options.d.ts +125 -0
  81. package/dist/types/provider.d.ts +21 -0
  82. package/dist/types/request.d.ts +8 -0
  83. package/dist/types/router.d.ts +7 -0
  84. package/dist/types/scheme.d.ts +108 -0
  85. package/dist/types/store.d.ts +30 -0
  86. package/dist/types/strategy.d.ts +16 -0
  87. package/dist/types/utils.d.ts +5 -0
  88. package/dist/utils/index.d.ts +28 -0
  89. package/dist/utils/index.mjs +123 -0
  90. package/dist/utils/provider.d.ts +13 -0
  91. package/dist/utils/provider.mjs +360 -0
  92. package/package.json +5 -3
@@ -0,0 +1,47 @@
1
+ import { assignDefaults, assignAbsoluteEndpoints, addLocalAuthorize } from "../../utils/provider";
2
+ import { LOCALDEFAULTS } from "../inc/index.mjs";
3
+ export function laravelJWT(nuxt, strategy) {
4
+ const { url } = strategy;
5
+ if (!url) {
6
+ throw new Error("url is required for laravel jwt!");
7
+ }
8
+ const DEFAULTS = Object.assign(LOCALDEFAULTS, {
9
+ name: "laravelJWT",
10
+ scheme: "laravelJWT",
11
+ endpoints: {
12
+ login: {
13
+ url: url + "/api/auth/login"
14
+ },
15
+ refresh: {
16
+ url: url + "/api/auth/refresh"
17
+ },
18
+ logout: {
19
+ url: url + "/api/auth/logout"
20
+ },
21
+ user: {
22
+ url: url + "/api/auth/user"
23
+ }
24
+ },
25
+ token: {
26
+ property: "access_token",
27
+ maxAge: 3600
28
+ },
29
+ refreshToken: {
30
+ property: false,
31
+ data: false,
32
+ maxAge: 1209600,
33
+ required: false,
34
+ tokenRequired: true
35
+ },
36
+ user: {
37
+ property: false
38
+ },
39
+ clientId: false,
40
+ grantType: false
41
+ });
42
+ assignDefaults(strategy, DEFAULTS);
43
+ assignAbsoluteEndpoints(strategy);
44
+ if (strategy.ssr) {
45
+ addLocalAuthorize(nuxt, strategy);
46
+ }
47
+ }
@@ -0,0 +1,12 @@
1
+ import type { ProviderPartialOptions, ProviderOptions } from '../../types';
2
+ import type { Oauth2SchemeOptions, RefreshSchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface LaravelPassportProviderOptions extends ProviderOptions, Oauth2SchemeOptions {
5
+ url: string;
6
+ }
7
+ export interface LaravelPassportPasswordProviderOptions extends ProviderOptions, RefreshSchemeOptions {
8
+ url: string;
9
+ }
10
+ export type PartialPassportOptions = ProviderPartialOptions<LaravelPassportProviderOptions>;
11
+ export type PartialPassportPasswordOptions = ProviderPartialOptions<LaravelPassportPasswordProviderOptions>;
12
+ export declare function laravelPassport(nuxt: Nuxt, strategy: PartialPassportOptions | PartialPassportPasswordOptions): void;
@@ -0,0 +1,69 @@
1
+ import { assignDefaults, addAuthorize, initializePasswordGrantFlow, assignAbsoluteEndpoints } from "../../utils/provider";
2
+ import { LOCALDEFAULTS } from "../inc/index.mjs";
3
+ function isPasswordGrant(strategy) {
4
+ return strategy.grantType === "password";
5
+ }
6
+ export function laravelPassport(nuxt, strategy) {
7
+ const { url } = strategy;
8
+ if (!url) {
9
+ throw new Error("url is required is laravel passport!");
10
+ }
11
+ const defaults = Object.assign(LOCALDEFAULTS, {
12
+ name: "laravelPassport",
13
+ token: {
14
+ property: "access_token",
15
+ type: "Bearer",
16
+ name: "Authorization",
17
+ maxAge: 60 * 60 * 24 * 365
18
+ },
19
+ refreshToken: {
20
+ property: "refresh_token",
21
+ data: "refresh_token",
22
+ maxAge: 60 * 60 * 24 * 30
23
+ },
24
+ user: {
25
+ property: false
26
+ }
27
+ });
28
+ let DEFAULTS;
29
+ if (isPasswordGrant(strategy)) {
30
+ DEFAULTS = {
31
+ ...defaults,
32
+ scheme: "refresh",
33
+ endpoints: {
34
+ token: url + "/oauth/token",
35
+ login: {
36
+ baseURL: ""
37
+ },
38
+ refresh: {
39
+ baseURL: ""
40
+ },
41
+ logout: false,
42
+ user: {
43
+ url: url + "/api/auth/user"
44
+ }
45
+ },
46
+ grantType: "password"
47
+ };
48
+ assignDefaults(strategy, DEFAULTS);
49
+ assignAbsoluteEndpoints(strategy);
50
+ initializePasswordGrantFlow(nuxt, strategy);
51
+ } else {
52
+ DEFAULTS = {
53
+ ...defaults,
54
+ scheme: "oauth2",
55
+ endpoints: {
56
+ authorization: url + "/oauth/authorize",
57
+ token: url + "/oauth/token",
58
+ userInfo: url + "/api/auth/user",
59
+ logout: false
60
+ },
61
+ responseType: "code",
62
+ grantType: "authorization_code",
63
+ scope: "*"
64
+ };
65
+ assignDefaults(strategy, DEFAULTS);
66
+ assignAbsoluteEndpoints(strategy);
67
+ addAuthorize(nuxt, strategy);
68
+ }
69
+ }
@@ -0,0 +1,6 @@
1
+ import type { ProviderPartialOptions, ProviderOptions } from '../../types';
2
+ import type { CookieSchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface LaravelSanctumProviderOptions extends ProviderOptions, CookieSchemeOptions {
5
+ }
6
+ export declare function laravelSanctum(nuxt: Nuxt, strategy: ProviderPartialOptions<LaravelSanctumProviderOptions>): void;
@@ -0,0 +1,52 @@
1
+ import { assignAbsoluteEndpoints, assignDefaults, addLocalAuthorize } from "../../utils/provider";
2
+ import { LOCALDEFAULTS } from "../inc/index.mjs";
3
+ export function laravelSanctum(nuxt, strategy) {
4
+ const { url } = strategy;
5
+ if (!url) {
6
+ throw new Error("URL is required with Laravel Sanctum!");
7
+ }
8
+ const endpointDefaults = {
9
+ credentials: "include"
10
+ };
11
+ const DEFAULTS = Object.assign(LOCALDEFAULTS, {
12
+ scheme: "cookie",
13
+ name: "laravelSanctum",
14
+ cookie: {
15
+ name: "XSRF-TOKEN"
16
+ },
17
+ endpoints: {
18
+ csrf: {
19
+ ...endpointDefaults,
20
+ url: "/sanctum/csrf-cookie"
21
+ },
22
+ login: {
23
+ ...endpointDefaults,
24
+ url: "/login"
25
+ },
26
+ refresh: {
27
+ ...endpointDefaults,
28
+ url: "/refresh"
29
+ },
30
+ logout: {
31
+ ...endpointDefaults,
32
+ url: "/logout"
33
+ },
34
+ user: {
35
+ ...endpointDefaults,
36
+ url: "/api/user"
37
+ }
38
+ },
39
+ user: {
40
+ property: false,
41
+ autoFetch: true
42
+ },
43
+ token: {
44
+ type: "Bearer"
45
+ }
46
+ });
47
+ assignDefaults(strategy, DEFAULTS);
48
+ assignAbsoluteEndpoints(strategy);
49
+ if (strategy.ssr) {
50
+ addLocalAuthorize(nuxt, strategy);
51
+ }
52
+ }
@@ -0,0 +1,4 @@
1
+ import { Oauth2Scheme } from '../schemes/oauth2';
2
+ export declare class Auth0Scheme extends Oauth2Scheme {
3
+ logout(): void;
4
+ }
@@ -0,0 +1,13 @@
1
+ import { withQuery } from "ufo";
2
+ import { Oauth2Scheme } from "../schemes/oauth2.mjs";
3
+ export class Auth0Scheme extends Oauth2Scheme {
4
+ logout() {
5
+ this.$auth.reset();
6
+ const opts = {
7
+ client_id: this.options.clientId,
8
+ returnTo: this.logoutRedirectURI
9
+ };
10
+ const url = withQuery(this.options.endpoints.logout, opts);
11
+ window.location.replace(url);
12
+ }
13
+ }
@@ -0,0 +1,8 @@
1
+ import type { SchemeOptions } from '../../types';
2
+ import type { Auth } from '..';
3
+ export declare class BaseScheme<OptionsT extends SchemeOptions> {
4
+ $auth: Auth;
5
+ options: OptionsT;
6
+ constructor($auth: Auth, ...options: OptionsT[]);
7
+ get name(): string;
8
+ }
@@ -0,0 +1,11 @@
1
+ import { defu } from "defu";
2
+ export class BaseScheme {
3
+ constructor($auth, ...options) {
4
+ this.$auth = $auth;
5
+ this.options = options.reduce((p, c) => defu(p, c), {});
6
+ }
7
+ options;
8
+ get name() {
9
+ return this.options.name;
10
+ }
11
+ }
@@ -0,0 +1,23 @@
1
+ import type { SchemePartialOptions, SchemeCheck, TokenableScheme, HTTPRequest, HTTPResponse } from '../../types';
2
+ import type { Auth } from '..';
3
+ import { LocalScheme, type LocalSchemeEndpoints, type LocalSchemeOptions } from './local';
4
+ export interface CookieSchemeEndpoints extends LocalSchemeEndpoints {
5
+ csrf?: HTTPRequest | false;
6
+ }
7
+ export interface CookieSchemeCookie {
8
+ name: string;
9
+ }
10
+ export interface CookieSchemeOptions extends LocalSchemeOptions {
11
+ url?: string;
12
+ endpoints: CookieSchemeEndpoints;
13
+ cookie: CookieSchemeCookie;
14
+ }
15
+ export declare class CookieScheme<OptionsT extends CookieSchemeOptions> extends LocalScheme<OptionsT> implements TokenableScheme<OptionsT> {
16
+ checkStatus: boolean;
17
+ constructor($auth: Auth, options: SchemePartialOptions<CookieSchemeOptions>);
18
+ mounted(): Promise<HTTPResponse<any> | void>;
19
+ check(): SchemeCheck;
20
+ login(endpoint: HTTPRequest): Promise<HTTPResponse<any> | void>;
21
+ fetchUser(endpoint?: HTTPRequest): Promise<HTTPResponse<any> | void>;
22
+ reset(): void;
23
+ }
@@ -0,0 +1,111 @@
1
+ import { LocalScheme } from "./local.mjs";
2
+ import { getProp } from "../../utils";
3
+ const DEFAULTS = {
4
+ name: "cookie",
5
+ cookie: {
6
+ name: void 0
7
+ },
8
+ endpoints: {
9
+ csrf: false
10
+ },
11
+ token: {
12
+ type: false,
13
+ property: "",
14
+ maxAge: false,
15
+ global: false,
16
+ required: false
17
+ },
18
+ user: {
19
+ property: false,
20
+ autoFetch: true
21
+ }
22
+ };
23
+ export class CookieScheme extends LocalScheme {
24
+ checkStatus = false;
25
+ constructor($auth, options) {
26
+ super($auth, options, DEFAULTS);
27
+ }
28
+ async mounted() {
29
+ if (process.server) {
30
+ this.$auth.ctx.$http.setHeader("referer", this.$auth.ctx.ssrContext.event.node.req.headers.host);
31
+ }
32
+ if (this.options.token?.type) {
33
+ return super.mounted();
34
+ }
35
+ this.checkStatus = true;
36
+ return this.$auth.fetchUserOnce();
37
+ }
38
+ check() {
39
+ const response = { valid: false };
40
+ if (!super.check().valid && this.options.token?.type) {
41
+ return response;
42
+ }
43
+ if (!this.checkStatus) {
44
+ response.valid = true;
45
+ return response;
46
+ }
47
+ if (this.options.cookie.name) {
48
+ const cookies = this.$auth.$storage.getCookies();
49
+ response.valid = Boolean(cookies[this.options.cookie.name]);
50
+ return response;
51
+ }
52
+ response.valid = true;
53
+ return response;
54
+ }
55
+ async login(endpoint) {
56
+ this.$auth.reset();
57
+ if (this.options.endpoints.csrf) {
58
+ await this.$auth.request(this.options.endpoints.csrf);
59
+ }
60
+ if (this.options.token?.type) {
61
+ return super.login(endpoint, { reset: false });
62
+ }
63
+ if (!this.options.endpoints.login) {
64
+ return;
65
+ }
66
+ if (this.options.ssr) {
67
+ endpoint.baseURL = "";
68
+ }
69
+ const response = await this.$auth.request(endpoint, this.options.endpoints.login);
70
+ if (this.options.user.autoFetch) {
71
+ if (this.checkStatus) {
72
+ this.checkStatus = false;
73
+ }
74
+ await this.fetchUser();
75
+ }
76
+ return response;
77
+ }
78
+ async fetchUser(endpoint) {
79
+ if (!this.check().valid) {
80
+ return Promise.resolve();
81
+ }
82
+ if (!this.options.endpoints.user) {
83
+ this.$auth.setUser({});
84
+ return Promise.resolve();
85
+ }
86
+ if (this.checkStatus) {
87
+ this.checkStatus = false;
88
+ }
89
+ return this.$auth.requestWith(endpoint, this.options.endpoints.user).then((response) => {
90
+ const userData = getProp(response._data, this.options.user.property);
91
+ if (!userData) {
92
+ const error = new Error(`User Data response does not contain field ${this.options.user.property}`);
93
+ return Promise.reject(error);
94
+ }
95
+ this.$auth.setUser(userData);
96
+ return response;
97
+ }).catch((error) => {
98
+ this.$auth.callOnError(error, { method: "fetchUser" });
99
+ return Promise.reject(error);
100
+ });
101
+ }
102
+ reset() {
103
+ if (this.options.cookie.name) {
104
+ this.$auth.$storage.setCookie(this.options.cookie.name, null);
105
+ }
106
+ if (this.options.token?.type) {
107
+ return super.reset();
108
+ }
109
+ this.$auth.setUser(false);
110
+ }
111
+ }
@@ -0,0 +1,8 @@
1
+ export * from './base';
2
+ export * from './cookie';
3
+ export * from './local';
4
+ export * from './oauth2';
5
+ export * from './openIDConnect';
6
+ export * from './refresh';
7
+ export * from './auth0';
8
+ export * from './laravel-jwt';
@@ -0,0 +1,8 @@
1
+ export * from "./base.mjs";
2
+ export * from "./cookie.mjs";
3
+ export * from "./local.mjs";
4
+ export * from "./oauth2.mjs";
5
+ export * from "./openIDConnect.mjs";
6
+ export * from "./refresh.mjs";
7
+ export * from "./auth0.mjs";
8
+ export * from "./laravel-jwt.mjs";
@@ -0,0 +1,5 @@
1
+ import type { HTTPResponse } from '../../types';
2
+ import { RefreshScheme } from './refresh';
3
+ export declare class LaravelJWTScheme extends RefreshScheme {
4
+ protected updateTokens(response: HTTPResponse<any>): void;
5
+ }
@@ -0,0 +1,6 @@
1
+ import { RefreshScheme } from "./refresh.mjs";
2
+ export class LaravelJWTScheme extends RefreshScheme {
3
+ updateTokens(response) {
4
+ super.updateTokens(response);
5
+ }
6
+ }
@@ -0,0 +1,38 @@
1
+ import type { EndpointsOption, SchemePartialOptions, TokenableSchemeOptions, TokenableScheme, UserOptions, HTTPRequest, HTTPResponse, SchemeCheck } from '../../types';
2
+ import type { Auth } from '..';
3
+ import { Token, RequestHandler } from '../inc';
4
+ import { BaseScheme } from './base';
5
+ export interface LocalSchemeEndpoints extends EndpointsOption {
6
+ login: HTTPRequest;
7
+ logout: HTTPRequest | false;
8
+ user: HTTPRequest | false;
9
+ }
10
+ export interface LocalSchemeOptions extends TokenableSchemeOptions {
11
+ endpoints: LocalSchemeEndpoints;
12
+ user: UserOptions;
13
+ clientId: string;
14
+ grantType: 'implicit' | 'authorization_code' | 'client_credentials' | 'password' | 'refresh_token' | 'urn:ietf:params:oauth:grant-type:device_code';
15
+ scope: string | string[];
16
+ }
17
+ export declare class LocalScheme<OptionsT extends LocalSchemeOptions = LocalSchemeOptions> extends BaseScheme<OptionsT> implements TokenableScheme<OptionsT> {
18
+ token: Token;
19
+ requestHandler: RequestHandler;
20
+ constructor($auth: Auth, options: SchemePartialOptions<LocalSchemeOptions>, ...defaults: SchemePartialOptions<LocalSchemeOptions>[]);
21
+ check(checkStatus?: boolean): SchemeCheck;
22
+ mounted({ tokenCallback, refreshTokenCallback }?: {
23
+ tokenCallback?: (() => void) | undefined;
24
+ refreshTokenCallback?: (() => undefined) | undefined;
25
+ }): Promise<HTTPResponse<any> | void>;
26
+ login(endpoint: HTTPRequest, { reset }?: {
27
+ reset?: boolean | undefined;
28
+ }): Promise<HTTPResponse<any> | void>;
29
+ setUserToken(token: string): Promise<HTTPResponse<any> | void>;
30
+ fetchUser(endpoint?: HTTPRequest): Promise<HTTPResponse<any> | void>;
31
+ logout(endpoint?: HTTPRequest): Promise<void>;
32
+ reset({ resetInterceptor }?: {
33
+ resetInterceptor?: boolean | undefined;
34
+ }): void;
35
+ protected extractToken(response: HTTPResponse<any>): string;
36
+ protected updateTokens(response: HTTPResponse<any>): void;
37
+ protected initializeRequestInterceptor(): void;
38
+ }
@@ -0,0 +1,160 @@
1
+ import { getProp } from "../../utils";
2
+ import { Token, RequestHandler } from "../inc/index.mjs";
3
+ import { BaseScheme } from "./base.mjs";
4
+ const DEFAULTS = {
5
+ name: "local",
6
+ endpoints: {
7
+ login: {
8
+ url: "/api/auth/login",
9
+ method: "post"
10
+ },
11
+ logout: {
12
+ url: "/api/auth/logout",
13
+ method: "post"
14
+ },
15
+ user: {
16
+ url: "/api/auth/user",
17
+ method: "get"
18
+ }
19
+ },
20
+ token: {
21
+ expiresProperty: "expires_in",
22
+ property: "token",
23
+ type: "Bearer",
24
+ name: "Authorization",
25
+ maxAge: false,
26
+ global: true,
27
+ required: true,
28
+ prefix: "_token.",
29
+ expirationPrefix: "_token_expiration."
30
+ },
31
+ user: {
32
+ property: "user",
33
+ autoFetch: true
34
+ },
35
+ clientId: void 0,
36
+ grantType: void 0,
37
+ scope: void 0
38
+ };
39
+ export class LocalScheme extends BaseScheme {
40
+ token;
41
+ requestHandler;
42
+ constructor($auth, options, ...defaults) {
43
+ super($auth, options, ...defaults, DEFAULTS);
44
+ this.token = new Token(this, this.$auth.$storage);
45
+ this.requestHandler = new RequestHandler(this, process.server ? this.$auth.ctx.ssrContext.event.$http : this.$auth.ctx.$http, $auth);
46
+ }
47
+ check(checkStatus = false) {
48
+ const response = {
49
+ valid: false,
50
+ tokenExpired: false
51
+ };
52
+ const token = this.token.sync();
53
+ if (!token) {
54
+ return response;
55
+ }
56
+ if (!checkStatus) {
57
+ response.valid = true;
58
+ return response;
59
+ }
60
+ const tokenStatus = this.token.status();
61
+ if (tokenStatus.expired()) {
62
+ response.tokenExpired = true;
63
+ return response;
64
+ }
65
+ response.valid = true;
66
+ return response;
67
+ }
68
+ mounted({ tokenCallback = () => this.$auth.reset(), refreshTokenCallback = () => void 0 } = {}) {
69
+ const { tokenExpired, refreshTokenExpired } = this.check(true);
70
+ if (refreshTokenExpired && typeof refreshTokenCallback === "function") {
71
+ refreshTokenCallback();
72
+ } else if (tokenExpired && typeof tokenCallback === "function") {
73
+ tokenCallback();
74
+ }
75
+ this.initializeRequestInterceptor();
76
+ return this.$auth.fetchUserOnce();
77
+ }
78
+ async login(endpoint, { reset = true } = {}) {
79
+ if (!this.options.endpoints.login) {
80
+ return;
81
+ }
82
+ if (reset) {
83
+ this.$auth.reset({ resetInterceptor: false });
84
+ }
85
+ endpoint = endpoint || {};
86
+ endpoint.body = endpoint.body || {};
87
+ if (this.options.clientId) {
88
+ endpoint.body.client_id = this.options.clientId;
89
+ }
90
+ if (this.options.grantType) {
91
+ endpoint.body.grant_type = this.options.grantType;
92
+ }
93
+ if (this.options.scope) {
94
+ endpoint.body.scope = this.options.scope;
95
+ }
96
+ if (this.options.ssr) {
97
+ endpoint.baseURL = "";
98
+ }
99
+ const response = await this.$auth.request(endpoint, this.options.endpoints.login);
100
+ this.updateTokens(response);
101
+ if (!this.requestHandler.requestInterceptor) {
102
+ this.initializeRequestInterceptor();
103
+ }
104
+ if (this.options.user.autoFetch) {
105
+ await this.fetchUser();
106
+ }
107
+ return response;
108
+ }
109
+ setUserToken(token) {
110
+ this.token.set(token);
111
+ return this.fetchUser();
112
+ }
113
+ async fetchUser(endpoint) {
114
+ if (!this.check().valid) {
115
+ return Promise.resolve();
116
+ }
117
+ if (!this.options.endpoints.user) {
118
+ this.$auth.setUser({});
119
+ return Promise.resolve();
120
+ }
121
+ return this.$auth.requestWith(endpoint, this.options.endpoints.user).then((response) => {
122
+ const userData = getProp(response._data, this.options.user.property);
123
+ if (!userData) {
124
+ const error = new Error(`User Data response does not contain field ${this.options.user.property}`);
125
+ return Promise.reject(error);
126
+ }
127
+ this.$auth.setUser(userData);
128
+ return response;
129
+ }).catch((error) => {
130
+ this.$auth.callOnError(error, { method: "fetchUser" });
131
+ return Promise.reject(error);
132
+ });
133
+ }
134
+ async logout(endpoint = {}) {
135
+ if (this.options.endpoints.logout) {
136
+ await this.$auth.requestWith(endpoint, this.options.endpoints.logout).catch((err) => console.error(err));
137
+ }
138
+ this.$auth.reset();
139
+ this.$auth.redirect("logout");
140
+ }
141
+ reset({ resetInterceptor = true } = {}) {
142
+ this.$auth.setUser(false);
143
+ this.token.reset();
144
+ if (resetInterceptor) {
145
+ this.requestHandler.reset();
146
+ }
147
+ }
148
+ extractToken(response) {
149
+ return getProp(response._data, this.options.token.property);
150
+ }
151
+ updateTokens(response) {
152
+ let tokenExpiresIn = false;
153
+ const token = this.options.token?.required ? this.extractToken(response) : true;
154
+ tokenExpiresIn = this.options.token?.maxAge || getProp(response._data, this.options.token.expiresProperty);
155
+ this.token.set(token, tokenExpiresIn);
156
+ }
157
+ initializeRequestInterceptor() {
158
+ this.requestHandler.initializeRequestInterceptor();
159
+ }
160
+ }
@@ -0,0 +1,61 @@
1
+ import type { RefreshableScheme, SchemePartialOptions, SchemeCheck, RefreshableSchemeOptions, UserOptions, SchemeOptions, HTTPResponse, EndpointsOption, TokenableSchemeOptions } from '../../types';
2
+ import type { IncomingMessage } from 'node:http';
3
+ import type { Auth } from '../core';
4
+ import { RefreshController, RequestHandler, Token, RefreshToken } from '../inc';
5
+ import { BaseScheme } from './base';
6
+ export interface Oauth2SchemeEndpoints extends EndpointsOption {
7
+ authorization: string;
8
+ token: string;
9
+ userInfo: string;
10
+ logout: string | false;
11
+ }
12
+ export interface Oauth2SchemeOptions extends SchemeOptions, TokenableSchemeOptions, RefreshableSchemeOptions {
13
+ endpoints: Oauth2SchemeEndpoints;
14
+ user: UserOptions;
15
+ responseMode: 'query.jwt' | 'fragment.jwt' | 'form_post.jwt' | 'jwt' | '';
16
+ responseType: 'code' | 'token' | 'id_token' | 'none' | string;
17
+ grantType: 'implicit' | 'authorization_code' | 'client_credentials' | 'password' | 'refresh_token' | 'urn:ietf:params:oauth:grant-type:device_code';
18
+ accessType: 'online' | 'offline';
19
+ redirectUri: string;
20
+ logoutRedirectUri: string;
21
+ clientId: string;
22
+ clientSecretTransport: 'body' | 'aurthorization_header';
23
+ scope: string | string[];
24
+ state: string;
25
+ codeChallengeMethod: 'implicit' | 'S256' | 'plain' | '' | false;
26
+ acrValues: string;
27
+ audience: string;
28
+ autoLogout: boolean;
29
+ clientWindow: boolean;
30
+ clientWindowWidth: number;
31
+ clientWindowHeight: number;
32
+ organization?: string;
33
+ }
34
+ export declare class Oauth2Scheme<OptionsT extends Oauth2SchemeOptions = Oauth2SchemeOptions> extends BaseScheme<OptionsT> implements RefreshableScheme {
35
+ #private;
36
+ req: IncomingMessage | undefined;
37
+ token: Token;
38
+ refreshToken: RefreshToken;
39
+ refreshController: RefreshController;
40
+ requestHandler: RequestHandler;
41
+ constructor($auth: Auth, options: SchemePartialOptions<Oauth2SchemeOptions>, ...defaults: SchemePartialOptions<Oauth2SchemeOptions>[]);
42
+ protected get scope(): string;
43
+ protected get redirectURI(): string;
44
+ protected get logoutRedirectURI(): string;
45
+ check(checkStatus?: boolean): SchemeCheck;
46
+ mounted(): Promise<HTTPResponse<any> | void>;
47
+ reset(): void;
48
+ login(options?: {
49
+ state?: string;
50
+ params?: any;
51
+ nonce?: string;
52
+ }): Promise<void>;
53
+ clientWindowCallback(event: MessageEvent): void;
54
+ clientWindowFeatures(clientWindowWidth: number, clientWindowHeight: number): string;
55
+ logout(): void;
56
+ fetchUser(): Promise<void>;
57
+ refreshTokens(): Promise<HTTPResponse<any> | void>;
58
+ protected updateTokens(response: HTTPResponse<any>): void;
59
+ protected pkceChallengeFromVerifier(v: string, hashValue: boolean): Promise<string>;
60
+ generateRandomString(): string;
61
+ }