Authorization.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div>
  3. <div
  4. class="bg-primary border-dividerLight top-upperSecondaryStickyFold sticky z-10 flex items-center justify-between flex-1 pl-4 border-b"
  5. >
  6. <span class="flex items-center">
  7. <label class="text-secondaryLight font-semibold">
  8. {{ $t("authorization.type") }}
  9. </label>
  10. <tippy
  11. ref="authTypeOptions"
  12. interactive
  13. trigger="click"
  14. theme="popover"
  15. arrow
  16. >
  17. <template #trigger>
  18. <span class="select-wrapper">
  19. <ButtonSecondary
  20. class="pr-8 ml-2 rounded-none"
  21. :label="authName"
  22. />
  23. </span>
  24. </template>
  25. <SmartItem
  26. label="None"
  27. @click.native="
  28. authType = 'none'
  29. $refs.authTypeOptions.tippy().hide()
  30. "
  31. />
  32. <SmartItem
  33. label="Basic Auth"
  34. @click.native="
  35. authType = 'basic'
  36. $refs.authTypeOptions.tippy().hide()
  37. "
  38. />
  39. <SmartItem
  40. label="Bearer Token"
  41. @click.native="
  42. authType = 'bearer'
  43. $refs.authTypeOptions.tippy().hide()
  44. "
  45. />
  46. <SmartItem
  47. label="OAuth 2.0"
  48. @click.native="
  49. authType = 'oauth-2'
  50. $refs.authTypeOptions.tippy().hide()
  51. "
  52. />
  53. </tippy>
  54. </span>
  55. <div class="flex">
  56. <!-- <SmartCheckbox
  57. :on="!URLExcludes.auth"
  58. @change="setExclude('auth', !$event)"
  59. >
  60. {{ $t("authorization.include_in_url") }}
  61. </SmartCheckbox> -->
  62. <SmartCheckbox
  63. :on="authActive"
  64. class="px-2"
  65. @change="authActive = !authActive"
  66. >
  67. {{ $t("state.enabled") }}
  68. </SmartCheckbox>
  69. <ButtonSecondary
  70. v-tippy="{ theme: 'tooltip' }"
  71. to="https://docs.hoppscotch.io/features/authorization"
  72. blank
  73. :title="$t('app.wiki')"
  74. svg="help-circle"
  75. />
  76. <ButtonSecondary
  77. v-tippy="{ theme: 'tooltip' }"
  78. :title="$t('action.clear')"
  79. svg="trash-2"
  80. @click.native="clearContent"
  81. />
  82. </div>
  83. </div>
  84. <div
  85. v-if="authType === 'none'"
  86. class="text-secondaryLight flex flex-col items-center justify-center p-4"
  87. >
  88. <img
  89. :src="`/images/states/${$colorMode.value}/login.svg`"
  90. loading="lazy"
  91. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  92. :alt="$t('empty.authorization')"
  93. />
  94. <span class="pb-4 text-center">
  95. {{ $t("empty.authorization") }}
  96. </span>
  97. <ButtonSecondary
  98. outline
  99. :label="$t('app.documentation')"
  100. to="https://docs.hoppscotch.io/features/authorization"
  101. blank
  102. svg="external-link"
  103. reverse
  104. class="mb-4"
  105. />
  106. </div>
  107. <div v-if="authType === 'basic'" class="border-dividerLight flex border-b">
  108. <div class="border-dividerLight w-2/3 border-r">
  109. <div class="border-dividerLight flex border-b">
  110. <SmartEnvInput
  111. v-model="basicUsername"
  112. :placeholder="$t('authorization.username')"
  113. styles="bg-transparent flex flex-1 py-1 px-4"
  114. />
  115. </div>
  116. <div class="border-dividerLight flex border-b">
  117. <SmartEnvInput
  118. v-model="basicPassword"
  119. :placeholder="$t('authorization.password')"
  120. styles="bg-transparent flex flex-1 py-1 px-4"
  121. />
  122. </div>
  123. </div>
  124. <div
  125. class="bg-primary top-upperTertiaryStickyFold min-w-46 max-w-1/3 z-9 sticky h-full p-4 overflow-auto"
  126. >
  127. <div class="p-2">
  128. <div class="text-secondaryLight pb-2">
  129. {{ $t("helpers.authorization") }}
  130. </div>
  131. <SmartAnchor
  132. class="link"
  133. :label="`${$t('authorization.learn')} \xA0 →`"
  134. to="https://docs.hoppscotch.io/features/authorization"
  135. blank
  136. />
  137. </div>
  138. </div>
  139. </div>
  140. <div v-if="authType === 'bearer'" class="border-dividerLight flex border-b">
  141. <div class="border-dividerLight w-2/3 border-r">
  142. <div class="border-dividerLight flex border-b">
  143. <SmartEnvInput
  144. v-model="bearerToken"
  145. placeholder="Token"
  146. styles="bg-transparent flex flex-1 py-1 px-4"
  147. />
  148. </div>
  149. </div>
  150. <div
  151. class="bg-primary top-upperTertiaryStickyFold min-w-46 max-w-1/3 z-9 sticky h-full p-4 overflow-auto"
  152. >
  153. <div class="p-2">
  154. <div class="text-secondaryLight pb-2">
  155. {{ $t("helpers.authorization") }}
  156. </div>
  157. <SmartAnchor
  158. class="link"
  159. :label="`${$t('authorization.learn')} \xA0 →`"
  160. to="https://docs.hoppscotch.io/features/authorization"
  161. blank
  162. />
  163. </div>
  164. </div>
  165. </div>
  166. <div
  167. v-if="authType === 'oauth-2'"
  168. class="border-dividerLight flex border-b"
  169. >
  170. <div class="border-dividerLight w-2/3 border-r">
  171. <div class="border-dividerLight flex border-b">
  172. <SmartEnvInput
  173. v-model="oauth2Token"
  174. placeholder="Token"
  175. styles="bg-transparent flex flex-1 py-1 px-4"
  176. />
  177. </div>
  178. <HttpOAuth2Authorization />
  179. </div>
  180. <div
  181. class="bg-primary top-upperTertiaryStickyFold min-w-46 max-w-1/3 z-9 sticky h-full p-4 overflow-auto"
  182. >
  183. <div class="p-2">
  184. <div class="text-secondaryLight pb-2">
  185. {{ $t("helpers.authorization") }}
  186. </div>
  187. <SmartAnchor
  188. class="link"
  189. :label="`${$t('authorization.learn')} \xA0 →`"
  190. to="https://docs.hoppscotch.io/features/authorization"
  191. blank
  192. />
  193. </div>
  194. </div>
  195. </div>
  196. </div>
  197. </template>
  198. <script lang="ts">
  199. import { computed, defineComponent, Ref } from "@nuxtjs/composition-api"
  200. import {
  201. HoppRESTAuthBasic,
  202. HoppRESTAuthBearer,
  203. HoppRESTAuthOAuth2,
  204. } from "~/helpers/types/HoppRESTAuth"
  205. import { pluckRef, useStream } from "~/helpers/utils/composables"
  206. import { restAuth$, setRESTAuth } from "~/newstore/RESTSession"
  207. import { useSetting } from "~/newstore/settings"
  208. export default defineComponent({
  209. setup() {
  210. const auth = useStream(
  211. restAuth$,
  212. { authType: "none", authActive: true },
  213. setRESTAuth
  214. )
  215. const authType = pluckRef(auth, "authType")
  216. const authName = computed(() => {
  217. if (authType.value === "basic") return "Basic Auth"
  218. else if (authType.value === "bearer") return "Bearer"
  219. else if (authType.value === "oauth-2") return "OAuth 2.0"
  220. else return "None"
  221. })
  222. const authActive = pluckRef(auth, "authActive")
  223. const basicUsername = pluckRef(auth as Ref<HoppRESTAuthBasic>, "username")
  224. const basicPassword = pluckRef(auth as Ref<HoppRESTAuthBasic>, "password")
  225. const bearerToken = pluckRef(auth as Ref<HoppRESTAuthBearer>, "token")
  226. const oauth2Token = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "token")
  227. const URLExcludes = useSetting("URL_EXCLUDES")
  228. const clearContent = () => {
  229. auth.value = {
  230. authType: "none",
  231. authActive: true,
  232. }
  233. }
  234. return {
  235. auth,
  236. authType,
  237. authName,
  238. authActive,
  239. basicUsername,
  240. basicPassword,
  241. bearerToken,
  242. oauth2Token,
  243. URLExcludes,
  244. clearContent,
  245. }
  246. },
  247. })
  248. </script>