@oslokommune/auth-bff 2.0.0-beta2 → 2.0.0-beta4

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 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-beta4
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 set in json-files that look like this:
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`. But this can be overridden:
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 [config.ts](src/config.ts) description of all config parameters
163
+ ℹ️ [See `config.ts` for a description of all config parameters](src/config.ts)
122
164
 
123
- ### Using with ID-porten (via `okdata`):
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 AuthContext
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
- This package also includes a React component for handling authentication state
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/auth-bff",
3
- "version": "2.0.0-beta2",
3
+ "version": "2.0.0-beta4",
4
4
  "repository": "https://github.com/oslokommune/auth-bff.git",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -98,7 +98,7 @@ export type BffConfig = {
98
98
  [path: string]: string;
99
99
  };
100
100
  /**
101
- * List of claims in the access token that are returned by the /user-endpoint. By default all are returned
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
  */
@@ -124,5 +124,5 @@ export type BffConfig = {
124
124
  };
125
125
  export declare function getEnv(env: string, defaultVal?: string, parseFn?: (val: string) => string): string;
126
126
  export declare function getSsmParameter(name: string, withDecryption?: boolean): Promise<string>;
127
- export declare function loadConfig(configFile?: string): Promise<BffConfig>;
127
+ export declare function loadConfig(configFile?: string | Array<string>): Promise<BffConfig>;
128
128
  //# sourceMappingURL=config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,OAAO,MAAM,iBAAiB,CAAC;AAEtC,MAAM,MAAM,SAAS,GAAG;IACtB;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;;OAIG;IACH,MAAM,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;IACzB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC,aAAa,CAAA;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;;;OAIG;IACH,cAAc,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,CAAA;IACnD;;OAEG;IACH,qBAAqB,EAAE,MAAM,CAAA;IAC7B;;;;OAIG;IACH,qBAAqB,EAAE,MAAM,CAAA;IAC7B;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH,gBAAgB,EAAE,QAAQ,GAAG,UAAU,CAAA;IACvC;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B;;;;OAIG;IAC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M,CAAA;IACnB;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;;;OAIG;IACH,cAAc,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,CAAA;IACnD;;OAEG;IACH,qBAAqB,EAAE,MAAM,CAAA;IAC7B;;;;OAIG;IACH,qBAAqB,EAAE,MAAM,CAAA;IAC7B;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH,gBAAgB,EAAE,QAAQ,GAAG,UAAU,CAAA;IACvC;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B;;;;OAIG;IACH,YAAY,EAAE;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;IACxC;;;;OAIG;IACH,UAAU,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;IAC1B;;;;;;;;;;;;;;;;OAgBG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC,CAAA;CACjF,CAAA;AAUD,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,UAQzF;AAID,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,cAAc,GAAE,OAAc,mBAMjF;AAID,wBAAsB,UAAU,CAAC,UAAU,GAAE,MAAM,GAAG,KAAK,CAAC,MAAM,CAAqB,sBAyBtF"}
@@ -1,10 +1,5 @@
1
- import { ViteDevServer } from 'vite';
1
+ import { Plugin } from 'vite';
2
2
  export default function bff({ configFile }?: {
3
- configFile?: string;
4
- }): {
5
- name: string;
6
- apply: string;
7
- configureServer: ({ middlewares }: ViteDevServer) => Promise<void>;
8
- configurePreviewServer: ({ middlewares }: ViteDevServer) => Promise<void>;
9
- };
3
+ configFile?: string | Array<string>;
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,EAAC,aAAa,EAAC,MAAM,MAAM,CAAA;AAsBlC,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,EAAC,UAAU,EAAC,GAAE;IAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAM;;;uCAnBrC,aAAa;8CAAb,aAAa;EA0B3C"}
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,GAAG,KAAK,CAAC,MAAM,CAAC,CAAA;CAAM,GAAG,MAAM,CAO5F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/auth-bff",
3
- "version": "2.0.0-beta2",
3
+ "version": "2.0.0-beta4",
4
4
  "repository": "https://github.com/oslokommune/auth-bff.git",
5
5
  "publishConfig": {
6
6
  "access": "public"