Skip to content

Commit b01121d

Browse files
committed
feat(config): 💥 Changed providerInfo to userInfo to align with oidc naming
BREAKING CHANGE: Changed config field `providerInfo` to `userInfo`.
1 parent 20c92ef commit b01121d

File tree

14 files changed

+57
-43
lines changed

14 files changed

+57
-43
lines changed

README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Nuxt][nuxt-src]][nuxt-href]
99

1010
Welcome to __Nuxt OIDC Auth__, a Nuxt module focusing on native OIDC (OpenID Connect) based authentication for Nuxt with a high level of customizability and security for SSR applications.
11-
This module doesn't use any external dependencies outside of the [unjs](https://unjs.io/) ecosystem except for token validation (the well known `jose` library for JWT interactions).
11+
This module doesn't use any external dependencies outside of the [unjs](https://unjs.io/) ecosystem except for token validation (the well known and tested `jose` library for JWT interactions).
1212
This module's session implementation is based on [nuxt-auth-utils](https://github.com/Atinux/nuxt-auth-utils).
1313

1414
<!--- [Playground Demo](https://stackblitz.com/github/itpropro/nuxt-oidc-auth/tree/main/playground) -->
@@ -28,15 +28,29 @@ This module's session implementation is based on [nuxt-auth-utils](https://githu
2828

2929
If you are looking for a module that supports local authentication (and more) provided by your Nuxt server, check out the nuxt-auth module from sidebase (powered by authjs and NextAuth) ➡️ [nuxt-auth](https://github.com/sidebase/nuxt-auth)
3030

31-
## Quick Setup
31+
### Recent breaking changes
3232

33-
### 1. Add `nuxt-oidc-auth` dependency to your project
33+
:warning: Since 0.16.0, the data from the providers userInfo endpoint is written into `userInfo` on the user object instead of `providerInfo`.
34+
Please adjust your `nuxt.config.ts` and .env/environment files and configurations accordingly.
35+
The `useOidcAuth` composable has introduced an alias from `providerInfo` to `userInfo`, but this will be removed in future versions.
36+
37+
## Installation
38+
39+
### Add `nuxt-oidc-auth` dependency to your project
40+
41+
With nuxi
3442

3543
```bash
36-
npx nuxi@latest module add nuxt-oidc-auth
44+
pnpm dlx nuxi@latest module add nuxt-oidc-auth
3745
```
3846

39-
### 2. Add `nuxt-oidc-auth` to the `modules` section of `nuxt.config.ts`
47+
or manually
48+
49+
```bash
50+
pnpm add -D nuxt-oidc-auth
51+
```
52+
53+
Add `nuxt-oidc-auth` to the `modules` section of `nuxt.config.ts`
4054

4155
```js
4256
export default defineNuxtConfig({
@@ -46,15 +60,15 @@ export default defineNuxtConfig({
4660
})
4761
```
4862

49-
### 3. Set secrets
63+
### Set secrets
5064

5165
Nuxt OIDC Auth uses three different secrets to encrypt the user session, the individual auth sessions and the persistent server side token store. You can set them using environment variables or in the `.env` file.
5266
All of the secrets are auto generated if not set, but should be set manually in production. This is especially important for the session storage, as it won't be accessible anymore if the secret changes, for example, after a server restart.
5367

5468
If you need a reference how you could generate random secrets or keys, we created an example as a starting point: [Secrets generation example](https://stackblitz.com/edit/nuxt-oidc-auth-keygen?file=index.js)
5569

56-
- NUXT_OIDC_SESSION_SECRET (random string): This should be a at least 48 characters random string. It is used to encrypt the user session.
5770
- NUXT_OIDC_TOKEN_KEY (random key): This needs to be a random cryptographic AES key in base64. Used to encrypt the server side token store. You can generate a key in JS with `await subtle.exportKey('raw', await subtle.generateKey({ name: 'AES-GCM', length: 256, }, true, ['encrypt', 'decrypt']))`. You just have to encode it to base64 afterwards.
71+
- NUXT_OIDC_SESSION_SECRET (random string): This should be a at least 48 characters random string. It is used to encrypt the user session.
5872
- NUXT_OIDC_AUTH_SESSION_SECRET (random string): This should be a at least 48 characters random string. It is used to encrypt the individual sessions during OAuth flows.
5973

6074
Add a `NUXT_OIDC_SESSION_SECRET` env variable with at least 48 characters in the `.env` file.
@@ -66,7 +80,7 @@ NUXT_OIDC_SESSION_SECRET=48_characters_random_string
6680
NUXT_OIDC_AUTH_SESSION_SECRET=48_characters_random_string
6781
```
6882

69-
### 4. That's it! You can now add authentication to your Nuxt app ✨
83+
That's it! You can now add authentication with a predifined provider or a custom OIDC provider to your Nuxt app ✨
7084

7185
## Supported OpenID Connect Providers
7286

@@ -114,7 +128,6 @@ Nuxt OIDC Auth automatically adds some API routes to interact with the current u
114128
- `loggedIn`
115129
- `user`
116130
- `currentProvider`
117-
- ~~`configuredProviders`~~ - Deprecated
118131
- `fetch`
119132
- `refresh`
120133
- `login`
@@ -181,10 +194,6 @@ The current user object.
181194

182195
The name of the currently logged in provider.
183196

184-
### ~~`configuredProviders` => (string[])~~ - Deprecated due to security concerns (exposes potentially sensitive information)
185-
186-
~~An array that contains the names of the configured providers.~~
187-
188197
### `fetch()` => (void)
189198

190199
Fetches/updates the current user session.
@@ -236,17 +245,17 @@ The `user` object provided by `useOidcAuth` contains the following properties:
236245
| loggedInAt | `number` | Login timestamp in second precision |
237246
| updatedAt | `number` | Refresh timestamp in second precision |
238247
| expireAt | `number` | Session expiration timestamp in second precision. Either `loggedInAt` plus session max age or expiration of access token if available. |
239-
| providerInfo | `Record<string, unknown>` | Additional information coming from the provider's userinfo endpoint |
248+
| userInfo | `Record<string, unknown>` | Additional information coming from the provider's userinfo endpoint |
240249
| userName | `string` | Coming either from the provider or from the configured mapped claim |
241250
| claims | `Record<string, unknown>` | Additional optional claims from the id token, if `optionalClaims` setting is configured. |
242251
| accessToken | `string` | Exposed access token, only existent when `exposeAccessToken` is configured. |
243252
| idToken | `string` | Exposed access token, only existent when `exposeIdToken` is configured. |
244253

245-
You can define the type for your provider info by creating a type declaration file (for example, `auth.d.ts`) in your project:
254+
You can extend the type for your provider info by creating a type declaration file (for example, `auth.d.ts`) in your project:
246255

247256
```ts
248257
declare module '#oidc-auth' {
249-
interface ProviderInfo {
258+
interface UserInfo {
250259
// define the type here e.g.,
251260
providerName: string
252261
}
@@ -406,7 +415,7 @@ export default defineNuxtConfig({
406415
| responseMode | `'query'` \| `'fragment'` \| `'form_post'` \| `string` (optional) | - | Response mode for authentication request |
407416
| authorizationUrl | `string` (optional) | - | Authorization endpoint URL |
408417
| tokenUrl | `string` (optional) | - | Token endpoint URL |
409-
| userinfoUrl | `string` (optional) | '' | Userinfo endpoint URL |
418+
| userInfoUrl | `string` (optional) | '' | Userinfo endpoint URL |
410419
| redirectUri | `string` (optional) | - | Redirect URI |
411420
| grantType | `'authorization_code'` \| 'refresh_token' (optional) | `authorization_code` | Grant Type |
412421
| scope | `string[]` (optional) | `['openid']` | Scope |
@@ -420,12 +429,12 @@ export default defineNuxtConfig({
420429
| tokenRequestType | `'form'` \| `'form-urlencoded'` \| `'json'` (optional) | `'form'` | Token request type |
421430
| audience | `string` (optional) | - | Audience used for token validation (not included in requests by default, use additionalTokenParameters or additionalAuthParameters to add it) |
422431
| requiredProperties | `string[]` | - | Required properties of the configuration that will be validated at runtime. |
423-
| filterUserinfo | `string[]`(optional) | - | Filter userinfo response to only include these properties. |
432+
| filterUserInfo | `string[]`(optional) | - | Filter userinfo response to only include these properties. |
424433
| skipAccessTokenParsing | `boolean` (optional) | - | Skip access token parsing (for providers that don't follow the OIDC spec/don't issue JWT access tokens). |
425434
| logoutRedirectParameterName | `string` (optional) | - | Query parameter name for logout redirect. Will be appended to the logoutUrl as a query parameter. |
426435
| additionalAuthParameters | `Record<string, string>` (optional) | - | Additional parameters to be added to the authorization request. See [Provider specific configurations](#provider-specific-configurations) for possible parameters. |
427436
| additionalTokenParameters | `Record<string, string>` (optional) | - | Additional parameters to be added to the token request. See [Provider specific configurations](#provider-specific-configurations) for possible parameters. |
428-
| baseUrl | `string` (optional) | - | Base URL for the provider, used when to dynamically create authorizationUrl, tokenUrl, userinfoUrl and logoutUrl if possible. |
437+
| baseUrl | `string` (optional) | - | Base URL for the provider, used when to dynamically create authorizationUrl, tokenUrl, userInfoUrl and logoutUrl if possible. |
429438
| openIdConfiguration | `Record<string, unknown>` or `function (config) => Record<string, unknown>` (optional) | - | OpenID Configuration object or function promise that resolves to an OpenID Configuration object. |
430439
| validateAccessToken | `boolean` (optional) | `true` | Validate access token. |
431440
| validateIdToken | `boolean` (optional) | `true` | Validate id token. |
@@ -498,7 +507,7 @@ Make sure to set the callback URL in your OAuth app settings as `<your-domain>/a
498507

499508
### Keycloak
500509

501-
For Keycloak you have to provide at least the `baseUrl`, `clientId` and `clientSecret` properties. The `baseUrl` is used to dynamically create the `authorizationUrl`, `tokenUrl` and `userinfoUrl`.
510+
For Keycloak you have to provide at least the `baseUrl`, `clientId` and `clientSecret` properties. The `baseUrl` is used to dynamically create the `authorizationUrl`, `tokenUrl` and `userInfoUrl`.
502511
Please include the realm you want to use in the `baseUrl` (e.g. `https://<keycloak-url>/realms/<realm>`).
503512
Also remember to enable `Client authentication` to be able to get a client secret.
504513

@@ -510,7 +519,7 @@ The following fields in the returned [user object](#user-object) can be configur
510519
- `claims`: `devMode.claims` setting
511520
- `provider`: `devMode.provider` setting
512521
- `userName`: `devMode.userName` setting
513-
- `providerInfo`: `devMode.providerInfo` setting
522+
- `userInfo`: `devMode.userInfo` setting
514523
- `idToken`: `devMode.idToken` setting
515524
- `accessToken`: `devMode.accessToken` setting
516525

playground/nuxt.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default defineNuxtConfig({
5656
redirectUri: 'http://localhost:3000/auth/github/callback',
5757
clientId: '',
5858
clientSecret: '',
59-
filterUserinfo: ['login', 'id', 'avatar_url', 'name', 'email'],
59+
filterUserInfo: ['login', 'id', 'avatar_url', 'name', 'email'],
6060
},
6161
keycloak: {
6262
audience: 'account',
@@ -81,7 +81,7 @@ export default defineNuxtConfig({
8181
enabled: false,
8282
generateAccessToken: true,
8383
userName: 'Test User',
84-
providerInfo: { providerName: 'test' },
84+
userInfo: { providerName: 'test' },
8585
claims: { customclaim01: 'foo', customclaim02: 'bar' },
8686
issuer: 'dev-issuer',
8787
audience: 'dev-app',

playground/pages/auth/login.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
definePageMeta({
33
layout: 'authentication',
44
})
5-
const { currentProvider, login } = useOidcAuth()
5+
const { currentProvider, login, user } = useOidcAuth()
66
const { providers } = useProviders(currentProvider.value as string)
77
</script>
88

@@ -13,7 +13,7 @@ const { providers } = useProviders(currentProvider.value as string)
1313
:key="index"
1414
class="btn-base btn-login"
1515
:disabled="provider.disabled"
16-
@click="login(provider.name, { test: 'thiswillappearinentra', test2: 'thiswillbeignored' })"
16+
@click="login(provider.name as any, { test: 'thiswillappearinentra', test2: 'thiswillbeignored' })"
1717
>
1818
<span :class="provider.icon" />
1919
<span class="pl-2">{{ provider.label }}</span>

playground/pages/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const { providers } = useProviders(currentProvider.value as string)
2121
:key="index"
2222
class="btn-base btn-login"
2323
:disabled="provider.disabled"
24-
@click="login(provider.name)"
24+
@click="login(provider.name as any)"
2525
>
2626
<span :class="provider.icon" />
2727
<span class="pl-2">{{ provider.label }}</span>
@@ -30,7 +30,7 @@ const { providers } = useProviders(currentProvider.value as string)
3030
<p>Current provider: {{ currentProvider }}</p>
3131
<button
3232
class="btn-base btn-login"
33-
:disabled="!loggedIn || !user.canRefresh"
33+
:disabled="!loggedIn || !user?.canRefresh"
3434
@click="refresh()"
3535
>
3636
<span class="i-majesticons-refresh" />

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default defineNuxtModule<ModuleOptions>({
165165
if (baseUrl) {
166166
(options.providers[provider] as OidcProviderConfig).authorizationUrl = generateProviderUrl(baseUrl as string, providerPresets[provider].authorizationUrl);
167167
(options.providers[provider] as OidcProviderConfig).tokenUrl = generateProviderUrl(baseUrl as string, providerPresets[provider].tokenUrl);
168-
(options.providers[provider] as OidcProviderConfig).userinfoUrl = generateProviderUrl(baseUrl as string, providerPresets[provider].userinfoUrl)
168+
(options.providers[provider] as OidcProviderConfig).userInfoUrl = generateProviderUrl(baseUrl as string, providerPresets[provider].userInfoUrl)
169169
}
170170

171171
// Add login handler

src/runtime/composables/oidcAuth.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ const useSessionState = () => useState<UserSession>('nuxt-oidc-auth-session', un
66

77
export function useOidcAuth() {
88
const sessionState: Ref<UserSession> = useSessionState()
9-
const user: ComputedRef<UserSession> = computed(() => sessionState.value || undefined)
9+
const user: ComputedRef<UserSession | undefined> = computed(() => sessionState.value!
10+
? {
11+
...sessionState.value.userInfo && { providerInfo: sessionState.value.userInfo },
12+
...sessionState.value,
13+
}
14+
: undefined)
1015
const loggedIn: ComputedRef<boolean> = computed<boolean>(() => {
1116
return Boolean(sessionState.value?.expireAt)
1217
})

src/runtime/providers/apple.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type AppleRequiredFields = 'clientId' | 'clientSecret' | 'authorizationUrl' | 't
66
export const apple = defineOidcProvider<OidcProviderConfig, AppleRequiredFields>({
77
authorizationUrl: 'https://appleid.apple.com/auth/oauth2/v2/authorize',
88
tokenUrl: 'https://appleid.apple.com/auth/oauth2/v2/token',
9-
userinfoUrl: '',
9+
userInfoUrl: '',
1010
tokenRequestType: 'json',
1111
responseType: 'code',
1212
authenticationScheme: 'body',

src/runtime/providers/auth0.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const auth0 = defineOidcProvider<Auth0ProviderConfig, Auth0RequiredFields
1515
responseType: 'code',
1616
tokenRequestType: 'json',
1717
authenticationScheme: 'body',
18-
userinfoUrl: 'userinfo',
18+
userInfoUrl: 'userinfo',
1919
grantType: 'authorization_code',
2020
scope: ['openid'],
2121
pkce: true,

src/runtime/providers/github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type GithubRequiredFields = 'clientId' | 'clientSecret' | 'redirectUri'
66
export const github = defineOidcProvider<OidcProviderConfig, GithubRequiredFields>({
77
authorizationUrl: 'https://github.com/login/oauth/authorize',
88
tokenUrl: 'https://github.com/login/oauth/access_token',
9-
userinfoUrl: 'https://api.github.com/user',
9+
userInfoUrl: 'https://api.github.com/user',
1010
tokenRequestType: 'json',
1111
responseType: 'code',
1212
authenticationScheme: 'body',

src/runtime/providers/keycloak.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface KeycloakProviderConfig {
1111
export const keycloak = defineOidcProvider<KeycloakProviderConfig, KeycloakRequiredFields>({
1212
authorizationUrl: 'protocol/openid-connect/auth',
1313
tokenUrl: 'protocol/openid-connect/token',
14-
userinfoUrl: 'protocol/openid-connect/userinfo',
14+
userInfoUrl: 'protocol/openid-connect/userinfo',
1515
tokenRequestType: 'form-urlencoded',
1616
responseType: 'code',
1717
authenticationScheme: 'header',

0 commit comments

Comments
 (0)