password.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { createHash } from 'crypto';
  2. import JSEncrypt from 'jsencrypt';
  3. import { LocalScheme } from '~auth/runtime';
  4. // const { createHash } = await import('node:crypto');
  5. let publicKey;
  6. const encrypt = new JSEncrypt();
  7. export default class CustomScheme extends LocalScheme {
  8. async login(endpoint, { reset = true } = {}) {
  9. endpoint.data = Object.assign({}, endpoint.data);
  10. if (!this.options.endpoints.login) {
  11. return;
  12. }
  13. if (reset) {
  14. this.$auth.reset({ resetInterceptor: false });
  15. }
  16. if (this.options.clientId) {
  17. endpoint.data.client_id = this.options.clientId;
  18. }
  19. if (this.options.grantType) {
  20. endpoint.data.grant_type = this.options.grantType;
  21. }
  22. if (this.options.scope) {
  23. endpoint.data.scope = this.options.scope;
  24. }
  25. if (!publicKey) {
  26. publicKey = (
  27. await this.$auth.ctx.$axios.$post('/user/v1/client/login/getPbKey')
  28. ).data.publicKey;
  29. encrypt.setPublicKey(publicKey);
  30. }
  31. console.log(
  32. '🚀 ~ file: password.js ~ line 37 ~ CustomScheme ~ login ~ publicKey',
  33. publicKey,
  34. );
  35. const md5 = createHash('md5');
  36. endpoint.data.password = encrypt.encrypt(
  37. md5.update(endpoint.data.password, 'utf-8').digest('hex'),
  38. );
  39. // privateEncrypt(publicKey, endpoint.data.md5).toString('base64');
  40. // endpoint.data.uuid = '132514568481654';
  41. endpoint.data.client = 7;
  42. const response = await this.$auth.request(
  43. endpoint,
  44. this.options.endpoints.login,
  45. );
  46. this.updateTokens(response);
  47. if (!this.requestHandler.interceptor) {
  48. this.initializeRequestInterceptor();
  49. }
  50. if (this.options.user.autoFetch) {
  51. await this.fetchUser();
  52. }
  53. return response;
  54. }
  55. }