OAuth2Authorization.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="flex flex-col">
  3. <div class="border-b border-dividerLight flex">
  4. <input
  5. id="oidcDiscoveryURL"
  6. v-model="oidcDiscoveryURL"
  7. class="bg-transparent flex flex-1 py-2 px-4"
  8. placeholder="OpenID Connect Discovery URL"
  9. name="oidcDiscoveryURL"
  10. />
  11. </div>
  12. <div class="border-b border-dividerLight flex">
  13. <input
  14. id="authURL"
  15. v-model="authURL"
  16. class="bg-transparent flex flex-1 py-2 px-4"
  17. placeholder="Authentication URL"
  18. name="authURL"
  19. />
  20. </div>
  21. <div class="border-b border-dividerLight flex">
  22. <input
  23. id="accessTokenURL"
  24. v-model="accessTokenURL"
  25. class="bg-transparent flex flex-1 py-2 px-4"
  26. placeholder="Access Token URL"
  27. name="accessTokenURL"
  28. />
  29. </div>
  30. <div class="border-b border-dividerLight flex">
  31. <input
  32. id="clientID"
  33. v-model="clientID"
  34. class="bg-transparent flex flex-1 py-2 px-4"
  35. placeholder="Client ID"
  36. name="clientID"
  37. />
  38. </div>
  39. <div class="border-b border-dividerLight flex">
  40. <input
  41. id="scope"
  42. v-model="scope"
  43. class="bg-transparent flex flex-1 py-2 px-4"
  44. placeholder="Scope"
  45. name="scope"
  46. />
  47. </div>
  48. <div class="p-2">
  49. <ButtonSecondary
  50. filled
  51. :label="`${t('authorization.generate_token')}`"
  52. @click.native="handleAccessTokenRequest()"
  53. />
  54. </div>
  55. </div>
  56. </template>
  57. <script lang="ts">
  58. import { Ref } from "@nuxtjs/composition-api"
  59. import { HoppRESTAuthOAuth2 } from "@hoppscotch/data"
  60. import {
  61. pluckRef,
  62. useI18n,
  63. useStream,
  64. useToast,
  65. } from "~/helpers/utils/composables"
  66. import { restAuth$, setRESTAuth } from "~/newstore/RESTSession"
  67. import { tokenRequest } from "~/helpers/oauth"
  68. export default {
  69. setup() {
  70. const t = useI18n()
  71. const toast = useToast()
  72. const auth = useStream(
  73. restAuth$,
  74. { authType: "none", authActive: true },
  75. setRESTAuth
  76. )
  77. const oidcDiscoveryURL = pluckRef(
  78. auth as Ref<HoppRESTAuthOAuth2>,
  79. "oidcDiscoveryURL"
  80. )
  81. const authURL = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "authURL")
  82. const accessTokenURL = pluckRef(
  83. auth as Ref<HoppRESTAuthOAuth2>,
  84. "accessTokenURL"
  85. )
  86. const clientID = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "clientID")
  87. const scope = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "scope")
  88. const handleAccessTokenRequest = async () => {
  89. if (
  90. oidcDiscoveryURL.value === "" &&
  91. (authURL.value === "" || accessTokenURL.value === "")
  92. ) {
  93. toast.error(`${t("complete_config_urls")}`)
  94. return
  95. }
  96. try {
  97. const tokenReqParams = {
  98. grantType: "code",
  99. oidcDiscoveryUrl: oidcDiscoveryURL.value,
  100. authUrl: authURL.value,
  101. accessTokenUrl: accessTokenURL.value,
  102. clientId: clientID.value,
  103. scope: scope.value,
  104. }
  105. await tokenRequest(tokenReqParams)
  106. } catch (e) {
  107. toast.error(`${e}`)
  108. }
  109. }
  110. return {
  111. oidcDiscoveryURL,
  112. authURL,
  113. accessTokenURL,
  114. clientID,
  115. scope,
  116. handleAccessTokenRequest,
  117. t,
  118. }
  119. },
  120. }
  121. </script>