Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Revert auth removal #118

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,29 @@ npm install imgur
const imgur = require('imgur');
```

#### Authentication

First, you must register your application with Imgur if you haven't already done so.

You can set either your client ID or access token (you must perform this flow separately):
#### Dealing with client IDs:

```javascript
// Setting
imgur.setClientId('aCs53GSs4tga0ikp');

// ...or..
// Getting
imgur.getClientId();

// Saving to disk. Returns a promise.
// NOTE: path is optional. Defaults to ~/.imgur
imgur
.saveClientId(path)
.then(() => {
console.log('Saved.');
})
.catch((err) => {
console.log(err.message);
});

imgur.setAccessToken('youraccesstoken');
// Loading from disk
// NOTE: path is optional. Defaults to ~/.imgur
imgur.loadClientId(path).then(imgur.setClientId);
```

#### Dealing with API URL:
Expand Down Expand Up @@ -134,6 +145,15 @@ imgur.setMashapeKey(https://imgur-apiv3.p.mashape.com/);
imgur.getMashapeKey()
```

#### Dealing with credentials:

For when you want to upload images to an account.

```javascript
// Setting
imgur.setCredentials('email@domain.com', 'password', 'aCs53GSs4tga0ikp');
```

#### Uploading files; globbing supported:

```javascript
Expand Down
69 changes: 66 additions & 3 deletions lib/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const VERSION = require('../package.json').version;
let imgurClientId = process.env.IMGUR_CLIENT_ID || 'f0ea04148a54268';
let imgurApiUrl = process.env.IMGUR_API_URL || 'https://api.imgur.com/3/';
let imgurMashapeKey = process.env.IMGUR_MASHAPE_KEY;
let imgurUsername = null;
let imgurPassword = null;
let imgurAccessToken = null;

// An IIFE that returns the OS-specific home directory
Expand Down Expand Up @@ -81,7 +83,7 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => {
throw new Error('Invalid operation');
}

const authorizationHeader = imgur._getAuthorizationHeader();
const authorizationHeader = await imgur._getAuthorizationHeader();

if (imgurMashapeKey) {
options.headers = {
Expand Down Expand Up @@ -132,12 +134,54 @@ imgur._request = async (options) => await got(options).json();
*
* @returns {promise}
*/
imgur._getAuthorizationHeader = () => {
imgur._getAuthorizationHeader = async () => {
if (imgurAccessToken) {
return `Bearer ${imgurAccessToken}`;
}

return `Client-ID ${imgurClientId}`;
if (!(imgurUsername && imgurPassword)) {
return `Client-ID ${imgurClientId}`;
}

const options = {
uri: 'https://api.imgur.com/oauth2/authorize',
method: 'GET',
encoding: 'utf8',
searchParams: {
client_id: imgurClientId,
response_type: 'token',
},
};

let response;

response = await imgur._request(options);
const authorize_token = response.headers['set-cookie'][0].match(
'(^|;)[s]*authorize_token=([^;]*)'
)[2];

options.method = 'POST';
options.form = {
username: imgurUsername,
password: imgurPassword,
allow: authorize_token,
};
options.headers = {
Cookie: 'authorize_token=' + authorize_token,
};

response = await imgur._request(options);
const location = response.headers.location;
const token = JSON.parse(
'{"' +
decodeURI(location.slice(location.indexOf('#') + 1))
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
imgurAccessToken = token.access_token;
return `Bearer ${imgurAccessToken}`;
};

/**
Expand All @@ -151,6 +195,25 @@ imgur.setAccessToken = function (accessToken) {
}
};

/**
* Set your credentials
* @link https://api.imgur.com/#register
* @param {string} username
* @param {string} password
* @param {string} clientId
*/
imgur.setCredentials = (username, password, clientId) => {
if (clientId && typeof clientId === 'string') {
imgurClientId = clientId;
}
if (username && typeof username === 'string') {
imgurUsername = username;
}
if (password && typeof password === 'string') {
imgurPassword = password;
}
};

/**
* Attempt to load the client ID from disk
* @param {string} path - path to file with client id
Expand Down