oauth2-authorize.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import win from "core/window"
  2. import Im from "immutable"
  3. import oauth2Authorize from "core/oauth2-authorize"
  4. import * as utils from "core/utils"
  5. describe("oauth2", () => {
  6. let mockSchema = {
  7. flow: "accessCode",
  8. authorizationUrl: "https://testAuthorizationUrl"
  9. }
  10. let authConfig = {
  11. auth: { schema: { get: (key)=> mockSchema[key] }, scopes: ["scope1", "scope2"] },
  12. authActions: {},
  13. errActions: {},
  14. configs: { oauth2RedirectUrl: "" },
  15. authConfigs: {}
  16. }
  17. let authConfig2 = {
  18. auth: { schema: { get: (key)=> mockSchema[key] }, scopes: Im.List(["scope2","scope3"]) },
  19. authActions: {},
  20. errActions: {},
  21. configs: { oauth2RedirectUrl: "" },
  22. authConfigs: {}
  23. }
  24. beforeEach(() => {
  25. win.open = jest.fn()
  26. })
  27. describe("authorize redirect", () => {
  28. it("should build authorize url", () => {
  29. const windowOpenSpy = jest.spyOn(win, "open")
  30. oauth2Authorize(authConfig)
  31. expect(windowOpenSpy.mock.calls.length).toEqual(1)
  32. expect(windowOpenSpy.mock.calls[0][0]).toMatch("https://testAuthorizationUrl?response_type=code&redirect_uri=&scope=scope1%20scope2&state=")
  33. windowOpenSpy.mockReset()
  34. })
  35. it("should build authorize url relative", function () {
  36. const windowOpenSpy = jest.spyOn(win, "open")
  37. let relativeMockSchema = {
  38. flow: "accessCode",
  39. authorizationUrl: "/testAuthorizationUrl"
  40. }
  41. let relativeAuthConfig = {
  42. auth: { schema: { get: (key) => relativeMockSchema[key] } },
  43. authActions: {},
  44. errActions: {},
  45. configs: { oauth2RedirectUrl: "" },
  46. authConfigs: {},
  47. currentServer: "https://currentserver"
  48. }
  49. oauth2Authorize(relativeAuthConfig)
  50. expect(windowOpenSpy.mock.calls.length).toEqual(1)
  51. expect(windowOpenSpy.mock.calls[0][0]).toMatch("https://currentserver/testAuthorizationUrl?response_type=code&redirect_uri=&state=")
  52. windowOpenSpy.mockReset()
  53. })
  54. it("should append query parameters to authorizeUrl with query parameters", () => {
  55. const windowOpenSpy = jest.spyOn(win, "open")
  56. mockSchema.authorizationUrl = "https://testAuthorizationUrl?param=1"
  57. oauth2Authorize(authConfig)
  58. expect(windowOpenSpy.mock.calls.length).toEqual(1)
  59. expect(windowOpenSpy.mock.calls[0][0]).toMatch("https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&scope=scope1%20scope2&state=")
  60. windowOpenSpy.mockReset()
  61. })
  62. it("should send code_challenge when using authorizationCode flow with usePkceWithAuthorizationCodeGrant enabled", () => {
  63. const windowOpenSpy = jest.spyOn(win, "open")
  64. mockSchema.flow = "authorizationCode"
  65. const expectedCodeVerifier = "mock_code_verifier"
  66. const expectedCodeChallenge = "mock_code_challenge"
  67. const generateCodeVerifierSpy = jest.spyOn(utils, "generateCodeVerifier").mockImplementation(() => expectedCodeVerifier)
  68. const createCodeChallengeSpy = jest.spyOn(utils, "createCodeChallenge").mockImplementation(() => expectedCodeChallenge)
  69. authConfig.authConfigs.usePkceWithAuthorizationCodeGrant = true
  70. oauth2Authorize(authConfig)
  71. expect(win.open.mock.calls.length).toEqual(1)
  72. const actualUrl = new URLSearchParams(win.open.mock.calls[0][0])
  73. expect(actualUrl.get("code_challenge")).toBe(expectedCodeChallenge)
  74. expect(actualUrl.get("code_challenge_method")).toBe("S256")
  75. expect(createCodeChallengeSpy.mock.calls.length).toEqual(1)
  76. expect(createCodeChallengeSpy.mock.calls[0][0]).toBe(expectedCodeVerifier)
  77. // The code_verifier should be stored to be able to send in
  78. // on the TokenUrl call
  79. expect(authConfig.auth.codeVerifier).toBe(expectedCodeVerifier)
  80. // Restore spies
  81. windowOpenSpy.mockReset()
  82. generateCodeVerifierSpy.mockReset()
  83. createCodeChallengeSpy.mockReset()
  84. })
  85. it("should add list of scopes to authorizeUrl", () => {
  86. const windowOpenSpy = jest.spyOn(win, "open")
  87. mockSchema.authorizationUrl = "https://testAuthorizationUrl?param=1"
  88. oauth2Authorize(authConfig2)
  89. expect(windowOpenSpy.mock.calls.length).toEqual(1)
  90. expect(windowOpenSpy.mock.calls[0][0]).toMatch("https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&scope=scope2%20scope3&state=")
  91. windowOpenSpy.mockReset()
  92. })
  93. })
  94. })