Authorization.vue 8.1 KB

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