Authorization.vue 7.9 KB

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