fix(auth0-express): improve authRequired and middleware documentation#7
fix(auth0-express): improve authRequired and middleware documentation#7frederikprijck wants to merge 1 commit into
Conversation
f3f3d9f to
a022b69
Compare
✅ claudelint — All checks passedFull report |
28623cd to
5459b8b
Compare
✅ skillsaw — All checks passedFull report
|
✅ Action performedReview finished.
|
📝 WalkthroughWalkthroughDocumentation-only updates to three Auth0 Express skill markdown files. Changes correct the ChangesAuth0 Express Skill Documentation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsStopped waiting for pipeline failures after 30000ms. One of your pipelines takes longer than our 30000ms fetch window to run, so review may not consider pipeline-failure results for inline comments if any failures occurred after the fetch window. Increase the timeout if you want to wait longer or run a Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@plugins/auth0/skills/auth0-express/references/api.md`:
- Around line 207-248: The Authorization Code Flow section's code example with
response_type: 'code' is incomplete regarding refresh token availability. Update
the code example in the Authorization Code Flow section to include the scope
parameter in authorizationParams, explicitly adding the 'offline_access' scope.
This scope is required to request and receive refresh tokens from Auth0 and
should be combined with other standard scopes like 'openid profile email' to
demonstrate a complete and correct configuration.
In `@plugins/auth0/skills/auth0-express/references/integration.md`:
- Around line 174-196: Add clarification to the code example showing automatic
token refresh to document that the offline_access scope must be included during
initial authentication for the refresh() method to work properly. This note
should be placed before or within the code example to make it clear that without
the offline_access scope, the refresh token will not be available and the
refresh() method cannot successfully obtain a new access token.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: a5fc7ed9-b813-4ad3-90be-69acbae26316
📒 Files selected for processing (3)
plugins/auth0/skills/auth0-express/SKILL.mdplugins/auth0/skills/auth0-express/references/api.mdplugins/auth0/skills/auth0-express/references/integration.md
| ## Response Type Options | ||
|
|
||
| The `response_type` parameter controls the authentication flow: | ||
|
|
||
| ### Authorization Code Flow | ||
|
|
||
| ```javascript | ||
| app.use(auth({ | ||
| authorizationParams: { | ||
| response_type: 'code' // when needing access tokens for APIs, requires clientSecret. | ||
| }, | ||
| clientSecret: process.env.CLIENT_SECRET | ||
| })); | ||
| ``` | ||
|
|
||
| **Benefits:** | ||
| - Most secure option for server-side applications that need access tokens | ||
| - Access token never exposed to browser | ||
| - Enables refresh tokens | ||
| - Required for API access | ||
|
|
||
| **Requirements:** | ||
| - `clientSecret` must be configured | ||
|
|
||
| ### Implicit Flow (Default) | ||
|
|
||
| ```javascript | ||
| app.use(auth({ | ||
| authorizationParams: { | ||
| response_type: 'id_token' // Default if not specified | ||
| } | ||
| })); | ||
| ``` | ||
|
|
||
| **Characteristics:** | ||
| - Default behavior if `response_type` not specified | ||
| - No `clientSecret` required | ||
| - ID token only (no access token for APIs) | ||
| - Simpler for authentication-only use cases | ||
|
|
||
| **Note:** For calling APIs, use `response_type: 'code'` to receive access tokens. | ||
|
|
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
What is the default response_type in express-openid-connect if authorizationParams.response_type is not specified?
💡 Result:
In the express-openid-connect library, the default value for response_type within authorizationParams is 'id_token' [1][2]. When authorizationParams is not explicitly provided by your application, the library defaults the entire object to [1][3]: { response_type: 'id_token', response_mode: 'form_post', scope: 'openid profile email' } This configuration uses the Implicit Flow [4][2]. If you override authorizationParams and specify a different response_type (such as 'code'), you are responsible for ensuring that all other required parameters, such as the appropriate scope and response_mode, are correctly set [1][2].
Citations:
- 1: https://auth0.github.io/express-openid-connect/interfaces/ConfigParams.html
- 2: https://deepwiki.com/auth0/express-openid-connect/4.3-authentication-parameters
- 3: https://github.com/auth0/express-openid-connect/blob/4824f378/index.d.ts
- 4: https://auth0.com/blog/auth0-s-express-openid-connect-sdk/
🌐 Web query:
express-openid-connect authorization code flow refresh tokens
💡 Result:
The express-openid-connect middleware supports refresh tokens within the Authorization Code flow [1][2]. To utilize them, you must configure your application to request the offline_access scope [3][4][2]. Configuration Requirements: 1. Authorization Code Flow: You must set authorizationParams.response_type to 'code' (or 'code id_token') in your configuration [4][2]. This requires a clientSecret to be provided [3][5][2]. 2. Scope: You must include 'offline_access' in your authorizationParams.scope [3][2]. Once these conditions are met, the middleware will store the refresh token in the session [2][6]. You can access it via req.oidc.refreshToken [7][6]. Token Refresh Process: To refresh an expired access token, you should use the refresh method available on the req.oidc.accessToken object [4][8]. Example Usage: app.get('/', async (req, res) => { let { access_token, isExpired, refresh } = req.oidc.accessToken; if (isExpired) { ({ access_token } = await refresh); } // Proceed with authorized API call using the (possibly refreshed) access_token }); The refresh method performs the following steps: 1. Validates that a refresh token is available [4][6]. 2. Calls the Identity Provider's token endpoint to exchange the refresh token for new tokens [4][2][6]. 3. Automatically updates the user's session with the new access token and any other received tokens (e.g., new refresh token or ID token) [4][2][6]. 4. Returns the new AccessToken object [5][8][6]. If the authorization server does not return a new refresh token, the existing one is preserved in the session for future use [2][6].
Citations:
- 1: https://deepwiki.com/auth0/express-openid-connect/5-authentication-flows
- 2: https://deepwiki.com/auth0/express-openid-connect/8-token-management
- 3: https://github.com/auth0/express-openid-connect/blob/master/EXAMPLES.md
- 4: https://deepwiki.com/auth0/express-openid-connect/8.2-token-refresh
- 5: https://deepwiki.com/auth0/express-openid-connect/8.3-calling-external-apis
- 6: https://deepwiki.com/auth0/express-openid-connect/3.2-request-and-response-contexts
- 7: https://auth0.github.io/express-openid-connect/interfaces/RequestContext.html
- 8: https://auth0.github.io/express-openid-connect/interfaces/AccessToken.html
🌐 Web query:
express-openid-connect response_type code access token configuration
💡 Result:
To configure express-openid-connect to obtain an access token, you must use the Authorization Code flow by setting the response_type to 'code' within the authorizationParams of your middleware configuration [1][2][3]. Key requirements for this configuration include: 1. Response Type: You must set authorizationParams.response_type to 'code' (or 'code id_token') [4][3][5]. The default configuration uses 'id_token', which does not provide an access token [1][4]. 2. Audience: You must specify an audience parameter in authorizationParams to identify the target API for which the access token is requested [1][6][3]. 3. Scope: Your scope must include 'openid' along with any required API-specific permissions [1][6][3]. Including 'offline_access' in the scope will also allow you to obtain a refresh token [2][6][5]. 4. Client Secret: Using 'code' as the response_type requires you to provide a valid client secret in your main configuration [2][7][3]. Example configuration: app.use( auth({ authorizationParams: { response_type: 'code', audience: 'https://your-api-identifier', scope: 'openid profile email read:reports', }, // Ensure clientSecret is provided in your environment or config })); Once configured, the access token is automatically made available on the request context as req.oidc.accessToken [2][3][5]. You can access it in your routes as follows: app.get('/api-call', requiresAuth, async (req, res) => { const { token_type, access_token } = req.oidc.accessToken; // Use access_token to call your API }); If you include 'offline_access' in your scope, you can also access a refresh method on the req.oidc.accessToken object to obtain new access tokens when the current one expires [2][7][5].
Citations:
- 1: https://auth0.github.io/express-openid-connect/interfaces/ConfigParams.html
- 2: https://github.com/auth0/express-openid-connect/blob/master/EXAMPLES.md
- 3: https://deepwiki.com/auth0/express-openid-connect/8.3-calling-external-apis
- 4: https://deepwiki.com/auth0/express-openid-connect/4.3-authentication-parameters
- 5: https://deepwiki.com/auth0/express-openid-connect/8-token-management
- 6: https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-express/references/integration.md
- 7: https://github.com/auth0/express-openid-connect/blob/4824f378/EXAMPLES.md
Add offline_access scope requirement to Authorization Code Flow section.
The documentation correctly identifies the default response_type as 'id_token' and accurately describes the authorization code flow benefits. However, the Authorization Code Flow section (lines 207–220) incompletely describes refresh token availability. Refresh tokens are not automatically provided with response_type: 'code'—the 'offline_access' scope must be explicitly included in authorizationParams.scope to request and receive a refresh token.
Update the code example to demonstrate the complete configuration:
app.use(auth({
authorizationParams: {
response_type: 'code',
scope: 'openid profile email offline_access' // offline_access required for refresh token
},
clientSecret: process.env.CLIENT_SECRET
}));🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@plugins/auth0/skills/auth0-express/references/api.md` around lines 207 - 248,
The Authorization Code Flow section's code example with response_type: 'code' is
incomplete regarding refresh token availability. Update the code example in the
Authorization Code Flow section to include the scope parameter in
authorizationParams, explicitly adding the 'offline_access' scope. This scope is
required to request and receive refresh tokens from Auth0 and should be combined
with other standard scopes like 'openid profile email' to demonstrate a complete
and correct configuration.
| **Automatic Token Refresh:** | ||
|
|
||
| The `req.oidc.accessToken` object includes methods to check expiration and refresh: | ||
|
|
||
| // Access refresh token | ||
| app.get('/refresh', requiresAuth(), (req, res) => { | ||
| ```javascript | ||
| app.get('/api', requiresAuth(), async (req, res) => { | ||
| let { access_token, isExpired, refresh } = req.oidc.accessToken; | ||
|
|
||
| // Check if token is expired | ||
| if (isExpired()) { | ||
| // Automatically refresh and get new token | ||
| const result = await refresh(); | ||
| access_token = result.access_token; | ||
| } | ||
|
|
||
| // Use fresh access token | ||
| const response = await fetch('https://api.example.com/data', { | ||
| headers: { Authorization: `Bearer ${access_token}` } | ||
| }); | ||
|
|
||
| res.json(await response.json()); | ||
| }); | ||
| ``` |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
In express-openid-connect, are isExpired() and refresh() callable methods on the accessToken object? What does refresh() return?
💡 Result:
Yes, isExpired and refresh are both callable methods on the accessToken object in express-openid-connect [1][2]. isExpired: This method returns a boolean value (true if the access token has expired, false otherwise) [1][2]. It internally delegates to the underlying openid-client library's check of the access token's expiration timestamp [3][4]. refresh: This method performs a refresh token grant exchange with the identity provider and updates the session with the new tokens [1][3]. It returns a Promise that resolves to the new AccessToken object [1][2]. When called, it updates the session's access token, preserves relevant data like the ID token or refresh token if not returned, and clears the cached TokenSet to ensure the updated information is used subsequently [3][4]. Note that for this method to be functional, a refresh token must have been obtained during the initial authentication (typically by including the offline_access scope) [3][4][5].
Citations:
- 1: https://auth0.github.io/express-openid-connect/interfaces/AccessToken.html
- 2: https://github.com/auth0/express-openid-connect/blob/master/index.d.ts
- 3: https://deepwiki.com/auth0/express-openid-connect/8.2-token-refresh
- 4: https://deepwiki.com/auth0/express-openid-connect/8-token-management
- 5: https://auth0.com/docs/quickstart/webapp/express
Add a note about the offline_access scope requirement.
The isExpired() and refresh() methods are correctly documented as callable methods on the accessToken object, and refresh() correctly returns a Promise that resolves to an AccessToken object with the access_token property. However, the code example omits a critical prerequisite: the refresh token is only available if the offline_access scope was included during initial authentication. Update the example to clarify this requirement.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@plugins/auth0/skills/auth0-express/references/integration.md` around lines
174 - 196, Add clarification to the code example showing automatic token refresh
to document that the offline_access scope must be included during initial
authentication for the refresh() method to work properly. This note should be
placed before or within the code example to make it clear that without the
offline_access scope, the refresh token will not be available and the refresh()
method cannot successfully obtain a new access token.
Description
This PR improves the
auth0-expressskill documentation in three areas:Bug fix:
authRequiredwas incorrectly documented as defaulting tofalse; the actual default inexpress-openid-connectistrue. Fixed in SKILL.md, api.md, and the Quick Reference section.Missing request properties:
req.oidc.idTokenClaims— decoded ID token claims objectreq.oidc.fetchUserInfo()— fetch additional user info from the/userinfoendpointreq.oidc.idTokenandreq.oidc.refreshTokentypes (string)New documentation:
requiresAuth(),claimEquals(),claimIncludes(),claimCheck()integration.mdwith examples for each helperfetchUserInfo()integration example inintegration.mdisExpired()andrefresh()on theaccessTokenobjectapi.mdexplaining code vs id_token flowsReferences
https://github.com/auth0/express-openid-connect/blob/master/lib/config.js#L215
Checklist
Summary by CodeRabbit