Authorization.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <template>
  2. <div>
  3. <div
  4. class="
  5. bg-primary
  6. border-b border-dividerLight
  7. flex flex-1
  8. top-upperSecondaryStickyFold
  9. pl-4
  10. z-10
  11. sticky
  12. items-center
  13. justify-between
  14. "
  15. >
  16. <span class="flex items-center">
  17. <label class="font-semibold text-secondaryLight">
  18. {{ $t("authorization.type") }}
  19. </label>
  20. <tippy
  21. ref="authTypeOptions"
  22. interactive
  23. trigger="click"
  24. theme="popover"
  25. arrow
  26. >
  27. <template #trigger>
  28. <span class="select-wrapper">
  29. <ButtonSecondary
  30. class="rounded-none ml-2 pr-8"
  31. :label="authName"
  32. />
  33. </span>
  34. </template>
  35. <SmartItem
  36. label="None"
  37. @click.native="
  38. authType = 'none'
  39. $refs.authTypeOptions.tippy().hide()
  40. "
  41. />
  42. <SmartItem
  43. label="Basic Auth"
  44. @click.native="
  45. authType = 'basic'
  46. $refs.authTypeOptions.tippy().hide()
  47. "
  48. />
  49. <SmartItem
  50. label="Bearer Token"
  51. @click.native="
  52. authType = 'bearer'
  53. $refs.authTypeOptions.tippy().hide()
  54. "
  55. />
  56. <SmartItem
  57. label="OAuth 2.0"
  58. @click.native="
  59. authType = 'oauth-2'
  60. $refs.authTypeOptions.tippy().hide()
  61. "
  62. />
  63. </tippy>
  64. </span>
  65. <div class="flex">
  66. <!-- <SmartToggle
  67. :on="!URLExcludes.auth"
  68. @change="setExclude('auth', !$event)"
  69. >
  70. {{ $t("authorization.include_in_url") }}
  71. </SmartToggle> -->
  72. <SmartToggle
  73. :on="authActive"
  74. class="px-2"
  75. @change="authActive = !authActive"
  76. >
  77. {{ $t("state.enabled") }}
  78. </SmartToggle>
  79. <ButtonSecondary
  80. v-tippy="{ theme: 'tooltip' }"
  81. to="https://docs.hoppscotch.io/features/authorization"
  82. blank
  83. :title="$t('app.wiki')"
  84. svg="help-circle"
  85. />
  86. <ButtonSecondary
  87. v-tippy="{ theme: 'tooltip' }"
  88. :title="$t('action.clear')"
  89. svg="trash-2"
  90. @click.native="clearContent"
  91. />
  92. </div>
  93. </div>
  94. <div
  95. v-if="authType === 'none'"
  96. class="flex flex-col text-secondaryLight p-4 items-center justify-center"
  97. >
  98. <span class="text-center pb-4">
  99. {{ $t("empty.authorization") }}
  100. </span>
  101. <ButtonSecondary
  102. outline
  103. :label="$t('app.documentation')"
  104. to="https://docs.hoppscotch.io"
  105. blank
  106. svg="external-link"
  107. reverse
  108. />
  109. </div>
  110. <div v-if="authType === 'basic'" class="border-b border-dividerLight flex">
  111. <div class="border-r border-dividerLight w-2/3">
  112. <div
  113. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  114. >
  115. <SmartEnvInput
  116. v-if="EXPERIMENTAL_URL_BAR_ENABLED"
  117. v-model="basicUsername"
  118. class="bg-transparent flex flex-1 py-1 px-4"
  119. :placeholder="$t('authorization.username')"
  120. />
  121. <input
  122. v-else
  123. id="http_basic_user"
  124. v-model="basicUsername"
  125. class="bg-transparent flex flex-1 py-2 px-4"
  126. :placeholder="$t('authorization.username')"
  127. name="http_basic_user"
  128. />
  129. </div>
  130. <div
  131. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  132. >
  133. <SmartEnvInput
  134. v-if="EXPERIMENTAL_URL_BAR_ENABLED"
  135. v-model="basicPassword"
  136. class="bg-transparent flex flex-1 py-1 px-4"
  137. :placeholder="$t('authorization.password')"
  138. />
  139. <input
  140. v-else
  141. id="http_basic_passwd"
  142. v-model="basicPassword"
  143. class="bg-transparent flex flex-1 py-2 px-4"
  144. :placeholder="$t('authorization.password')"
  145. name="http_basic_passwd"
  146. :type="passwordFieldType"
  147. />
  148. <span>
  149. <ButtonSecondary
  150. :svg="passwordFieldType === 'text' ? 'eye' : 'eye-off'"
  151. @click.native="switchVisibility"
  152. />
  153. </span>
  154. </div>
  155. </div>
  156. <div
  157. class="
  158. bg-primary
  159. h-full
  160. top-upperTertiaryStickyFold
  161. min-w-46
  162. max-w-1/3
  163. p-4
  164. z-9
  165. sticky
  166. overflow-auto
  167. "
  168. >
  169. <div class="p-2">
  170. <div class="text-secondaryLight pb-2">
  171. {{ $t("helpers.authorization") }}
  172. </div>
  173. <SmartAnchor
  174. class="link"
  175. :label="`${$t('authorization.learn')} \xA0 →`"
  176. to="https://docs.hoppscotch.io/"
  177. blank
  178. />
  179. </div>
  180. </div>
  181. </div>
  182. <div v-if="authType === 'bearer'" class="border-b border-dividerLight flex">
  183. <div class="border-r border-dividerLight w-2/3">
  184. <div
  185. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  186. >
  187. <SmartEnvInput
  188. v-if="EXPERIMENTAL_URL_BAR_ENABLED"
  189. v-model="bearerToken"
  190. class="bg-transparent flex flex-1 py-1 px-4"
  191. placeholder="Token"
  192. />
  193. <input
  194. v-else
  195. id="bearer_token"
  196. v-model="bearerToken"
  197. class="bg-transparent flex flex-1 py-2 px-4"
  198. placeholder="Token"
  199. name="bearer_token"
  200. />
  201. </div>
  202. </div>
  203. <div
  204. class="
  205. bg-primary
  206. h-full
  207. top-upperTertiaryStickyFold
  208. min-w-46
  209. max-w-1/3
  210. p-4
  211. z-9
  212. sticky
  213. overflow-auto
  214. "
  215. >
  216. <div class="p-2">
  217. <div class="text-secondaryLight pb-2">
  218. {{ $t("helpers.authorization") }}
  219. </div>
  220. <SmartAnchor
  221. class="link"
  222. :label="`${$t('authorization.learn')} \xA0 →`"
  223. to="https://docs.hoppscotch.io/"
  224. blank
  225. />
  226. </div>
  227. </div>
  228. </div>
  229. <div
  230. v-if="authType === 'oauth-2'"
  231. class="border-b border-dividerLight flex"
  232. >
  233. <div class="border-r border-dividerLight w-2/3">
  234. <div
  235. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  236. >
  237. <SmartEnvInput
  238. v-if="EXPERIMENTAL_URL_BAR_ENABLED"
  239. v-model="oauth2Token"
  240. class="bg-transparent flex flex-1 py-1 px-4"
  241. placeholder="Token"
  242. />
  243. <input
  244. v-else
  245. id="oauth2_token"
  246. v-model="oauth2Token"
  247. class="bg-transparent flex flex-1 py-2 px-4"
  248. placeholder="Token"
  249. name="oauth2_token"
  250. />
  251. </div>
  252. <HttpOAuth2Authorization />
  253. </div>
  254. <div
  255. class="
  256. bg-primary
  257. h-full
  258. top-upperTertiaryStickyFold
  259. min-w-46
  260. max-w-1/3
  261. p-4
  262. z-9
  263. sticky
  264. overflow-auto
  265. "
  266. >
  267. <div class="p-2">
  268. <div class="text-secondaryLight pb-2">
  269. {{ $t("helpers.authorization") }}
  270. </div>
  271. <SmartAnchor
  272. class="link"
  273. :label="`${$t('authorization.learn')} \xA0 →`"
  274. to="https://docs.hoppscotch.io/"
  275. blank
  276. />
  277. </div>
  278. </div>
  279. </div>
  280. </div>
  281. </template>
  282. <script lang="ts">
  283. import { computed, defineComponent, Ref, ref } from "@nuxtjs/composition-api"
  284. import {
  285. HoppRESTAuthBasic,
  286. HoppRESTAuthBearer,
  287. HoppRESTAuthOAuth2,
  288. } from "~/helpers/types/HoppRESTAuth"
  289. import { pluckRef, useStream } from "~/helpers/utils/composables"
  290. import { restAuth$, setRESTAuth } from "~/newstore/RESTSession"
  291. import { useSetting } from "~/newstore/settings"
  292. export default defineComponent({
  293. setup() {
  294. const auth = useStream(
  295. restAuth$,
  296. { authType: "none", authActive: true },
  297. setRESTAuth
  298. )
  299. const authType = pluckRef(auth, "authType")
  300. const authName = computed(() => {
  301. if (authType.value === "basic") return "Basic Auth"
  302. else if (authType.value === "bearer") return "Bearer"
  303. else if (authType.value === "oauth-2") return "OAuth 2.0"
  304. else return "None"
  305. })
  306. const authActive = pluckRef(auth, "authActive")
  307. const basicUsername = pluckRef(auth as Ref<HoppRESTAuthBasic>, "username")
  308. const basicPassword = pluckRef(auth as Ref<HoppRESTAuthBasic>, "password")
  309. const bearerToken = pluckRef(auth as Ref<HoppRESTAuthBearer>, "token")
  310. const oauth2Token = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "token")
  311. const URLExcludes = useSetting("URL_EXCLUDES")
  312. const passwordFieldType = ref("password")
  313. const clearContent = () => {
  314. auth.value = {
  315. authType: "none",
  316. authActive: true,
  317. }
  318. }
  319. const switchVisibility = () => {
  320. if (passwordFieldType.value === "text")
  321. passwordFieldType.value = "password"
  322. else passwordFieldType.value = "text"
  323. }
  324. return {
  325. auth,
  326. authType,
  327. authName,
  328. authActive,
  329. basicUsername,
  330. basicPassword,
  331. bearerToken,
  332. oauth2Token,
  333. URLExcludes,
  334. passwordFieldType,
  335. clearContent,
  336. switchVisibility,
  337. EXPERIMENTAL_URL_BAR_ENABLED: useSetting("EXPERIMENTAL_URL_BAR_ENABLED"),
  338. }
  339. },
  340. })
  341. </script>