@ghuri/socialauth 0.0.0-stage → 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +188 -2
- package/dist/admin.d.ts +22 -0
- package/dist/admin.js +35 -0
- package/dist/admin.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-ZNZG4OU3.js +77 -0
- package/dist/chunk-ZNZG4OU3.js.map +1 -0
- package/dist/index.d.ts +299 -0
- package/dist/index.js +1301 -0
- package/dist/index.js.map +1 -0
- package/dist/models-Cepu1xG8.d.ts +126 -0
- package/dist/testing.d.ts +24 -0
- package/dist/testing.js +64 -0
- package/dist/testing.js.map +1 -0
- package/package.json +54 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { Context } from 'hono';
|
|
2
|
+
import { VerifyFunction, OAuth2Strategy, Profile, OAuthTokens } from '@ghuri/oauth';
|
|
3
|
+
import { a as SocialApp, S as SocialAccount, b as SocialToken } from './models-Cepu1xG8.js';
|
|
4
|
+
export { e as emptyDict } from './models-Cepu1xG8.js';
|
|
5
|
+
import * as _ghuri_core from '@ghuri/core';
|
|
6
|
+
import { AppConfig, Signal, ViewFunction } from '@ghuri/core';
|
|
7
|
+
import '@ghuri/orm';
|
|
8
|
+
|
|
9
|
+
/** Credentials of one provider app, as in settings (`PROVIDERS.github.APP`) or a `SocialApp` row. */
|
|
10
|
+
interface AppSettings {
|
|
11
|
+
clientId: string;
|
|
12
|
+
secret?: string;
|
|
13
|
+
key?: string;
|
|
14
|
+
/** For `openid_connect`: the id used in URLs (`/accounts/okta/login/`). */
|
|
15
|
+
providerId?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
/** Provider-specific settings (`serverUrl` for OpenID Connect, `tenant` for Microsoft…). */
|
|
18
|
+
settings?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
interface ProviderSettings {
|
|
21
|
+
APP?: AppSettings;
|
|
22
|
+
/** Several apps of one provider (`openid_connect`). */
|
|
23
|
+
APPS?: AppSettings[];
|
|
24
|
+
SCOPE?: string[];
|
|
25
|
+
AUTH_PARAMS?: Record<string, string>;
|
|
26
|
+
/** Override whether this provider's emails count as verified. */
|
|
27
|
+
VERIFIED_EMAIL?: boolean;
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
interface SocialAccountSettings {
|
|
31
|
+
PROVIDERS?: Record<string, ProviderSettings>;
|
|
32
|
+
ADAPTER?: new () => SocialAccountAdapter;
|
|
33
|
+
AUTO_SIGNUP?: boolean;
|
|
34
|
+
EMAIL_AUTHENTICATION?: boolean;
|
|
35
|
+
EMAIL_REQUIRED?: boolean;
|
|
36
|
+
LOGIN_ON_GET?: boolean;
|
|
37
|
+
STORE_TOKENS?: boolean;
|
|
38
|
+
LOGIN_RESPONSE?: 'session' | 'jwt';
|
|
39
|
+
FRONTEND_URL?: string;
|
|
40
|
+
/** Where browser flows go on errors (default `LOGIN_URL`), with `?error=`. */
|
|
41
|
+
LOGIN_ERROR_URL?: string;
|
|
42
|
+
}
|
|
43
|
+
interface ResolvedSocialSettings {
|
|
44
|
+
providers: Record<string, ProviderSettings>;
|
|
45
|
+
adapter?: new () => SocialAccountAdapter;
|
|
46
|
+
autoSignup: boolean;
|
|
47
|
+
emailAuthentication: boolean;
|
|
48
|
+
emailRequired: boolean;
|
|
49
|
+
loginOnGet: boolean;
|
|
50
|
+
storeTokens: boolean;
|
|
51
|
+
loginResponse: 'session' | 'jwt';
|
|
52
|
+
frontendUrl?: string;
|
|
53
|
+
loginErrorUrl: string;
|
|
54
|
+
}
|
|
55
|
+
declare function socialSettings(): ResolvedSocialSettings;
|
|
56
|
+
|
|
57
|
+
interface StrategyContext {
|
|
58
|
+
/** The id used in URLs and stored on `SocialAccount.provider` (`github`, or an OIDC app's `providerId`). */
|
|
59
|
+
key: string;
|
|
60
|
+
app: AppSettings;
|
|
61
|
+
settings: ProviderSettings;
|
|
62
|
+
callbackURL: string;
|
|
63
|
+
verify: VerifyFunction;
|
|
64
|
+
}
|
|
65
|
+
declare abstract class Provider {
|
|
66
|
+
abstract readonly id: string;
|
|
67
|
+
abstract readonly name: string;
|
|
68
|
+
/** Several apps per provider, each with its own `providerId` (OpenID Connect). */
|
|
69
|
+
readonly multipleApps: boolean;
|
|
70
|
+
/** Default scopes when `SCOPE` is not set. */
|
|
71
|
+
readonly defaultScope: string[];
|
|
72
|
+
abstract createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
73
|
+
scope(settings: ProviderSettings): string[];
|
|
74
|
+
/** May this provider's email be trusted to belong to the user? */
|
|
75
|
+
emailVerified(profile: Profile, settings: ProviderSettings): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Native / mobile login: verify a token the app obtained with the provider's own SDK and return the profile.
|
|
78
|
+
* The token must have been issued to one of our client ids. Default: ID tokens of OpenID Connect providers.
|
|
79
|
+
*/
|
|
80
|
+
verifyNativeToken(ctx: StrategyContext, tokens: {
|
|
81
|
+
accessToken?: string;
|
|
82
|
+
idToken?: string;
|
|
83
|
+
}): Promise<Profile>;
|
|
84
|
+
profileFromClaims(key: string, claims: Record<string, any>): Profile;
|
|
85
|
+
}
|
|
86
|
+
declare class GitHubProvider extends Provider {
|
|
87
|
+
readonly id = "github";
|
|
88
|
+
readonly name = "GitHub";
|
|
89
|
+
readonly defaultScope: string[];
|
|
90
|
+
private api;
|
|
91
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
92
|
+
private profile;
|
|
93
|
+
/** Checks with GitHub that the token belongs to our OAuth app (`POST /applications/{client_id}/token`). */
|
|
94
|
+
verifyNativeToken(ctx: StrategyContext, tokens: {
|
|
95
|
+
accessToken?: string;
|
|
96
|
+
}): Promise<Profile>;
|
|
97
|
+
}
|
|
98
|
+
declare class GoogleProvider extends Provider {
|
|
99
|
+
readonly id = "google";
|
|
100
|
+
readonly name = "Google";
|
|
101
|
+
readonly defaultScope: string[];
|
|
102
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
103
|
+
}
|
|
104
|
+
declare class FacebookProvider extends Provider {
|
|
105
|
+
readonly id = "facebook";
|
|
106
|
+
readonly name = "Facebook";
|
|
107
|
+
readonly defaultScope: string[];
|
|
108
|
+
private graph;
|
|
109
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
110
|
+
private profile;
|
|
111
|
+
/** Checks the token with `debug_token` (issued to our app id) before trusting it. */
|
|
112
|
+
verifyNativeToken(ctx: StrategyContext, tokens: {
|
|
113
|
+
accessToken?: string;
|
|
114
|
+
}): Promise<Profile>;
|
|
115
|
+
}
|
|
116
|
+
declare class MicrosoftProvider extends Provider {
|
|
117
|
+
readonly id = "microsoft";
|
|
118
|
+
readonly name = "Microsoft";
|
|
119
|
+
readonly defaultScope: string[];
|
|
120
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
121
|
+
profileFromClaims(key: string, claims: Record<string, any>): Profile;
|
|
122
|
+
emailVerified(profile: Profile, settings: ProviderSettings): boolean;
|
|
123
|
+
}
|
|
124
|
+
declare class DiscordProvider extends Provider {
|
|
125
|
+
readonly id = "discord";
|
|
126
|
+
readonly name = "Discord";
|
|
127
|
+
readonly defaultScope: string[];
|
|
128
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
129
|
+
verifyNativeToken(): Promise<Profile>;
|
|
130
|
+
}
|
|
131
|
+
declare class GitLabProvider extends Provider {
|
|
132
|
+
readonly id = "gitlab";
|
|
133
|
+
readonly name = "GitLab";
|
|
134
|
+
readonly defaultScope: string[];
|
|
135
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
136
|
+
profileFromClaims(key: string, claims: Record<string, any>): Profile;
|
|
137
|
+
}
|
|
138
|
+
declare class OpenIDConnectProvider extends Provider {
|
|
139
|
+
readonly id = "openid_connect";
|
|
140
|
+
readonly name = "OpenID Connect";
|
|
141
|
+
readonly multipleApps = true;
|
|
142
|
+
readonly defaultScope: string[];
|
|
143
|
+
createStrategy(ctx: StrategyContext): OAuth2Strategy;
|
|
144
|
+
}
|
|
145
|
+
/** Add (or replace) a provider: `registerProvider(new MyProvider())`. */
|
|
146
|
+
declare function registerProvider(provider: Provider): void;
|
|
147
|
+
declare function getProvider(id: string): Provider | undefined;
|
|
148
|
+
declare function allProviders(): Provider[];
|
|
149
|
+
|
|
150
|
+
interface ResolvedProvider {
|
|
151
|
+
/** The id in URLs and on `SocialAccount.provider`. */
|
|
152
|
+
key: string;
|
|
153
|
+
provider: Provider;
|
|
154
|
+
app: AppSettings;
|
|
155
|
+
settings: ProviderSettings;
|
|
156
|
+
/** The `SocialApp` row, when the credentials came from the database. */
|
|
157
|
+
row?: SocialApp;
|
|
158
|
+
}
|
|
159
|
+
/** The provider for `key` (`github`, or the `providerId` of a multi-app provider), or null if not configured. */
|
|
160
|
+
declare function resolveProvider(key: string): Promise<ResolvedProvider | null>;
|
|
161
|
+
/** Every configured provider (for login buttons). */
|
|
162
|
+
declare function configuredProviders(): Promise<ResolvedProvider[]>;
|
|
163
|
+
|
|
164
|
+
type Process = 'login' | 'connect';
|
|
165
|
+
/** `browser`: redirects (the OAuth callback); `api`: JSON (native tokens, SPA calls). */
|
|
166
|
+
type Mode = 'browser' | 'api';
|
|
167
|
+
interface SocialLogin extends ResolvedProvider {
|
|
168
|
+
profile: Profile;
|
|
169
|
+
tokens?: OAuthTokens;
|
|
170
|
+
/** Whether the provider vouches for `profile.email`. */
|
|
171
|
+
emailVerified: boolean;
|
|
172
|
+
process: Process;
|
|
173
|
+
next?: string;
|
|
174
|
+
}
|
|
175
|
+
declare class SocialAuthError extends Error {
|
|
176
|
+
readonly status: number;
|
|
177
|
+
readonly code: string;
|
|
178
|
+
constructor(message: string, status?: number, code?: string);
|
|
179
|
+
}
|
|
180
|
+
declare function buildLogin(resolved: ResolvedProvider, profile: Profile, opts: {
|
|
181
|
+
tokens?: OAuthTokens;
|
|
182
|
+
process?: Process;
|
|
183
|
+
next?: string;
|
|
184
|
+
}): SocialLogin;
|
|
185
|
+
/** A one-time code an SPA exchanges for a JWT pair at `token/` (tokens never travel in URLs). */
|
|
186
|
+
declare function makeLoginCode(user: any): Promise<string>;
|
|
187
|
+
/** The user of a login code, once (replays are refused while the cache remembers the code). */
|
|
188
|
+
declare function redeemLoginCode(code: string): Promise<any | null>;
|
|
189
|
+
/** Log the user in and answer for the mode and `LOGIN_RESPONSE`. */
|
|
190
|
+
declare function finishLogin(c: Context, user: any, login: SocialLogin, mode: Mode): Promise<Response>;
|
|
191
|
+
/** Process a successful provider login (django-allauth's `complete_social_login`). */
|
|
192
|
+
declare function completeSocialLogin(c: Context, login: SocialLogin, mode: Mode): Promise<Response>;
|
|
193
|
+
|
|
194
|
+
/** Throw from `preSocialLogin()` to answer with your own response. */
|
|
195
|
+
declare class ImmediateResponse extends Error {
|
|
196
|
+
readonly response: Response;
|
|
197
|
+
constructor(response: Response);
|
|
198
|
+
}
|
|
199
|
+
interface SignupData {
|
|
200
|
+
username?: string;
|
|
201
|
+
email?: string;
|
|
202
|
+
}
|
|
203
|
+
declare class SocialAccountAdapter {
|
|
204
|
+
/** May new users sign up through social login? */
|
|
205
|
+
isOpenForSignup(_c: Context, _login: SocialLogin): boolean | Promise<boolean>;
|
|
206
|
+
/** Called after the provider answered, before anything is looked up or created. */
|
|
207
|
+
preSocialLogin(_c: Context, _login: SocialLogin): Promise<void>;
|
|
208
|
+
/** May the user be created without showing the signup form? */
|
|
209
|
+
isAutoSignupAllowed(_c: Context, login: SocialLogin): boolean | Promise<boolean>;
|
|
210
|
+
/** A new, unsaved user instance. */
|
|
211
|
+
newUser(_c: Context, _login: SocialLogin): any;
|
|
212
|
+
/** Copy profile data (and signup form data) onto the new user. */
|
|
213
|
+
populateUser(_c: Context, login: SocialLogin, user: any, data: SignupData): any;
|
|
214
|
+
/** Save the new user (with an unusable password). */
|
|
215
|
+
saveUser(_c: Context, _login: SocialLogin, user: any): Promise<any>;
|
|
216
|
+
/** Username candidates, best first. */
|
|
217
|
+
usernameCandidates(login: SocialLogin): string[];
|
|
218
|
+
/** A username that is free, from the profile. */
|
|
219
|
+
generateUsername(_c: Context, login: SocialLogin): Promise<string>;
|
|
220
|
+
/** Where to go after connecting an account. */
|
|
221
|
+
getConnectRedirectUrl(_c: Context, _account: SocialAccount): string;
|
|
222
|
+
/** Customize error responses; return undefined for the default. */
|
|
223
|
+
authenticationError(_c: Context, _error: {
|
|
224
|
+
provider: string;
|
|
225
|
+
message: string;
|
|
226
|
+
code?: string;
|
|
227
|
+
}): Response | undefined | Promise<Response | undefined>;
|
|
228
|
+
}
|
|
229
|
+
declare function getAdapter(): SocialAccountAdapter;
|
|
230
|
+
|
|
231
|
+
/** The `socialaccount` app (django-allauth's): `SocialApp`, `SocialAccount`, `SocialToken`. */
|
|
232
|
+
declare class SocialAuthConfig extends AppConfig {
|
|
233
|
+
name: string;
|
|
234
|
+
label: string;
|
|
235
|
+
verboseName: string;
|
|
236
|
+
path: string | undefined;
|
|
237
|
+
migrations: Record<string, unknown>;
|
|
238
|
+
models: {
|
|
239
|
+
SocialApp: typeof SocialApp;
|
|
240
|
+
SocialAccount: typeof SocialAccount;
|
|
241
|
+
SocialToken: typeof SocialToken;
|
|
242
|
+
};
|
|
243
|
+
ready(): void;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Before a social login is processed (after the provider answered). */
|
|
247
|
+
declare const preSocialLogin: Signal<{
|
|
248
|
+
c: Context;
|
|
249
|
+
login: SocialLogin;
|
|
250
|
+
}>;
|
|
251
|
+
/** A provider account was linked to a user (signup or connect). */
|
|
252
|
+
declare const socialAccountAdded: Signal<{
|
|
253
|
+
c: Context;
|
|
254
|
+
login: SocialLogin;
|
|
255
|
+
account: SocialAccount;
|
|
256
|
+
}>;
|
|
257
|
+
/** An existing link logged in again (profile and tokens refreshed). */
|
|
258
|
+
declare const socialAccountUpdated: Signal<{
|
|
259
|
+
c: Context;
|
|
260
|
+
login: SocialLogin;
|
|
261
|
+
account: SocialAccount;
|
|
262
|
+
}>;
|
|
263
|
+
/** A link was removed from the connections page. */
|
|
264
|
+
declare const socialAccountRemoved: Signal<{
|
|
265
|
+
c: Context;
|
|
266
|
+
account: SocialAccount;
|
|
267
|
+
}>;
|
|
268
|
+
|
|
269
|
+
declare function storeTokens(login: SocialLogin, account: SocialAccount): Promise<void>;
|
|
270
|
+
/**
|
|
271
|
+
* A usable access token for the account (refreshed with the stored refresh token when it expired), or null
|
|
272
|
+
* when tokens are not stored or cannot be refreshed.
|
|
273
|
+
*/
|
|
274
|
+
declare function getAccessToken(account: SocialAccount): Promise<string | null>;
|
|
275
|
+
|
|
276
|
+
/** `GET providers/`: configured providers with their login URLs (for SPA buttons). */
|
|
277
|
+
declare const providersView: ViewFunction;
|
|
278
|
+
/** `<provider>/login/`: POST starts the flow; GET confirms first (unless LOGIN_ON_GET). */
|
|
279
|
+
declare const loginView: ViewFunction;
|
|
280
|
+
/** `<provider>/login/callback/`: the provider sends the browser back here. */
|
|
281
|
+
declare const callbackView: ViewFunction;
|
|
282
|
+
/**
|
|
283
|
+
* `POST <provider>/token/` `{ id_token }` or `{ access_token }`: log in with a token a native app got from the
|
|
284
|
+
* provider's SDK. The token is checked against our client id(s) before it is trusted.
|
|
285
|
+
*/
|
|
286
|
+
declare const nativeTokenView: ViewFunction;
|
|
287
|
+
/** `POST token/` `{ code }`: exchange the one-time code from an SPA redirect for a JWT pair. */
|
|
288
|
+
declare const tokenExchangeView: ViewFunction;
|
|
289
|
+
/** `signup/`: finish a social signup that needs a username or email. */
|
|
290
|
+
declare const signupView: ViewFunction;
|
|
291
|
+
/** `connections/`: list the user's social accounts (GET) and disconnect one (POST `{ account }`). */
|
|
292
|
+
declare const connectionsView: ViewFunction;
|
|
293
|
+
/**
|
|
294
|
+
* `path('accounts/', include(socialUrls))` → `providers/`, `token/`, `signup/`, `connections/`,
|
|
295
|
+
* `<provider>/login/`, `<provider>/login/callback/`, `<provider>/token/`.
|
|
296
|
+
*/
|
|
297
|
+
declare const socialUrls: _ghuri_core.UrlPatterns;
|
|
298
|
+
|
|
299
|
+
export { type AppSettings, DiscordProvider, FacebookProvider, GitHubProvider, GitLabProvider, GoogleProvider, ImmediateResponse, MicrosoftProvider, type Mode, OpenIDConnectProvider, type Process, Provider, type ProviderSettings, type ResolvedProvider, type ResolvedSocialSettings, type SignupData, SocialAccount, SocialAccountAdapter, type SocialAccountSettings, SocialApp, SocialAuthConfig, SocialAuthError, type SocialLogin, SocialToken, type StrategyContext, allProviders, buildLogin, callbackView, completeSocialLogin, configuredProviders, connectionsView, finishLogin, getAccessToken, getAdapter, getProvider, loginView, makeLoginCode, nativeTokenView, preSocialLogin, providersView, redeemLoginCode, registerProvider, resolveProvider, signupView, socialAccountAdded, socialAccountRemoved, socialAccountUpdated, socialSettings, socialUrls, storeTokens, tokenExchangeView };
|