admin-auth.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template lang='pug'>
  2. v-container(fluid, grid-list-lg)
  3. v-layout(row, wrap)
  4. v-flex(xs12)
  5. .admin-header
  6. img.animated.fadeInUp(src='/_assets/svg/icon-unlock.svg', alt='Authentication', style='width: 80px;')
  7. .admin-header-title
  8. .headline.primary--text.animated.fadeInLeft {{ $t('admin:auth.title') }}
  9. .subtitle-1.grey--text.animated.fadeInLeft.wait-p4s {{ $t('admin:auth.subtitle') }}
  10. v-spacer
  11. v-btn.animated.fadeInDown.wait-p3s(icon, outlined, color='grey', href='https://docs.requarks.io/auth', target='_blank')
  12. v-icon mdi-help-circle
  13. v-btn.animated.fadeInDown.wait-p2s.mx-3(icon, outlined, color='grey', @click='refresh')
  14. v-icon mdi-refresh
  15. v-btn.animated.fadeInDown(color='success', @click='save', depressed, large)
  16. v-icon(left) mdi-check
  17. span {{$t('common:actions.apply')}}
  18. v-flex(lg3, xs12)
  19. v-card.animated.fadeInUp
  20. v-toolbar(flat, color='teal', dark, dense)
  21. .subtitle-1 {{$t('admin:auth.activeStrategies')}}
  22. v-list(two-line, dense).py-0
  23. draggable(
  24. v-model='activeStrategies'
  25. handle='.is-handle'
  26. direction='vertical'
  27. :store='order'
  28. )
  29. transition-group
  30. v-list-item(
  31. v-for='(str, idx) in activeStrategies'
  32. :key='str.key'
  33. @click='selectedStrategy = str.key'
  34. :class='selectedStrategy === str.key ? ($vuetify.theme.dark ? `grey darken-5` : `teal lighten-5`) : ``'
  35. )
  36. v-list-item-avatar.is-handle(size='24')
  37. v-icon(:color='selectedStrategy === str.key ? `teal` : `grey`') mdi-drag-horizontal
  38. v-list-item-content
  39. v-list-item-title.body-2(:class='selectedStrategy === str.key ? `teal--text` : ``') {{ str.displayName }}
  40. v-list-item-subtitle: .caption(:class='selectedStrategy === str.key ? `teal--text ` : ``') {{ str.strategy.title }}
  41. v-list-item-avatar(v-if='selectedStrategy === str.key', size='24')
  42. v-icon.animated.fadeInLeft(color='teal', large) mdi-chevron-right
  43. v-card-chin
  44. v-menu(offset-y, bottom, min-width='250px', max-width='550px', max-height='50vh', style='flex: 1 1;', center)
  45. template(v-slot:activator='{ on }')
  46. v-btn(v-on='on', color='primary', depressed, block)
  47. v-icon(left) mdi-plus
  48. span {{$t('admin:auth.addStrategy')}}
  49. v-list(dense)
  50. template(v-for='(str, idx) of strategies')
  51. v-list-item(
  52. :key='str.key'
  53. :disabled='str.isDisabled'
  54. @click='addStrategy(str)'
  55. )
  56. v-list-item-avatar(height='24', width='48', tile)
  57. v-img(:src='str.logo', width='48px', height='24px', contain, :style='str.isDisabled ? `opacity: .25;` : ``')
  58. v-list-item-content
  59. v-list-item-title {{str.title}}
  60. v-list-item-subtitle: .caption(:style='str.isDisabled ? `opacity: .4;` : ``') {{str.description}}
  61. v-divider(v-if='idx < strategies.length - 1')
  62. v-flex(xs12, lg9)
  63. v-card.animated.fadeInUp.wait-p2s
  64. v-toolbar(color='primary', dense, flat, dark)
  65. .subtitle-1 {{strategy.displayName}} #[em ({{strategy.strategy.title}})]
  66. v-spacer
  67. v-btn(small, outlined, dark, color='white', :disabled='strategy.key === `local`', @click='deleteStrategy()')
  68. v-icon(left) mdi-close
  69. span {{$t('common:actions.delete')}}
  70. v-card-info(color='blue')
  71. div
  72. span {{strategy.strategy.description}}
  73. .caption: a(:href='strategy.strategy.website') {{strategy.strategy.website}}
  74. v-spacer
  75. .admin-providerlogo
  76. img(:src='strategy.strategy.logo', :alt='strategy.strategy.title')
  77. v-card-text
  78. .overline.mb-5 {{$t('admin:auth.strategyConfiguration')}}
  79. v-text-field.mb-3(
  80. outlined
  81. label='Display Name'
  82. v-model='strategy.displayName'
  83. prepend-icon='mdi-format-title'
  84. hint='The title shown to the end user for this authentication strategy.'
  85. persistent-hint
  86. )
  87. template(v-for='cfg in strategy.config')
  88. v-select.mb-3(
  89. v-if='cfg.value.type === "string" && cfg.value.enum'
  90. outlined
  91. :items='cfg.value.enum'
  92. :key='cfg.key'
  93. :label='cfg.value.title'
  94. v-model='cfg.value.value'
  95. prepend-icon='mdi-cog-box'
  96. :hint='cfg.value.hint ? cfg.value.hint : ""'
  97. persistent-hint
  98. :class='cfg.value.hint ? "mb-2" : ""'
  99. :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
  100. )
  101. v-switch.mb-6(
  102. v-else-if='cfg.value.type === "boolean"'
  103. :key='cfg.key'
  104. :label='cfg.value.title'
  105. v-model='cfg.value.value'
  106. color='primary'
  107. prepend-icon='mdi-cog-box'
  108. :hint='cfg.value.hint ? cfg.value.hint : ""'
  109. persistent-hint
  110. inset
  111. )
  112. v-textarea.mb-3(
  113. v-else-if='cfg.value.type === "string" && cfg.value.multiline'
  114. outlined
  115. :key='cfg.key'
  116. :label='cfg.value.title'
  117. v-model='cfg.value.value'
  118. prepend-icon='mdi-cog-box'
  119. :hint='cfg.value.hint ? cfg.value.hint : ""'
  120. persistent-hint
  121. :class='cfg.value.hint ? "mb-2" : ""'
  122. )
  123. v-text-field.mb-3(
  124. v-else
  125. outlined
  126. :key='cfg.key'
  127. :label='cfg.value.title'
  128. v-model='cfg.value.value'
  129. prepend-icon='mdi-cog-box'
  130. :hint='cfg.value.hint ? cfg.value.hint : ""'
  131. persistent-hint
  132. :class='cfg.value.hint ? "mb-2" : ""'
  133. :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
  134. )
  135. v-divider.mt-3
  136. .overline.my-5 {{$t('admin:auth.registration')}}
  137. .pr-3
  138. v-switch.ml-3(
  139. v-model='strategy.selfRegistration'
  140. :label='$t(`admin:auth.selfRegistration`)'
  141. color='primary'
  142. :hint='$t(`admin:auth.selfRegistrationHint`)'
  143. persistent-hint
  144. inset
  145. )
  146. v-combobox.ml-3.mt-3(
  147. :label='$t(`admin:auth.domainsWhitelist`)'
  148. v-model='strategy.domainWhitelist'
  149. prepend-icon='mdi-email-check-outline'
  150. outlined
  151. :disabled='!strategy.selfRegistration'
  152. :hint='$t(`admin:auth.domainsWhitelistHint`)'
  153. persistent-hint
  154. small-chips
  155. deletable-chips
  156. clearable
  157. multiple
  158. chips
  159. )
  160. v-autocomplete.mt-3.ml-3(
  161. outlined
  162. :disabled='!strategy.selfRegistration'
  163. :items='groups'
  164. item-text='name'
  165. item-value='id'
  166. :label='$t(`admin:auth.autoEnrollGroups`)'
  167. v-model='strategy.autoEnrollGroups'
  168. prepend-icon='mdi-account-group'
  169. :hint='$t(`admin:auth.autoEnrollGroupsHint`)'
  170. small-chips
  171. persistent-hint
  172. deletable-chips
  173. clearable
  174. multiple
  175. chips
  176. )
  177. v-card.mt-4.wiki-form.animated.fadeInUp.wait-p4s(v-if='selectedStrategy !== `local`')
  178. v-toolbar(color='primary', dense, flat, dark)
  179. .subtitle-1 {{$t('admin:auth.configReference')}}
  180. v-card-text
  181. .body-2 {{$t('admin:auth.configReferenceSubtitle')}}
  182. v-alert.mt-3.radius-7(v-if='host.length < 8', color='red', outlined, :value='true', icon='mdi-alert')
  183. i18next(path='admin:auth.siteUrlNotSetup', tag='span')
  184. strong(place='siteUrl') {{$t('admin:general.siteUrl')}}
  185. strong(place='general') {{$t('admin:general.title')}}
  186. .pa-3.mt-3.radius-7.grey(v-else, :class='$vuetify.theme.dark ? `darken-3-d5` : `lighten-3`')
  187. .body-2: strong {{$t('admin:auth.allowedWebOrigins')}}
  188. .body-2 {{host}}
  189. v-divider.my-3
  190. .body-2: strong {{$t('admin:auth.callbackUrl')}}
  191. .body-2 {{host}}/login/{{strategy.key}}/callback
  192. v-divider.my-3
  193. .body-2: strong {{$t('admin:auth.loginUrl')}}
  194. .body-2 {{host}}/login
  195. v-divider.my-3
  196. .body-2: strong {{$t('admin:auth.logoutUrl')}}
  197. .body-2 {{host}}
  198. v-divider.my-3
  199. .body-2: strong {{$t('admin:auth.tokenEndpointAuthMethod')}}
  200. .body-2 HTTP-POST
  201. </template>
  202. <script>
  203. import _ from 'lodash'
  204. import gql from 'graphql-tag'
  205. import { v4 as uuid } from 'uuid'
  206. import groupsQuery from 'gql/admin/auth/auth-query-groups.gql'
  207. import hostQuery from 'gql/admin/auth/auth-query-host.gql'
  208. import draggable from 'vuedraggable'
  209. export default {
  210. components: {
  211. draggable
  212. },
  213. filters: {
  214. startCase(val) { return _.startCase(val) }
  215. },
  216. data() {
  217. return {
  218. groups: [],
  219. strategies: [],
  220. activeStrategies: [],
  221. selectedStrategy: '',
  222. host: '',
  223. strategy: {
  224. strategy: {}
  225. }
  226. }
  227. },
  228. computed: {
  229. order: {
  230. get () {
  231. return this.strategies
  232. },
  233. set (val) {
  234. }
  235. }
  236. },
  237. watch: {
  238. selectedStrategy(newValue, oldValue) {
  239. this.strategy = _.find(this.activeStrategies, ['key', newValue]) || {}
  240. },
  241. activeStrategies(newValue, oldValue) {
  242. this.selectedStrategy = 'local'
  243. }
  244. },
  245. methods: {
  246. async refresh() {
  247. await this.$apollo.queries.strategies.refetch()
  248. await this.$apollo.queries.activeStrategies.refetch()
  249. this.$store.commit('showNotification', {
  250. message: this.$t('admin:auth.refreshSuccess'),
  251. style: 'success',
  252. icon: 'cached'
  253. })
  254. },
  255. addStrategy (str) {
  256. const newStr = {
  257. key: uuid(),
  258. strategy: str,
  259. config: str.props.map(c => ({
  260. key: c.key,
  261. value: {
  262. ...c,
  263. value: c.default
  264. }
  265. })),
  266. order: this.activeStrategies.length,
  267. displayName: str.title,
  268. selfRegistration: false,
  269. domainWhitelist: [],
  270. autoEnrollGroups: []
  271. }
  272. this.activeStrategies = [...this.activeStrategies, newStr]
  273. this.$nextTick(() => {
  274. this.selectedStrategy = newStr.key
  275. })
  276. },
  277. deleteStrategy () {
  278. this.activeStrategies = _.reject(this.activeStrategies, ['key', this.strategy.key])
  279. },
  280. async save() {
  281. this.$store.commit(`loadingStart`, 'admin-auth-savestrategies')
  282. try {
  283. const resp = await this.$apollo.mutate({
  284. mutation: gql`
  285. mutation($strategies: [AuthenticationStrategyInput]!) {
  286. authentication {
  287. updateStrategies(strategies: $strategies) {
  288. responseResult {
  289. succeeded
  290. errorCode
  291. slug
  292. message
  293. }
  294. }
  295. }
  296. }
  297. `,
  298. variables: {
  299. strategies: this.activeStrategies.map(str => ({
  300. key: str.key,
  301. strategyKey: str.strategy.key,
  302. displayName: str.displayName,
  303. order: str.order,
  304. config: str.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })})),
  305. selfRegistration: str.selfRegistration,
  306. domainWhitelist: str.domainWhitelist,
  307. autoEnrollGroups: str.autoEnrollGroups
  308. }))
  309. }
  310. })
  311. if (_.get(resp, 'data.authentication.updateStrategies.responseResult.succeeded', false)) {
  312. this.$store.commit('showNotification', {
  313. message: this.$t('admin:auth.saveSuccess'),
  314. style: 'success',
  315. icon: 'check'
  316. })
  317. } else {
  318. throw new Error(_.get(resp, 'data.authentication.updateStrategies.responseResult.message', this.$t('common:error.unexpected')))
  319. }
  320. } catch (err) {
  321. this.$store.commit('pushGraphError', err)
  322. }
  323. this.$store.commit(`loadingStop`, 'admin-auth-savestrategies')
  324. }
  325. },
  326. apollo: {
  327. strategies: {
  328. query: gql`
  329. query {
  330. authentication {
  331. strategies {
  332. key
  333. title
  334. description
  335. isAvailable
  336. useForm
  337. logo
  338. website
  339. props {
  340. key
  341. value
  342. }
  343. }
  344. }
  345. }
  346. `,
  347. fetchPolicy: 'network-only',
  348. update: (data) => _.get(data, 'authentication.strategies', []).map(str => ({
  349. ...str,
  350. isDisabled: !str.isAvailable || str.key === `local`,
  351. props: _.sortBy(str.props.map(cfg => ({
  352. key: cfg.key,
  353. ...JSON.parse(cfg.value)
  354. })), [t => t.order])
  355. })),
  356. watchLoading (isLoading) {
  357. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-strategies-refresh')
  358. }
  359. },
  360. activeStrategies: {
  361. query: gql`
  362. query {
  363. authentication {
  364. activeStrategies {
  365. key
  366. strategy {
  367. key
  368. title
  369. description
  370. useForm
  371. logo
  372. website
  373. }
  374. config {
  375. key
  376. value
  377. }
  378. order
  379. displayName
  380. selfRegistration
  381. domainWhitelist
  382. autoEnrollGroups
  383. }
  384. }
  385. }
  386. `,
  387. fetchPolicy: 'network-only',
  388. update: (data) => _.get(data, 'authentication.activeStrategies', []).map(str => ({
  389. ...str,
  390. config: _.sortBy(str.config.map(cfg => ({
  391. ...cfg,
  392. value: JSON.parse(cfg.value)
  393. })), [t => t.value.order])
  394. })),
  395. watchLoading (isLoading) {
  396. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-activestrategies-refresh')
  397. }
  398. },
  399. groups: {
  400. query: groupsQuery,
  401. fetchPolicy: 'network-only',
  402. update: (data) => data.groups.list,
  403. watchLoading (isLoading) {
  404. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
  405. }
  406. },
  407. host: {
  408. query: hostQuery,
  409. fetchPolicy: 'network-only',
  410. update: (data) => _.cloneDeep(data.site.config.host),
  411. watchLoading (isLoading) {
  412. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-host-refresh')
  413. }
  414. }
  415. }
  416. }
  417. </script>