admin-auth.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. .authlogo
  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. template(v-if='strategy.useForm')
  178. v-divider.mt-3
  179. .d-flex.my-5.align-center
  180. .overline {{$t('admin:auth.security')}}
  181. v-chip.ml-3.grey--text(outlined, small, label) Coming soon
  182. v-switch.ml-3(
  183. v-if='strategy.key === `local`'
  184. :disabled='!strategy.selfRegistration || true'
  185. v-model='strategy.recaptcha'
  186. label='Use reCAPTCHA by Google'
  187. color='primary'
  188. hint='Protects against spam robots and malicious registrations.'
  189. persistent-hint
  190. inset
  191. )
  192. v-switch.ml-3(
  193. v-model='strategy.recaptcha'
  194. :disabled='true'
  195. :label='$t(`admin:auth.force2fa`)'
  196. color='primary'
  197. :hint='$t(`admin:auth.force2faHint`)'
  198. persistent-hint
  199. inset
  200. )
  201. v-card.mt-4.wiki-form.animated.fadeInUp.wait-p4s(v-if='selectedStrategy !== `local`')
  202. v-toolbar(color='primary', dense, flat, dark)
  203. .subtitle-1 {{$t('admin:auth.configReference')}}
  204. v-card-text
  205. .body-2 {{$t('admin:auth.configReferenceSubtitle')}}
  206. v-alert.mt-3.radius-7(v-if='host.length < 8', color='red', outlined, :value='true', icon='mdi-alert')
  207. i18next(path='admin:auth.siteUrlNotSetup', tag='span')
  208. strong(place='siteUrl') {{$t('admin:general.siteUrl')}}
  209. strong(place='general') {{$t('admin:general.title')}}
  210. .pa-3.mt-3.radius-7.grey(v-else, :class='$vuetify.theme.dark ? `darken-3-d5` : `lighten-3`')
  211. .body-2: strong {{$t('admin:auth.allowedWebOrigins')}}
  212. .body-2 {{host}}
  213. v-divider.my-3
  214. .body-2: strong {{$t('admin:auth.callbackUrl')}}
  215. .body-2 {{host}}/login/{{strategy.key}}/callback
  216. v-divider.my-3
  217. .body-2: strong {{$t('admin:auth.loginUrl')}}
  218. .body-2 {{host}}/login
  219. v-divider.my-3
  220. .body-2: strong {{$t('admin:auth.logoutUrl')}}
  221. .body-2 {{host}}
  222. v-divider.my-3
  223. .body-2: strong {{$t('admin:auth.tokenEndpointAuthMethod')}}
  224. .body-2 HTTP-POST
  225. </template>
  226. <script>
  227. import _ from 'lodash'
  228. import gql from 'graphql-tag'
  229. import { v4 as uuid } from 'uuid'
  230. import groupsQuery from 'gql/admin/auth/auth-query-groups.gql'
  231. import hostQuery from 'gql/admin/auth/auth-query-host.gql'
  232. import draggable from 'vuedraggable'
  233. export default {
  234. components: {
  235. draggable
  236. },
  237. filters: {
  238. startCase(val) { return _.startCase(val) }
  239. },
  240. data() {
  241. return {
  242. groups: [],
  243. strategies: [],
  244. activeStrategies: [],
  245. selectedStrategy: '',
  246. host: '',
  247. strategy: {
  248. strategy: {}
  249. }
  250. }
  251. },
  252. computed: {
  253. order: {
  254. get () {
  255. return this.strategies
  256. },
  257. set (val) {
  258. }
  259. }
  260. },
  261. watch: {
  262. selectedStrategy(newValue, oldValue) {
  263. this.strategy = _.find(this.activeStrategies, ['key', newValue]) || {}
  264. },
  265. activeStrategies(newValue, oldValue) {
  266. this.selectedStrategy = 'local'
  267. }
  268. },
  269. methods: {
  270. async refresh() {
  271. await this.$apollo.queries.strategies.refetch()
  272. await this.$apollo.queries.activeStrategies.refetch()
  273. this.$store.commit('showNotification', {
  274. message: this.$t('admin:auth.refreshSuccess'),
  275. style: 'success',
  276. icon: 'cached'
  277. })
  278. },
  279. addStrategy (str) {
  280. const newStr = {
  281. key: uuid(),
  282. strategy: str,
  283. config: str.props.map(c => ({
  284. key: c.key,
  285. value: {
  286. ...c,
  287. value: c.default
  288. }
  289. })),
  290. order: this.activeStrategies.length,
  291. displayName: str.title,
  292. selfRegistration: false,
  293. domainWhitelist: [],
  294. autoEnrollGroups: []
  295. }
  296. this.activeStrategies = [...this.activeStrategies, newStr]
  297. this.$nextTick(() => {
  298. this.selectedStrategy = newStr.key
  299. })
  300. },
  301. deleteStrategy () {
  302. this.activeStrategies = _.reject(this.activeStrategies, ['key', this.strategy.key])
  303. },
  304. async save() {
  305. this.$store.commit(`loadingStart`, 'admin-auth-savestrategies')
  306. try {
  307. const resp = await this.$apollo.mutate({
  308. mutation: gql`
  309. mutation($strategies: [AuthenticationStrategyInput]!) {
  310. authentication {
  311. updateStrategies(strategies: $strategies) {
  312. responseResult {
  313. succeeded
  314. errorCode
  315. slug
  316. message
  317. }
  318. }
  319. }
  320. }
  321. `,
  322. variables: {
  323. strategies: this.activeStrategies.map(str => ({
  324. key: str.key,
  325. strategyKey: str.strategy.key,
  326. displayName: str.displayName,
  327. order: str.order,
  328. config: str.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })})),
  329. selfRegistration: str.selfRegistration,
  330. domainWhitelist: str.domainWhitelist,
  331. autoEnrollGroups: str.autoEnrollGroups
  332. }))
  333. }
  334. })
  335. if (_.get(resp, 'data.authentication.updateStrategies.responseResult.succeeded', false)) {
  336. this.$store.commit('showNotification', {
  337. message: this.$t('admin:auth.saveSuccess'),
  338. style: 'success',
  339. icon: 'check'
  340. })
  341. } else {
  342. throw new Error(_.get(resp, 'data.authentication.updateStrategies.responseResult.message', this.$t('common:error.unexpected')))
  343. }
  344. } catch (err) {
  345. this.$store.commit('pushGraphError', err)
  346. }
  347. this.$store.commit(`loadingStop`, 'admin-auth-savestrategies')
  348. }
  349. },
  350. apollo: {
  351. strategies: {
  352. query: gql`
  353. query {
  354. authentication {
  355. strategies {
  356. key
  357. title
  358. description
  359. isAvailable
  360. useForm
  361. logo
  362. website
  363. props {
  364. key
  365. value
  366. }
  367. }
  368. }
  369. }
  370. `,
  371. fetchPolicy: 'network-only',
  372. update: (data) => _.get(data, 'authentication.strategies', []).map(str => ({
  373. ...str,
  374. isDisabled: !str.isAvailable || str.key === `local`,
  375. props: _.sortBy(str.props.map(cfg => ({
  376. key: cfg.key,
  377. ...JSON.parse(cfg.value)
  378. })), [t => t.order])
  379. })),
  380. watchLoading (isLoading) {
  381. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-strategies-refresh')
  382. }
  383. },
  384. activeStrategies: {
  385. query: gql`
  386. query {
  387. authentication {
  388. activeStrategies {
  389. key
  390. strategy {
  391. key
  392. title
  393. description
  394. useForm
  395. logo
  396. website
  397. }
  398. config {
  399. key
  400. value
  401. }
  402. order
  403. displayName
  404. selfRegistration
  405. domainWhitelist
  406. autoEnrollGroups
  407. }
  408. }
  409. }
  410. `,
  411. fetchPolicy: 'network-only',
  412. update: (data) => _.get(data, 'authentication.activeStrategies', []).map(str => ({
  413. ...str,
  414. config: _.sortBy(str.config.map(cfg => ({
  415. ...cfg,
  416. value: JSON.parse(cfg.value)
  417. })), [t => t.value.order])
  418. })),
  419. watchLoading (isLoading) {
  420. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-activestrategies-refresh')
  421. }
  422. },
  423. groups: {
  424. query: groupsQuery,
  425. fetchPolicy: 'network-only',
  426. update: (data) => data.groups.list,
  427. watchLoading (isLoading) {
  428. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
  429. }
  430. },
  431. host: {
  432. query: hostQuery,
  433. fetchPolicy: 'network-only',
  434. update: (data) => _.cloneDeep(data.site.config.host),
  435. watchLoading (isLoading) {
  436. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-host-refresh')
  437. }
  438. }
  439. }
  440. }
  441. </script>
  442. <style lang='scss' scoped>
  443. .authlogo {
  444. width: 250px;
  445. height: 60px;
  446. float:right;
  447. display: flex;
  448. justify-content: flex-end;
  449. align-items: center;
  450. margin-left: 16px;
  451. img {
  452. max-width: 100%;
  453. max-height: 50px;
  454. }
  455. }
  456. </style>