Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
compwright committed Sep 22, 2018
0 parents commit c411f7d
Show file tree
Hide file tree
Showing 12 changed files with 586 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .gitignore
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
tests
.travis.yml
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js

node_js:
- 8
- 10
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Jonathon Hill

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.
106 changes: 106 additions & 0 deletions README.md
@@ -0,0 +1,106 @@
# axios-oauth-client

[![Build Status](https://travis-ci.org/compwright/axios-oauth-client.svg?branch=master)](https://travis-ci.org/compwright/axios-oauth-client)

OAuth 2.0 client utils for axios

## Installation

```bash
$ npm install --save axios-oauth-client axios-token-interceptor axios
```

## Axios OAuth 2.0 Client

### Authorization Code grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getAuthorizationCode = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'authorization_code',
client_id: 'foo',
client_secret: 'bar',
redirect_uri: '...',
code: '...',
scope: 'baz',
});

const auth = await getAuthorizationCode(); // => { "access_token": "...", "expires_in": 900, ... }
```

### Owner Credentials grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getOwnerCredentials = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'password',
client_id: 'foo',
client_secret: 'bar',
username: 'asdf',
password: 'yuio',
scope: 'baz'
});

const auth = await getOwnerCredentials(); // => { "access_token": "...", "expires_in": 900, ... }
```

### Client Credentials grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getClientCredentials = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'client_credentials',
client_id: 'foo',
client_secret: 'bar',
scope: 'baz'
});

const auth = await getClientCredentials(); // => { "access_token": "...", "expires_in": 900, ... }
```

### Refresh Token grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getRefreshToken = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'refresh_token',
client_id: 'foo',
client_secret: 'bar',
refresh_token: '...',
scope: 'baz'
});

const auth = await getRefreshToken(); // => { "access_token": "...", "refresh_token": "...", "expires_in": 900, ... }
```

## Axios OAuth 2.0 Authentication interceptor

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
const tokenProvider = require('axios-token-interceptor');

const getOwnerCredentials = oauth.client(axios.create(), {
// see example above
})

const instance = axios.create();
instance.interceptors.request.use(
// Wraps axios-token-interceptor with oauth-specific configuration,
// fetches the token using the desired claim method, and caches
// until the token expires
oauth.interceptor(tokenProvider, getOwnerCredentials)
);
```

## License

MIT

0 comments on commit c411f7d

Please sign in to comment.