OAuth2Authorization.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, useContext } from "@nuxtjs/composition-api"
  59. import { pluckRef, useStream } from "~/helpers/utils/composables"
  60. import { HoppRESTAuthOAuth2 } from "~/helpers/types/HoppRESTAuth"
  61. import { restAuth$, setRESTAuth } from "~/newstore/RESTSession"
  62. import { tokenRequest } from "~/helpers/oauth"
  63. export default {
  64. setup() {
  65. const {
  66. $toast,
  67. app: { i18n },
  68. } = useContext()
  69. const $t = i18n.t.bind(i18n)
  70. const auth = useStream(
  71. restAuth$,
  72. { authType: "none", authActive: true },
  73. setRESTAuth
  74. )
  75. const oidcDiscoveryURL = pluckRef(
  76. auth as Ref<HoppRESTAuthOAuth2>,
  77. "oidcDiscoveryURL"
  78. )
  79. const authURL = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "authURL")
  80. const accessTokenURL = pluckRef(
  81. auth as Ref<HoppRESTAuthOAuth2>,
  82. "accessTokenURL"
  83. )
  84. const clientID = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "clientID")
  85. const scope = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "scope")
  86. const handleAccessTokenRequest = async () => {
  87. if (
  88. oidcDiscoveryURL.value === "" &&
  89. (authURL.value === "" || accessTokenURL.value === "")
  90. ) {
  91. $toast.error(`${$t("complete_config_urls")}`, {
  92. icon: "error",
  93. })
  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. icon: "code",
  109. })
  110. }
  111. }
  112. return {
  113. oidcDiscoveryURL,
  114. authURL,
  115. accessTokenURL,
  116. clientID,
  117. scope,
  118. handleAccessTokenRequest,
  119. }
  120. },
  121. }
  122. </script>