@oslokommune/auth-bff 2.0.0-beta2 → 2.0.0-beta3
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/README.md +110 -6
- package/dist/package.json +1 -1
- package/dist/src/config.d.ts +1 -1
- package/dist/src/vite-plugin.d.ts +2 -7
- package/dist/src/vite-plugin.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,9 +60,51 @@ npm install -g @oslokommune/auth-bff
|
|
|
60
60
|
auth-bff
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
## Running in Docker
|
|
64
|
+
|
|
65
|
+
When running in docker you should specify the version to use, and make sure it matches the one used in package.json.
|
|
66
|
+
|
|
67
|
+
Example dockerfile:
|
|
68
|
+
```dockerfile
|
|
69
|
+
FROM node:23-alpine AS base
|
|
70
|
+
|
|
71
|
+
FROM base AS react-build
|
|
72
|
+
WORKDIR /home/react
|
|
73
|
+
COPY package*.json /home/react
|
|
74
|
+
RUN npm install
|
|
75
|
+
COPY . ./
|
|
76
|
+
RUN npm run build
|
|
77
|
+
|
|
78
|
+
FROM base
|
|
79
|
+
WORKDIR /application
|
|
80
|
+
EXPOSE 8080
|
|
81
|
+
COPY --from=react-build /home/react/dist /application/dist
|
|
82
|
+
ENV NODE_ENV=production
|
|
83
|
+
RUN npm install -g @oslokommune/auth-bff@2.0.0-beta3
|
|
84
|
+
COPY bff.config.json /application/
|
|
85
|
+
CMD ["auth-bff"]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
To use different configuration for different environments, you can create separate config files for each and select it at build time (using build args).
|
|
89
|
+
For example, with `bff.config.dev.json` and `bff.config.prod.json`:
|
|
90
|
+
|
|
91
|
+
```dockerfile
|
|
92
|
+
ARG ENVIRONMENT=''
|
|
93
|
+
COPY bff.config.${ENVIRONMENT}.json /application/bff.config.json
|
|
94
|
+
CMD ["auth-bff"]
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Or select it at runtime, using an env var:
|
|
98
|
+
|
|
99
|
+
```dockerfile
|
|
100
|
+
COPY bff.config*.json /application/
|
|
101
|
+
CMD exec auth-bff --configFile bff.config.${ENVIRONMENT}.json
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
|
|
63
105
|
## Configuration
|
|
64
106
|
|
|
65
|
-
Configuration is
|
|
107
|
+
Configuration is defined in json-files that look like this:
|
|
66
108
|
|
|
67
109
|
```json
|
|
68
110
|
{
|
|
@@ -81,7 +123,7 @@ Configuration is set in json-files that look like this:
|
|
|
81
123
|
}
|
|
82
124
|
```
|
|
83
125
|
|
|
84
|
-
By default it looks for a file named `bff.config.json
|
|
126
|
+
By default it looks for a file named `bff.config.json`, but this can be overridden:
|
|
85
127
|
|
|
86
128
|
Vite:
|
|
87
129
|
|
|
@@ -118,9 +160,9 @@ AWS Parameter store:
|
|
|
118
160
|
This loads from the configured AWS environment. For this to work on your local machine the `AWS_PROFILE` environment
|
|
119
161
|
variable must be set, and you must be signed in to that profile
|
|
120
162
|
|
|
121
|
-
See
|
|
163
|
+
ℹ️ [See `config.ts` for a description of all config parameters](src/config.ts)
|
|
122
164
|
|
|
123
|
-
|
|
165
|
+
## Using with ID-porten (via `okdata`):
|
|
124
166
|
|
|
125
167
|
`auth-bff` Has special support for keys generated by [`okdata`](https://github.com/oslokommune/okdata-cli).
|
|
126
168
|
|
|
@@ -214,6 +256,16 @@ resource "aws_dynamodb_table" "session_dynamodb_table" {
|
|
|
214
256
|
}
|
|
215
257
|
```
|
|
216
258
|
|
|
259
|
+
Then remember to update the config to use the new table:
|
|
260
|
+
|
|
261
|
+
```json
|
|
262
|
+
{
|
|
263
|
+
"sessionStoreOptions": {
|
|
264
|
+
"table": "myteam-dev-myservice-sessions"
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
217
269
|
The read/write limits above are just an example, and can be changed/removed
|
|
218
270
|
|
|
219
271
|
## Required AWS permissions
|
|
@@ -233,8 +285,60 @@ dynamodb:Scan
|
|
|
233
285
|
dynamodb:UpdateItem
|
|
234
286
|
```
|
|
235
287
|
|
|
236
|
-
## React
|
|
288
|
+
## React component
|
|
289
|
+
|
|
290
|
+
This package also includes a React component for handling authentication state. It will redirect to login if required
|
|
291
|
+
and optionally automatically poll for changes to authentication state.
|
|
292
|
+
|
|
293
|
+
### AuthContextProvider
|
|
294
|
+
|
|
295
|
+
```tsx
|
|
296
|
+
import {AuthContextProvider} from "@oslokommune/auth-bff/react";
|
|
297
|
+
import {PktLoader} from "@oslokommune/punkt-react";
|
|
298
|
+
|
|
299
|
+
const fiveMinutes = 5 * 60 * 1000;
|
|
300
|
+
|
|
301
|
+
<AuthContextProvider authRequired={true} loaderComponent={<PktLoader/>} pollInterval={fiveMinues}>
|
|
302
|
+
<App/>
|
|
303
|
+
</AuthContextProvider>
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
| Option | Description |
|
|
307
|
+
|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
308
|
+
| authRequired | Whether authentication is required. If true, will redirect to login before rendering child components (default: true) |
|
|
309
|
+
| loaderComponent | React component to display while loading auth state. (default: null) |
|
|
310
|
+
| baseUrl | Must be set to the same baseUrl as in the json config for login/logout to work correctly (default: '') |
|
|
311
|
+
| pollInterval | Minimum interval in milliseconds between checks if session is still active. Will set authState to 'expired' if session is expired (default: disabled) |
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
### useAuthContext
|
|
315
|
+
|
|
316
|
+
Hook to get current AuthState. Must be called in a component inside the AuthContextProvider.
|
|
317
|
+
```tsx
|
|
318
|
+
import {useAuthContext} from "@oslokommune/auth-bff/react";
|
|
237
319
|
|
|
238
|
-
|
|
320
|
+
const {user, authState, login} = useAuthContext()
|
|
321
|
+
if(authState === 'authenticated') {
|
|
322
|
+
console.log(`Hello, ${user.pid}`)
|
|
323
|
+
} else {
|
|
324
|
+
login()
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
```
|
|
239
328
|
|
|
329
|
+
| Property | Description |
|
|
330
|
+
|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
331
|
+
| user | The currently logged in user, or null if not logged in. User object contains the id_token claims returned from the usr endpoint. See config option `userClaims`. |
|
|
332
|
+
| login | Function that will redirect to the login endpoint when invoked |
|
|
333
|
+
| logout | Function that will redirect to the logout endpoint when invoked |
|
|
334
|
+
| authState | Current auth state. See table below for values | | |
|
|
335
|
+
|
|
336
|
+
#### AuthState
|
|
337
|
+
| Value | Description |
|
|
338
|
+
|-----------------|------------------------------------------------------------------------------------------------------------------|
|
|
339
|
+
| pending | Initial value before auth state has been determined |
|
|
340
|
+
| authenticated | User is authenticated. |
|
|
341
|
+
| unauthenticated | User is not authenticated |
|
|
342
|
+
| expired | User was authenticated, but the session has expired. Can be used to display message to user or redirect to login | | |
|
|
343
|
+
| error | Failed to determine auth state | | |
|
|
240
344
|
|
package/dist/package.json
CHANGED
package/dist/src/config.d.ts
CHANGED
|
@@ -98,7 +98,7 @@ export type BffConfig = {
|
|
|
98
98
|
[path: string]: string;
|
|
99
99
|
};
|
|
100
100
|
/**
|
|
101
|
-
* List of claims in the
|
|
101
|
+
* List of claims in the id_token that are returned by the /user-endpoint. By default all are returned
|
|
102
102
|
*
|
|
103
103
|
* Example: `["pid"]`
|
|
104
104
|
*/
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
2
|
export default function bff({ configFile }?: {
|
|
3
3
|
configFile?: string;
|
|
4
|
-
}):
|
|
5
|
-
name: string;
|
|
6
|
-
apply: string;
|
|
7
|
-
configureServer: ({ middlewares }: ViteDevServer) => Promise<void>;
|
|
8
|
-
configurePreviewServer: ({ middlewares }: ViteDevServer) => Promise<void>;
|
|
9
|
-
};
|
|
4
|
+
}): Plugin;
|
|
10
5
|
//# sourceMappingURL=vite-plugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-plugin.d.ts","sourceRoot":"","sources":["../../src/vite-plugin.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"vite-plugin.d.ts","sourceRoot":"","sources":["../../src/vite-plugin.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,MAAM,EAAC,MAAM,MAAM,CAAA;AAsB1C,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,EAAC,UAAU,EAAC,GAAE;IAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAM,GAAG,MAAM,CAO5E"}
|