Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

[Rest Server] Application token api #3774

Merged
merged 4 commits into from
Nov 1, 2019
Merged

Conversation

sunqinzheng
Copy link
Contributor

@sunqinzheng sunqinzheng commented Oct 25, 2019

Changes

Token format

  • Application will be a JSON Web Token with an additional boolean field application
    • User / frontend service can decode the jwt and get if the token is an application token
  • Since we will query k8s secret when authentication (check if it is revoked), the admin field in jwt is removed. The permission info will be retrieved dynamically from k8s secret

Storage

  • The signed jwt list will be stored in the pai-user-token namespace of k8s secret
  • Each user's token list will be a k8s secret item
    • name: username's hex string
    • data: uuid -> token
  • The namespace will be created (if not exist) when rest server starts

Authentication error

  • Token is expired error message is removed.
  • Only a token is invalid error message will be shown now.

k8s secret model

  • Since previous k8s secret interface is implemented for specific use, a new k8s secret interface (model) for general use is implemented.

API routes

  • GET /api/v1/token
    • Get list of available tokens (portal token + application token)
  • POST /api/v1/token/application
    • Generate an application token
  • DELETE /api/v1/token/:token
    • Revoke a token (portal token / application token)

Test Cases

Token

  1. Login and get the browser token from cookies
  2. Decode the jwt token and the data should not conclude admin field.
  3. Check if the token is stored in k8s secret's pai-user-token namespace as the structure described before.
  4. Revoke the browser token through rest api
  5. View home page through web portal, and "invalid token" alert message will be popped out.

Application Token

Tested in unit test "userToken.js"

@sunqinzheng sunqinzheng marked this pull request as ready for review October 28, 2019 06:42
Copy link
Member

@abuccts abuccts left a comment

Choose a reason for hiding this comment

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

LGTM

token: token,
admin: admin,
hasGitHubPAT: userInfo.extension.hasOwnProperty('githubPAT')&& Boolean(userInfo.extension['githubPAT']),
hasGitHubPAT: userItem.extension.githubPAT,
Copy link
Member

Choose a reason for hiding this comment

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

do not need hasGitHubPAT any more?

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 don't know the current state of github pat feature.
I just keep the original logic and simplify the code.

if (token.username === username && token.application) {
return token;
} else {
throw new Error('Invalid authorization header');
}
Copy link
Contributor

@Binyang2014 Binyang2014 Oct 29, 2019

Choose a reason for hiding this comment

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

Not quiet understand for this line. In which situation, the HTTP authentication header will be "Basic credentials" #Closed

Copy link
Contributor Author

@sunqinzheng sunqinzheng Oct 29, 2019

Choose a reason for hiding this comment

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

Other platforms (github, vso) uses basic authentication for their pats (BASIC username:pat_string).
I provide a similar authentication option here if user is more used to them. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, got it


In reply to: 339970596 [](ancestors = 339970596)

return axios.create(config);
};

const serialize = (object) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Encode data to base64?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an inner function and I think it won't be used by other components.
I prefer using a shorter function name.

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 will add some comments on them to improve code readability

};


const deserialize = (object) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

decode data from base64


const createNamespace = async (namespace) => {
const url = new URL(`/api/v1/namespaces/`, apiserver.uri).href;
const config = {
Copy link
Contributor

@ydye ydye Oct 30, 2019

Choose a reason for hiding this comment

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

Is it deplicate with initClient ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a namespace api with different base uri.
Maybe it should be in another file like k8s-util.js or k8s-namespace.js.
I think we can reorganize the k8s api file structure later.

});
};

const purge = (data) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Delete invalid token?

@sunqinzheng sunqinzheng merged commit b6bd2ab into master Nov 1, 2019
@sunqinzheng sunqinzheng deleted the qinsu/rest/token-api branch November 1, 2019 01:24
@abuccts abuccts mentioned this pull request Nov 25, 2019
42 tasks
@yiyione
Copy link
Contributor

yiyione commented Nov 25, 2019

Token

  • Login and get the browser token from cookies
  • Decode the jwt token and the data should not conclude admin field.
  • Check if the token is stored in k8s secret's pai-user-token namespace as the structure described before.
  • Revoke the browser token through rest api
    🍏 App Token: "Applications are not allowed to do this operation."
    🍏 Browser Token: "revoke successfully"
  • View job list through web portal, and "invalid token" alert message will be popped out.
    🍎 e.forEach is not a function

Application Token

  • Tested in unit test "userToken.js"

@hzy46 hzy46 mentioned this pull request Apr 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants