Added support for OAuth2 and improved error handling#37
Added support for OAuth2 and improved error handling#37stevenbarash wants to merge 23 commits intomainfrom
Conversation
CLI Authentication sample app
use enchanted link
* Add renovate.json * Update renovate.json --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Omer Cohen <omer@descope.com>
* fixes * readme
* break into different command parameters * set specific versions
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…ed clarity on authentication methods, including OTP and OAuth2. Refactor auth logic to remove JWT caching and streamline output options for session and refresh tokens. Improve error handling and user feedback in CLI commands.
…d the CLI structure by consolidating command definitions into a single `buildProgram` function. Added `test:watch` script for improved testing workflow and included new dependencies for coverage support.
…s dependencies in package.json to their latest versions for improved performance and security. Removed obsolete coverage files and scripts to streamline the project structure.
…tion for OAuth2 login command in README. Improved code documentation in `auth.ts` and `index.ts`, including detailed descriptions for functions and parameters. Streamlined the OAuth2 PKCE flow and ensured proper error handling and resource cleanup during authentication.
There was a problem hiding this comment.
Pull Request Overview
This PR adds OAuth2 authentication with PKCE support and improves error handling for a CLI authentication tool. The changes modernize the codebase with TypeScript, testing infrastructure, and standardized command outputs.
- OAuth2 PKCE login via browser with local HTTP callback server
- New CLI commands for token validation, refresh, and user info retrieval
- Comprehensive test suite and linting/formatting stack
Reviewed Changes
Copilot reviewed 10 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/index.ts | Complete rewrite of CLI program with modular structure and new OAuth2/validation commands |
| src/auth.ts | New OAuth2 PKCE implementation with browser integration and local callback server |
| src/descopeCli.ts | Legacy helper module removed |
| test/cli.test.ts | New test suite covering CLI commands with mocked dependencies |
| test/auth.test.ts | New test suite for OAuth2 flow with HTTP server mocking |
| package.json | Updated dependencies and added test/lint scripts |
| README.md | Comprehensive documentation with command examples and usage instructions |
| .eslintrc.json | ESLint configuration for TypeScript |
| renovate.json | Renovate configuration for dependency updates |
| start.sh | Build step added before running CLI |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (!res.ok) { | ||
| console.log(`Error ${res.error?.errorCode}: ${res.error?.errorDescription}`); |
There was a problem hiding this comment.
This error check uses the wrong response object. It should check jwt instead of res since jwt is the response from the verify operation, not res which is from the signUpOrIn operation.
| if (!res.ok) { | |
| console.log(`Error ${res.error?.errorCode}: ${res.error?.errorDescription}`); | |
| if (!jwt.ok) { | |
| console.log(`Error ${jwt.error?.errorCode}: ${jwt.error?.errorDescription}`); |
| import * as os from "os"; | ||
| import * as path from "path"; | ||
|
|
||
| const LIVE = false; // remove live-mode test functionality |
There was a problem hiding this comment.
This unused constant should be removed since it's not referenced anywhere in the code and the comment suggests it's meant to be removed.
| const LIVE = false; // remove live-mode test functionality |
gaokevin1
left a comment
There was a problem hiding this comment.
Overall looks good, just a few minor comments.
| exec(`xdg-open "${url}"`); | ||
| }; | ||
|
|
||
| export const descopeOAuthLogin = async ( |
There was a problem hiding this comment.
Wondering why we don't use a library here, instead of doing all of this manually?
Is there a particular reason we wouldn't use a common OIDC client (based on Node), other than that we should have one ourselves so it's better to show it this way?
There was a problem hiding this comment.
Good idea - agreed that a standard library would be better
Co-authored-by: Kevin J Gao <32936811+gaokevin1@users.noreply.github.com>
Co-authored-by: Kevin J Gao <32936811+gaokevin1@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Added
login-oauth(opens browser, handles local callback, exchanges code → tokens).-p, --projectId <projectId>(required)-b, --baseUrl <url>(defaulthttps://api.descope.com)-c, --callbackPort <port>(default8088)-o, --output <session|refresh|json>(defaultsession)validate— validate session token; prints{ ok, sub, exp }.validate-and-refresh— validate session; if invalid, refresh with provided refresh token.Changed
login(existed): streamlined tosignUpOrIn.emailthenverify.email; clearer prompts and errors.me(existed upstream or feature-equivalent): standardized to print JSON to stdout and exit with non‑zero on failure.refresh(existed upstream or feature-equivalent): standardized to print the new session JWT to stdout and exit with non‑zero on failure.buildProgram,run) and clearer stdout-focused outputs.test: build then run tests;test:watch.lint: run ESLint (with Prettier integration).start.sh: builds before running the CLI.src/**/*.Removed
Security and hardening
statevalidation to mitigate CSRF/code interception.openid profile email.Dependency updates
@descope/node-sdk→ 1.7.15commander→ 14.0.1node-fetch→ 3.3.2rimraf→ 6.0.1@types/node→ 24.5.1typescript→ 5.9.2eslint→ 9.35.0,@typescript-eslint/*→ 8.44.0prettier→ 3.6.2,eslint-plugin-prettier→ 5.5.4vitest→ 1.6.0Usage (npm)
npm installnpm run buildnode build/index.js login -p <PROJECT_ID> -e <EMAIL>node build/index.js login-oauth -p <PROJECT_ID> -o sessionnode build/index.js login-oauth -p <PROJECT_ID> -o refreshnode build/index.js login-oauth -p <PROJECT_ID> -o jsonnode build/index.js me -p <PROJECT_ID> -r <REFRESH_TOKEN>node build/index.js validate -p <PROJECT_ID> -s <SESSION_JWT>node build/index.js refresh -p <PROJECT_ID> -r <REFRESH_TOKEN>node build/index.js validate-and-refresh -p <PROJECT_ID> -s <SESSION_JWT> -r <REFRESH_TOKEN>Notes
validate/validate-and-refresh, while standardizing outputs and error handling for existing commands.