Skip to content

Commit

Permalink
Import lukashroch/duo_universal_nodejs
Browse files Browse the repository at this point in the history
Commit 35cd9efb2f6fa78fbe2115b56a3071751966d4ed
  • Loading branch information
jeffreyparker committed Oct 4, 2021
1 parent d8c95e4 commit a95013c
Show file tree
Hide file tree
Showing 37 changed files with 16,684 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
root: true,
env: {
node: true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module',
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
plugins: ['@typescript-eslint', 'prettier'],
rules: {
'prettier/prettier': 'error',
'@typescript-eslint/no-unused-vars': ['warn', { ignoreRestSiblings: true }],
},
};
36 changes: 36 additions & 0 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

steps:
- name: Repository checkout
uses: actions/[email protected]
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
- name: Install latest npm
run: npm install -g npm
- name: Install dependencies
run: npm ci
- name: Run lint
run: npm run lint
- name: Run build
run: npm run build
- name: Run tests
run: npm run test:coverage
- name: Code coverage
if: ${{ matrix.node-version == '14.x' }}
uses: codecov/codecov-action@v1
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
coverage
dist
node_modules

# Log files
npm-debug.log*

# Editor directories and files
.idea
.vscode/*
!.vscode/launch.json
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
4 changes: 4 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
printWidth: 100,
singleQuote: true
};
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Lukas Hroch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
98 changes: 97 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,97 @@
This is a place holder for the README
# Duo Universal Node.js library

[![Build Status](https://github.com/lukashroch/duo_universal_nodejs/workflows/Node.js%20CI/badge.svg)](https://github.com/lukashroch/duo_universal_nodejs/actions/workflows/nodejs-ci.yml)
[![Coverage](https://img.shields.io/codecov/c/github/lukashroch/duo_universal_nodejs.svg)](https://app.codecov.io/gh/lukashroch/duo_universal_nodejs)
![David](https://img.shields.io/david/lukashroch/duo_universal_nodejs)
[![GitHub license](https://img.shields.io/github/license/lukashroch/duo_universal_nodejs)](https://github.com/lukashroch/duo_universal_nodejs/blob/master/LICENSE)

Duo Web v4 SDK - Duo Universal Prompt implementation for Node.js

- follows [Duo Web v4 SDK](https://duo.com/docs/duoweb) implementation
- largely based on Duo Web v4 SDKs for [other languages](https://github.com/duosecurity)

## Installation

```
npm install duo_universal
```

## Usage

Read official Duo Web v4 SDK - Duo Universal Prompt docs (https://duo.com/docs/duoweb) to get familiar with the implementation details.

### 1. Import client

```ts
import { Client } from 'duo_universal';
```

### 2. Create client

Creates new client instance. Provide your Duo Security application credentials and host URL. Include redirect URL to make a way back to your application.

```ts
const client = new Client({
clientId: 'yourDuoApplicationClientId',
clientSecret: 'yourDuoApplicationSecret',
apiHost: 'api-12345678.duosecurity.com',
redirectUrl: 'http://localhost:3000/redirect',
});
```

### 3. Heath check

Determines if Duo’s servers are accessible and available to accept the 2FA request.

```ts
const status = await client.healthCheck();
```

### 4. Generate state

Generates new state (random string) to link the with authentication attempt. Store appropriately, so you can retrieve/compare on callback.

```ts
const state = client.generateState();
```

### 5. Create authentication URL

Creates authentication URL to redirect user to Duo Security Universal prompt. Provide user identifier and state generated in previous step.

```ts
const authUrl = client.createAuthUrl('username', 'state');
```

### 6. Token & code exchange

Exchanges received `duo code` from callback redirect for token result.

```ts
const token = await client.exchangeAuthorizationCodeFor2FAResult('duoCode', 'username');
```

## Example

Complete example of implementation can be found in [example folder](https://github.com/lukashroch/duo_universal_nodejs/tree/master/example). It's a simple express-based application. Please follow the README instructions in `example` folder to spin it up.

## Contribute

Fork the repository

Install dependencies
```
npm install
```

Make your proposed changes. Add tests if applicable, lint the code. Submit a pull request.

## Tests
```
npm test
```

## Lint
```
npm lint
```
4 changes: 4 additions & 0 deletions example/.env-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CLIENT_ID=
CLIENT_SECRET=
API_HOST=
REDIRECT_URL=http://localhost:3000/redirect
15 changes: 15 additions & 0 deletions example/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
root: true,
env: {
node: true,
},
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module',
},
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
plugins: ['prettier'],
rules: {
'prettier/prettier': 'error',
},
};
5 changes: 5 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules

# Local env files
.env*
!.env-template
38 changes: 38 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Duo Universal Node.js example

A simple Node.js web application that serves a logon page integrated with Duo 2FA.

## Setup

Clone the repository
```
git clone https://github.com/lukashroch/duo_universal_nodejs.git
```

Navigate to example folder
```
cd duo_universal_nodejs/example
```

Install dependencies
```
npm install
```

Copy .env-template file
```
cp .env-template .env
```

## Run the application

1. Copy the Client ID, Client Secret, and API Hostname values for your Web SDK application into the .env file

2. Start the application
```
npm run start
```

3. Navigate to http://localhost:3000

4. Log in with enrolled Duo user (any password will work, example doesn't verify first authentication step)
Loading

0 comments on commit a95013c

Please sign in to comment.