You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.7 KiB

  1. // installable packages
  2. const base64url = require("base64url");
  3. const { v4: uuidv4 } = require("uuid");
  4. const fetch = require("node-fetch").default;
  5. // native packages
  6. const crypto = require("crypto");
  7. const { URL, URLSearchParams } = require("url");
  8. // config
  9. const BASE_URL = "http://localhost:8080";
  10. const REALM = "demorealm";
  11. const REDIRECT_URI = "com.juice.booster3://auth";
  12. const CLIENT_ID = "native-webview";
  13. const sha256encrypt = (code) => {
  14. const base64Digest = crypto
  15. .createHash("sha256")
  16. .update(code)
  17. .digest("base64");
  18. return base64url.fromBase64(base64Digest);
  19. };
  20. const nonceFactory = () => {
  21. const timestamp = Date.now();
  22. return sha256encrypt(timestamp.toString());
  23. };
  24. function generateAuthUrl() {
  25. const nonce = uuidv4();
  26. const state = uuidv4();
  27. const url = new URL(
  28. `${BASE_URL}/auth/realms/${REALM}/protocol/openid-connect/auth`,
  29. );
  30. url.searchParams.append("client_id", CLIENT_ID);
  31. url.searchParams.append("redirect_uri", REDIRECT_URI);
  32. url.searchParams.append("state", state);
  33. url.searchParams.append("response_mode", "fragment");
  34. url.searchParams.append("response_type", "code");
  35. url.searchParams.append("scope", "openid");
  36. url.searchParams.append("nonce", nonce);
  37. console.log(url.href);
  38. }
  39. async function getTokenByUrl(urlString) {
  40. let url = new URL(urlString);
  41. let code = new URLSearchParams(url.hash).get("code");
  42. console.log(`Auth with code is: ${code}`);
  43. url = new URL(
  44. `${BASE_URL}/auth/realms/${REALM}/protocol/openid-connect/token`,
  45. );
  46. const payload = {
  47. client_id: CLIENT_ID,
  48. code,
  49. grant_type: "authorization_code",
  50. redirect_uri: REDIRECT_URI,
  51. };
  52. let response = await fetch(url, {
  53. method: "POST",
  54. headers: {
  55. "Content-Type": "application/x-www-form-urlencoded",
  56. Accept: "*/*",
  57. },
  58. redirect: "manual",
  59. body: new URLSearchParams(payload).toString(),
  60. });
  61. const token = await response.json();
  62. console.log(token);
  63. }
  64. async function refresh(refresh_token) {
  65. const url = new URL(
  66. `${BASE_URL}/auth/realms/${REALM}/protocol/openid-connect/token`,
  67. );
  68. const payload = {
  69. client_id: CLIENT_ID,
  70. refresh_token,
  71. grant_type: "refresh_token",
  72. redirect_uri: REDIRECT_URI,
  73. };
  74. let response = await fetch(url, {
  75. method: "POST",
  76. headers: {
  77. "Content-Type": "application/x-www-form-urlencoded",
  78. Accept: "*/*",
  79. },
  80. redirect: "manual",
  81. body: new URLSearchParams(payload).toString(),
  82. });
  83. const token = await response.json();
  84. console.log(token);
  85. }
  86. if (require.main === module) {
  87. const redirectUri = process.argv[2];
  88. if (redirectUri == null) {
  89. generateAuthUrl();
  90. } else {
  91. getTokenByUrl(redirectUri);
  92. }
  93. }