Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [sc-130912] Remove token delete/list APIs from particle-api-js #187

Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 3 additions & 22 deletions src/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ const fs = require('../fs');
const packageJson = require('../package.json');

/**
* @typedef {string} AccessToken
*/

/**
* @typedef {object} BasicAuth
* @property {string} username
* @property {string} password
*/

/**
* @typedef {AccessToken | BasicAuth} Auth Prefer using an access token over basic auth for better security
* @typedef {string} Auth Access token to use for the request
*/

/**
Expand Down Expand Up @@ -394,20 +384,11 @@ class Agent {
* @returns {object} The original request.
*/
_getAuthorizationHeader(auth){
if (!auth) {
return {};
}
if (typeof auth === 'string') {
return { Authorization: `Bearer ${auth}` };
}
let encoded;
if (this.isForBrowser()) {
encoded = btoa(`${auth.username}:${auth.password}`);
} else {
encoded = Buffer.from(`${auth.username}:${auth.password}`)
.toString('base64');
}
return { Authorization: `Basic ${encoded}` };

return {};
}

/**
Expand Down
43 changes: 0 additions & 43 deletions src/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,6 @@ class Particle {
});
}

/**
* Revoke an access token
* @param {Object} options Options for this API call
* @param {String} options.username Username of the Particle cloud account that the token belongs to.
* @param {String} options.password Password for the account
* @param {String} options.token Access token you wish to revoke
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
* @returns {Promise} A promise
*/
deleteAccessToken({ username, password, token, headers, context }){
return this.delete({
uri: `/v1/access_tokens/${token}`,
auth: { username, password },
headers,
data: { access_token: token },
context
});
}

/**
* Revoke the current session access token
* @param {Object} options Options for this API call
Expand Down Expand Up @@ -363,26 +343,6 @@ class Particle {
});
}

/**
* List all valid access tokens for a Particle Cloud account
* @param {Object} options Options for this API call
* @param {String} options.username Username
* @param {String} options.password Password
* @param {String} options.otp Current one-time-password generated from the authentication application
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
* @returns {Promise} A promise
*/
listAccessTokens({ username, password, otp, headers, context }){
return this.get({
uri: '/v1/access_tokens',
auth: { username, password },
query: otp ? { otp } : undefined,
headers,
context
});
}

/**
* Retrieves the information that is used to identify the current login for tracking.
* @param {Object} [options] Options for this API call
Expand Down Expand Up @@ -2852,7 +2812,4 @@ class Particle {
}
}

// Aliases for backwards compatibility
Particle.prototype.removeAccessToken = Particle.prototype.deleteAccessToken;

module.exports = Particle;
22 changes: 0 additions & 22 deletions test/Agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,6 @@ describe('Agent', () => {
const headers = agent._getAuthorizationHeader(auth);
expect(headers).to.eql({ Authorization: bearer });
});

if (typeof window !== 'undefined') {
it('supports auth with user/pass in browsers', () => {
const auth = {
username: 'test@particle.io',
password: 'super_secret'
};
const basic = 'Basic dGVzdEBwYXJ0aWNsZS5pbzpzdXBlcl9zZWNyZXQ=';
const headers = agent._getAuthorizationHeader(auth);
expect(headers).to.eql({ Authorization: basic });
});
} else {
it('supports auth with user/pass in node', () => {
const auth = {
username: 'test@particle.io',
password: 'super_secret'
};
const basic = 'Basic dGVzdEBwYXJ0aWNsZS5pbzpzdXBlcl9zZWNyZXQ=';
const headers = agent._getAuthorizationHeader(auth);
expect(headers).to.eql({ Authorization: basic });
});
}
});

describe('request', () => {
Expand Down
56 changes: 0 additions & 56 deletions test/Particle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,6 @@ describe('ParticleAPI', () => {
});
});

describe('.deleteAccessToken', () => {
it('sends request', () => {
return api.deleteAccessToken(props).then((results) => {
results.should.match({
method: 'delete',
uri: `/v1/access_tokens/${props.token}`,
auth: {
username: props.username,
password: props.password
}
});
});
});
});

describe('.deleteCurrentAccessToken', () => {
it('sends request', () => {
return api.deleteCurrentAccessToken(props).then((results) => {
Expand All @@ -428,41 +413,6 @@ describe('ParticleAPI', () => {
});
});

describe('.listAccessTokens', () => {
let options;

beforeEach(() => {
options = {
username: props.username,
password: props.password,
otp: props.otp
};
});

it('sends credentials', () => {
delete options.otp;
return api.listAccessTokens(options)
.then(({ auth, query }) => {
expect(auth).to.be.an('object');
expect(auth).to.have.property('username', options.username);
expect(auth).to.have.property('password', options.password);
expect(query).to.equal(undefined);
});
});

it('includes otp when provided', () => {
return api.listAccessTokens(options)
.then(({ auth, query }) => {
expect(auth).to.be.an('object');
expect(auth).to.have.property('username', options.username);
expect(auth).to.have.property('password', options.password);
expect(query).to.be.an('object');
expect(query).to.have.property('otp', props.otp);
expect(props.otp).to.be.a('string').with.lengthOf(6);
});
});
});

describe('.listDevices', () => {
describe('user scope', () => {
it('generates request', () => {
Expand Down Expand Up @@ -2987,12 +2937,6 @@ describe('ParticleAPI', () => {
});
});

describe('backwards-compatibility function aliases', () => {
it('maps removeAccessToken to deleteAccessToken', () => {
api.removeAccessToken.should.equal(api.deleteAccessToken);
});
});

describe('.deviceUri', () => {
describe('user scope', () => {
it('gets the user device uri', () => {
Expand Down