Skip to content

Commit

Permalink
refactor: assert issuer properties, state error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Aug 23, 2018
1 parent 6530496 commit 2aed999
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
26 changes: 15 additions & 11 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function assignErrSrc(sourceName) {
}

function authorizationParams(params) {
assert.equal(typeof params, 'object', 'you must provide an object');
assert(_.isPlainObject(params), 'pass a plain object as the first argument');

const authParams = Object.assign(
{ client_id: this.client_id, scope: 'openid', response_type: 'code' },
Expand Down Expand Up @@ -194,6 +194,10 @@ function assertSigningAlgValuesSupport(endpoint, issuer, properties) {
}
}

function assertIssuerConfiguration(issuer, endpoint) {
assert(issuer[endpoint], `${endpoint} must be configured on the issuer`);
}

class Client {
/**
* @name constructor
Expand Down Expand Up @@ -235,7 +239,7 @@ class Client {
* @api public
*/
authorizationUrl(params) {
assert(this.issuer.authorization_endpoint, 'authorization_endpoint must be configured');
assertIssuerConfiguration(this.issuer, 'authorization_endpoint');
const target = url.parse(this.issuer.authorization_endpoint, true);
target.search = null;
Object.assign(target.query, authorizationParams.call(this, params));
Expand Down Expand Up @@ -321,11 +325,11 @@ class Client {
if (this.default_max_age && !checks.max_age) checks.max_age = this.default_max_age;

if (!params.state && checks.state) {
return Promise.reject(new Error('state missing from response'));
return Promise.reject(new Error('state missing from the response'));
}

if (params.state && !checks.state) {
return Promise.reject(new Error('checks.state missing'));
return Promise.reject(new Error('checks.state argument is missing'));
}

if (checks.state !== params.state) {
Expand Down Expand Up @@ -399,11 +403,11 @@ class Client {
const params = _.pick(parameters, CALLBACK_PROPERTIES);

if (!params.state && checks.state) {
return Promise.reject(new Error('state missing from response'));
return Promise.reject(new Error('state missing from the response'));
}

if (params.state && !checks.state) {
return Promise.reject(new Error('checks.state missing'));
return Promise.reject(new Error('checks.state argument is missing'));
}

if (checks.state !== params.state) {
Expand Down Expand Up @@ -744,7 +748,7 @@ class Client {
* @api public
*/
grant(body) {
assert(this.issuer.token_endpoint, 'issuer must be configured with token endpoint');
assertIssuerConfiguration(this.issuer, 'token_endpoint');
return this.authenticatedPost('token', { body: _.omitBy(body, _.isUndefined) })
.then(expectResponseWithBody(200))
.then(response => new TokenSet(JSON.parse(response.body)));
Expand All @@ -755,7 +759,7 @@ class Client {
* @api public
*/
revoke(token, hint) {
assert(this.issuer.revocation_endpoint, 'issuer must be configured with revocation endpoint');
assertIssuerConfiguration(this.issuer, 'revocation_endpoint');
assert(!hint || typeof hint === 'string', 'hint must be a string');

const body = { token };
Expand All @@ -774,7 +778,7 @@ class Client {
* @api public
*/
introspect(token, hint) {
assert(this.issuer.introspection_endpoint, 'issuer must be configured with introspection endpoint');
assertIssuerConfiguration(this.issuer, 'introspection_endpoint');
assert(!hint || typeof hint === 'string', 'hint must be a string');

const body = { token };
Expand Down Expand Up @@ -943,7 +947,7 @@ class Client {
* @api public
*/
static register(properties, { initialAccessToken, keystore } = {}) {
assert(this.issuer.registration_endpoint, 'issuer does not support dynamic registration');
assertIssuerConfiguration(this.issuer, 'registration_endpoint');

if (keystore !== undefined && !(properties.jwks || properties.jwks_uri)) {
checkStore.call(this, keystore);
Expand Down Expand Up @@ -986,7 +990,7 @@ class Client {
* @api public
*/
requestObject(request = {}, algorithms = {}) {
assert.equal(typeof request, 'object', 'pass an object as the first argument');
assert(_.isPlainObject(request), 'pass a plain object as the first argument');

_.defaults(algorithms, {
sign: this.request_object_signing_alg,
Expand Down
8 changes: 4 additions & 4 deletions test/client/client_instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ const encode = object => base64url.encode(JSON.stringify(object));
state: 'should be checked for this',
}).then(fail, (error) => {
expect(error).to.be.instanceof(Error);
expect(error).to.have.property('message', 'checks.state missing');
expect(error).to.have.property('message', 'checks.state argument is missing');
});
});

Expand All @@ -278,7 +278,7 @@ const encode = object => base64url.encode(JSON.stringify(object));
})
.then(fail, (error) => {
expect(error).to.be.instanceof(Error);
expect(error).to.have.property('message', 'state missing from response');
expect(error).to.have.property('message', 'state missing from the response');
});
});

Expand Down Expand Up @@ -479,7 +479,7 @@ const encode = object => base64url.encode(JSON.stringify(object));
state: 'should be checked for this',
}).then(fail, (error) => {
expect(error).to.be.instanceof(Error);
expect(error).to.have.property('message', 'checks.state missing');
expect(error).to.have.property('message', 'checks.state argument is missing');
});
});

Expand All @@ -489,7 +489,7 @@ const encode = object => base64url.encode(JSON.stringify(object));
})
.then(fail, (error) => {
expect(error).to.be.instanceof(Error);
expect(error).to.have.property('message', 'state missing from response');
expect(error).to.have.property('message', 'state missing from the response');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/client/register_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const issuer = new Issuer({
const issuer = new Issuer({}); // eslint-disable-line no-shadow
expect(function () {
issuer.Client.register();
}).to.throw('issuer does not support dynamic registration');
}).to.throw('registration_endpoint must be configured on the issuer');
});

it('accepts and assigns the discovered metadata', function () {
Expand Down

0 comments on commit 2aed999

Please sign in to comment.