@adonisjs/ally 4.1.4 → 5.0.0-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.
Files changed (76) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +25 -42
  3. package/build/configure.d.ts +5 -0
  4. package/build/configure.js +18 -0
  5. package/build/index.d.ts +11 -0
  6. package/build/index.js +19 -0
  7. package/build/providers/ally_provider.d.ts +9 -0
  8. package/build/providers/ally_provider.js +21 -0
  9. package/build/src/{AbstractDrivers/Oauth1/index.d.ts → abstract_drivers/oauth1.d.ts} +6 -14
  10. package/build/src/{AbstractDrivers/Oauth1/index.js → abstract_drivers/oauth1.js} +31 -29
  11. package/build/src/{AbstractDrivers/Oauth2/index.d.ts → abstract_drivers/oauth2.d.ts} +6 -10
  12. package/build/src/{AbstractDrivers/Oauth2/index.js → abstract_drivers/oauth2.js} +26 -25
  13. package/build/src/ally_manager.d.ts +14 -0
  14. package/build/src/ally_manager.js +40 -0
  15. package/build/src/bindings/http_context.d.ts +6 -0
  16. package/build/src/bindings/http_context.js +19 -0
  17. package/build/src/bindings/types.d.ts +12 -0
  18. package/build/{adonis-typings/context.js → src/bindings/types.js} +2 -1
  19. package/build/src/{Config/index.js → defaults/config.js} +12 -15
  20. package/build/src/define_config.d.ts +12 -0
  21. package/build/src/define_config.js +26 -0
  22. package/build/src/{Drivers/Discord/index.d.ts → drivers/discord.d.ts} +4 -4
  23. package/build/src/{Drivers/Discord/index.js → drivers/discord.js} +32 -35
  24. package/build/src/{Drivers/Facebook/index.d.ts → drivers/facebook.d.ts} +4 -4
  25. package/build/src/{Drivers/Facebook/index.js → drivers/facebook.js} +44 -47
  26. package/build/src/{Drivers/Github/index.d.ts → drivers/github.d.ts} +4 -4
  27. package/build/src/{Drivers/Github/index.js → drivers/github.js} +33 -36
  28. package/build/src/{Drivers/Google/index.d.ts → drivers/google.d.ts} +8 -4
  29. package/build/src/{Drivers/Google/index.js → drivers/google.js} +42 -35
  30. package/build/src/{Drivers/LinkedIn/index.d.ts → drivers/linked_in.d.ts} +4 -4
  31. package/build/src/{Drivers/LinkedIn/index.js → drivers/linked_in.js} +35 -38
  32. package/build/src/{Drivers/Spotify/index.d.ts → drivers/spotify.d.ts} +4 -4
  33. package/build/src/{Drivers/Spotify/index.js → drivers/spotify.js} +32 -35
  34. package/build/src/{Drivers/Twitter/index.d.ts → drivers/twitter.d.ts} +5 -5
  35. package/build/src/{Drivers/Twitter/index.js → drivers/twitter.js} +36 -38
  36. package/build/src/drivers_collection.d.ts +19 -0
  37. package/build/src/drivers_collection.js +50 -0
  38. package/build/src/exceptions.d.ts +2 -0
  39. package/build/src/exceptions.js +11 -0
  40. package/build/src/{RedirectRequest/index.d.ts → redirect_request.d.ts} +11 -9
  41. package/build/src/redirect_request.js +63 -0
  42. package/build/src/types.d.ts +383 -0
  43. package/build/{adonis-typings/ally.js → src/types.js} +2 -1
  44. package/build/stubs/config.stub +7 -0
  45. package/build/stubs/main.d.ts +1 -0
  46. package/build/{adonis-typings/container.js → stubs/main.js} +3 -1
  47. package/build/stubs/types.stub +12 -0
  48. package/package.json +116 -152
  49. package/build/adonis-typings/ally.d.ts +0 -418
  50. package/build/adonis-typings/container.d.ts +0 -6
  51. package/build/adonis-typings/context.d.ts +0 -6
  52. package/build/adonis-typings/index.d.ts +0 -3
  53. package/build/adonis-typings/index.js +0 -11
  54. package/build/instructions.js +0 -187
  55. package/build/instructions.md +0 -46
  56. package/build/providers/AllyProvider.d.ts +0 -19
  57. package/build/providers/AllyProvider.js +0 -40
  58. package/build/src/Ally/index.d.ts +0 -23
  59. package/build/src/Ally/index.js +0 -36
  60. package/build/src/AllyManager/index.d.ts +0 -70
  61. package/build/src/AllyManager/index.js +0 -146
  62. package/build/src/Exceptions/index.d.ts +0 -8
  63. package/build/src/Exceptions/index.js +0 -24
  64. package/build/src/RedirectRequest/index.js +0 -55
  65. package/build/standalone.d.ts +0 -7
  66. package/build/standalone.js +0 -22
  67. package/build/templates/config/ally.txt +0 -44
  68. package/build/templates/config/partials/discord.txt +0 -11
  69. package/build/templates/config/partials/facebook.txt +0 -11
  70. package/build/templates/config/partials/github.txt +0 -11
  71. package/build/templates/config/partials/google.txt +0 -11
  72. package/build/templates/config/partials/linkedin.txt +0 -11
  73. package/build/templates/config/partials/spotify.txt +0 -12
  74. package/build/templates/config/partials/twitter.txt +0 -11
  75. package/build/templates/contracts/ally.txt +0 -53
  76. /package/build/src/{Config/index.d.ts → defaults/config.d.ts} +0 -0
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # The MIT License
2
2
 
3
- Copyright 2022 Harminder Virk, contributors
3
+ Copyright (c) 2023 Harminder Virk
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
package/README.md CHANGED
@@ -1,53 +1,36 @@
1
- <div align="center">
2
- <img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px">
3
- </div>
1
+ # @adonisjs/ally
4
2
 
5
3
  <br />
6
4
 
7
- <div align="center">
8
- <h3>Social Authentication</h3>
9
- <p>Social authentication provider for AdonisJS. Supports <strong>Github</strong>, <strong>Google</strong>, <strong>Twitter</strong>, <strong>Facebook</strong>, <strong>Discord</strong>, <strong>Spotify</strong>, and <strong>LinkedIn</strong>.</p>
10
- </div>
5
+ [![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![snyk-image]][snyk-url]
11
6
 
12
- <br />
7
+ ## Introduction
8
+ Social authentication provider for AdonisJS. Supports *Github*, Google, Twitter, Facebook, Discord, Spotify, and LinkedIn.
13
9
 
14
- <div align="center">
15
-
16
- [![gh-workflow-image]][gh-workflow-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url] [![synk-image]][synk-url]
17
-
18
- </div>
19
-
20
- <div align="center">
21
- <h3>
22
- <a href="https://adonisjs.com/">
23
- Website
24
- </a>
25
- <span> | </span>
26
- <a href="https://docs.adonisjs.com/guides/auth/social">
27
- Guides
28
- </a>
29
- <span> | </span>
30
- <a href="CONTRIBUTING.md">
31
- Contributing
32
- </a>
33
- </h3>
34
- </div>
35
-
36
- <div align="center">
37
- <sub>Built with ❤︎ by <a href="https://twitter.com/AmanVirk1">Harminder Virk</a>
38
- </div>
39
-
40
- [gh-workflow-image]: https://img.shields.io/github/workflow/status/adonisjs/ally/test?style=for-the-badge
41
- [gh-workflow-url]: https://github.com/adonisjs/ally/actions/workflows/test.yml "Github action"
10
+ ## Official Documentation
11
+ The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/auth/social)
42
12
 
43
- [typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
44
- [typescript-url]: "typescript"
13
+ ## Contributing
14
+ One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
15
+
16
+ We encourage you to read the [contribution guide](https://github.com/adonisjs/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.
17
+
18
+ ## Code of Conduct
19
+ In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/adonisjs/.github/blob/main/docs/CODE_OF_CONDUCT.md).
20
+
21
+ ## License
22
+ AdonisJS ally is open-sourced software licensed under the [MIT license](LICENSE.md).
23
+
24
+ [gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/ally/test.yml?style=for-the-badge
25
+ [gh-workflow-url]: https://github.com/adonisjs/ally/actions/workflows/test.yml "Github action"
45
26
 
46
27
  [npm-image]: https://img.shields.io/npm/v/@adonisjs/ally/latest.svg?style=for-the-badge&logo=npm
47
28
  [npm-url]: https://www.npmjs.com/package/@adonisjs/ally/v/latest "npm"
48
29
 
49
- [license-image]: https://img.shields.io/npm/l/@adonisjs/ally?color=blueviolet&style=for-the-badge
50
- [license-url]: LICENSE.md "license"
30
+ [typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
31
+
32
+ [license-url]: LICENSE.md
33
+ [license-image]: https://img.shields.io/github/license/adonisjs/ally?style=for-the-badge
51
34
 
52
- [synk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/ally?label=Synk%20Vulnerabilities&style=for-the-badge
53
- [synk-url]: https://snyk.io/test/github/adonisjs/ally?targetFile=package.json "synk"
35
+ [snyk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/ally?label=Snyk%20Vulnerabilities&style=for-the-badge
36
+ [snyk-url]: https://snyk.io/test/github/adonisjs/ally?targetFile=package.json "snyk"
@@ -0,0 +1,5 @@
1
+ import type Configure from '@adonisjs/core/commands/configure';
2
+ /**
3
+ * Configures the package
4
+ */
5
+ export declare function configure(command: Configure): Promise<void>;
@@ -0,0 +1,18 @@
1
+ /*
2
+ * @adonisjs/ally
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ /**
10
+ * Configures the package
11
+ */
12
+ export async function configure(command) {
13
+ await command.publishStub('config.stub');
14
+ await command.publishStub('types.stub');
15
+ await command.updateRcFile((rcFile) => {
16
+ rcFile.addProvider('@adonisjs/ally/ally_provider');
17
+ });
18
+ }
@@ -0,0 +1,11 @@
1
+ import './src/bindings/types.js';
2
+ export { HttpClient as ApiRequest } from '@poppinss/oauth-client';
3
+ export * as errors from './src/exceptions.js';
4
+ export { AllyManager } from './src/ally_manager.js';
5
+ export { defineConfig } from './src/define_config.js';
6
+ export { RedirectRequest } from './src/redirect_request.js';
7
+ export { Oauth1Driver } from './src/abstract_drivers/oauth1.js';
8
+ export { Oauth2Driver } from './src/abstract_drivers/oauth2.js';
9
+ export { default as driversList } from './src/drivers_collection.js';
10
+ export { stubsRoot } from './stubs/main.js';
11
+ export { configure } from './configure.js';
package/build/index.js ADDED
@@ -0,0 +1,19 @@
1
+ /*
2
+ * @adonisjs/ally
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import './src/bindings/types.js';
10
+ export { HttpClient as ApiRequest } from '@poppinss/oauth-client';
11
+ export * as errors from './src/exceptions.js';
12
+ export { AllyManager } from './src/ally_manager.js';
13
+ export { defineConfig } from './src/define_config.js';
14
+ export { RedirectRequest } from './src/redirect_request.js';
15
+ export { Oauth1Driver } from './src/abstract_drivers/oauth1.js';
16
+ export { Oauth2Driver } from './src/abstract_drivers/oauth2.js';
17
+ export { default as driversList } from './src/drivers_collection.js';
18
+ export { stubsRoot } from './stubs/main.js';
19
+ export { configure } from './configure.js';
@@ -0,0 +1,9 @@
1
+ import { ApplicationService } from '@adonisjs/core/types';
2
+ /**
3
+ * AllyProvider extends the HTTP context with the "ally" property
4
+ */
5
+ export default class AllyProvider {
6
+ protected app: ApplicationService;
7
+ constructor(app: ApplicationService);
8
+ boot(): Promise<void>;
9
+ }
@@ -0,0 +1,21 @@
1
+ /*
2
+ * @adonisjs/ally
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { extendHttpContext } from '../src/bindings/http_context.js';
10
+ /**
11
+ * AllyProvider extends the HTTP context with the "ally" property
12
+ */
13
+ export default class AllyProvider {
14
+ app;
15
+ constructor(app) {
16
+ this.app = app;
17
+ }
18
+ async boot() {
19
+ extendHttpContext(this.app.config.get('ally'));
20
+ }
21
+ }
@@ -1,13 +1,13 @@
1
- /// <reference path="../../../adonis-typings/index.d.ts" />
2
1
  import { Oauth1Client } from '@poppinss/oauth-client';
3
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
4
- import { AllyUserContract, Oauth1AccessToken, Oauth1DriverConfig, ApiRequestContract, AllyDriverContract, RedirectRequestContract } from '@ioc:Adonis/Addons/Ally';
5
- import { RedirectRequest } from '../../RedirectRequest';
2
+ import type { HttpContext } from '@adonisjs/core/http';
3
+ import { AllyUserContract, Oauth1AccessToken, Oauth1DriverConfig, ApiRequestContract, AllyDriverContract, RedirectRequestContract } from '../types.js';
4
+ import { RedirectRequest } from '../redirect_request.js';
6
5
  /**
7
6
  * Abstract implementation for an Oauth1 driver
8
7
  */
9
8
  export declare abstract class Oauth1Driver<Token extends Oauth1AccessToken, Scopes extends string> extends Oauth1Client<Token> implements AllyDriverContract<Token, Scopes> {
10
- protected ctx: HttpContextContract;
9
+ #private;
10
+ protected ctx: HttpContext;
11
11
  config: Oauth1DriverConfig;
12
12
  /**
13
13
  * The cookie name for storing the "oauth_token". Must be unique for your
@@ -84,7 +84,7 @@ export declare abstract class Oauth1Driver<Token extends Oauth1AccessToken, Scop
84
84
  * The cookie name for storing the secret
85
85
  */
86
86
  protected get oauthSecretCookieName(): string;
87
- constructor(ctx: HttpContextContract, config: Oauth1DriverConfig);
87
+ constructor(ctx: HttpContext, config: Oauth1DriverConfig);
88
88
  /**
89
89
  * The Oauth1Client will use the instance returned from this method to
90
90
  * build the redirect url
@@ -102,14 +102,6 @@ export declare abstract class Oauth1Driver<Token extends Oauth1AccessToken, Scop
102
102
  * This child class must call this method inside the constructor.
103
103
  */
104
104
  protected loadState(): void;
105
- /**
106
- * Persists the token (aka state) inside the cookie
107
- */
108
- private persistToken;
109
- /**
110
- * Persists the secret inside the cookie
111
- */
112
- private persistSecret;
113
105
  /**
114
106
  * Perform stateless authentication. Only applicable for Oauth1 client
115
107
  */
@@ -1,44 +1,47 @@
1
- "use strict";
2
1
  /*
3
2
  * @adonisjs/ally
4
3
  *
5
- * (c) Harminder Virk <virk@adonisjs.com>
4
+ * (c) AdonisJS
6
5
  *
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.Oauth1Driver = void 0;
12
- /// <reference path="../../../adonis-typings/index.ts" />
13
- const utils_1 = require("@poppinss/utils");
14
- const oauth_client_1 = require("@poppinss/oauth-client");
15
- const Exceptions_1 = require("../../Exceptions");
16
- const RedirectRequest_1 = require("../../RedirectRequest");
9
+ import { Exception } from '@poppinss/utils';
10
+ import { Oauth1Client } from '@poppinss/oauth-client';
11
+ import * as errors from '../exceptions.js';
12
+ import { RedirectRequest } from '../redirect_request.js';
17
13
  /**
18
14
  * Abstract implementation for an Oauth1 driver
19
15
  */
20
- class Oauth1Driver extends oauth_client_1.Oauth1Client {
21
- constructor(ctx, config) {
22
- super(config);
23
- this.ctx = ctx;
24
- this.config = config;
25
- /**
26
- * Oauth client version
27
- */
28
- this.version = 'oauth1';
29
- }
16
+ export class Oauth1Driver extends Oauth1Client {
17
+ ctx;
18
+ config;
19
+ /**
20
+ * Oauth client version
21
+ */
22
+ version = 'oauth1';
23
+ /**
24
+ * The value of "oauth_token" and "oauth_secret" from the cookies
25
+ */
26
+ oauthTokenCookieValue;
27
+ oauthSecretCookieValue;
30
28
  /**
31
29
  * The cookie name for storing the secret
32
30
  */
33
31
  get oauthSecretCookieName() {
34
32
  return `${this.oauthTokenCookieName}_secret`;
35
33
  }
34
+ constructor(ctx, config) {
35
+ super(config);
36
+ this.ctx = ctx;
37
+ this.config = config;
38
+ }
36
39
  /**
37
40
  * The Oauth1Client will use the instance returned from this method to
38
41
  * build the redirect url
39
42
  */
40
43
  urlBuilder(url) {
41
- return new RedirectRequest_1.RedirectRequest(url, this.scopeParamName, this.scopesSeparator);
44
+ return new RedirectRequest(url, this.scopeParamName, this.scopesSeparator);
42
45
  }
43
46
  /**
44
47
  * Loads the value of state from the cookie and removes it right
@@ -66,7 +69,7 @@ class Oauth1Driver extends oauth_client_1.Oauth1Client {
66
69
  /**
67
70
  * Persists the token (aka state) inside the cookie
68
71
  */
69
- persistToken(token) {
72
+ #persistToken(token) {
70
73
  this.ctx.response.encryptedCookie(this.oauthTokenCookieName, token, {
71
74
  sameSite: false,
72
75
  httpOnly: true,
@@ -75,7 +78,7 @@ class Oauth1Driver extends oauth_client_1.Oauth1Client {
75
78
  /**
76
79
  * Persists the secret inside the cookie
77
80
  */
78
- persistSecret(secret) {
81
+ #persistSecret(secret) {
79
82
  this.ctx.response.encryptedCookie(this.oauthSecretCookieName, secret, {
80
83
  sameSite: false,
81
84
  httpOnly: true,
@@ -85,7 +88,7 @@ class Oauth1Driver extends oauth_client_1.Oauth1Client {
85
88
  * Perform stateless authentication. Only applicable for Oauth1 client
86
89
  */
87
90
  stateless() {
88
- throw new utils_1.Exception('OAuth1 does not support stateless authorization');
91
+ throw new Exception('OAuth1 does not support stateless authorization');
89
92
  }
90
93
  /**
91
94
  * Returns the redirect URL for the request.
@@ -102,8 +105,8 @@ class Oauth1Driver extends oauth_client_1.Oauth1Client {
102
105
  * Storing token and secret inside cookies. We need them
103
106
  * later
104
107
  */
105
- this.persistToken(token);
106
- this.persistSecret(secret);
108
+ this.#persistToken(token);
109
+ this.#persistSecret(secret);
107
110
  const url = await this.redirectUrl((request) => {
108
111
  request.param(this.oauthTokenParamName, token);
109
112
  if (typeof callback === 'function') {
@@ -157,14 +160,14 @@ class Oauth1Driver extends oauth_client_1.Oauth1Client {
157
160
  * We expect the user to handle errors before calling this method
158
161
  */
159
162
  if (this.hasError()) {
160
- throw Exceptions_1.OauthException.missingAuthorizationCode(this.oauthTokenVerifierName);
163
+ throw new errors.E_OAUTH_MISSING_CODE([this.oauthTokenVerifierName]);
161
164
  }
162
165
  /**
163
166
  * We expect the user to properly handle the state mis-match use case before
164
167
  * calling this method
165
168
  */
166
169
  if (this.stateMisMatch()) {
167
- throw Exceptions_1.OauthException.stateMisMatch();
170
+ throw new errors.E_OAUTH_STATE_MISMATCH();
168
171
  }
169
172
  /**
170
173
  * Get access token by providing the authorization code
@@ -180,7 +183,6 @@ class Oauth1Driver extends oauth_client_1.Oauth1Client {
180
183
  * Not applicable with Oauth1
181
184
  */
182
185
  async userFromToken() {
183
- throw new utils_1.Exception('"userFromToken" is not available with Oauth1. Use "userFromTokenAndSecret" instead');
186
+ throw new Exception('"userFromToken" is not available with Oauth1. Use "userFromTokenAndSecret" instead');
184
187
  }
185
188
  }
186
- exports.Oauth1Driver = Oauth1Driver;
@@ -1,13 +1,13 @@
1
- /// <reference path="../../../adonis-typings/index.d.ts" />
2
1
  import { Oauth2Client } from '@poppinss/oauth-client';
3
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
4
- import { AllyUserContract, Oauth2AccessToken, Oauth2DriverConfig, ApiRequestContract, AllyDriverContract, RedirectRequestContract } from '@ioc:Adonis/Addons/Ally';
5
- import { RedirectRequest } from '../../RedirectRequest';
2
+ import type { HttpContext } from '@adonisjs/core/http';
3
+ import { AllyUserContract, Oauth2AccessToken, Oauth2DriverConfig, ApiRequestContract, AllyDriverContract, RedirectRequestContract } from '../types.js';
4
+ import { RedirectRequest } from '../redirect_request.js';
6
5
  /**
7
6
  * Abstract implementation for an Oauth2 driver
8
7
  */
9
8
  export declare abstract class Oauth2Driver<Token extends Oauth2AccessToken, Scopes extends string> extends Oauth2Client<Token> implements AllyDriverContract<Token, Scopes> {
10
- protected ctx: HttpContextContract;
9
+ #private;
10
+ protected ctx: HttpContext;
11
11
  config: Oauth2DriverConfig;
12
12
  /**
13
13
  * Is the authorization process stateless?
@@ -81,7 +81,7 @@ export declare abstract class Oauth2Driver<Token extends Oauth2AccessToken, Scop
81
81
  * The value of state read from the cookies.
82
82
  */
83
83
  protected stateCookieValue?: string;
84
- constructor(ctx: HttpContextContract, config: Oauth2DriverConfig);
84
+ constructor(ctx: HttpContext, config: Oauth2DriverConfig);
85
85
  /**
86
86
  * The Oauth2Client will use the instance returned from this method to
87
87
  * build the redirect url
@@ -99,10 +99,6 @@ export declare abstract class Oauth2Driver<Token extends Oauth2AccessToken, Scop
99
99
  * This child class must call this method inside the constructor.
100
100
  */
101
101
  protected loadState(): void;
102
- /**
103
- * Persists the state inside the cookie
104
- */
105
- private persistState;
106
102
  /**
107
103
  * Perform stateless authentication. Only applicable for Oauth2 client
108
104
  */
@@ -1,42 +1,44 @@
1
- "use strict";
2
1
  /*
3
2
  * @adonisjs/ally
4
3
  *
5
- * (c) Harminder Virk <virk@adonisjs.com>
4
+ * (c) AdonisJS
6
5
  *
7
6
  * For the full copyright and license information, please view the LICENSE
8
7
  * file that was distributed with this source code.
9
8
  */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.Oauth2Driver = void 0;
12
- /// <reference path="../../../adonis-typings/index.ts" />
13
- const utils_1 = require("@poppinss/utils");
14
- const oauth_client_1 = require("@poppinss/oauth-client");
15
- const Exceptions_1 = require("../../Exceptions");
16
- const RedirectRequest_1 = require("../../RedirectRequest");
9
+ import { Exception } from '@poppinss/utils';
10
+ import { Oauth2Client } from '@poppinss/oauth-client';
11
+ import * as errors from '../exceptions.js';
12
+ import { RedirectRequest } from '../redirect_request.js';
17
13
  /**
18
14
  * Abstract implementation for an Oauth2 driver
19
15
  */
20
- class Oauth2Driver extends oauth_client_1.Oauth2Client {
16
+ export class Oauth2Driver extends Oauth2Client {
17
+ ctx;
18
+ config;
19
+ /**
20
+ * Is the authorization process stateless?
21
+ */
22
+ isStateless = false;
23
+ /**
24
+ * Oauth client version
25
+ */
26
+ version = 'oauth2';
27
+ /**
28
+ * The value of state read from the cookies.
29
+ */
30
+ stateCookieValue;
21
31
  constructor(ctx, config) {
22
32
  super(config);
23
33
  this.ctx = ctx;
24
34
  this.config = config;
25
- /**
26
- * Is the authorization process stateless?
27
- */
28
- this.isStateless = false;
29
- /**
30
- * Oauth client version
31
- */
32
- this.version = 'oauth2';
33
35
  }
34
36
  /**
35
37
  * The Oauth2Client will use the instance returned from this method to
36
38
  * build the redirect url
37
39
  */
38
40
  urlBuilder(url) {
39
- return new RedirectRequest_1.RedirectRequest(url, this.scopeParamName, this.scopesSeparator);
41
+ return new RedirectRequest(url, this.scopeParamName, this.scopesSeparator);
40
42
  }
41
43
  /**
42
44
  * Loads the value of state from the cookie and removes it right
@@ -59,7 +61,7 @@ class Oauth2Driver extends oauth_client_1.Oauth2Client {
59
61
  /**
60
62
  * Persists the state inside the cookie
61
63
  */
62
- persistState() {
64
+ #persistState() {
63
65
  if (this.isStateless) {
64
66
  return;
65
67
  }
@@ -89,7 +91,7 @@ class Oauth2Driver extends oauth_client_1.Oauth2Client {
89
91
  */
90
92
  async redirect(callback) {
91
93
  const url = await this.redirectUrl((request) => {
92
- const state = this.persistState();
94
+ const state = this.#persistState();
93
95
  state && request.param(this.stateParamName, state);
94
96
  if (typeof callback === 'function') {
95
97
  callback(request);
@@ -145,14 +147,14 @@ class Oauth2Driver extends oauth_client_1.Oauth2Client {
145
147
  * We expect the user to handle errors before calling this method
146
148
  */
147
149
  if (this.hasError()) {
148
- throw Exceptions_1.OauthException.missingAuthorizationCode(this.codeParamName);
150
+ throw new errors.E_OAUTH_MISSING_CODE([this.codeParamName]);
149
151
  }
150
152
  /**
151
153
  * We expect the user to properly handle the state mis-match use case before
152
154
  * calling this method
153
155
  */
154
156
  if (this.stateMisMatch()) {
155
- throw Exceptions_1.OauthException.stateMisMatch();
157
+ throw new errors.E_OAUTH_STATE_MISMATCH();
156
158
  }
157
159
  /**
158
160
  * Get access token by providing the authorization code
@@ -168,7 +170,6 @@ class Oauth2Driver extends oauth_client_1.Oauth2Client {
168
170
  * Not applicable with Oauth2
169
171
  */
170
172
  async userFromTokenAndSecret() {
171
- throw new utils_1.Exception('"userFromTokenAndSecret" is not applicable with Oauth2. Use "userFromToken" instead');
173
+ throw new Exception('"userFromTokenAndSecret" is not applicable with Oauth2. Use "userFromToken" instead');
172
174
  }
173
175
  }
174
- exports.Oauth2Driver = Oauth2Driver;
@@ -0,0 +1,14 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import type { AllyManagerDriverFactory } from './types.js';
3
+ /**
4
+ * AllyManager is used to create instances of a social drivers during an
5
+ * HTTP request. The drivers are cached during the lifecycle of a request.
6
+ */
7
+ export declare class AllyManager<KnownSocialProviders extends Record<string, AllyManagerDriverFactory>> {
8
+ #private;
9
+ constructor(config: KnownSocialProviders, ctx: HttpContext);
10
+ /**
11
+ * Returns the driver instance of a social provider
12
+ */
13
+ use<SocialProvider extends keyof KnownSocialProviders>(provider: SocialProvider): ReturnType<KnownSocialProviders[SocialProvider]>;
14
+ }
@@ -0,0 +1,40 @@
1
+ /*
2
+ * @adonisjs/ally
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { RuntimeException } from '@poppinss/utils';
10
+ /**
11
+ * AllyManager is used to create instances of a social drivers during an
12
+ * HTTP request. The drivers are cached during the lifecycle of a request.
13
+ */
14
+ export class AllyManager {
15
+ /**
16
+ * Config with the list of social providers
17
+ */
18
+ #config;
19
+ #ctx;
20
+ #driversCache = new Map();
21
+ constructor(config, ctx) {
22
+ this.#ctx = ctx;
23
+ this.#config = config;
24
+ }
25
+ /**
26
+ * Returns the driver instance of a social provider
27
+ */
28
+ use(provider) {
29
+ if (this.#driversCache.has(provider)) {
30
+ return this.#driversCache.get(provider);
31
+ }
32
+ const driver = this.#config[provider];
33
+ if (!driver) {
34
+ throw new RuntimeException(`Unknown ally provider "${String(provider)}". Make sure it is registered inside the config/ally.ts file`);
35
+ }
36
+ const driverInstance = driver(this.#ctx);
37
+ this.#driversCache.set(provider, driverInstance);
38
+ return driverInstance;
39
+ }
40
+ }
@@ -0,0 +1,6 @@
1
+ import './types.js';
2
+ import { AllyManagerDriverFactory, SocialProviders } from '../types.js';
3
+ /**
4
+ * Extends HttpContext class with the ally getter
5
+ */
6
+ export declare function extendHttpContext(config: SocialProviders extends Record<string, AllyManagerDriverFactory> ? SocialProviders : never): void;
@@ -0,0 +1,19 @@
1
+ /*
2
+ * @adonisjs/ally
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { HttpContext } from '@adonisjs/core/http';
10
+ import './types.js';
11
+ import { AllyManager } from '../ally_manager.js';
12
+ /**
13
+ * Extends HttpContext class with the ally getter
14
+ */
15
+ export function extendHttpContext(config) {
16
+ HttpContext.getter('ally', function () {
17
+ return new AllyManager(config, this);
18
+ }, true);
19
+ }
@@ -0,0 +1,12 @@
1
+ import type { AllyManager } from '../ally_manager.js';
2
+ import type { AllyManagerDriverFactory, SocialProviders } from '../types.js';
3
+ /**
4
+ * In order for types to get picked up, this module must get
5
+ * imported by TypeScript. Therefore, we export this module
6
+ * from the package entrypoint
7
+ */
8
+ declare module '@adonisjs/core/http' {
9
+ interface HttpContext {
10
+ ally: AllyManager<SocialProviders extends Record<string, AllyManagerDriverFactory> ? SocialProviders : never>;
11
+ }
12
+ }
@@ -1,8 +1,9 @@
1
1
  /*
2
2
  * @adonisjs/ally
3
3
  *
4
- * (c) Harminder Virk <virk@adonisjs.com>
4
+ * (c) AdonisJS
5
5
  *
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
+ export {};