Authorization.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. <!-- <SmartCheckbox
  67. :on="!URLExcludes.auth"
  68. @change="setExclude('auth', !$event)"
  69. >
  70. {{ $t("authorization.include_in_url") }}
  71. </SmartCheckbox> -->
  72. <SmartCheckbox
  73. :on="authActive"
  74. class="px-2"
  75. @change="authActive = !authActive"
  76. >
  77. {{ $t("state.enabled") }}
  78. </SmartCheckbox>
  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. <img
  99. :src="`/images/states/${$colorMode.value}/login.svg`"
  100. loading="lazy"
  101. class="flex-col my-4 object-contain object-center h-16 w-16 inline-flex"
  102. :alt="$t('empty.authorization')"
  103. />
  104. <span class="text-center pb-4">
  105. {{ $t("empty.authorization") }}
  106. </span>
  107. <ButtonSecondary
  108. outline
  109. :label="$t('app.documentation')"
  110. to="https://docs.hoppscotch.io"
  111. blank
  112. svg="external-link"
  113. reverse
  114. class="mb-4"
  115. />
  116. </div>
  117. <div v-if="authType === 'basic'" class="border-b border-dividerLight flex">
  118. <div class="border-r border-dividerLight w-2/3">
  119. <div class="border-b border-dividerLight flex">
  120. <SmartEnvInput
  121. v-model="basicUsername"
  122. :placeholder="$t('authorization.username')"
  123. styles="bg-transparent flex flex-1 py-1 px-4"
  124. />
  125. </div>
  126. <div class="border-b border-dividerLight flex">
  127. <SmartEnvInput
  128. v-model="basicPassword"
  129. :placeholder="$t('authorization.password')"
  130. styles="bg-transparent flex flex-1 py-1 px-4"
  131. />
  132. </div>
  133. </div>
  134. <div
  135. class="
  136. bg-primary
  137. h-full
  138. top-upperTertiaryStickyFold
  139. min-w-46
  140. max-w-1/3
  141. p-4
  142. z-9
  143. sticky
  144. overflow-auto
  145. "
  146. >
  147. <div class="p-2">
  148. <div class="text-secondaryLight pb-2">
  149. {{ $t("helpers.authorization") }}
  150. </div>
  151. <SmartAnchor
  152. class="link"
  153. :label="`${$t('authorization.learn')} \xA0 →`"
  154. to="https://docs.hoppscotch.io/"
  155. blank
  156. />
  157. </div>
  158. </div>
  159. </div>
  160. <div v-if="authType === 'bearer'" class="border-b border-dividerLight flex">
  161. <div class="border-r border-dividerLight w-2/3">
  162. <div class="border-b border-dividerLight flex">
  163. <SmartEnvInput
  164. v-model="bearerToken"
  165. placeholder="Token"
  166. styles="bg-transparent flex flex-1 py-1 px-4"
  167. />
  168. </div>
  169. </div>
  170. <div
  171. class="
  172. bg-primary
  173. h-full
  174. top-upperTertiaryStickyFold
  175. min-w-46
  176. max-w-1/3
  177. p-4
  178. z-9
  179. sticky
  180. overflow-auto
  181. "
  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/"
  191. blank
  192. />
  193. </div>
  194. </div>
  195. </div>
  196. <div
  197. v-if="authType === 'oauth-2'"
  198. class="border-b border-dividerLight flex"
  199. >
  200. <div class="border-r border-dividerLight w-2/3">
  201. <div class="border-b border-dividerLight flex">
  202. <SmartEnvInput
  203. v-model="oauth2Token"
  204. placeholder="Token"
  205. styles="bg-transparent flex flex-1 py-1 px-4"
  206. />
  207. </div>
  208. <HttpOAuth2Authorization />
  209. </div>
  210. <div
  211. class="
  212. bg-primary
  213. h-full
  214. top-upperTertiaryStickyFold
  215. min-w-46
  216. max-w-1/3
  217. p-4
  218. z-9
  219. sticky
  220. overflow-auto
  221. "
  222. >
  223. <div class="p-2">
  224. <div class="text-secondaryLight pb-2">
  225. {{ $t("helpers.authorization") }}
  226. </div>
  227. <SmartAnchor
  228. class="link"
  229. :label="`${$t('authorization.learn')} \xA0 →`"
  230. to="https://docs.hoppscotch.io/"
  231. blank
  232. />
  233. </div>
  234. </div>
  235. </div>
  236. </div>
  237. </template>
  238. <script lang="ts">
  239. import { computed, defineComponent, Ref } from "@nuxtjs/composition-api"
  240. import {
  241. HoppRESTAuthBasic,
  242. HoppRESTAuthBearer,
  243. HoppRESTAuthOAuth2,
  244. } from "~/helpers/types/HoppRESTAuth"
  245. import { pluckRef, useStream } from "~/helpers/utils/composables"
  246. import { restAuth$, setRESTAuth } from "~/newstore/RESTSession"
  247. import { useSetting } from "~/newstore/settings"
  248. export default defineComponent({
  249. setup() {
  250. const auth = useStream(
  251. restAuth$,
  252. { authType: "none", authActive: true },
  253. setRESTAuth
  254. )
  255. const authType = pluckRef(auth, "authType")
  256. const authName = computed(() => {
  257. if (authType.value === "basic") return "Basic Auth"
  258. else if (authType.value === "bearer") return "Bearer"
  259. else if (authType.value === "oauth-2") return "OAuth 2.0"
  260. else return "None"
  261. })
  262. const authActive = pluckRef(auth, "authActive")
  263. const basicUsername = pluckRef(auth as Ref<HoppRESTAuthBasic>, "username")
  264. const basicPassword = pluckRef(auth as Ref<HoppRESTAuthBasic>, "password")
  265. const bearerToken = pluckRef(auth as Ref<HoppRESTAuthBearer>, "token")
  266. const oauth2Token = pluckRef(auth as Ref<HoppRESTAuthOAuth2>, "token")
  267. const URLExcludes = useSetting("URL_EXCLUDES")
  268. const clearContent = () => {
  269. auth.value = {
  270. authType: "none",
  271. authActive: true,
  272. }
  273. }
  274. return {
  275. auth,
  276. authType,
  277. authName,
  278. authActive,
  279. basicUsername,
  280. basicPassword,
  281. bearerToken,
  282. oauth2Token,
  283. URLExcludes,
  284. clearContent,
  285. }
  286. },
  287. })
  288. </script>