Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9840423
Merge pull request #1 from descope-sample-apps/init
rishibhargava Dec 3, 2022
4905b74
small fixes
gshriki Dec 3, 2022
c998729
fixes
gshriki Dec 5, 2022
e46f388
fixes
gshriki Dec 5, 2022
448b81b
Merge pull request #2 from descope-sample-apps/small-fixes
gshriki Dec 5, 2022
6b516a5
use enchanted link
gshriki Dec 9, 2022
c4b89a9
Merge pull request #3 from descope-sample-apps/enchanted-link
gshriki Dec 9, 2022
5452541
Configure Renovate (#4)
renovate[bot] Jun 7, 2023
c77f6f7
Code fixes (#7)
gshriki Oct 18, 2023
5061291
break into different command parameters (#9)
gshriki Oct 23, 2023
1dfd477
Update devDependencies to v7 (#10)
renovate[bot] Mar 9, 2024
045743d
chore(git): ignore oauth-implemented-version and untrack from repo
stevenbarash Sep 17, 2025
be442ce
feat(auth): add OAuth2 login command to CLI
stevenbarash Sep 17, 2025
3231224
added oauth 2 support
stevenbarash Sep 17, 2025
934990e
add oauth
stevenbarash Sep 17, 2025
a128659
Enhance README and CLI functionality: Update documentation for improv…
stevenbarash Sep 17, 2025
e6292da
Refactor CLI authentication commands and update test scripts: Enhance…
stevenbarash Sep 17, 2025
68d5efa
Update package dependencies and remove coverage files: Updated variou…
stevenbarash Sep 17, 2025
734f8e0
Enhance README and refactor authentication logic: Added new output op…
stevenbarash Sep 17, 2025
e2e646c
revert to last compatible chalk version
stevenbarash Sep 17, 2025
c3a54bd
Update README.md
stevenbarash Sep 25, 2025
a1deea9
Update README.md
stevenbarash Sep 25, 2025
c513820
Potential fix for code scanning alert no. 5: Log injection
stevenbarash Sep 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules/
.env
build/
.vscode/
# Ignore oauth implementation package
oauth-implemented-version/
126 changes: 119 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,122 @@
# cli-authentication
## Descope CLI Authentication

Sample app to authenticate CLI scripts with Descope methose - OTP, TOTP and Enchanted Links.
This repo showcases how to authenticate users using a Node CLI and the Descope Node SDK.
Authenticate users from the command line using the Descope Node SDK. Supports OTP (email) and OAuth2 (Auth Code Flow with PKCE), token validation/refresh, and fetching user info. OAuth login prints the full session token to stdout; no token cache is used.
Once a user is signed in - you can use the session JWT to send to any backend service to be validated with Descope SDK.

Environment parameters needed:
### 🎨 Features
- **Descope Node SDK**: End-to-end auth from the CLI
- **OTP (Email) Auth**: Signs users in with OTP, creates an account for them if they don't already exist
- **OAuth2 with PKCE**: Browser-based login, local callback server
- **Stdout token output**: `login-oauth` prints the full session token to stdout
- **Commands**: `login`, `login-oauth`, `me`, `validate`, `refresh`, `validate-and-refresh`

|Variable|Description|
|-|-|
| `DESCOPE_PROJECT_ID` | Descope project ID |
| `REDIRECT_URI` | Redirect URI to be used with enchanted links |
### ✨ Made with
- [Descope](https://www.descope.com/)

## ⚙️ Requirements
- Node.js 20.7+ (tested with 20.x)
- npm
- Descope Project ID

## 🚀 Setup
1) Clone the repository
```
git clone https://github.com/descope-sample-apps/cli-authentication.git
cd cli-authentication
```

2) Install dependencies (npm)
```
npm install
```

3) Build
```
npm run build
```

## 🔧 Usage (CLI Commands)
Run commands after building, e.g. `node build/index.js <command> [options]`.

### 1) OTP Email Login
```
node build/index.js login -p <PROJECT_ID> -e <EMAIL>
```
Flow:
- Sends an email OTP using the email OTP function as part of our SDK (no flows)
- Prompts for the code and verifies it.
- Prints the returned JWT payload.

### 2) OAuth2 (PKCE) Login via Browser (prints token to stdout)
```
node build/index.js login-oauth -p <PROJECT_ID> \
[-b <BASE_URL>] [-c <CALLBACK_PORT>] [-o session|refresh|json]
```
- Opens the system browser to authenticate.
- Spins up a local HTTP callback server on `http://localhost:<CALLBACK_PORT>/callback` (default `8088`).
- Exchanges the authorization code for tokens and prints to stdout based on `-o`.

Options:
- `-b, --baseUrl` Descope base URL (default `https://api.descope.com`)
- `-c, --callbackPort` Local callback port (default `8088`)
- `-o, --output` What to print (default `session`). One of:
- `session` — session token only
- `refresh` — refresh token only (if provided by your configuration)
- `json` — both tokens as `{ sessionJwt, refreshJwt }`

Note: Ensure your Descope project allows the redirect URI `http://localhost:<CALLBACK_PORT>/callback`.

### 3) Get Current User (me)
```
node build/index.js me -p <PROJECT_ID> -r <REFRESH_TOKEN>
```
Returns the user info JSON for the provided refresh token.

## 🔑 How to get a refresh token
- OAuth login (recommended):
- Print only the refresh token:
```
node build/index.js login-oauth -p <PROJECT_ID> -o refresh
```
- Or print both tokens as JSON and take `refreshJwt`:
```
node build/index.js login-oauth -p <PROJECT_ID> -o json
```
- If `refreshJwt` is empty, configure your Descope project to issue refresh tokens for OAuth (e.g., enable refresh tokens/consent and include the `offline_access` scope).
- OTP flow:
- After verifying the OTP with `login`, the returned object may include `refreshJwt`.

### 4) Validate Session Token
```
node build/index.js validate -p <PROJECT_ID> -s <SESSION_JWT>
```
Validates a session token and prints the result.

### 5) Refresh Session
```
node build/index.js refresh -p <PROJECT_ID> -r <REFRESH_TOKEN>
```
Exchanges the refresh token for a new session token.

### 6) Validate And Refresh
```
node build/index.js validate-and-refresh -p <PROJECT_ID> -s <SESSION_JWT> -r <REFRESH_TOKEN>
```
Validates the session token; if invalid, refreshes using the provided refresh token.

### Alternative runner
This repo includes `start.sh` for convenience:
```
./start.sh <command> [options]
```
(It builds then runs `node build/index.js`.)

## 🗄️ Token Cache
Token cache is disabled. Tokens are not stored on disk.

## ⚠️ Issue Reporting
Open issues and feature requests at the repository issues page.

## 📜 License
ISC — see `package.json` for details.
Loading