@adonisjs/ally 5.1.0 → 6.0.0-next.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.
- package/build/{chunk-N72DEJC2.js → chunk-KSJ4CFTC.js} +45 -8
- package/build/{chunk-VHORNQLN.js → chunk-KWRXS6EG.js} +65 -28
- package/build/{chunk-PZ5AY32C.js → chunk-MLKGABMK.js} +0 -1
- package/build/{chunk-NZT2DLWM.js → chunk-SZ4YJCVU.js} +15 -3
- package/build/chunk-WM3V3APX.js +210 -0
- package/build/index.d.ts +8 -8
- package/build/index.js +6 -8
- package/build/providers/ally_provider.d.ts +1 -1
- package/build/providers/ally_provider.js +3 -4
- package/build/src/abstract_drivers/oauth1.d.ts +115 -49
- package/build/src/abstract_drivers/oauth2.d.ts +121 -50
- package/build/src/ally_manager.d.ts +31 -4
- package/build/src/debug.d.ts +9 -0
- package/build/src/define_config.d.ts +45 -11
- package/build/src/drivers/discord.d.ts +76 -11
- package/build/src/drivers/discord.js +47 -12
- package/build/src/drivers/facebook.d.ts +73 -10
- package/build/src/drivers/facebook.js +44 -11
- package/build/src/drivers/github.d.ts +85 -13
- package/build/src/drivers/github.js +56 -14
- package/build/src/drivers/google.d.ts +80 -11
- package/build/src/drivers/google.js +50 -12
- package/build/src/drivers/linked_in.d.ts +76 -10
- package/build/src/drivers/linked_in.js +47 -12
- package/build/src/drivers/linked_in_openid_connect.d.ts +72 -9
- package/build/src/drivers/linked_in_openid_connect.js +42 -10
- package/build/src/drivers/spotify.d.ts +73 -10
- package/build/src/drivers/spotify.js +44 -11
- package/build/src/drivers/twitter.d.ts +65 -8
- package/build/src/drivers/twitter.js +37 -9
- package/build/src/errors.d.ts +10 -2
- package/build/src/redirect_request.d.ts +47 -7
- package/build/src/types.d.ts +1 -1
- package/build/src/types.js +0 -1
- package/package.json +57 -68
- package/build/chunk-GWAQFMNS.js +0 -164
- package/build/chunk-GWAQFMNS.js.map +0 -1
- package/build/chunk-N72DEJC2.js.map +0 -1
- package/build/chunk-NZT2DLWM.js.map +0 -1
- package/build/chunk-PZ5AY32C.js.map +0 -1
- package/build/chunk-VHORNQLN.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/providers/ally_provider.js.map +0 -1
- package/build/src/drivers/discord.js.map +0 -1
- package/build/src/drivers/facebook.js.map +0 -1
- package/build/src/drivers/github.js.map +0 -1
- package/build/src/drivers/google.js.map +0 -1
- package/build/src/drivers/linked_in.js.map +0 -1
- package/build/src/drivers/linked_in_openid_connect.js.map +0 -1
- package/build/src/drivers/spotify.js.map +0 -1
- package/build/src/drivers/twitter.js.map +0 -1
- package/build/src/types.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__export
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-MLKGABMK.js";
|
|
4
4
|
|
|
5
5
|
// src/errors.ts
|
|
6
6
|
var errors_exports = {};
|
|
@@ -8,7 +8,7 @@ __export(errors_exports, {
|
|
|
8
8
|
E_OAUTH_MISSING_CODE: () => E_OAUTH_MISSING_CODE,
|
|
9
9
|
E_OAUTH_STATE_MISMATCH: () => E_OAUTH_STATE_MISMATCH
|
|
10
10
|
});
|
|
11
|
-
import { createError } from "@
|
|
11
|
+
import { createError } from "@adonisjs/core/exceptions";
|
|
12
12
|
var E_OAUTH_MISSING_CODE = createError(
|
|
13
13
|
'Cannot request access token. Redirect request is missing the "%s" param',
|
|
14
14
|
"E_OAUTH_MISSING_CODE",
|
|
@@ -26,21 +26,44 @@ var RedirectRequest = class extends UrlBuilder {
|
|
|
26
26
|
#scopesTransformer;
|
|
27
27
|
#scopeParamName;
|
|
28
28
|
#scopeSeparator;
|
|
29
|
+
/**
|
|
30
|
+
* @param baseUrl - The authorization URL for the OAuth provider
|
|
31
|
+
* @param scopeParamName - The query parameter name for scopes (e.g., 'scope')
|
|
32
|
+
* @param scopeSeparator - The character used to separate multiple scopes (e.g., ' ' or ',')
|
|
33
|
+
*/
|
|
29
34
|
constructor(baseUrl, scopeParamName, scopeSeparator) {
|
|
30
35
|
super(baseUrl);
|
|
31
36
|
this.#scopeParamName = scopeParamName;
|
|
32
37
|
this.#scopeSeparator = scopeSeparator;
|
|
33
38
|
}
|
|
34
39
|
/**
|
|
35
|
-
* Register a custom function to transform scopes
|
|
36
|
-
* to
|
|
40
|
+
* Register a custom function to transform scopes before they are
|
|
41
|
+
* added to the authorization URL. This is useful for providers that
|
|
42
|
+
* require scope prefixes or transformations.
|
|
43
|
+
*
|
|
44
|
+
* @param callback - Function that transforms the scopes array
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* request.transformScopes((scopes) => {
|
|
49
|
+
* return scopes.map(scope => `https://provider.com/auth/${scope}`)
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
37
52
|
*/
|
|
38
53
|
transformScopes(callback) {
|
|
39
54
|
this.#scopesTransformer = callback;
|
|
40
55
|
return this;
|
|
41
56
|
}
|
|
42
57
|
/**
|
|
43
|
-
* Define
|
|
58
|
+
* Define the scopes to request during authorization. This replaces
|
|
59
|
+
* any previously set scopes.
|
|
60
|
+
*
|
|
61
|
+
* @param scopes - Array of scope strings to request
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* request.scopes(['user:email', 'read:org'])
|
|
66
|
+
* ```
|
|
44
67
|
*/
|
|
45
68
|
scopes(scopes) {
|
|
46
69
|
if (typeof this.#scopesTransformer === "function") {
|
|
@@ -50,7 +73,17 @@ var RedirectRequest = class extends UrlBuilder {
|
|
|
50
73
|
return this;
|
|
51
74
|
}
|
|
52
75
|
/**
|
|
53
|
-
* Merge
|
|
76
|
+
* Merge additional scopes with any existing scopes. This is useful
|
|
77
|
+
* for adding scopes without replacing the default ones.
|
|
78
|
+
*
|
|
79
|
+
* @param scopes - Array of scope strings to merge
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* request
|
|
84
|
+
* .scopes(['user:email'])
|
|
85
|
+
* .mergeScopes(['read:org'])
|
|
86
|
+
* ```
|
|
54
87
|
*/
|
|
55
88
|
mergeScopes(scopes) {
|
|
56
89
|
if (typeof this.#scopesTransformer === "function") {
|
|
@@ -66,7 +99,12 @@ var RedirectRequest = class extends UrlBuilder {
|
|
|
66
99
|
return this;
|
|
67
100
|
}
|
|
68
101
|
/**
|
|
69
|
-
* Clear existing scopes
|
|
102
|
+
* Clear all existing scopes from the authorization request.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* request.clearScopes().scopes(['user'])
|
|
107
|
+
* ```
|
|
70
108
|
*/
|
|
71
109
|
clearScopes() {
|
|
72
110
|
this.clearParam(this.#scopeParamName);
|
|
@@ -80,4 +118,3 @@ export {
|
|
|
80
118
|
errors_exports,
|
|
81
119
|
RedirectRequest
|
|
82
120
|
};
|
|
83
|
-
//# sourceMappingURL=chunk-N72DEJC2.js.map
|
|
@@ -2,49 +2,58 @@ import {
|
|
|
2
2
|
E_OAUTH_MISSING_CODE,
|
|
3
3
|
E_OAUTH_STATE_MISMATCH,
|
|
4
4
|
RedirectRequest
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-KSJ4CFTC.js";
|
|
6
6
|
|
|
7
7
|
// src/abstract_drivers/oauth1.ts
|
|
8
|
-
import { Exception } from "@
|
|
8
|
+
import { Exception } from "@adonisjs/core/exceptions";
|
|
9
9
|
import { Oauth1Client } from "@poppinss/oauth-client/oauth1";
|
|
10
10
|
var Oauth1Driver = class extends Oauth1Client {
|
|
11
|
+
/**
|
|
12
|
+
* @param ctx - The current HTTP context
|
|
13
|
+
* @param config - OAuth1 driver configuration
|
|
14
|
+
*/
|
|
11
15
|
constructor(ctx, config) {
|
|
12
16
|
super(config);
|
|
13
17
|
this.ctx = ctx;
|
|
14
18
|
this.config = config;
|
|
15
19
|
}
|
|
16
20
|
/**
|
|
17
|
-
*
|
|
21
|
+
* OAuth protocol version identifier
|
|
18
22
|
*/
|
|
19
23
|
version = "oauth1";
|
|
20
24
|
/**
|
|
21
|
-
*
|
|
25
|
+
* Cached OAuth token and secret values read from cookies
|
|
22
26
|
*/
|
|
23
27
|
oauthTokenCookieValue;
|
|
24
28
|
oauthSecretCookieValue;
|
|
25
29
|
/**
|
|
26
|
-
* The cookie name for storing the secret
|
|
30
|
+
* The cookie name for storing the OAuth token secret.
|
|
31
|
+
* Automatically derived from the token cookie name.
|
|
27
32
|
*/
|
|
28
33
|
get oauthSecretCookieName() {
|
|
29
34
|
return `${this.oauthTokenCookieName}_secret`;
|
|
30
35
|
}
|
|
31
36
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
37
|
+
* Creates a URL builder instance for constructing authorization URLs
|
|
38
|
+
* with scope support.
|
|
39
|
+
*
|
|
40
|
+
* @param url - The base authorization URL
|
|
34
41
|
*/
|
|
35
42
|
urlBuilder(url) {
|
|
36
43
|
return new RedirectRequest(url, this.scopeParamName, this.scopesSeparator);
|
|
37
44
|
}
|
|
38
45
|
/**
|
|
39
|
-
* Loads the
|
|
40
|
-
*
|
|
41
|
-
*
|
|
46
|
+
* Loads the OAuth token and secret from encrypted cookies and immediately
|
|
47
|
+
* clears the cookies. This must be called by child classes in their
|
|
48
|
+
* constructor to enable token verification.
|
|
42
49
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* constructor(ctx: HttpContext, config: DriverConfig) {
|
|
53
|
+
* super(ctx, config)
|
|
54
|
+
* this.loadState()
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
48
57
|
*/
|
|
49
58
|
loadState() {
|
|
50
59
|
this.oauthTokenCookieValue = this.ctx.request.encryptedCookie(this.oauthTokenCookieName);
|
|
@@ -53,7 +62,7 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
53
62
|
this.ctx.response.clearCookie(this.oauthSecretCookieName);
|
|
54
63
|
}
|
|
55
64
|
/**
|
|
56
|
-
*
|
|
65
|
+
* Stores the OAuth token in an encrypted cookie for later use
|
|
57
66
|
*/
|
|
58
67
|
#persistToken(token) {
|
|
59
68
|
this.ctx.response.encryptedCookie(this.oauthTokenCookieName, token, {
|
|
@@ -62,7 +71,7 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
62
71
|
});
|
|
63
72
|
}
|
|
64
73
|
/**
|
|
65
|
-
*
|
|
74
|
+
* Stores the OAuth token secret in an encrypted cookie for later use
|
|
66
75
|
*/
|
|
67
76
|
#persistSecret(secret) {
|
|
68
77
|
this.ctx.response.encryptedCookie(this.oauthSecretCookieName, secret, {
|
|
@@ -71,19 +80,37 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
71
80
|
});
|
|
72
81
|
}
|
|
73
82
|
/**
|
|
74
|
-
*
|
|
83
|
+
* OAuth1 does not support stateless authentication due to the
|
|
84
|
+
* three-legged authentication flow requiring token persistence.
|
|
75
85
|
*/
|
|
76
86
|
stateless() {
|
|
77
87
|
throw new Exception("OAuth1 does not support stateless authorization");
|
|
78
88
|
}
|
|
79
89
|
/**
|
|
80
|
-
*
|
|
90
|
+
* Get the authorization redirect URL without performing the redirect.
|
|
91
|
+
* Useful when you need to manually handle the redirect or use the URL
|
|
92
|
+
* in a different context.
|
|
93
|
+
*
|
|
94
|
+
* @param callback - Optional callback to customize the redirect request
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const url = await ally.use('twitter').redirectUrl()
|
|
99
|
+
* ```
|
|
81
100
|
*/
|
|
82
101
|
async redirectUrl(callback) {
|
|
83
102
|
return this.getRedirectUrl(callback);
|
|
84
103
|
}
|
|
85
104
|
/**
|
|
86
|
-
* Redirect user
|
|
105
|
+
* Redirect the user to the OAuth provider's authorization page.
|
|
106
|
+
* The request token is automatically obtained and stored in cookies.
|
|
107
|
+
*
|
|
108
|
+
* @param callback - Optional callback to customize the redirect request
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* await ally.use('twitter').redirect()
|
|
113
|
+
* ```
|
|
87
114
|
*/
|
|
88
115
|
async redirect(callback) {
|
|
89
116
|
const { token, secret } = await this.getRequestToken();
|
|
@@ -98,19 +125,21 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
98
125
|
this.ctx.response.redirect(url);
|
|
99
126
|
}
|
|
100
127
|
/**
|
|
101
|
-
*
|
|
128
|
+
* Check if the OAuth token from the callback matches the token
|
|
129
|
+
* stored in the cookie.
|
|
102
130
|
*/
|
|
103
131
|
stateMisMatch() {
|
|
104
132
|
return this.oauthTokenCookieValue !== this.ctx.request.input(this.oauthTokenParamName);
|
|
105
133
|
}
|
|
106
134
|
/**
|
|
107
|
-
*
|
|
135
|
+
* Check if an error was returned by the OAuth provider.
|
|
108
136
|
*/
|
|
109
137
|
hasError() {
|
|
110
138
|
return !!this.getError();
|
|
111
139
|
}
|
|
112
140
|
/**
|
|
113
|
-
* Get the
|
|
141
|
+
* Get the error code or message returned by the OAuth provider.
|
|
142
|
+
* Returns 'unknown_error' if no verifier is present and no error was specified.
|
|
114
143
|
*/
|
|
115
144
|
getError() {
|
|
116
145
|
const error = this.ctx.request.input(this.errorParamName);
|
|
@@ -123,19 +152,28 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
123
152
|
return null;
|
|
124
153
|
}
|
|
125
154
|
/**
|
|
126
|
-
*
|
|
155
|
+
* Get the OAuth verifier from the callback request.
|
|
127
156
|
*/
|
|
128
157
|
getCode() {
|
|
129
158
|
return this.ctx.request.input(this.oauthTokenVerifierName, null);
|
|
130
159
|
}
|
|
131
160
|
/**
|
|
132
|
-
*
|
|
161
|
+
* Check if the OAuth verifier is present in the callback request.
|
|
133
162
|
*/
|
|
134
163
|
hasCode() {
|
|
135
164
|
return !!this.getCode();
|
|
136
165
|
}
|
|
137
166
|
/**
|
|
138
|
-
*
|
|
167
|
+
* Exchange the request token and verifier for an access token.
|
|
168
|
+
* This method validates the token and checks for errors before
|
|
169
|
+
* making the request.
|
|
170
|
+
*
|
|
171
|
+
* @param callback - Optional callback to customize the token request
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const token = await ally.use('twitter').accessToken()
|
|
176
|
+
* ```
|
|
139
177
|
*/
|
|
140
178
|
async accessToken(callback) {
|
|
141
179
|
if (this.hasError()) {
|
|
@@ -155,7 +193,7 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
155
193
|
);
|
|
156
194
|
}
|
|
157
195
|
/**
|
|
158
|
-
* Not applicable with
|
|
196
|
+
* Not applicable with OAuth1. Use `userFromTokenAndSecret` instead.
|
|
159
197
|
*/
|
|
160
198
|
async userFromToken() {
|
|
161
199
|
throw new Exception(
|
|
@@ -167,4 +205,3 @@ var Oauth1Driver = class extends Oauth1Client {
|
|
|
167
205
|
export {
|
|
168
206
|
Oauth1Driver
|
|
169
207
|
};
|
|
170
|
-
//# sourceMappingURL=chunk-VHORNQLN.js.map
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// src/ally_manager.ts
|
|
2
|
-
import { RuntimeException } from "@
|
|
2
|
+
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
3
3
|
var AllyManager = class {
|
|
4
|
+
/**
|
|
5
|
+
* @param config - Map of provider names to driver factory functions
|
|
6
|
+
* @param ctx - The current HTTP context
|
|
7
|
+
*/
|
|
4
8
|
constructor(config, ctx) {
|
|
5
9
|
this.config = config;
|
|
6
10
|
this.#ctx = ctx;
|
|
@@ -8,7 +12,16 @@ var AllyManager = class {
|
|
|
8
12
|
#ctx;
|
|
9
13
|
#driversCache = /* @__PURE__ */ new Map();
|
|
10
14
|
/**
|
|
11
|
-
*
|
|
15
|
+
* Get a driver instance for the specified social provider. The driver
|
|
16
|
+
* instance is cached for the duration of the HTTP request.
|
|
17
|
+
*
|
|
18
|
+
* @param provider - The name of the social provider (e.g., 'github', 'google')
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const github = ally.use('github')
|
|
23
|
+
* await github.redirect()
|
|
24
|
+
* ```
|
|
12
25
|
*/
|
|
13
26
|
use(provider) {
|
|
14
27
|
if (this.#driversCache.has(provider)) {
|
|
@@ -31,4 +44,3 @@ var AllyManager = class {
|
|
|
31
44
|
export {
|
|
32
45
|
AllyManager
|
|
33
46
|
};
|
|
34
|
-
//# sourceMappingURL=chunk-NZT2DLWM.js.map
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import {
|
|
2
|
+
E_OAUTH_MISSING_CODE,
|
|
3
|
+
E_OAUTH_STATE_MISMATCH,
|
|
4
|
+
RedirectRequest
|
|
5
|
+
} from "./chunk-KSJ4CFTC.js";
|
|
6
|
+
|
|
7
|
+
// src/abstract_drivers/oauth2.ts
|
|
8
|
+
import { Exception } from "@adonisjs/core/exceptions";
|
|
9
|
+
import { Oauth2Client } from "@poppinss/oauth-client/oauth2";
|
|
10
|
+
var Oauth2Driver = class extends Oauth2Client {
|
|
11
|
+
/**
|
|
12
|
+
* @param ctx - The current HTTP context
|
|
13
|
+
* @param config - OAuth2 driver configuration
|
|
14
|
+
*/
|
|
15
|
+
constructor(ctx, config) {
|
|
16
|
+
super(config);
|
|
17
|
+
this.ctx = ctx;
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Whether the authorization process is stateless. When true,
|
|
22
|
+
* state verification via cookies is disabled.
|
|
23
|
+
*/
|
|
24
|
+
isStateless = false;
|
|
25
|
+
/**
|
|
26
|
+
* OAuth protocol version identifier
|
|
27
|
+
*/
|
|
28
|
+
version = "oauth2";
|
|
29
|
+
/**
|
|
30
|
+
* Cached state value read from the cookie
|
|
31
|
+
*/
|
|
32
|
+
stateCookieValue;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a URL builder instance for constructing authorization URLs
|
|
35
|
+
* with scope support.
|
|
36
|
+
*
|
|
37
|
+
* @param url - The base authorization URL
|
|
38
|
+
*/
|
|
39
|
+
urlBuilder(url) {
|
|
40
|
+
return new RedirectRequest(url, this.scopeParamName, this.scopesSeparator);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Loads the state value from the encrypted cookie and immediately clears
|
|
44
|
+
* the cookie. This must be called by child classes in their constructor
|
|
45
|
+
* to enable CSRF protection.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* constructor(ctx: HttpContext, config: DriverConfig) {
|
|
50
|
+
* super(ctx, config)
|
|
51
|
+
* this.loadState()
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
loadState() {
|
|
56
|
+
if (this.isStateless) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.stateCookieValue = this.ctx.request.encryptedCookie(this.stateCookieName);
|
|
60
|
+
this.ctx.response.clearCookie(this.stateCookieName);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Stores the CSRF state in an encrypted cookie for later verification
|
|
64
|
+
*/
|
|
65
|
+
#persistState() {
|
|
66
|
+
if (this.isStateless) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const state = this.getState();
|
|
70
|
+
this.ctx.response.encryptedCookie(this.stateCookieName, state, {
|
|
71
|
+
sameSite: false,
|
|
72
|
+
httpOnly: true
|
|
73
|
+
});
|
|
74
|
+
return state;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Enable stateless authentication by disabling CSRF state verification.
|
|
78
|
+
* Only use this in scenarios where state verification is not required.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* await ally.use('github').stateless().redirect()
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
stateless() {
|
|
86
|
+
this.isStateless = true;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get the authorization redirect URL without performing the redirect.
|
|
91
|
+
* Useful when you need to manually handle the redirect or use the URL
|
|
92
|
+
* in a different context.
|
|
93
|
+
*
|
|
94
|
+
* @param callback - Optional callback to customize the redirect request
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const url = await ally.use('github').redirectUrl((request) => {
|
|
99
|
+
* request.scopes(['user:email'])
|
|
100
|
+
* })
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
async redirectUrl(callback) {
|
|
104
|
+
const url = this.getRedirectUrl(callback);
|
|
105
|
+
return url;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Redirect the user to the OAuth provider's authorization page.
|
|
109
|
+
* The state parameter is automatically set for CSRF protection.
|
|
110
|
+
*
|
|
111
|
+
* @param callback - Optional callback to customize the redirect request
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* await ally.use('github').redirect((request) => {
|
|
116
|
+
* request.scopes(['user:email', 'read:org'])
|
|
117
|
+
* request.param('allow_signup', 'false')
|
|
118
|
+
* })
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
async redirect(callback) {
|
|
122
|
+
const url = await this.redirectUrl((request) => {
|
|
123
|
+
const state = this.#persistState();
|
|
124
|
+
state && request.param(this.stateParamName, state);
|
|
125
|
+
if (typeof callback === "function") {
|
|
126
|
+
callback(request);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
this.ctx.response.redirect(url);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Check if the state parameter from the callback matches the state
|
|
133
|
+
* stored in the cookie. Returns false in stateless mode.
|
|
134
|
+
*/
|
|
135
|
+
stateMisMatch() {
|
|
136
|
+
if (this.isStateless) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
return this.stateCookieValue !== this.ctx.request.input(this.stateParamName);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Check if an error was returned by the OAuth provider.
|
|
143
|
+
*/
|
|
144
|
+
hasError() {
|
|
145
|
+
return !!this.getError();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Get the error code or message returned by the OAuth provider.
|
|
149
|
+
* Returns 'unknown_error' if no code is present and no error was specified.
|
|
150
|
+
*/
|
|
151
|
+
getError() {
|
|
152
|
+
const error = this.ctx.request.input(this.errorParamName);
|
|
153
|
+
if (error) {
|
|
154
|
+
return error;
|
|
155
|
+
}
|
|
156
|
+
if (!this.hasCode()) {
|
|
157
|
+
return "unknown_error";
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Get the authorization code from the callback request.
|
|
163
|
+
*/
|
|
164
|
+
getCode() {
|
|
165
|
+
return this.ctx.request.input(this.codeParamName, null);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Check if the authorization code is present in the callback request.
|
|
169
|
+
*/
|
|
170
|
+
hasCode() {
|
|
171
|
+
return !!this.getCode();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Exchange the authorization code for an access token. This method
|
|
175
|
+
* validates the state and checks for errors before making the request.
|
|
176
|
+
*
|
|
177
|
+
* @param callback - Optional callback to customize the token request
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const token = await ally.use('github').accessToken()
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
async accessToken(callback) {
|
|
185
|
+
if (this.hasError()) {
|
|
186
|
+
throw new E_OAUTH_MISSING_CODE([this.codeParamName]);
|
|
187
|
+
}
|
|
188
|
+
if (this.stateMisMatch()) {
|
|
189
|
+
throw new E_OAUTH_STATE_MISMATCH();
|
|
190
|
+
}
|
|
191
|
+
return this.getAccessToken((request) => {
|
|
192
|
+
request.field(this.codeParamName, this.getCode());
|
|
193
|
+
if (typeof callback === "function") {
|
|
194
|
+
callback(request);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Not applicable with OAuth2. Use `userFromToken` instead.
|
|
200
|
+
*/
|
|
201
|
+
async userFromTokenAndSecret() {
|
|
202
|
+
throw new Exception(
|
|
203
|
+
'"userFromTokenAndSecret" is not applicable with Oauth2. Use "userFromToken" instead'
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export {
|
|
209
|
+
Oauth2Driver
|
|
210
|
+
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { HttpClient as ApiRequest } from '@poppinss/oauth-client';
|
|
2
|
-
export * as errors from './src/errors.
|
|
3
|
-
export { configure } from './configure.
|
|
4
|
-
export { stubsRoot } from './stubs/main.
|
|
5
|
-
export { AllyManager } from './src/ally_manager.
|
|
6
|
-
export { defineConfig, services } from './src/define_config.
|
|
7
|
-
export { RedirectRequest } from './src/redirect_request.
|
|
8
|
-
export { Oauth1Driver } from './src/abstract_drivers/oauth1.
|
|
9
|
-
export { Oauth2Driver } from './src/abstract_drivers/oauth2.
|
|
2
|
+
export * as errors from './src/errors.ts';
|
|
3
|
+
export { configure } from './configure.ts';
|
|
4
|
+
export { stubsRoot } from './stubs/main.ts';
|
|
5
|
+
export { AllyManager } from './src/ally_manager.ts';
|
|
6
|
+
export { defineConfig, services } from './src/define_config.ts';
|
|
7
|
+
export { RedirectRequest } from './src/redirect_request.ts';
|
|
8
|
+
export { Oauth1Driver } from './src/abstract_drivers/oauth1.ts';
|
|
9
|
+
export { Oauth2Driver } from './src/abstract_drivers/oauth2.ts';
|
package/build/index.js
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Oauth1Driver
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-KWRXS6EG.js";
|
|
4
4
|
import {
|
|
5
5
|
AllyManager
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-SZ4YJCVU.js";
|
|
7
7
|
import {
|
|
8
8
|
Oauth2Driver
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-WM3V3APX.js";
|
|
10
10
|
import {
|
|
11
11
|
RedirectRequest,
|
|
12
12
|
errors_exports
|
|
13
|
-
} from "./chunk-
|
|
14
|
-
import "./chunk-
|
|
13
|
+
} from "./chunk-KSJ4CFTC.js";
|
|
14
|
+
import "./chunk-MLKGABMK.js";
|
|
15
15
|
|
|
16
16
|
// index.ts
|
|
17
17
|
import { HttpClient } from "@poppinss/oauth-client";
|
|
18
18
|
|
|
19
19
|
// stubs/main.ts
|
|
20
|
-
|
|
21
|
-
var stubsRoot = getDirname(import.meta.url);
|
|
20
|
+
var stubsRoot = import.meta.dirname;
|
|
22
21
|
|
|
23
22
|
// configure.ts
|
|
24
23
|
var AVAILABLE_PROVIDERS = [
|
|
@@ -156,4 +155,3 @@ export {
|
|
|
156
155
|
services,
|
|
157
156
|
stubsRoot
|
|
158
157
|
};
|
|
159
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AllyManager
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-SZ4YJCVU.js";
|
|
4
|
+
import "../chunk-MLKGABMK.js";
|
|
5
5
|
|
|
6
6
|
// providers/ally_provider.ts
|
|
7
7
|
import { configProvider } from "@adonisjs/core";
|
|
8
8
|
import { HttpContext } from "@adonisjs/core/http";
|
|
9
|
-
import { RuntimeException } from "@
|
|
9
|
+
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
10
10
|
var AllyProvider = class {
|
|
11
11
|
constructor(app) {
|
|
12
12
|
this.app = app;
|
|
@@ -31,4 +31,3 @@ var AllyProvider = class {
|
|
|
31
31
|
export {
|
|
32
32
|
AllyProvider as default
|
|
33
33
|
};
|
|
34
|
-
//# sourceMappingURL=ally_provider.js.map
|