@pofsis-nbt/pofsis-auth 2.0.1 → 2.1.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/Documentation.md +151 -147
- package/README.md +143 -133
- package/dist/PofsisAuth.d.ts +1 -1
- package/dist/PofsisAuth.d.ts.map +1 -1
- package/dist/{PofsisAuthClient-CEw_lL5S.js → PofsisAuthClient-Duybofpq.js} +162 -46
- package/dist/PofsisAuthClient-Duybofpq.js.map +1 -0
- package/dist/PofsisAuthClient-c4lzLzjV.cjs +2 -0
- package/dist/PofsisAuthClient-c4lzLzjV.cjs.map +1 -0
- package/dist/{RequireAuth-CPhXupYh.cjs → RequireAuth-BkYhk-9p.cjs} +2 -2
- package/dist/{RequireAuth-CPhXupYh.cjs.map → RequireAuth-BkYhk-9p.cjs.map} +1 -1
- package/dist/{RequireAuth-DFa8_MR1.js → RequireAuth-DroX56rd.js} +67 -62
- package/dist/{RequireAuth-DFa8_MR1.js.map → RequireAuth-DroX56rd.js.map} +1 -1
- package/dist/auth/authLaunch.d.ts.map +1 -1
- package/dist/auth/config.d.ts.map +1 -1
- package/dist/core/PofsisAuthClient.d.ts +2 -0
- package/dist/core/PofsisAuthClient.d.ts.map +1 -1
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/popupProtocol.d.ts +11 -1
- package/dist/core/popupProtocol.d.ts.map +1 -1
- package/dist/core.cjs +1 -1
- package/dist/core.js +2 -2
- package/dist/elements/index.d.ts.map +1 -1
- package/dist/elements.cjs +1 -1
- package/dist/elements.cjs.map +1 -1
- package/dist/elements.js +8 -3
- package/dist/elements.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/pofsis-auth-elements.js +1 -1
- package/dist/pofsis-auth-elements.js.map +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.js +1 -1
- package/dist/types.d.ts +13 -2
- package/dist/types.d.ts.map +1 -1
- package/docs/popup-integration.md +188 -162
- package/package.json +87 -87
- package/dist/PofsisAuthClient-B3YHqgZL.cjs +0 -2
- package/dist/PofsisAuthClient-B3YHqgZL.cjs.map +0 -1
- package/dist/PofsisAuthClient-CEw_lL5S.js.map +0 -1
|
@@ -1,162 +1,188 @@
|
|
|
1
|
-
# POFSIS Auth popup integration
|
|
2
|
-
|
|
3
|
-
`@pofsis-nbt/pofsis-auth` opens the hosted POFSIS Auth UI in a popup and completes OAuth 2.0 Authorization Code with PKCE in the calling tool. Passwords, OTPs, passkeys, refresh tokens, and Cognito credentials remain owned by POFSIS Auth.
|
|
4
|
-
|
|
5
|
-
## Prerequisites
|
|
6
|
-
|
|
7
|
-
- Register the tool's exact origin, callback URI, and logout URI with POFSIS Auth for each environment.
|
|
8
|
-
- Use HTTPS outside local development and allow the tool origin in Auth CORS configuration.
|
|
9
|
-
- Obtain the immutable registered application UUID and allowed public scopes.
|
|
10
|
-
- Never put an OAuth client secret or organization service credential in a browser application.
|
|
11
|
-
|
|
12
|
-
## Package entry points
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
npm install @pofsis-nbt/pofsis-auth
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
| Import | Use |
|
|
19
|
-
| ---------------------------------- | ----------------------------------------------- |
|
|
20
|
-
| `@pofsis-nbt/pofsis-auth/core` | Framework-neutral browser SDK |
|
|
21
|
-
| `@pofsis-nbt/pofsis-auth/react` | React provider, hooks, guards, and API helper |
|
|
22
|
-
| `@pofsis-nbt/pofsis-auth/elements` | Optional `<pofsis-sign-in>` Web Component |
|
|
23
|
-
| `@pofsis-nbt/pofsis-auth` | Backward-compatible React API plus core exports |
|
|
24
|
-
|
|
25
|
-
## Framework-neutral setup
|
|
26
|
-
|
|
27
|
-
```js
|
|
28
|
-
import { createPofsisAuth,
|
|
29
|
-
|
|
30
|
-
const auth = createPofsisAuth({
|
|
31
|
-
applicationId: '11111111-1111-4111-8111-111111111111',
|
|
32
|
-
apiBaseUrl: 'https://auth-api.example.com',
|
|
33
|
-
authUrl: 'https://auth.example.com/authorize',
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
// Run this before rendering the normal app on /auth/callback.
|
|
37
|
-
if (location.pathname === '/auth/callback') {
|
|
38
|
-
const relayed =
|
|
39
|
-
if (!relayed) {
|
|
40
|
-
const params = new URLSearchParams(location.search)
|
|
41
|
-
await auth.completeRedirectCallback(params.get('code'), params.get('state'))
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
Call popup authentication directly from a click or other user gesture:
|
|
47
|
-
|
|
48
|
-
```js
|
|
49
|
-
document.querySelector('#login').addEventListener('click', async () => {
|
|
50
|
-
const result = await auth.login({
|
|
51
|
-
presentation: 'popup',
|
|
52
|
-
screen: 'login',
|
|
53
|
-
returnUrl: location.pathname + location.search,
|
|
54
|
-
})
|
|
55
|
-
if (result.status === 'authenticated') renderUser(result.user)
|
|
56
|
-
})
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
The SDK calls `window.open()` itself. A blocked popup falls back to a full-page redirect. Route guards should explicitly use `presentation: 'redirect'` because they may not run during a user gesture.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
|
78
|
-
|
|
|
79
|
-
| `
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
|
|
85
|
-
`
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
- `redirecting
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
The
|
|
1
|
+
# POFSIS Auth popup and modal integration
|
|
2
|
+
|
|
3
|
+
`@pofsis-nbt/pofsis-auth` opens the hosted POFSIS Auth UI in a popup, an SDK-owned iframe modal, or a full-page redirect and completes OAuth 2.0 Authorization Code with PKCE in the calling tool. Passwords, OTPs, passkeys, refresh tokens, and Cognito credentials remain owned by POFSIS Auth.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Register the tool's exact origin, callback URI, and logout URI with POFSIS Auth for each environment.
|
|
8
|
+
- Use HTTPS outside local development and allow the tool origin in Auth CORS configuration.
|
|
9
|
+
- Obtain the immutable registered application UUID and allowed public scopes.
|
|
10
|
+
- Never put an OAuth client secret or organization service credential in a browser application.
|
|
11
|
+
|
|
12
|
+
## Package entry points
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @pofsis-nbt/pofsis-auth
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
| Import | Use |
|
|
19
|
+
| ---------------------------------- | ----------------------------------------------- |
|
|
20
|
+
| `@pofsis-nbt/pofsis-auth/core` | Framework-neutral browser SDK |
|
|
21
|
+
| `@pofsis-nbt/pofsis-auth/react` | React provider, hooks, guards, and API helper |
|
|
22
|
+
| `@pofsis-nbt/pofsis-auth/elements` | Optional `<pofsis-sign-in>` Web Component |
|
|
23
|
+
| `@pofsis-nbt/pofsis-auth` | Backward-compatible React API plus core exports |
|
|
24
|
+
|
|
25
|
+
## Framework-neutral setup
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { createPofsisAuth, relayAuthCallback } from '@pofsis-nbt/pofsis-auth/core'
|
|
29
|
+
|
|
30
|
+
const auth = createPofsisAuth({
|
|
31
|
+
applicationId: '11111111-1111-4111-8111-111111111111',
|
|
32
|
+
apiBaseUrl: 'https://auth-api.example.com',
|
|
33
|
+
authUrl: 'https://auth.example.com/authorize',
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// Run this before rendering the normal app on /auth/callback.
|
|
37
|
+
if (location.pathname === '/auth/callback') {
|
|
38
|
+
const relayed = relayAuthCallback()
|
|
39
|
+
if (!relayed) {
|
|
40
|
+
const params = new URLSearchParams(location.search)
|
|
41
|
+
await auth.completeRedirectCallback(params.get('code'), params.get('state'))
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Call popup authentication directly from a click or other user gesture:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
document.querySelector('#login').addEventListener('click', async () => {
|
|
50
|
+
const result = await auth.login({
|
|
51
|
+
presentation: 'popup',
|
|
52
|
+
screen: 'login',
|
|
53
|
+
returnUrl: location.pathname + location.search,
|
|
54
|
+
})
|
|
55
|
+
if (result.status === 'authenticated') renderUser(result.user)
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The SDK calls `window.open()` itself. A blocked popup falls back to a full-page redirect. Route guards should explicitly use `presentation: 'redirect'` because they may not run during a user gesture.
|
|
60
|
+
|
|
61
|
+
To keep Auth visually inside the tool, request the modal presentation:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const result = await auth.login({
|
|
65
|
+
presentation: 'modal',
|
|
66
|
+
screen: 'login',
|
|
67
|
+
returnUrl: location.pathname + location.search,
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The SDK creates an accessible modal and embeds the hosted Auth origin in an iframe. The tool cannot inspect credential fields. Close, Escape, and backdrop dismissal resolve with `status: 'cancelled'`. A modal does not silently fall back to popup or redirect if embedding fails.
|
|
72
|
+
|
|
73
|
+
## Initialization parameters
|
|
74
|
+
|
|
75
|
+
| Parameter | Required | Default | Meaning |
|
|
76
|
+
| ---------------------- | -------: | ---------------------- | -------------------------------- |
|
|
77
|
+
| `applicationId` | yes | - | Immutable `applications.id` UUID |
|
|
78
|
+
| `apiBaseUrl` | yes | - | Public POFSIS Auth API base URL |
|
|
79
|
+
| `authUrl` | yes | - | Hosted Auth `/authorize` URL |
|
|
80
|
+
| `scope` | no | `openid profile email` | Registered OAuth scopes |
|
|
81
|
+
| `callbackPath` | no | `/auth/callback` | Local tool callback route |
|
|
82
|
+
| `popup.width` | no | `480` | Popup width in pixels |
|
|
83
|
+
| `popup.height` | no | `720` | Popup height in pixels |
|
|
84
|
+
| `popup.name` | no | `pofsis-auth` | Reused browser window name |
|
|
85
|
+
| `popup.timeoutMs` | no | `300000` | Authentication timeout |
|
|
86
|
+
| `modal.width` | no | `480` | Modal width in pixels |
|
|
87
|
+
| `modal.height` | no | `720` | Modal height in pixels |
|
|
88
|
+
| `modal.timeoutMs` | no | `300000` | Authentication timeout |
|
|
89
|
+
| `modal.readyTimeoutMs` | no | `15000` | Secure iframe handshake timeout |
|
|
90
|
+
|
|
91
|
+
## Launch parameters
|
|
92
|
+
|
|
93
|
+
| Parameter | Required | Default | Meaning |
|
|
94
|
+
| -------------- | -------: | ------------ | ---------------------------------------------------- |
|
|
95
|
+
| `presentation` | no | `popup` | `popup`, iframe `modal`, or full-page `redirect` |
|
|
96
|
+
| `screen` | no | `login` | `login`, `signup`, or `reset` |
|
|
97
|
+
| `method` | no | Auth default | `password`, `passkey`, or `otp` |
|
|
98
|
+
| `loginHint` | no | - | Editable email or Philippine mobile hint |
|
|
99
|
+
| `returnUrl` | no | - | Safe application-relative route restored after login |
|
|
100
|
+
|
|
101
|
+
`returnUrl` must start with one `/` and must not start with `//`. Do not put invitation secrets or external URLs in it.
|
|
102
|
+
|
|
103
|
+
Convenience methods use the same protocol:
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
await auth.signup({ loginHint: 'employee@example.com', method: 'otp' })
|
|
107
|
+
await auth.resetPassword({ loginHint: 'employee@example.com' })
|
|
108
|
+
await auth.login({ method: 'password' })
|
|
109
|
+
await auth.login({ method: 'passkey' })
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## React
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { PofsisAuth, useAuth } from '@pofsis-nbt/pofsis-auth/react'
|
|
116
|
+
import '@pofsis-nbt/pofsis-auth/style.css'
|
|
117
|
+
|
|
118
|
+
function SignInButton() {
|
|
119
|
+
const { login } = useAuth()
|
|
120
|
+
return <button onClick={() => void login({ presentation: 'modal' })}>Sign in</button>
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function App() {
|
|
124
|
+
return (
|
|
125
|
+
<PofsisAuth
|
|
126
|
+
applicationId="11111111-1111-4111-8111-111111111111"
|
|
127
|
+
apiBaseUrl="https://auth-api.example.com"
|
|
128
|
+
authUrl="https://auth.example.com/authorize"
|
|
129
|
+
modal={{ width: 480, height: 720 }}
|
|
130
|
+
>
|
|
131
|
+
<SignInButton />
|
|
132
|
+
</PofsisAuth>
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`RequireAuth` deliberately uses redirect presentation. The provider handles popup, modal, and full-page callbacks at `callbackPath`.
|
|
138
|
+
|
|
139
|
+
## Web Component
|
|
140
|
+
|
|
141
|
+
Import the package entry or host the generated standalone `dist/pofsis-auth-elements.js` bundle:
|
|
142
|
+
|
|
143
|
+
```html
|
|
144
|
+
<script type="module" src="https://auth.pofsis.com/sdk/pofsis-auth-elements.js"></script>
|
|
145
|
+
|
|
146
|
+
<pofsis-sign-in
|
|
147
|
+
application-id="11111111-1111-4111-8111-111111111111"
|
|
148
|
+
api-base-url="https://auth-api.example.com"
|
|
149
|
+
auth-url="https://auth.example.com/authorize"
|
|
150
|
+
return-url="/dashboard"
|
|
151
|
+
presentation="modal"
|
|
152
|
+
>Sign in</pofsis-sign-in
|
|
153
|
+
>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The element dispatches `pofsis-auth-authenticated`, `pofsis-auth-cancelled`, `pofsis-auth-redirecting`, and `pofsis-auth-error`. It is only a launch control; credential fields always render on the hosted Auth origin.
|
|
157
|
+
|
|
158
|
+
## Iframe security and hosting
|
|
159
|
+
|
|
160
|
+
- The Auth frontend verifies a parent-frame handshake against the registered OAuth client before rendering credentials.
|
|
161
|
+
- The callback accepts messages only from the exact iframe window, expected origin, state, and correlation ID.
|
|
162
|
+
- The iframe is sandboxed and receives only the capabilities needed for forms, scripts, external legal links, and WebAuthn.
|
|
163
|
+
- Auth HTML must not send `X-Frame-Options: DENY` or `SAMEORIGIN`. A CSP `frame-ancestors` directive must include every registered tool origin.
|
|
164
|
+
- Third-party-cookie or WebAuthn restrictions vary by browser. Keep popup or redirect available as an explicit consumer fallback when required by browser policy.
|
|
165
|
+
|
|
166
|
+
## Session and API calls
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
const unsubscribe = auth.subscribe((session) => updateHeader(session?.user))
|
|
170
|
+
const token = auth.getAccessToken()
|
|
171
|
+
await auth.refreshSession()
|
|
172
|
+
await auth.logout()
|
|
173
|
+
unsubscribe()
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Access tokens are held in memory. Use `getAccessToken()` to add a bearer token to the tool's own API calls. Do not write access tokens to URLs or browser storage.
|
|
177
|
+
|
|
178
|
+
## Results and failures
|
|
179
|
+
|
|
180
|
+
- `authenticated`: code exchange succeeded and includes the user.
|
|
181
|
+
- `cancelled`: the user dismissed the popup or modal; this is not an authentication error.
|
|
182
|
+
- `redirecting`: the SDK started full-page navigation, including popup-blocked fallback.
|
|
183
|
+
- Invalid state, origin, popup or iframe source, or correlation messages are ignored or rejected.
|
|
184
|
+
- A timeout rejects the launch and closes the popup or modal. Failure to validate the iframe parent rejects with an embedding error.
|
|
185
|
+
|
|
186
|
+
For callback URI mismatches, verify the exact deployed origin and registered URI. For CORS or silent-session failures, verify Auth's origin registry and credentialed cookie configuration. A cancelled or unavailable passkey remains an Auth-screen concern and must retain the password fallback.
|
|
187
|
+
|
|
188
|
+
The frontend SDK must never call `/api/internal`, Cognito administration APIs, or organization provisioning APIs. Those flows remain tool-backend to Auth-backend operations.
|
package/package.json
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@pofsis-nbt/pofsis-auth",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "POFSIS OAuth/PKCE authentication SDK for browser applications.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.js",
|
|
13
|
-
"require": "./dist/index.cjs",
|
|
14
|
-
"default": "./dist/index.js"
|
|
15
|
-
},
|
|
16
|
-
"./core": {
|
|
17
|
-
"types": "./dist/core/index.d.ts",
|
|
18
|
-
"import": "./dist/core.js",
|
|
19
|
-
"require": "./dist/core.cjs",
|
|
20
|
-
"default": "./dist/core.js"
|
|
21
|
-
},
|
|
22
|
-
"./react": {
|
|
23
|
-
"types": "./dist/react/index.d.ts",
|
|
24
|
-
"import": "./dist/react.js",
|
|
25
|
-
"require": "./dist/react.cjs",
|
|
26
|
-
"default": "./dist/react.js"
|
|
27
|
-
},
|
|
28
|
-
"./elements": {
|
|
29
|
-
"types": "./dist/elements/index.d.ts",
|
|
30
|
-
"import": "./dist/elements.js",
|
|
31
|
-
"require": "./dist/elements.cjs",
|
|
32
|
-
"default": "./dist/elements.js"
|
|
33
|
-
},
|
|
34
|
-
"./style.css": "./dist/style.css"
|
|
35
|
-
},
|
|
36
|
-
"files": [
|
|
37
|
-
"dist",
|
|
38
|
-
"README.md",
|
|
39
|
-
"Documentation.md",
|
|
40
|
-
"docs"
|
|
41
|
-
],
|
|
42
|
-
"sideEffects": [
|
|
43
|
-
"**/*.css"
|
|
44
|
-
],
|
|
45
|
-
"engines": {
|
|
46
|
-
"node": "^20.19.0 || >=22.12.0"
|
|
47
|
-
},
|
|
48
|
-
"scripts": {
|
|
49
|
-
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
50
|
-
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
51
|
-
"test": "tsx --test src/**/*.test.ts",
|
|
52
|
-
"check": "npm run typecheck && npm test",
|
|
53
|
-
"build": "npm run clean && vite build && vite build --config vite.elements.config.ts && tsc -p tsconfig.build.json",
|
|
54
|
-
"prepack": "npm run check && npm run build",
|
|
55
|
-
"pack:check": "npm pack --dry-run --json",
|
|
56
|
-
"format": "prettier --write .",
|
|
57
|
-
"format:check": "prettier --check ."
|
|
58
|
-
},
|
|
59
|
-
"peerDependencies": {
|
|
60
|
-
"axios": ">=1",
|
|
61
|
-
"react": ">=18",
|
|
62
|
-
"react-dom": ">=18"
|
|
63
|
-
},
|
|
64
|
-
"peerDependenciesMeta": {
|
|
65
|
-
"axios": {
|
|
66
|
-
"optional": true
|
|
67
|
-
},
|
|
68
|
-
"react": {
|
|
69
|
-
"optional": true
|
|
70
|
-
},
|
|
71
|
-
"react-dom": {
|
|
72
|
-
"optional": true
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
"devDependencies": {
|
|
76
|
-
"@types/node": "^26.0.0",
|
|
77
|
-
"@types/react": "^19.2.0",
|
|
78
|
-
"@types/react-dom": "^19.2.0",
|
|
79
|
-
"axios": "^1.19.0",
|
|
80
|
-
"prettier": "^3.6.0",
|
|
81
|
-
"react": "^19.2.0",
|
|
82
|
-
"react-dom": "^19.2.0",
|
|
83
|
-
"tsx": "^4.20.0",
|
|
84
|
-
"typescript": "^5.9.0",
|
|
85
|
-
"vite": "^8.2.0"
|
|
86
|
-
}
|
|
87
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@pofsis-nbt/pofsis-auth",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "POFSIS OAuth/PKCE authentication SDK for browser applications.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./core": {
|
|
17
|
+
"types": "./dist/core/index.d.ts",
|
|
18
|
+
"import": "./dist/core.js",
|
|
19
|
+
"require": "./dist/core.cjs",
|
|
20
|
+
"default": "./dist/core.js"
|
|
21
|
+
},
|
|
22
|
+
"./react": {
|
|
23
|
+
"types": "./dist/react/index.d.ts",
|
|
24
|
+
"import": "./dist/react.js",
|
|
25
|
+
"require": "./dist/react.cjs",
|
|
26
|
+
"default": "./dist/react.js"
|
|
27
|
+
},
|
|
28
|
+
"./elements": {
|
|
29
|
+
"types": "./dist/elements/index.d.ts",
|
|
30
|
+
"import": "./dist/elements.js",
|
|
31
|
+
"require": "./dist/elements.cjs",
|
|
32
|
+
"default": "./dist/elements.js"
|
|
33
|
+
},
|
|
34
|
+
"./style.css": "./dist/style.css"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md",
|
|
39
|
+
"Documentation.md",
|
|
40
|
+
"docs"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": [
|
|
43
|
+
"**/*.css"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
50
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
51
|
+
"test": "tsx --test src/**/*.test.ts",
|
|
52
|
+
"check": "npm run typecheck && npm test",
|
|
53
|
+
"build": "npm run clean && vite build && vite build --config vite.elements.config.ts && tsc -p tsconfig.build.json",
|
|
54
|
+
"prepack": "npm run check && npm run build",
|
|
55
|
+
"pack:check": "npm pack --dry-run --json",
|
|
56
|
+
"format": "prettier --write .",
|
|
57
|
+
"format:check": "prettier --check ."
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"axios": ">=1",
|
|
61
|
+
"react": ">=18",
|
|
62
|
+
"react-dom": ">=18"
|
|
63
|
+
},
|
|
64
|
+
"peerDependenciesMeta": {
|
|
65
|
+
"axios": {
|
|
66
|
+
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"react": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"react-dom": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@types/node": "^26.0.0",
|
|
77
|
+
"@types/react": "^19.2.0",
|
|
78
|
+
"@types/react-dom": "^19.2.0",
|
|
79
|
+
"axios": "^1.19.0",
|
|
80
|
+
"prettier": "^3.6.0",
|
|
81
|
+
"react": "^19.2.0",
|
|
82
|
+
"react-dom": "^19.2.0",
|
|
83
|
+
"tsx": "^4.20.0",
|
|
84
|
+
"typescript": "^5.9.0",
|
|
85
|
+
"vite": "^8.2.0"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
async function e(e){if(e.status===204)return null;let t=await e.text();return t?JSON.parse(t):null}async function t(t,n,r){let i=await fetch(`${t.apiBaseUrl}${n}`,{credentials:`include`,...r,headers:{Accept:`application/json`,...r?.body&&!(r.body instanceof FormData)?{"Content-Type":`application/json`}:{},...r?.headers}});if(!i.ok){let e=Error(`Auth API ${n} failed (${i.status})`);throw e.status=i.status,e}return{status:i.status,data:await e(i)}}async function n(e){let{data:n}=await t(e,`/oauth/applications/${encodeURIComponent(e.applicationId)}/client?origin=${encodeURIComponent(window.location.origin)}`);return n}async function r(e,n,r,i){let{data:a}=await t(e,`/oauth/token`,{method:`POST`,body:JSON.stringify({grant_type:`authorization_code`,client_id:i.client_id,redirect_uri:i.redirect_uri,code:n,code_verifier:r})});return a}async function i(e,n){let{data:r}=await t(e,`/oauth/token`,{method:`POST`,body:JSON.stringify({grant_type:`refresh_token`,client_id:n})});return r||null}async function a(e){try{return await t(e,`/auth/session`),!0}catch{return!1}}async function o(e){await t(e,`/auth/logout`,{method:`POST`})}async function s(e,n){let{data:r}=await t(e,`/auth/profile/me`,{headers:{Authorization:`Bearer ${n}`}});return r}async function c(e,n,r){let{data:i}=await t(e,`/auth/profile/me`,{method:`PATCH`,headers:{Authorization:`Bearer ${n}`},body:JSON.stringify(r)});return i}async function l(e,n,r,i){let a=new FormData;a.append(`file`,i);let{data:o}=await t(e,`/auth/profile/me/images/${r}`,{method:`POST`,headers:{Authorization:`Bearer ${n}`},body:a});return o}async function u(e,n,r){let{data:i}=await t(e,`/auth/profile/me/images/${r}`,{method:`DELETE`,headers:{Authorization:`Bearer ${n}`}});return i}function d(e){return e.replace(/\/$/,``)}function f(e={}){let t=e.applicationId?.trim(),n=e.apiBaseUrl?.trim(),r=e.authUrl?.trim(),i=e.scope?.trim()||`openid profile email`,a=e.callbackPath?.trim()||`/auth/callback`,o={width:e.popup?.width??480,height:e.popup?.height??720,name:e.popup?.name?.trim()||`pofsis-auth`,timeoutMs:e.popup?.timeoutMs??3e5},s=[];if(t||s.push(`applicationId`),n||s.push(`apiBaseUrl`),r||s.push(`authUrl`),s.length)throw Error(`@pofsis-nbt/pofsis-auth missing config: ${s.join(`, `)}`);if(!t||!n||!r)throw Error(`@pofsis-nbt/pofsis-auth missing config`);if(!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(t))throw Error(`@pofsis-nbt/pofsis-auth applicationId must be a UUID`);if(!a.startsWith(`/`)||a.startsWith(`//`))throw Error(`@pofsis-nbt/pofsis-auth callbackPath must be application-relative`);if(o.width<=0||o.height<=0||o.timeoutMs<=0)throw Error(`@pofsis-nbt/pofsis-auth popup dimensions and timeout must be positive`);return{applicationId:t.toLowerCase(),apiBaseUrl:d(n),authUrl:d(r),scope:i,callbackPath:a,popup:o}}var p=/^[^\s@]+@[^\s@]+\.[^\s@]+$/,m=/^(?:\+63|63|0)?9\d{9}$/,h=new Set([`password`,`passkey`,`otp`]),g=new Set([`login`,`signup`,`reset`]),_=320;function v(e,t=!1){let n=typeof e==`string`?e.trim():``;if(!n||n.length>_)return;if(p.test(n))return n.toLowerCase();let r=n.replace(/[\s()-]/g,``);if(!t&&m.test(r))return`+63${r.replace(/^(?:\+63|63|0)/,``)}`}function y(e,t=`login`){let n=typeof e==`string`?{returnUrl:e}:e||{},r=n.screen===`register`?`signup`:n.screen,i=g.has(r)?r:t,a=h.has(n.method)?n.method:void 0,o=i===`login`||i===`signup`&&(a===`password`||a===`otp`)?a:void 0;return{returnUrl:n.returnUrl,screen:i,loginHint:v(n.loginHint),method:o}}function b(e){let t=new URLSearchParams(e),n=t.get(`login_hint`),r=n===null?v(t.get(`email`),!0):v(n);return y({screen:t.get(`screen`)||void 0,loginHint:r,method:t.get(`method`)||void 0})}function x({authUrl:e,query:t,options:n}){let r=y(n),i=new URL(e);if(i.search=t.toString(),r.loginHint&&i.searchParams.set(`login_hint`,r.loginHint),r.screen===`login`&&r.method&&i.searchParams.set(`method`,r.method),r.screen===`login`)return i.toString();let a=r.screen===`signup`?`/signup`:`/forgot-password`,o=new URL(a,i),s=t.get(`client_id`);return s&&o.searchParams.set(`client_id`,s),o.searchParams.set(`return_to`,`${i.pathname}${i.search}`),r.loginHint&&o.searchParams.set(`login_hint`,r.loginHint),r.screen===`signup`&&o.searchParams.set(`method`,r.method||`password`),o.toString()}var S=`pofsis-auth:callback`;function C(e){if(!e)return;let t=e.indexOf(`.`);return t>0?e.slice(0,t):void 0}function w(e=window.location.origin){if(!window.opener||window.opener.closed)return!1;let t=new URLSearchParams(window.location.search),n=t.get(`state`)||void 0,r={type:S,code:t.get(`code`)||void 0,state:n,error:t.get(`error`)||void 0,correlationId:C(n||null)};return window.opener.postMessage(r,e),window.close(),!0}var T=e=>btoa(String.fromCharCode(...e)).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``),E=()=>T(crypto.getRandomValues(new Uint8Array(32)));async function D(e){return T(new Uint8Array(await crypto.subtle.digest(`SHA-256`,new TextEncoder().encode(e))))}var O=e=>e?.startsWith(`/`)&&!e.startsWith(`//`)?e:void 0,k=e=>({token_type:e.token_type,expires_in:e.expires_in,user:e.user}),A=class{config;token=null;session=null;client=null;clientPromise=null;refreshPromise=null;subscribers=new Set;attempt=null;listening=!1;constructor(e){this.config=f(e),this.attach()}attach(){this.listening||=(window.addEventListener(`message`,this.onPopupMessage),!0)}destroy(){window.removeEventListener(`message`,this.onPopupMessage),this.listening=!1;let e=this.finishAttempt();e&&(e.popup.close(),e.resolve({status:`cancelled`})),this.subscribers.clear()}getUser(){return this.session?.user??null}getSession(){return this.session}getAccessToken(){return this.token}subscribe(e){return this.attach(),this.subscribers.add(e),e(this.session),()=>{this.subscribers.delete(e)}}emit(){for(let e of this.subscribers)e(this.session)}accept(e){this.token=e.access_token,this.session=k(e),this.emit()}loadClient(){return this.clientPromise||=n(this.config).then(e=>this.client=e).catch(e=>{throw this.clientPromise=null,e}),this.clientPromise}getOAuthClient(){return this.loadClient()}async refreshSession(){let e=await this.loadClient();return this.refreshPromise||=i(this.config,e.client_id).then(e=>{e?this.accept(e):this.clearSession()}).finally(()=>this.refreshPromise=null),this.refreshPromise}async isRemoteSessionActive(){return a(this.config)}clearSession(){this.token=null,this.session=null,this.emit()}replaceUser(e){this.session&&(this.session={...this.session,user:e},this.emit())}async logout(){try{await o(this.config)}finally{this.clearSession()}}signup(e={}){return this.login({...e,screen:`signup`})}resetPassword(e={}){return this.login({...e,screen:`reset`})}async login(e={}){let t=y(e);return(e.presentation??`popup`)===`popup`?this.openPopup({...e,...t}):this.startRedirect({...e,...t})}async authorizationUrl(e,t,n){let r=await this.loadClient(),i=new URLSearchParams({client_id:r.client_id,redirect_uri:r.redirect_uri,response_type:`code`,scope:this.config.scope,state:t,code_challenge:await D(n),code_challenge_method:`S256`});return x({authUrl:this.config.authUrl,query:i,options:e})}storageKey(e){return`${this.config.applicationId}.auth.${e}`}async startRedirect(e){let t=E(),n=E();sessionStorage.setItem(this.storageKey(`state`),t),sessionStorage.setItem(this.storageKey(`verifier`),n);let r=O(e.returnUrl);return r?sessionStorage.setItem(this.storageKey(`return-path`),r):sessionStorage.removeItem(this.storageKey(`return-path`)),window.location.assign(await this.authorizationUrl(e,t,n)),{status:`redirecting`}}async openPopup(e){if(this.attempt&&!this.attempt.popup.closed)return this.attempt.popup.focus(),new Promise((e,t)=>{let n=this.attempt,r=n.resolve,i=n.reject;n.resolve=t=>{r(t),e(t)},n.reject=e=>{i(e),t(e)}});let{width:t,height:n,name:r,timeoutMs:i}=this.config.popup,a=Math.max(0,window.screenX+(window.outerWidth-t)/2),o=Math.max(0,window.screenY+(window.outerHeight-n)/2),s=window.open(`about:blank`,r,`popup=yes,width=${t},height=${n},left=${Math.round(a)},top=${Math.round(o)}`);if(!s)return this.startRedirect({...e,presentation:`redirect`});let c=E(),l=`${c}.${E()}`,u=E(),d=O(e.returnUrl);return new Promise((t,n)=>{let r=setInterval(()=>{s.closed&&this.finishAttempt()?.resolve({status:`cancelled`})},250),a=setTimeout(()=>{let e=this.finishAttempt();s.close(),e?.reject(Error(`POFSIS Auth popup timed out.`))},i);this.attempt={popup:s,state:l,verifier:u,correlationId:c,returnUrl:d,resolve:t,reject:n,timer:r,timeout:a},this.authorizationUrl(e,l,u).then(e=>s.location.replace(e)).catch(e=>{let t=this.finishAttempt();s.close(),t?.reject(e instanceof Error?e:Error(String(e)))})})}finishAttempt(){let e=this.attempt;return e?(clearInterval(e.timer),clearTimeout(e.timeout),this.attempt=null,e):null}onPopupMessage=e=>{let t=this.attempt,n=e.data;if(!t||e.origin!==window.location.origin||e.source!==t.popup||n?.type!==`pofsis-auth:callback`||n.correlationId!==t.correlationId||C(n.state||null)!==t.correlationId||n.state!==t.state)return;let r=this.finishAttempt();if(r){if(n.error||!n.code){r.popup.close(),r.reject(Error(n.error||`POFSIS Auth callback was incomplete.`));return}this.completeCode(n.code,n.state,r.verifier).then(()=>{r.popup.close(),r.returnUrl&&window.history.replaceState(null,``,r.returnUrl),r.resolve({status:`authenticated`,user:this.getUser()})}).catch(e=>{r.popup.close(),r.reject(e instanceof Error?e:Error(String(e)))})}};async completeCode(e,t,n){if(!t||!n)throw Error(`Authentication state could not be verified.`);let i=await this.loadClient();this.accept(await r(this.config,e,n,i))}async completeRedirectCallback(e,t){let n=sessionStorage.getItem(this.storageKey(`state`)),r=sessionStorage.getItem(this.storageKey(`verifier`));if(sessionStorage.removeItem(this.storageKey(`state`)),sessionStorage.removeItem(this.storageKey(`verifier`)),!n||!r||t!==n)throw Error(`Authentication state could not be verified.`);await this.completeCode(e,t,r)}};function j(e){return new A(e)}Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return f}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,"l",{enumerable:!0,get:function(){return l}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return j}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return u}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return w}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return A}});
|
|
2
|
-
//# sourceMappingURL=PofsisAuthClient-B3YHqgZL.cjs.map
|