You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

433 lines
16 KiB

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