Skip to content

Commit

Permalink
feat: Allow state-specific arguments to be passed to the returned aut…
Browse files Browse the repository at this point in the history
…horization function rather than the function factory
  • Loading branch information
Jonathon Hill committed Oct 5, 2022
1 parent ca10d60 commit c23458a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 57 deletions.
52 changes: 24 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,66 @@ $ yarn add axios-oauth-client axios
### Authorization Code grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
import axios from 'axios'
import oauth from 'axios-oauth-client'
const getAuthorizationCode = oauth.authorizationCode(
axios.create(),
'https://oauth.com/2.0/token', // OAuth 2.0 token endpoint
'CLIENT_ID',
'CLIENT_SECRET',
'https://your-app.com/oauth-redirect', // Redirect URL for your app
'AUTHORIZATION_CODE',
'OPTIONAL_SCOPES'
);
'https://your-app.com/oauth-redirect' // Redirect URL for your app
)

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

### Owner Credentials grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
import axios from 'axios'
import oauth from 'axios-oauth-client'
const getOwnerCredentials = oauth.ownerCredentials(
axios.create(),
'https://oauth.com/2.0/token', // OAuth 2.0 token endpoint
'CLIENT_ID',
'CLIENT_SECRET',
'USERNAME',
'PASSWORD',
'OPTIONAL_SCOPES'
);
'CLIENT_SECRET'
)

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

### Client Credentials grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
import axios from 'axios'
import oauth from 'axios-oauth-client'
const getClientCredentials = oauth.clientCredentials(
axios.create(),
'https://oauth.com/2.0/token',
'CLIENT_ID',
'CLIENT_SECRET',
'OPTIONAL_SCOPES'
});
'CLIENT_SECRET'
})

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

### Refresh Token grant

```javascript
const axios = require('axios');
const oauth = require('axios-oauth-client');
import axios from 'axios'
import oauth from 'axios-oauth-client'
const getRefreshToken = oauth.refreshToken(
axios.create(),
'https://oauth.com/2.0/token',
'CLIENT_ID',
'CLIENT_SECRET',
'REFRESH_TOKEN',
'OPTIONAL_SCOPES'
);
'CLIENT_SECRET'
)

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

## License
Expand Down
16 changes: 9 additions & 7 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
const qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);

function oauth(axios, { url, ...credentials }) {
const config = {
return (moreCredentials = {}) => axios({
url,
method: "post",
data: qs__default.stringify(credentials)
};
return () => axios(config).then((res) => res.data);
data: qs__default.stringify({
...credentials,
...moreCredentials
})
}).then((res) => res.data);
}
oauth.authorizationCode = function(axios, url, client_id, client_secret, redirect_uri, code, scope = null) {
oauth.authorizationCode = function(axios, url, client_id, client_secret, redirect_uri, code = null, scope = null) {
return oauth(axios, {
url,
grant_type: "authorization_code",
Expand All @@ -25,7 +27,7 @@ oauth.authorizationCode = function(axios, url, client_id, client_secret, redirec
scope
});
};
oauth.ownerCredentials = function(axios, url, client_id, client_secret, username, password, scope = null) {
oauth.ownerCredentials = function(axios, url, client_id, client_secret, username = null, password = null, scope = null) {
return oauth(axios, {
url,
grant_type: "password",
Expand All @@ -45,7 +47,7 @@ oauth.clientCredentials = function(axios, url, client_id, client_secret, scope =
scope
});
};
oauth.refreshToken = function(axios, url, client_id, client_secret, refresh_token, scope = null) {
oauth.refreshToken = function(axios, url, client_id, client_secret, refresh_token = null, scope = null) {
return oauth(axios, {
url,
grant_type: "refresh_token",
Expand Down
16 changes: 9 additions & 7 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import qs from 'qs';

function oauth(axios, { url, ...credentials }) {
const config = {
return (moreCredentials = {}) => axios({
url,
method: "post",
data: qs.stringify(credentials)
};
return () => axios(config).then((res) => res.data);
data: qs.stringify({
...credentials,
...moreCredentials
})
}).then((res) => res.data);
}
oauth.authorizationCode = function(axios, url, client_id, client_secret, redirect_uri, code, scope = null) {
oauth.authorizationCode = function(axios, url, client_id, client_secret, redirect_uri, code = null, scope = null) {
return oauth(axios, {
url,
grant_type: "authorization_code",
Expand All @@ -19,7 +21,7 @@ oauth.authorizationCode = function(axios, url, client_id, client_secret, redirec
scope
});
};
oauth.ownerCredentials = function(axios, url, client_id, client_secret, username, password, scope = null) {
oauth.ownerCredentials = function(axios, url, client_id, client_secret, username = null, password = null, scope = null) {
return oauth(axios, {
url,
grant_type: "password",
Expand All @@ -39,7 +41,7 @@ oauth.clientCredentials = function(axios, url, client_id, client_secret, scope =
scope
});
};
oauth.refreshToken = function(axios, url, client_id, client_secret, refresh_token, scope = null) {
oauth.refreshToken = function(axios, url, client_id, client_secret, refresh_token = null, scope = null) {
return oauth(axios, {
url,
grant_type: "refresh_token",
Expand Down
16 changes: 9 additions & 7 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import qs from 'qs';

function oauth(axios, { url, ...credentials }) {
const config = {
return (moreCredentials = {}) => axios({
url,
method: "post",
data: qs.stringify(credentials)
};
return () => axios(config).then((res) => res.data);
data: qs.stringify({
...credentials,
...moreCredentials
})
}).then((res) => res.data);
}
oauth.authorizationCode = function(axios, url, client_id, client_secret, redirect_uri, code, scope = null) {
oauth.authorizationCode = function(axios, url, client_id, client_secret, redirect_uri, code = null, scope = null) {
return oauth(axios, {
url,
grant_type: "authorization_code",
Expand All @@ -19,7 +21,7 @@ oauth.authorizationCode = function(axios, url, client_id, client_secret, redirec
scope
});
};
oauth.ownerCredentials = function(axios, url, client_id, client_secret, username, password, scope = null) {
oauth.ownerCredentials = function(axios, url, client_id, client_secret, username = null, password = null, scope = null) {
return oauth(axios, {
url,
grant_type: "password",
Expand All @@ -39,7 +41,7 @@ oauth.clientCredentials = function(axios, url, client_id, client_secret, scope =
scope
});
};
oauth.refreshToken = function(axios, url, client_id, client_secret, refresh_token, scope = null) {
oauth.refreshToken = function(axios, url, client_id, client_secret, refresh_token = null, scope = null) {
return oauth(axios, {
url,
grant_type: "refresh_token",
Expand Down
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import qs from 'qs'
/* eslint-disable camelcase */

export default function oauth (axios, { url, ...credentials }) {
const config = {
return (moreCredentials = {}) => axios({
url,
method: 'post',
data: qs.stringify(credentials)
}

return () => axios(config).then(res => res.data)
data: qs.stringify({
...credentials,
...moreCredentials
})
}).then(res => res.data)
}

oauth.authorizationCode = function (axios, url, client_id, client_secret, redirect_uri, code, scope = null) {
oauth.authorizationCode = function (axios, url, client_id, client_secret, redirect_uri, code = null, scope = null) {
return oauth(axios, {
url,
grant_type: 'authorization_code',
Expand All @@ -24,7 +25,7 @@ oauth.authorizationCode = function (axios, url, client_id, client_secret, redire
})
}

oauth.ownerCredentials = function (axios, url, client_id, client_secret, username, password, scope = null) {
oauth.ownerCredentials = function (axios, url, client_id, client_secret, username = null, password = null, scope = null) {
return oauth(axios, {
url,
grant_type: 'password',
Expand All @@ -46,7 +47,7 @@ oauth.clientCredentials = function (axios, url, client_id, client_secret, scope
})
}

oauth.refreshToken = function (axios, url, client_id, client_secret, refresh_token, scope = null) {
oauth.refreshToken = function (axios, url, client_id, client_secret, refresh_token = null, scope = null) {
return oauth(axios, {
url,
grant_type: 'refresh_token',
Expand Down

0 comments on commit c23458a

Please sign in to comment.