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

Add authenticated property on Kuzzle object #390

merged 19 commits into from
May 25, 2019

Conversation

Aschen
Copy link
Contributor

@Aschen Aschen commented Apr 26, 2019

What does this PR do?

  • Add authenticated property on the Kuzzle object
  • Refactor the Auth controller to keep responsibility for the authentication token

How should this be manually tested?

  • Step 1 :
  • Step 2 :
  • Step 3 :

Other changes

Boyscout

@codecov-io
Copy link

codecov-io commented Apr 26, 2019

Codecov Report

Merging #390 into 6-dev will decrease coverage by 0.12%.
The diff coverage is 92%.

Impacted file tree graph

@@            Coverage Diff             @@
##            6-dev     #390      +/-   ##
==========================================
- Coverage   96.44%   96.31%   -0.13%     
==========================================
  Files          31       32       +1     
  Lines        1489     1519      +30     
==========================================
+ Hits         1436     1463      +27     
- Misses         53       56       +3
Impacted Files Coverage Δ
src/controllers/realtime/index.js 100% <100%> (ø) ⬆️
src/Kuzzle.js 94.59% <81.81%> (-0.54%) ⬇️
src/controllers/auth.js 92.85% <94.11%> (-0.17%) ⬇️
src/core/Jwt.js 95.23% <95.23%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff20858...a800b25. Read the comment docs.

src/Kuzzle.js Outdated
@@ -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

Copy link
Contributor

@scottinet scottinet left a comment

Choose a reason for hiding this comment

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

👍

  • I like the handling the token's expiration time. But I think you need to go further, and add a timeout handler triggering a tokenExpired event whence the token expires

👎

  • isLogged is a bad name, prefer isAuthenticated, or perhaps isLoggedIn
  • I don't see the point of the isLogged function, since it's specified that the jwt property holds a value if a not-expired token is set. Note that I didn't say anything about the token being valid: the SDK doesn't know if the token is still valid unless a call to the API is made, if auth:checkToken is invoked, or if a realtime subscription is active. So, unfortunately, that new function doesn't bring added value to the already existing jwt getter.

src/Kuzzle.js Outdated
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.

Copy link
Contributor

@scottinet scottinet left a comment

Choose a reason for hiding this comment

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

You need to update the jwt setter function, to maintain the new jwtExpiresAt property value

src/Kuzzle.js Outdated
@@ -203,6 +203,10 @@ class Kuzzle extends KuzzleEventEmitter {
return this.protocol.sslConnection;
}

get authenticated () {
return this.jwt !== null && this.jwt !== undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

(nitpicking) according to the jwt setter code, that property can never be null

@Aschen Aschen requested review from scottinet, alexandrebouthinon and Yoann-Abbes and removed request for scottinet May 7, 2019 09:29
@Aschen Aschen changed the title Add isLoggued Add authenticated property on Kuzzle object May 7, 2019
Copy link
Contributor

@scottinet scottinet left a comment

Choose a reason for hiding this comment

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

Minor issues aside, this looks great 👍

src/Kuzzle.js Show resolved Hide resolved
@@ -24,11 +58,15 @@ class AuthController extends BaseController {
* @param {string} token The jwt token to check
* @return {Promise|*|PromiseLike<T>|Promise<T>}
*/
checkToken (token) {
checkToken (token = undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The default parameter is useless: it'll set token to undefined only if it is... undefined.

@@ -24,11 +58,15 @@ class AuthController extends BaseController {
* @param {string} token The jwt token to check
* @return {Promise|*|PromiseLike<T>|Promise<T>}
*/
checkToken (token) {
checkToken (token = undefined) {
if (token === undefined && this.authenticationToken) {
Copy link
Contributor

Choose a reason for hiding this comment

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

(to be discussed) Personnally, I would throw if no token is defined or, perhaps better, I would let Kuzzle respond that it needs a token to be verified (like it was done before your changes)

And if users want to verify the stored token, they only need to use it like this: kuzzle.auth.checkToken(kuzzle.jwt)
Don't forget that the SDK automatically does an auth:checkToken on the stored jwt when a network connection is (re-)established.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know this was the original behaviour but most of the time users want to check if the token stored by the SDK is still valid so IMHO they will understand that if you call this method without a specific token, it will check the SDK internal token.
Also it's more simpler to get ride of the kuzzle.jwt argument.
But maybe I can throw an error if checkToken is called without a token and if there is no internal token

Copy link
Contributor

Choose a reason for hiding this comment

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

The token is already automatically checked by the SDK, but... why not In that case, you have to document it properly in the checkToken SDK documentation

src/core/Jwt.js Outdated Show resolved Hide resolved
test/mocks/generateJwt.mock.js Outdated Show resolved Hide resolved
@@ -24,11 +58,15 @@ class AuthController extends BaseController {
* @param {string} token The jwt token to check
* @return {Promise|*|PromiseLike<T>|Promise<T>}
*/
checkToken (token) {
checkToken (token = undefined) {
if (token === undefined && this.authenticationToken) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The token is already automatically checked by the SDK, but... why not In that case, you have to document it properly in the checkToken SDK documentation

src/core/Jwt.js Outdated Show resolved Hide resolved
@Aschen
Copy link
Contributor Author

Aschen commented May 20, 2019

@Yoann-Abbes and @alexandrebouthinon You might want to review this PR again since I extracted the JWT from Kuzzle object

@Aschen Aschen merged commit 69ea980 into 6-dev May 25, 2019
@Aschen Aschen deleted the add-isLoggued branch May 25, 2019 16:11
scottinet pushed a commit to kuzzleio/documentation that referenced this pull request Jun 11, 2019
## What does this PR do?

Document kuzzleio/sdk-javascript#390

Based on #315

Original PR on v2: #297
@Aschen Aschen mentioned this pull request Jun 14, 2019
Aschen added a commit that referenced this pull request Jun 14, 2019
Release 6.1.2

Bug fixes

    [ #398 ] Fix bulk return (Aschen)
    [ #394 ] Add default values for from/size to document:search (Aschen)
    [ #384 ] Fix search API: "sort" and "search_after" must be in the requests body (scottinet)

Enhancements

    [ #390 ] Add authenticated property on Kuzzle object (Aschen)
    [ #395 ] Proxify kuzzle to avoid mistyping error (thomasarbona)
    [ #389 ] Remove usage of _meta (Aschen)
    [ #391 ] Add isConnected (Aschen)
    [ #388 ] Use BaseController class for controllers (Aschen)
    [ #385 ] Add Security.createRestrictedUser method (Aschen)

Others

    [ #400 ] Fix large document search using scroll (stafyniaksacha)
    [ #387 ] SearchResult.next returns a new instance (Aschen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants