Authorization.vue 7.7 KB

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