@adonisjs/auth 8.2.3 → 9.0.0-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.
Files changed (137) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +19 -40
  3. package/build/configure.d.ts +5 -0
  4. package/build/configure.js +55 -0
  5. package/build/factories/database_token_factory.d.ts +36 -0
  6. package/build/factories/database_token_factory.js +54 -0
  7. package/build/factories/database_user_provider.d.ts +14 -0
  8. package/build/factories/database_user_provider.js +27 -0
  9. package/build/factories/lucid_user_provider.d.ts +28 -0
  10. package/build/factories/lucid_user_provider.js +68 -0
  11. package/build/factories/main.d.ts +4 -0
  12. package/build/factories/main.js +12 -0
  13. package/build/factories/session_guard_factory.d.ts +13 -0
  14. package/build/factories/session_guard_factory.js +24 -0
  15. package/build/index.d.ts +7 -0
  16. package/build/index.js +15 -0
  17. package/build/providers/auth_provider.d.ts +12 -0
  18. package/build/providers/auth_provider.js +27 -0
  19. package/build/services/auth.d.ts +3 -0
  20. package/build/services/auth.js +17 -0
  21. package/build/src/auth/auth_manager.d.ts +22 -0
  22. package/build/src/auth/auth_manager.js +34 -0
  23. package/build/src/auth/authenticator.d.ts +56 -0
  24. package/build/src/auth/authenticator.js +122 -0
  25. package/build/src/auth/debug.d.ts +3 -0
  26. package/build/{adonis-typings/container.js → src/auth/debug.js} +3 -1
  27. package/build/src/auth/define_config.d.ts +30 -0
  28. package/build/src/auth/define_config.js +54 -0
  29. package/build/src/auth/errors.d.ts +82 -0
  30. package/build/src/auth/errors.js +181 -0
  31. package/build/src/auth/middleware/auth_middleware.d.ts +13 -0
  32. package/build/src/auth/middleware/auth_middleware.js +6 -0
  33. package/build/src/auth/middleware/initialize_auth_middleware.d.ts +18 -0
  34. package/build/src/auth/middleware/initialize_auth_middleware.js +25 -0
  35. package/build/src/auth/symbols.d.ts +9 -0
  36. package/build/src/auth/symbols.js +17 -0
  37. package/build/src/auth/types.d.ts +75 -0
  38. package/build/{adonis-typings/context.js → src/auth/types.js} +2 -1
  39. package/build/src/auth/user_providers/main.d.ts +15 -0
  40. package/build/src/auth/user_providers/main.js +22 -0
  41. package/build/src/core/guard_user.d.ts +26 -0
  42. package/build/src/core/guard_user.js +29 -0
  43. package/build/src/core/token.d.ts +89 -0
  44. package/build/src/core/token.js +114 -0
  45. package/build/src/core/token_providers/database.d.ts +77 -0
  46. package/build/src/core/token_providers/database.js +113 -0
  47. package/build/src/core/types.d.ts +178 -0
  48. package/build/{adonis-typings/auth.js → src/core/types.js} +2 -1
  49. package/build/src/core/user_providers/database.d.ts +78 -0
  50. package/build/src/core/user_providers/database.js +117 -0
  51. package/build/src/core/user_providers/lucid.d.ts +61 -0
  52. package/build/src/core/user_providers/lucid.js +122 -0
  53. package/build/src/guards/session/define_config.d.ts +23 -0
  54. package/build/src/guards/session/define_config.js +56 -0
  55. package/build/src/guards/session/guard.d.ts +112 -0
  56. package/build/src/guards/session/guard.js +454 -0
  57. package/build/src/guards/session/main.d.ts +3 -0
  58. package/build/src/guards/session/main.js +11 -0
  59. package/build/src/guards/session/token.d.ts +57 -0
  60. package/build/src/guards/session/token.js +58 -0
  61. package/build/src/guards/session/token_providers/main.d.ts +33 -0
  62. package/build/src/guards/session/token_providers/main.js +42 -0
  63. package/build/src/guards/session/types.d.ts +96 -0
  64. package/build/{adonis-typings/events.js → src/guards/session/types.js} +2 -1
  65. package/build/stubs/config.stub +35 -0
  66. package/build/stubs/main.d.ts +1 -0
  67. package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
  68. package/package.json +99 -91
  69. package/build/adonis-typings/auth.d.ts +0 -635
  70. package/build/adonis-typings/container.d.ts +0 -6
  71. package/build/adonis-typings/context.d.ts +0 -6
  72. package/build/adonis-typings/events.d.ts +0 -10
  73. package/build/adonis-typings/index.d.ts +0 -5
  74. package/build/adonis-typings/index.js +0 -13
  75. package/build/adonis-typings/tests.d.ts +0 -23
  76. package/build/instructions.js +0 -338
  77. package/build/providers/AuthProvider.d.ts +0 -30
  78. package/build/providers/AuthProvider.js +0 -69
  79. package/build/src/Auth/index.d.ts +0 -97
  80. package/build/src/Auth/index.js +0 -155
  81. package/build/src/AuthManager/index.d.ts +0 -117
  82. package/build/src/AuthManager/index.js +0 -262
  83. package/build/src/Bindings/Tests.d.ts +0 -6
  84. package/build/src/Bindings/Tests.js +0 -69
  85. package/build/src/Clients/Oat/index.d.ts +0 -50
  86. package/build/src/Clients/Oat/index.js +0 -123
  87. package/build/src/Clients/Session/index.d.ts +0 -34
  88. package/build/src/Clients/Session/index.js +0 -72
  89. package/build/src/Exceptions/AuthenticationException.d.ts +0 -47
  90. package/build/src/Exceptions/AuthenticationException.js +0 -142
  91. package/build/src/Exceptions/InvalidCredentialsException.d.ts +0 -34
  92. package/build/src/Exceptions/InvalidCredentialsException.js +0 -112
  93. package/build/src/Guards/Base/index.d.ts +0 -75
  94. package/build/src/Guards/Base/index.js +0 -138
  95. package/build/src/Guards/BasicAuth/index.d.ts +0 -67
  96. package/build/src/Guards/BasicAuth/index.js +0 -181
  97. package/build/src/Guards/Oat/index.d.ts +0 -149
  98. package/build/src/Guards/Oat/index.js +0 -347
  99. package/build/src/Guards/Session/index.d.ts +0 -127
  100. package/build/src/Guards/Session/index.js +0 -338
  101. package/build/src/TokenProviders/Database/index.d.ts +0 -43
  102. package/build/src/TokenProviders/Database/index.js +0 -126
  103. package/build/src/TokenProviders/Redis/index.d.ts +0 -44
  104. package/build/src/TokenProviders/Redis/index.js +0 -129
  105. package/build/src/Tokens/OpaqueToken/index.d.ts +0 -46
  106. package/build/src/Tokens/OpaqueToken/index.js +0 -43
  107. package/build/src/Tokens/ProviderToken/index.d.ts +0 -23
  108. package/build/src/Tokens/ProviderToken/index.js +0 -27
  109. package/build/src/UserProviders/Database/User.d.ts +0 -28
  110. package/build/src/UserProviders/Database/User.js +0 -74
  111. package/build/src/UserProviders/Database/index.d.ts +0 -75
  112. package/build/src/UserProviders/Database/index.js +0 -141
  113. package/build/src/UserProviders/Lucid/User.d.ts +0 -28
  114. package/build/src/UserProviders/Lucid/User.js +0 -74
  115. package/build/src/UserProviders/Lucid/index.d.ts +0 -72
  116. package/build/src/UserProviders/Lucid/index.js +0 -146
  117. package/build/standalone.d.ts +0 -1
  118. package/build/standalone.js +0 -13
  119. package/build/templates/config/auth.txt +0 -34
  120. package/build/templates/config/partials/api-guard.txt +0 -22
  121. package/build/templates/config/partials/basic-guard.txt +0 -19
  122. package/build/templates/config/partials/tokens-provider-database.txt +0 -19
  123. package/build/templates/config/partials/tokens-provider-redis.txt +0 -22
  124. package/build/templates/config/partials/user-provider-database.txt +0 -43
  125. package/build/templates/config/partials/user-provider-lucid.txt +0 -45
  126. package/build/templates/config/partials/web-guard.txt +0 -17
  127. package/build/templates/contract/auth.txt +0 -55
  128. package/build/templates/contract/partials/api-guard.txt +0 -14
  129. package/build/templates/contract/partials/basic-guard.txt +0 -14
  130. package/build/templates/contract/partials/user-provider-database.txt +0 -16
  131. package/build/templates/contract/partials/user-provider-lucid.txt +0 -16
  132. package/build/templates/contract/partials/web-guard.txt +0 -14
  133. package/build/templates/middleware/Auth.txt +0 -76
  134. package/build/templates/middleware/SilentAuth.txt +0 -21
  135. package/build/templates/migrations/api_tokens.txt +0 -25
  136. package/build/templates/migrations/auth.txt +0 -24
  137. package/build/templates/model.txt +0 -30
@@ -1,155 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/auth
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.Auth = void 0;
12
- /**
13
- * Auth class exposes the API to obtain guard instances for a given
14
- * HTTP request.
15
- */
16
- class Auth {
17
- constructor(manager, ctx) {
18
- this.manager = manager;
19
- this.ctx = ctx;
20
- /**
21
- * We keep a per request singleton instances for each instantiated mapping
22
- */
23
- this.mappingsCache = new Map();
24
- /**
25
- * The default guard is always the one defined inside the config, until
26
- * manually overwritten by the user
27
- */
28
- this.defaultGuard = this.manager.defaultGuard;
29
- }
30
- /**
31
- * Returns an instance of a named or the default mapping
32
- */
33
- use(mapping) {
34
- mapping = mapping || this.defaultGuard;
35
- if (!this.mappingsCache.has(mapping)) {
36
- this.ctx.logger.trace('instantiating auth mapping', { name: mapping });
37
- this.mappingsCache.set(mapping, this.manager.makeMapping(this.ctx, mapping));
38
- }
39
- return this.mappingsCache.get(mapping);
40
- }
41
- /**
42
- * Guard name for the default mapping
43
- */
44
- get name() {
45
- return this.use().name;
46
- }
47
- /**
48
- * Reference to the logged in user
49
- */
50
- get user() {
51
- return this.use().user;
52
- }
53
- /**
54
- * Reference to the default guard config
55
- */
56
- get config() {
57
- return this.use().config;
58
- }
59
- /**
60
- * Find if the user has been logged out in the current request
61
- */
62
- get isLoggedOut() {
63
- return this.use().isLoggedOut;
64
- }
65
- /**
66
- * A boolean to know if user is a guest or not. It is
67
- * always opposite of [[isLoggedIn]]
68
- */
69
- get isGuest() {
70
- return this.use().isGuest;
71
- }
72
- /**
73
- * A boolean to know if user is logged in or not
74
- */
75
- get isLoggedIn() {
76
- return this.use().isLoggedIn;
77
- }
78
- /**
79
- * A boolean to know if user is retrieved by authenticating
80
- * the current request or not.
81
- */
82
- get isAuthenticated() {
83
- return this.use().isAuthenticated;
84
- }
85
- /**
86
- * Whether or not the authentication has been attempted
87
- * for the current request
88
- */
89
- get authenticationAttempted() {
90
- return this.use().authenticationAttempted;
91
- }
92
- /**
93
- * Reference to the provider for looking up the user
94
- */
95
- get provider() {
96
- return this.use().provider;
97
- }
98
- /**
99
- * Verify user credentials.
100
- */
101
- async verifyCredentials(uid, password) {
102
- return this.use().verifyCredentials(uid, password);
103
- }
104
- /**
105
- * Attempt to verify user credentials and perform login
106
- */
107
- async attempt(uid, password, ...args) {
108
- return this.use().attempt(uid, password, ...args);
109
- }
110
- /**
111
- * Login a user without any verification
112
- */
113
- async login(user, ...args) {
114
- return this.use().login(user, ...args);
115
- }
116
- /**
117
- * Login a user using their id
118
- */
119
- async loginViaId(id, ...args) {
120
- return this.use().loginViaId(id, ...args);
121
- }
122
- /**
123
- * Attempts to authenticate the user for the current HTTP request. An exception
124
- * is raised when unable to do so
125
- */
126
- async authenticate() {
127
- return this.use().authenticate();
128
- }
129
- /**
130
- * Attempts to authenticate the user for the current HTTP request and supresses
131
- * exceptions raised by the [[authenticate]] method and returns a boolean
132
- */
133
- async check() {
134
- return this.use().check();
135
- }
136
- /**
137
- * Logout user
138
- */
139
- async logout(...args) {
140
- return this.use().logout(...args);
141
- }
142
- /**
143
- * Serialize toJSON
144
- */
145
- toJSON() {
146
- return {
147
- defaultGuard: this.defaultGuard,
148
- guards: [...this.mappingsCache.keys()].reduce((result, key) => {
149
- result[key] = this.mappingsCache.get(key).toJSON();
150
- return result;
151
- }, {}),
152
- };
153
- }
154
- }
155
- exports.Auth = Auth;
@@ -1,117 +0,0 @@
1
- /// <reference types="@adonisjs/application/build/adonis-typings/application" />
2
- import { AuthConfig, GuardsList, AuthManagerContract, ExtendGuardCallback, ExtendProviderCallback, ExtendClientCallback } from '@ioc:Adonis/Addons/Auth';
3
- import { ApplicationContract } from '@ioc:Adonis/Core/Application';
4
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
5
- /**
6
- * Auth manager to manage guards and providers object. The extend API can
7
- * be used to add custom guards and providers
8
- */
9
- export declare class AuthManager implements AuthManagerContract {
10
- application: ApplicationContract;
11
- private config;
12
- /**
13
- * Extended set of testing clients
14
- */
15
- private extendedClients;
16
- /**
17
- * Extended set of providers
18
- */
19
- private extendedProviders;
20
- /**
21
- * Extend set of guards
22
- */
23
- private extendedGuards;
24
- /**
25
- * Reference to the default guard
26
- */
27
- defaultGuard: keyof GuardsList;
28
- constructor(application: ApplicationContract, config: AuthConfig);
29
- /**
30
- * Verifies and returns an instance of the event emitter
31
- */
32
- private getEmitter;
33
- /**
34
- * Lazily makes an instance of the lucid provider
35
- */
36
- private makeLucidProvider;
37
- /**
38
- * Lazily makes an instance of the database provider
39
- */
40
- private makeDatabaseProvider;
41
- /**
42
- * Returns an instance of the extended provider
43
- */
44
- private makeExtendedProvider;
45
- /**
46
- * Lazily makes an instance of the token database provider
47
- */
48
- private makeTokenDatabaseProvider;
49
- /**
50
- * Lazily makes an instance of the token redis provider
51
- */
52
- private makeTokenRedisProvider;
53
- /**
54
- * Returns an instance of the session guard
55
- */
56
- private makeSessionGuard;
57
- /**
58
- * Returns an instance of the session guard
59
- */
60
- private makeOatGuard;
61
- /**
62
- * Returns an instance of the basic auth guard
63
- */
64
- private makeBasicAuthGuard;
65
- /**
66
- * Returns an instance of the extended guard
67
- */
68
- private makeExtendedGuard;
69
- /**
70
- * Returns an instance of the session client
71
- */
72
- private makeSessionClient;
73
- /**
74
- * Returns an instance of the session client
75
- */
76
- private makeOatClient;
77
- /**
78
- * Returns an instance of the extended client
79
- */
80
- private makeExtendedClient;
81
- /**
82
- * Makes client instance for the defined driver inside the
83
- * mapping config.
84
- */
85
- private makeClientInstance;
86
- /**
87
- * Makes instance of a provider based upon the driver value
88
- */
89
- private makeUserProviderInstance;
90
- /**
91
- * Makes instance of a provider based upon the driver value
92
- */
93
- private makeTokenProviderInstance;
94
- /**
95
- * Makes guard instance for the defined driver inside the
96
- * mapping config.
97
- */
98
- private makeGuardInstance;
99
- /**
100
- * Make an instance of a given mapping for the current HTTP request.
101
- */
102
- makeMapping(ctx: HttpContextContract, mapping: keyof GuardsList): any;
103
- /**
104
- * Returns an instance of the testing
105
- */
106
- client(mapping: keyof GuardsList): any;
107
- /**
108
- * Returns an instance of the auth class for the current request
109
- */
110
- getAuthForRequest(ctx: HttpContextContract): any;
111
- /**
112
- * Extend auth by adding custom providers and guards
113
- */
114
- extend(type: 'provider', name: string, callback: ExtendProviderCallback): void;
115
- extend(type: 'guard', name: string, callback: ExtendGuardCallback): void;
116
- extend(type: 'client', name: string, callback: ExtendClientCallback): void;
117
- }
@@ -1,262 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/auth
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.AuthManager = void 0;
12
- const utils_1 = require("@poppinss/utils");
13
- const Auth_1 = require("../Auth");
14
- /**
15
- * Auth manager to manage guards and providers object. The extend API can
16
- * be used to add custom guards and providers
17
- */
18
- class AuthManager {
19
- constructor(application, config) {
20
- this.application = application;
21
- this.config = config;
22
- /**
23
- * Extended set of testing clients
24
- */
25
- this.extendedClients = new Map();
26
- /**
27
- * Extended set of providers
28
- */
29
- this.extendedProviders = new Map();
30
- /**
31
- * Extend set of guards
32
- */
33
- this.extendedGuards = new Map();
34
- /**
35
- * Reference to the default guard
36
- */
37
- this.defaultGuard = this.config.guard;
38
- const validator = new utils_1.ManagerConfigValidator(config, 'auth', 'config/auth');
39
- validator.validateDefault('guard');
40
- validator.validateList('guards', 'guard');
41
- }
42
- /**
43
- * Verifies and returns an instance of the event emitter
44
- */
45
- getEmitter() {
46
- const hasEmitter = this.application.container.hasBinding('Adonis/Core/Event');
47
- if (!hasEmitter) {
48
- throw new utils_1.Exception('"Adonis/Core/Event" is required by the auth provider');
49
- }
50
- return this.application.container.use('Adonis/Core/Event');
51
- }
52
- /**
53
- * Lazily makes an instance of the lucid provider
54
- */
55
- makeLucidProvider(config) {
56
- return new (require('../UserProviders/Lucid').LucidProvider)(this.application, config);
57
- }
58
- /**
59
- * Lazily makes an instance of the database provider
60
- */
61
- makeDatabaseProvider(config) {
62
- const Database = this.application.container.use('Adonis/Lucid/Database');
63
- return new (require('../UserProviders/Database').DatabaseProvider)(this.application, config, Database);
64
- }
65
- /**
66
- * Returns an instance of the extended provider
67
- */
68
- makeExtendedProvider(mapping, config) {
69
- const providerCallback = this.extendedProviders.get(config.driver);
70
- if (!providerCallback) {
71
- throw new utils_1.Exception(`Invalid provider "${config.driver}"`);
72
- }
73
- return providerCallback(this, mapping, config);
74
- }
75
- /**
76
- * Lazily makes an instance of the token database provider
77
- */
78
- makeTokenDatabaseProvider(config) {
79
- const Database = this.application.container.use('Adonis/Lucid/Database');
80
- return new (require('../TokenProviders/Database').TokenDatabaseProvider)(config, Database);
81
- }
82
- /**
83
- * Lazily makes an instance of the token redis provider
84
- */
85
- makeTokenRedisProvider(config) {
86
- if (!this.application.container.hasBinding('Adonis/Addons/Redis')) {
87
- throw new utils_1.Exception('"@adonisjs/redis" is required to use the "redis" token provider');
88
- }
89
- const Redis = this.application.container.use('Adonis/Addons/Redis');
90
- return new (require('../TokenProviders/Redis').TokenRedisProvider)(config, Redis);
91
- }
92
- /**
93
- * Returns an instance of the session guard
94
- */
95
- makeSessionGuard(mapping, config, provider, ctx) {
96
- const { SessionGuard } = require('../Guards/Session');
97
- return new SessionGuard(mapping, config, this.getEmitter(), provider, ctx);
98
- }
99
- /**
100
- * Returns an instance of the session guard
101
- */
102
- makeOatGuard(mapping, config, provider, ctx) {
103
- const { OATGuard } = require('../Guards/Oat');
104
- const tokenProvider = this.makeTokenProviderInstance(config.tokenProvider);
105
- return new OATGuard(mapping, config, this.getEmitter(), provider, ctx, tokenProvider);
106
- }
107
- /**
108
- * Returns an instance of the basic auth guard
109
- */
110
- makeBasicAuthGuard(mapping, config, provider, ctx) {
111
- const { BasicAuthGuard } = require('../Guards/BasicAuth');
112
- return new BasicAuthGuard(mapping, config, this.getEmitter(), provider, ctx);
113
- }
114
- /**
115
- * Returns an instance of the extended guard
116
- */
117
- makeExtendedGuard(mapping, config, provider, ctx) {
118
- const guardCallback = this.extendedGuards.get(config.driver);
119
- if (!guardCallback) {
120
- throw new utils_1.Exception(`Invalid guard driver "${config.driver}" property`);
121
- }
122
- return guardCallback(this, mapping, config, provider, ctx);
123
- }
124
- /**
125
- * Returns an instance of the session client
126
- */
127
- makeSessionClient(mapping, config, provider) {
128
- const { SessionClient } = require('../Clients/Session');
129
- return new SessionClient(mapping, config, provider);
130
- }
131
- /**
132
- * Returns an instance of the session client
133
- */
134
- makeOatClient(mapping, config, provider) {
135
- const { OATClient } = require('../Clients/Oat');
136
- const tokenProvider = this.makeTokenProviderInstance(config.tokenProvider);
137
- return new OATClient(mapping, config, provider, tokenProvider);
138
- }
139
- /**
140
- * Returns an instance of the extended client
141
- */
142
- makeExtendedClient(mapping, config, provider) {
143
- const clientCallback = this.extendedClients.get(config.driver);
144
- if (!clientCallback) {
145
- throw new utils_1.Exception(`Invalid guard driver "${config.driver}" property`);
146
- }
147
- return clientCallback(this, mapping, config, provider);
148
- }
149
- /**
150
- * Makes client instance for the defined driver inside the
151
- * mapping config.
152
- */
153
- makeClientInstance(mapping, mappingConfig, provider) {
154
- if (!mappingConfig || !mappingConfig.driver) {
155
- throw new utils_1.Exception('Invalid auth config, missing "driver" property');
156
- }
157
- switch (mappingConfig.driver) {
158
- case 'session':
159
- return this.makeSessionClient(mapping, mappingConfig, provider);
160
- case 'oat':
161
- return this.makeOatClient(mapping, mappingConfig, provider);
162
- case 'basic':
163
- throw new utils_1.Exception('There is no testing client for basic auth. Use "request.basicAuth" method instead');
164
- default:
165
- return this.makeExtendedClient(mapping, mappingConfig, provider);
166
- }
167
- }
168
- /**
169
- * Makes instance of a provider based upon the driver value
170
- */
171
- makeUserProviderInstance(mapping, providerConfig) {
172
- if (!providerConfig || !providerConfig.driver) {
173
- throw new utils_1.Exception('Invalid auth config, missing "provider" or "provider.driver" property');
174
- }
175
- switch (providerConfig.driver) {
176
- case 'lucid':
177
- return this.makeLucidProvider(providerConfig);
178
- case 'database':
179
- return this.makeDatabaseProvider(providerConfig);
180
- default:
181
- return this.makeExtendedProvider(mapping, providerConfig);
182
- }
183
- }
184
- /**
185
- * Makes instance of a provider based upon the driver value
186
- */
187
- makeTokenProviderInstance(providerConfig) {
188
- if (!providerConfig || !providerConfig.driver) {
189
- throw new utils_1.Exception('Invalid auth config, missing "tokenProvider" or "tokenProvider.driver" property');
190
- }
191
- switch (providerConfig.driver) {
192
- case 'database':
193
- return this.makeTokenDatabaseProvider(providerConfig);
194
- case 'redis':
195
- return this.makeTokenRedisProvider(providerConfig);
196
- default:
197
- throw new utils_1.Exception(`Invalid token provider "${providerConfig.driver}"`);
198
- }
199
- }
200
- /**
201
- * Makes guard instance for the defined driver inside the
202
- * mapping config.
203
- */
204
- makeGuardInstance(mapping, mappingConfig, provider, ctx) {
205
- if (!mappingConfig || !mappingConfig.driver) {
206
- throw new utils_1.Exception('Invalid auth config, missing "driver" property');
207
- }
208
- switch (mappingConfig.driver) {
209
- case 'session':
210
- return this.makeSessionGuard(mapping, mappingConfig, provider, ctx);
211
- case 'oat':
212
- return this.makeOatGuard(mapping, mappingConfig, provider, ctx);
213
- case 'basic':
214
- return this.makeBasicAuthGuard(mapping, mappingConfig, provider, ctx);
215
- default:
216
- return this.makeExtendedGuard(mapping, mappingConfig, provider, ctx);
217
- }
218
- }
219
- /**
220
- * Make an instance of a given mapping for the current HTTP request.
221
- */
222
- makeMapping(ctx, mapping) {
223
- const mappingConfig = this.config.guards[mapping];
224
- if (mappingConfig === undefined) {
225
- throw new utils_1.Exception(`Invalid guard "${mapping}". Make sure the guard is defined inside the config/auth file`);
226
- }
227
- const provider = this.makeUserProviderInstance(mapping, mappingConfig.provider);
228
- return this.makeGuardInstance(mapping, mappingConfig, provider, ctx);
229
- }
230
- /**
231
- * Returns an instance of the testing
232
- */
233
- client(mapping) {
234
- const mappingConfig = this.config.guards[mapping];
235
- if (mappingConfig === undefined) {
236
- throw new utils_1.Exception(`Invalid guard "${mapping}". Make sure the guard is defined inside the config/auth file`);
237
- }
238
- const provider = this.makeUserProviderInstance(mapping, mappingConfig.provider);
239
- return this.makeClientInstance(mapping, mappingConfig, provider);
240
- }
241
- /**
242
- * Returns an instance of the auth class for the current request
243
- */
244
- getAuthForRequest(ctx) {
245
- return new Auth_1.Auth(this, ctx);
246
- }
247
- extend(type, name, callback) {
248
- if (type === 'provider') {
249
- this.extendedProviders.set(name, callback);
250
- return;
251
- }
252
- if (type === 'client') {
253
- this.extendedClients.set(name, callback);
254
- return;
255
- }
256
- if (type === 'guard') {
257
- this.extendedGuards.set(name, callback);
258
- return;
259
- }
260
- }
261
- }
262
- exports.AuthManager = AuthManager;
@@ -1,6 +0,0 @@
1
- import { AuthManagerContract } from '@ioc:Adonis/Addons/Auth';
2
- import { ContainerBindings } from '@ioc:Adonis/Core/Application';
3
- /**
4
- * Define test bindings
5
- */
6
- export declare function defineTestsBindings(ApiRequest: ContainerBindings['Japa/Preset/ApiRequest'], ApiClient: ContainerBindings['Japa/Preset/ApiClient'], AuthManager: AuthManagerContract): void;
@@ -1,69 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/auth
4
- *
5
- * (c) AdonisJS Auth
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.defineTestsBindings = void 0;
12
- /**
13
- * Define test bindings
14
- */
15
- function defineTestsBindings(ApiRequest, ApiClient, AuthManager) {
16
- /**
17
- * Set "sessionClient" on the api request
18
- */
19
- ApiRequest.getter('authManager', function () {
20
- return AuthManager;
21
- }, true);
22
- /**
23
- * Login user using the default guard
24
- */
25
- ApiRequest.macro('loginAs', function (user) {
26
- this['authData'] = {
27
- client: this.authManager.client(this.authManager.defaultGuard),
28
- args: [user],
29
- };
30
- return this;
31
- });
32
- /**
33
- * Login user using a custom guard
34
- */
35
- ApiRequest.macro('guard', function (mapping) {
36
- return {
37
- loginAs: (...args) => {
38
- this['authData'] = {
39
- client: this.authManager.client(mapping),
40
- args,
41
- };
42
- return this;
43
- },
44
- };
45
- });
46
- /**
47
- * Hook into the request and login the user
48
- */
49
- ApiClient.setup(async (request) => {
50
- const authData = request['authData'];
51
- if (!authData) {
52
- return;
53
- }
54
- const requestData = await authData.client.login(...authData.args);
55
- if (requestData.headers) {
56
- request.headers(requestData.headers);
57
- }
58
- if (requestData.session) {
59
- request.session(requestData.session);
60
- }
61
- if (requestData.cookies) {
62
- request.cookies(requestData.cookies);
63
- }
64
- return async () => {
65
- await authData.client.logout(...authData.args);
66
- };
67
- });
68
- }
69
- exports.defineTestsBindings = defineTestsBindings;
@@ -1,50 +0,0 @@
1
- import { OATGuardConfig, OATLoginOptions, OATClientContract, UserProviderContract, ClientRequestData } from '@ioc:Adonis/Addons/Auth';
2
- import { TokenProviderContract } from '@ioc:Adonis/Addons/Auth';
3
- /**
4
- * OAT client to login a user during tests using the
5
- * opaque tokens guard
6
- */
7
- export declare class OATClient implements OATClientContract<any> {
8
- name: string;
9
- config: OATGuardConfig<any>;
10
- private provider;
11
- tokenProvider: TokenProviderContract;
12
- constructor(name: string, config: OATGuardConfig<any>, provider: UserProviderContract<any>, tokenProvider: TokenProviderContract);
13
- /**
14
- * Token generated during the login call
15
- */
16
- private tokenId?;
17
- /**
18
- * Length of the raw token. The hash length will vary
19
- */
20
- private tokenLength;
21
- /**
22
- * Token type for the persistance store
23
- */
24
- private tokenType;
25
- /**
26
- * Returns the provider user instance from the regular user details. Raises
27
- * exception when id is missing
28
- */
29
- private getUserForLogin;
30
- /**
31
- * Converts value to a sha256 hash
32
- */
33
- private generateHash;
34
- /**
35
- * Converts expiry duration to an absolute date/time value
36
- */
37
- private getExpiresAtDate;
38
- /**
39
- * Generates a new token + hash for the persistance
40
- */
41
- private generateTokenForPersistance;
42
- /**
43
- * Returns the request data to mark user as logged in
44
- */
45
- login(user: any, options?: OATLoginOptions): Promise<ClientRequestData>;
46
- /**
47
- * Logout user
48
- */
49
- logout(): Promise<void>;
50
- }