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

Add authenticated property on Kuzzle object #390

Merged
merged 19 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 2 additions & 5 deletions features/steps/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ Then(/^the collection(?: '(.*?)')? should exist$/, async function (collection) {
Then('the mapping of {string} should be updated', async function (collection) {
const mapping = await this.kuzzle.collection.getMapping(this.index, collection);

should(mapping[this.index].mappings[collection]).eql({
properties: {
gordon: {type: 'keyword'}
}
should(mapping[this.index].mappings[collection].properties).eql({
gordon: { type: 'keyword' }
});
});

Expand All @@ -129,7 +127,6 @@ Then('the specifications of {string} must not exist', async function (collection
catch (error) {
should(error.status).eql(404);
}

});

Then('they should be validated', function () {
Expand Down
11 changes: 11 additions & 0 deletions src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ class Kuzzle extends KuzzleEventEmitter {
return this.protocol.sslConnection;
}

isLoggued () {
if (!this.jwt) {
return false;
}

return this.jwtExpiresAt > Date.now();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This falls short as soon as the jwt property is set with a custom token (i.e. saved from an earlier connection), which is a very, very common situation.

For this to work, you must also update the jwt setter, decode the token's payload and retrieve its timeout value.

}

/**
* Emit an event to all registered listeners
* An event cannot be emitted multiple times before a timeout has been reached.
Expand Down Expand Up @@ -239,6 +247,7 @@ class Kuzzle extends KuzzleEventEmitter {

this.protocol.addListener('tokenExpired', () => {
this.jwt = undefined;
this.jwtExpiresAt = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just question from a JS noob, what is the difference between:

this.jwtExpiresAt = undefined;

and

delete this.jwtExpiresAt;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this.jwt = undefined the function hasOwnProperty('jwt') will still return true but in this case it's change nothing

this.emit('tokenExpired');
});

Expand Down Expand Up @@ -280,10 +289,12 @@ class Kuzzle extends KuzzleEventEmitter {
// shouldn't obtain an error but let's invalidate the token anyway
if (!res.valid) {
this.jwt = undefined;
this.jwtExpiresAt = undefined;
}
})
.catch(() => {
this.jwt = undefined;
this.jwtExpiresAt = undefined;
})
.then(() => this.emit('reconnected'));
}
Expand Down
17 changes: 10 additions & 7 deletions src/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,19 @@ class AuthController extends BaseController {
throw new Error('Kuzzle.auth.login: strategy is required');
}

const
request = {
strategy,
expiresIn,
body: credentials,
action: 'login'
};
const request = {
strategy,
expiresIn,
body: credentials,
action: 'login'
};

return this.query(request, {queuable: false})
.then(response => {
try {
this.kuzzle.jwt = response.result.jwt;
this.kuzzle.jwtExpiresAt = response.result.expiresAt;

this.kuzzle.emit('loginAttempt', {success: true});
}
catch (err) {
Expand All @@ -180,6 +181,7 @@ class AuthController extends BaseController {
}, {queuable: false})
.then(() => {
this.kuzzle.jwt = undefined;
this.kuzzle.jwtExpiresAt = undefined;
});
}

Expand Down Expand Up @@ -247,6 +249,7 @@ class AuthController extends BaseController {
return this.query(query, options)
.then(response => {
this.kuzzle.jwt = response.result.jwt;
this.kuzzle.jwtExpiresAt = response.result.expiresAt;

return response.result;
});
Expand Down
1 change: 1 addition & 0 deletions src/controllers/realtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class RealTimeController extends BaseController {

this.subscriptions = {};
this.kuzzle.jwt = undefined;
this.kuzzle.jwtExpiresAt = undefined;

const now = Date.now();
if ((now - this.lastExpirationTimestamp) > expirationThrottleDelay) {
Expand Down