@svadmin/sso 0.10.0 → 0.11.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/src/presets.ts DELETED
@@ -1,204 +0,0 @@
1
- /**
2
- * Social login presets — pre-configured SSO providers for common identity platforms.
3
- *
4
- * Each factory function returns an AuthProvider via `createSSOAuthProvider()`
5
- * with the correct issuer and default scopes for that platform.
6
- *
7
- * @example
8
- * ```ts
9
- * import { createGoogleAuth } from '@svadmin/sso';
10
- * import { setAuthProvider } from '@svadmin/core';
11
- *
12
- * setAuthProvider(createGoogleAuth('your-client-id'));
13
- * ```
14
- */
15
-
16
- import { createSSOAuthProvider, type SSOConfig } from './auth-provider';
17
-
18
- // ─── Shared helpers ──────────────────────────────────────────
19
-
20
- type PresetOptions = Omit<SSOConfig, 'issuer' | 'clientId'>;
21
-
22
- function resolveRedirectUri(opts?: PresetOptions): string {
23
- return opts?.redirectUri ?? (typeof window !== 'undefined' ? `${window.location.origin}/callback` : '/callback');
24
- }
25
-
26
- // ─── Google ──────────────────────────────────────────────────
27
-
28
- /**
29
- * Google Sign-In via OIDC (OpenID Connect).
30
- *
31
- * Prerequisites:
32
- * 1. Create OAuth2 credentials at https://console.cloud.google.com/apis/credentials
33
- * 2. Add authorized redirect URIs (e.g., `http://localhost:5173/callback`)
34
- *
35
- * @param clientId - Google OAuth2 Client ID
36
- */
37
- export function createGoogleAuth(clientId: string, opts?: PresetOptions) {
38
- return createSSOAuthProvider({
39
- issuer: 'https://accounts.google.com',
40
- clientId,
41
- scopes: ['openid', 'profile', 'email'],
42
- redirectUri: resolveRedirectUri(opts),
43
- ...opts,
44
- });
45
- }
46
-
47
- // ─── Microsoft / Azure AD (Entra ID) ────────────────────────
48
-
49
- /**
50
- * Microsoft / Azure AD Sign-In via OIDC.
51
- *
52
- * @param clientId - Azure App Registration Client ID
53
- * @param tenantId - Azure tenant ID. Default: 'common' (multi-tenant)
54
- */
55
- export function createMicrosoftAuth(
56
- clientId: string,
57
- tenantId = 'common',
58
- opts?: PresetOptions,
59
- ) {
60
- return createSSOAuthProvider({
61
- issuer: `https://login.microsoftonline.com/${tenantId}/v2.0`,
62
- clientId,
63
- scopes: ['openid', 'profile', 'email'],
64
- redirectUri: resolveRedirectUri(opts),
65
- ...opts,
66
- });
67
- }
68
-
69
- // ─── GitHub ──────────────────────────────────────────────────
70
-
71
- /**
72
- * GitHub Sign-In via OAuth2.
73
- *
74
- * Note: GitHub does NOT support OIDC discovery, so we manually specify endpoints.
75
- * The `issuer` field is used for identification only.
76
- *
77
- * Prerequisites:
78
- * 1. Create an OAuth App at https://github.com/settings/developers
79
- * 2. Set the callback URL to your redirect URI
80
- *
81
- * @param clientId - GitHub OAuth App Client ID
82
- */
83
- export function createGitHubAuth(clientId: string, opts?: PresetOptions) {
84
- // GitHub uses standard OAuth2, not OIDC — requires manual endpoint config.
85
- // We leverage the SSO provider but override the discovery with manual endpoints.
86
- const redirectUri = resolveRedirectUri(opts);
87
-
88
- return createSSOAuthProvider({
89
- issuer: 'https://github.com',
90
- clientId,
91
- scopes: ['read:user', 'user:email'],
92
- redirectUri,
93
- // GitHub doesn't support OIDC discovery, provide manual endpoints
94
- manualEndpoints: {
95
- authorization_endpoint: 'https://github.com/login/oauth/authorize',
96
- token_endpoint: 'https://github.com/login/oauth/access_token',
97
- userinfo_endpoint: 'https://api.github.com/user',
98
- },
99
- mapIdentity: (userinfo) => ({
100
- id: String(userinfo.id ?? ''),
101
- name: (userinfo.name as string) ?? (userinfo.login as string) ?? '',
102
- avatar: (userinfo.avatar_url as string) ?? undefined,
103
- email: (userinfo.email as string) ?? undefined,
104
- }),
105
- ...opts,
106
- });
107
- }
108
-
109
- // ─── GitLab ──────────────────────────────────────────────────
110
-
111
- /**
112
- * GitLab Sign-In via OIDC.
113
- *
114
- * @param clientId - GitLab Application ID
115
- * @param instanceUrl - GitLab instance URL. Default: 'https://gitlab.com'
116
- */
117
- export function createGitLabAuth(
118
- clientId: string,
119
- instanceUrl = 'https://gitlab.com',
120
- opts?: PresetOptions,
121
- ) {
122
- return createSSOAuthProvider({
123
- issuer: instanceUrl,
124
- clientId,
125
- scopes: ['openid', 'profile', 'email'],
126
- redirectUri: resolveRedirectUri(opts),
127
- ...opts,
128
- });
129
- }
130
-
131
- // ─── Keycloak ────────────────────────────────────────────────
132
-
133
- /**
134
- * Keycloak Sign-In via OIDC.
135
- *
136
- * @param baseUrl - Keycloak base URL (e.g., 'https://keycloak.example.com')
137
- * @param realm - Keycloak realm name
138
- * @param clientId - Keycloak Client ID
139
- */
140
- export function createKeycloakAuth(
141
- baseUrl: string,
142
- realm: string,
143
- clientId: string,
144
- opts?: PresetOptions,
145
- ) {
146
- return createSSOAuthProvider({
147
- issuer: `${baseUrl.replace(/\/$/, '')}/realms/${realm}`,
148
- clientId,
149
- scopes: ['openid', 'profile', 'email'],
150
- redirectUri: resolveRedirectUri(opts),
151
- ...opts,
152
- });
153
- }
154
-
155
- // ─── Auth0 ───────────────────────────────────────────────────
156
-
157
- /**
158
- * Auth0 Sign-In via OIDC.
159
- *
160
- * @param domain - Auth0 domain (e.g., 'your-tenant.auth0.com')
161
- * @param clientId - Auth0 Client ID
162
- */
163
- export function createAuth0Auth(
164
- domain: string,
165
- clientId: string,
166
- opts?: PresetOptions,
167
- ) {
168
- return createSSOAuthProvider({
169
- issuer: `https://${domain.replace(/^https?:\/\//, '').replace(/\/$/, '')}`,
170
- clientId,
171
- scopes: ['openid', 'profile', 'email'],
172
- redirectUri: resolveRedirectUri(opts),
173
- ...opts,
174
- });
175
- }
176
-
177
- // ─── Type exports ────────────────────────────────────────────
178
-
179
- export type { PresetOptions };
180
-
181
- // ─── Supauth / GoTrue ────────────────────────────────────────
182
-
183
- /**
184
- * Supauth OIDC Sign-In (GoTrue-compatible identity provider).
185
- *
186
- * Compatible with SupaCloud, SupaOAuth, and any GoTrue-based auth backend.
187
- * Uses standard OIDC Authorization Code Flow with PKCE.
188
- *
189
- * @param issuer - Supauth issuer URL (e.g., 'https://auth.example.com')
190
- * @param clientId - OAuth2 Client ID
191
- */
192
- export function createSupauthAuth(
193
- clientId: string,
194
- opts?: PresetOptions & { issuer?: string },
195
- ) {
196
- const issuer = opts?.issuer ?? '';
197
- return createSSOAuthProvider({
198
- issuer,
199
- clientId,
200
- scopes: ['openid', 'profile', 'email'],
201
- redirectUri: resolveRedirectUri(opts),
202
- ...opts,
203
- });
204
- }