OAuth2Authorization.vue 3.4 KB

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