strapi-plugin-oidc 1.6.4 → 1.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ const react = require("react");
7
7
  const designSystem = require("@strapi/design-system");
8
8
  const icons = require("@strapi/icons");
9
9
  const reactIntl = require("react-intl");
10
- const index = require("./index-EAfqxfV4.js");
10
+ const index = require("./index-DVjS4hOr.js");
11
11
  const styled = require("styled-components");
12
12
  const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
13
13
  const styled__default = /* @__PURE__ */ _interopDefault(styled);
@@ -371,8 +371,7 @@ function AuditLog() {
371
371
  });
372
372
  return;
373
373
  }
374
- const text = await response.text();
375
- const blob = new Blob([text], { type: "application/x-ndjson" });
374
+ const blob = await response.blob();
376
375
  const url = URL.createObjectURL(blob);
377
376
  const a = document.createElement("a");
378
377
  a.href = url;
@@ -575,75 +574,201 @@ function formatDatetimeForFilename(date) {
575
574
  const seconds = String(date.getSeconds()).padStart(2, "0");
576
575
  return `${year}${month}${day}_${hours}${minutes}${seconds}`;
577
576
  }
578
- function deepClone(value) {
579
- return JSON.parse(JSON.stringify(value));
577
+ function downloadJson(basename, data) {
578
+ const datetime = formatDatetimeForFilename(/* @__PURE__ */ new Date());
579
+ const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" });
580
+ const url = URL.createObjectURL(blob);
581
+ const a = document.createElement("a");
582
+ a.href = url;
583
+ a.download = `${basename}-${datetime}.json`;
584
+ a.click();
585
+ URL.revokeObjectURL(url);
586
+ }
587
+ const initialState = {
588
+ current: {
589
+ oidcRoles: [],
590
+ users: [],
591
+ useWhitelist: false,
592
+ enforceOIDC: false
593
+ },
594
+ initial: {
595
+ oidcRoles: [],
596
+ users: [],
597
+ useWhitelist: false,
598
+ enforceOIDC: false
599
+ },
600
+ roles: [],
601
+ enforceOIDCConfig: null,
602
+ auditLogEnabled: true,
603
+ loading: false,
604
+ showSuccess: false,
605
+ showError: false
606
+ };
607
+ function reducer(state, action) {
608
+ switch (action.type) {
609
+ case "hydrate/roles":
610
+ return { ...state, roles: action.roles };
611
+ case "hydrate/oidcRoles": {
612
+ const snapshot = { oidcRoles: action.oidcRoles };
613
+ return {
614
+ ...state,
615
+ current: { ...state.current, ...snapshot },
616
+ initial: { ...state.initial, ...snapshot }
617
+ };
618
+ }
619
+ case "hydrate/whitelist": {
620
+ const snapshot = {
621
+ oidcRoles: action.snapshot.oidcRoles ?? state.current.oidcRoles,
622
+ users: action.snapshot.users ?? state.current.users,
623
+ useWhitelist: action.snapshot.useWhitelist ?? state.current.useWhitelist,
624
+ enforceOIDC: action.snapshot.enforceOIDC ?? state.current.enforceOIDC
625
+ };
626
+ return {
627
+ ...state,
628
+ current: snapshot,
629
+ initial: structuredClone(snapshot),
630
+ enforceOIDCConfig: action.enforceOIDCConfig,
631
+ auditLogEnabled: action.auditLogEnabled
632
+ };
633
+ }
634
+ case "patch/oidcRole":
635
+ return {
636
+ ...state,
637
+ current: {
638
+ ...state.current,
639
+ oidcRoles: state.current.oidcRoles.map(
640
+ (role) => role.oauth_type === action.oidcId ? { ...role, role: action.values } : role
641
+ )
642
+ }
643
+ };
644
+ case "user/add":
645
+ return {
646
+ ...state,
647
+ current: {
648
+ ...state.current,
649
+ users: [
650
+ ...state.current.users,
651
+ { email: action.email, createdAt: (/* @__PURE__ */ new Date()).toISOString() }
652
+ ]
653
+ }
654
+ };
655
+ case "user/delete": {
656
+ const updatedUsers = state.current.users.filter((u) => u.email !== action.email);
657
+ const updated = { users: updatedUsers };
658
+ let enforceOIDC = state.current.enforceOIDC;
659
+ if (state.current.useWhitelist && updatedUsers.length === 0) {
660
+ enforceOIDC = false;
661
+ }
662
+ return {
663
+ ...state,
664
+ current: { ...state.current, ...updated, enforceOIDC }
665
+ };
666
+ }
667
+ case "users/clear": {
668
+ const updated = { users: [] };
669
+ let enforceOIDC = state.current.enforceOIDC;
670
+ if (state.current.useWhitelist) {
671
+ enforceOIDC = false;
672
+ }
673
+ return {
674
+ ...state,
675
+ current: { ...state.current, ...updated, enforceOIDC }
676
+ };
677
+ }
678
+ case "users/replace":
679
+ return {
680
+ ...state,
681
+ current: { ...state.current, users: action.users }
682
+ };
683
+ case "toggle/useWhitelist": {
684
+ const useWhitelist = action.value;
685
+ let enforceOIDC = state.current.enforceOIDC;
686
+ if (useWhitelist && state.current.users.length === 0) {
687
+ enforceOIDC = false;
688
+ }
689
+ return {
690
+ ...state,
691
+ current: { ...state.current, useWhitelist, enforceOIDC }
692
+ };
693
+ }
694
+ case "toggle/enforceOIDC":
695
+ return {
696
+ ...state,
697
+ current: { ...state.current, enforceOIDC: action.value }
698
+ };
699
+ case "commit":
700
+ return {
701
+ ...state,
702
+ initial: structuredClone(
703
+ action.snapshot ? { ...state.current, ...action.snapshot } : state.current
704
+ )
705
+ };
706
+ case "loading":
707
+ return { ...state, loading: action.value };
708
+ case "flash/success":
709
+ return { ...state, showSuccess: true };
710
+ case "flash/error":
711
+ return { ...state, showError: true };
712
+ case "flash/clear":
713
+ return {
714
+ ...state,
715
+ showSuccess: action.kind === "success" ? false : state.showSuccess,
716
+ showError: action.kind === "error" ? false : state.showError
717
+ };
718
+ default:
719
+ return state;
720
+ }
721
+ }
722
+ function isDirtyPrimitive(a, b) {
723
+ return a !== b;
724
+ }
725
+ function isDirtyArray(a, b) {
726
+ return JSON.stringify(a) !== JSON.stringify(b);
580
727
  }
581
728
  function useOidcSettings() {
582
729
  const { get, put, post } = admin.useFetchClient();
583
- const [loading, setLoading] = react.useState(false);
584
- const [showSuccess, setSuccess] = react.useState(false);
585
- const [showError, setError] = react.useState(false);
586
- const [initialOidcRoles, setInitialOIDCRoles] = react.useState([]);
587
- const [oidcRoles, setOIDCRoles] = react.useState([]);
588
- const [roles, setRoles] = react.useState([]);
589
- const [initialUseWhitelist, setInitialUseWhitelist] = react.useState(false);
590
- const [useWhitelist, setUseWhitelist] = react.useState(false);
591
- const [initialEnforceOIDC, setInitialEnforceOIDC] = react.useState(false);
592
- const [enforceOIDC, setEnforceOIDC] = react.useState(false);
593
- const [enforceOIDCConfig, setEnforceOIDCConfig] = react.useState(null);
594
- const [initialUsers, setInitialUsers] = react.useState([]);
595
- const [users, setUsers] = react.useState([]);
596
- const [whitelistResponse, setWhitelistResponse] = react.useState({});
730
+ const [state, dispatch] = react.useReducer(reducer, initialState);
597
731
  react.useEffect(() => {
598
732
  get(`/strapi-plugin-oidc/oidc-roles`).then((response) => {
599
- setOIDCRoles(response.data);
600
- setInitialOIDCRoles(deepClone(response.data));
733
+ dispatch({ type: "hydrate/oidcRoles", oidcRoles: response.data });
601
734
  });
602
735
  get(`/admin/roles`).then((response) => {
603
- setRoles(response.data.data);
736
+ dispatch({ type: "hydrate/roles", roles: response.data.data });
604
737
  });
605
738
  get("/strapi-plugin-oidc/whitelist").then((response) => {
606
739
  const data = response.data;
607
- setWhitelistResponse(data);
608
- setUsers(data.whitelistUsers);
609
- setInitialUsers(deepClone(data.whitelistUsers));
610
- setUseWhitelist(data.useWhitelist);
611
- setInitialUseWhitelist(data.useWhitelist);
612
- setEnforceOIDC(data.enforceOIDC);
613
- setInitialEnforceOIDC(data.enforceOIDC);
614
- setEnforceOIDCConfig(data.enforceOIDCConfig ?? null);
740
+ dispatch({
741
+ type: "hydrate/whitelist",
742
+ snapshot: {
743
+ users: data.whitelistUsers,
744
+ useWhitelist: data.useWhitelist,
745
+ enforceOIDC: data.enforceOIDC
746
+ },
747
+ enforceOIDCConfig: data.enforceOIDCConfig ?? null,
748
+ auditLogEnabled: data.auditLogEnabled ?? true
749
+ });
615
750
  });
616
751
  }, [get]);
617
752
  const onChangeRole = react.useCallback((values, oidcId) => {
618
- setOIDCRoles(
619
- (prev) => prev.map((role) => role.oauth_type === oidcId ? { ...role, role: values } : role)
620
- );
753
+ dispatch({ type: "patch/oidcRole", oidcId, values });
621
754
  }, []);
622
755
  const onRegisterWhitelist = react.useCallback((email) => {
623
- setUsers((prev) => [...prev, { email, createdAt: (/* @__PURE__ */ new Date()).toISOString() }]);
756
+ dispatch({ type: "user/add", email });
757
+ }, []);
758
+ const onDeleteWhitelist = react.useCallback((email) => {
759
+ dispatch({ type: "user/delete", email });
624
760
  }, []);
625
- const onDeleteWhitelist = react.useCallback(
626
- (email) => {
627
- setUsers((prev) => {
628
- const updated = prev.filter((u) => u.email !== email);
629
- if (useWhitelist && updated.length === 0) setEnforceOIDC(false);
630
- return updated;
631
- });
632
- },
633
- [useWhitelist]
634
- );
635
761
  const onDeleteAll = react.useCallback(() => {
636
- setUsers([]);
637
- if (useWhitelist) setEnforceOIDC(false);
638
- }, [useWhitelist]);
762
+ dispatch({ type: "users/clear" });
763
+ }, []);
639
764
  const onImport = react.useCallback(
640
765
  async (emails) => {
641
766
  const response = await post("/strapi-plugin-oidc/whitelist/import", {
642
767
  users: emails.map((e) => ({ email: e }))
643
768
  });
644
769
  const refreshed = await get("/strapi-plugin-oidc/whitelist");
645
- setUsers(refreshed.data.whitelistUsers);
646
- setInitialUsers(deepClone(refreshed.data.whitelistUsers));
770
+ dispatch({ type: "users/replace", users: refreshed.data.whitelistUsers });
771
+ dispatch({ type: "commit" });
647
772
  return response.data.importedCount;
648
773
  },
649
774
  [post, get]
@@ -651,74 +776,77 @@ function useOidcSettings() {
651
776
  const onExport = react.useCallback(async () => {
652
777
  const response = await get("/strapi-plugin-oidc/whitelist/export");
653
778
  const data = response.data;
654
- const datetime = formatDatetimeForFilename(/* @__PURE__ */ new Date());
655
- const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" });
656
- const url = URL.createObjectURL(blob);
657
- const a = document.createElement("a");
658
- a.href = url;
659
- a.download = `strapi-oidc-whitelist-${datetime}.json`;
660
- a.click();
661
- URL.revokeObjectURL(url);
779
+ downloadJson("strapi-oidc-whitelist", data);
662
780
  }, [get]);
663
- const onToggleWhitelist = react.useCallback(
664
- (e) => {
665
- const checked = e.target.checked;
666
- setUseWhitelist(checked);
667
- if (checked && users.length === 0) setEnforceOIDC(false);
668
- },
669
- [users.length]
670
- );
781
+ const onToggleWhitelist = react.useCallback((e) => {
782
+ dispatch({ type: "toggle/useWhitelist", value: e.target.checked });
783
+ }, []);
671
784
  const onToggleEnforce = react.useCallback((e) => {
672
- setEnforceOIDC(e.target.checked);
785
+ dispatch({ type: "toggle/enforceOIDC", value: e.target.checked });
673
786
  }, []);
674
- const isDirty = useWhitelist !== initialUseWhitelist || enforceOIDC !== initialEnforceOIDC || JSON.stringify(oidcRoles) !== JSON.stringify(initialOidcRoles) || JSON.stringify(users) !== JSON.stringify(initialUsers);
787
+ const isDirty = react.useMemo(
788
+ () => isDirtyPrimitive(state.current.useWhitelist, state.initial.useWhitelist) || isDirtyPrimitive(state.current.enforceOIDC, state.initial.enforceOIDC) || isDirtyArray(state.current.oidcRoles, state.initial.oidcRoles) || isDirtyArray(state.current.users, state.initial.users),
789
+ [state.current, state.initial]
790
+ );
675
791
  const onSaveAll = react.useCallback(async () => {
676
- setLoading(true);
792
+ dispatch({ type: "loading", value: true });
677
793
  try {
678
- await put("/strapi-plugin-oidc/oidc-roles", {
679
- roles: oidcRoles.map((role) => ({ oauth_type: role.oauth_type, role: role.role }))
680
- });
681
- await put("/strapi-plugin-oidc/whitelist/sync", {
682
- users: users.map((u) => ({ email: u.email }))
683
- });
684
- await put("/strapi-plugin-oidc/whitelist/settings", { useWhitelist, enforceOIDC });
685
- setInitialOIDCRoles(deepClone(oidcRoles));
686
- setInitialUseWhitelist(useWhitelist);
687
- setInitialEnforceOIDC(enforceOIDC);
688
- get("/strapi-plugin-oidc/whitelist").then((getResponse) => {
689
- const data = getResponse.data;
690
- setWhitelistResponse(data);
691
- setUsers(data.whitelistUsers);
692
- setInitialUsers(deepClone(data.whitelistUsers));
794
+ await Promise.all([
795
+ put("/strapi-plugin-oidc/oidc-roles", {
796
+ roles: state.current.oidcRoles.map((role) => ({
797
+ oauth_type: role.oauth_type,
798
+ role: role.role
799
+ }))
800
+ }),
801
+ put("/strapi-plugin-oidc/whitelist/sync", {
802
+ users: state.current.users.map((u) => ({ email: u.email }))
803
+ }),
804
+ put("/strapi-plugin-oidc/whitelist/settings", {
805
+ useWhitelist: state.current.useWhitelist,
806
+ enforceOIDC: state.current.enforceOIDC
807
+ })
808
+ ]);
809
+ dispatch({ type: "commit" });
810
+ const getResponse = await get("/strapi-plugin-oidc/whitelist");
811
+ const data = getResponse.data;
812
+ dispatch({
813
+ type: "hydrate/whitelist",
814
+ snapshot: {
815
+ users: data.whitelistUsers,
816
+ useWhitelist: data.useWhitelist,
817
+ enforceOIDC: data.enforceOIDC
818
+ },
819
+ enforceOIDCConfig: data.enforceOIDCConfig ?? null,
820
+ auditLogEnabled: data.auditLogEnabled ?? true
693
821
  });
694
- setSuccess(true);
695
- setTimeout(() => setSuccess(false), 3e3);
822
+ dispatch({ type: "flash/success" });
823
+ setTimeout(() => dispatch({ type: "flash/clear", kind: "success" }), 3e3);
696
824
  } catch (e) {
697
825
  console.error(e);
698
- setError(true);
699
- setTimeout(() => setError(false), 3e3);
826
+ dispatch({ type: "flash/error" });
827
+ setTimeout(() => dispatch({ type: "flash/clear", kind: "error" }), 3e3);
700
828
  } finally {
701
- setLoading(false);
829
+ dispatch({ type: "loading", value: false });
702
830
  }
703
- }, [put, get, oidcRoles, users, useWhitelist, enforceOIDC]);
831
+ }, [put, get, state.current]);
704
832
  return {
705
833
  state: {
706
- loading,
707
- showSuccess,
708
- showError,
709
- oidcRoles,
710
- roles,
711
- useWhitelist,
712
- enforceOIDC,
713
- enforceOIDCConfig,
714
- initialEnforceOIDC,
715
- users,
834
+ loading: state.loading,
835
+ showSuccess: state.showSuccess,
836
+ showError: state.showError,
837
+ oidcRoles: state.current.oidcRoles,
838
+ roles: state.roles,
839
+ useWhitelist: state.current.useWhitelist,
840
+ enforceOIDC: state.current.enforceOIDC,
841
+ enforceOIDCConfig: state.enforceOIDCConfig,
842
+ initialEnforceOIDC: state.initial.enforceOIDC,
843
+ users: state.current.users,
716
844
  isDirty,
717
- auditLogEnabled: whitelistResponse.auditLogEnabled ?? true
845
+ auditLogEnabled: state.auditLogEnabled
718
846
  },
719
847
  actions: {
720
- setSuccess,
721
- setError,
848
+ setSuccess: (val) => dispatch({ type: val ? "flash/success" : "flash/clear", kind: "success" }),
849
+ setError: (val) => dispatch({ type: val ? "flash/error" : "flash/clear", kind: "error" }),
722
850
  onChangeRole,
723
851
  onRegisterWhitelist,
724
852
  onDeleteWhitelist,
@@ -23,9 +23,6 @@ const pluginPkg = {
23
23
  strapi
24
24
  };
25
25
  const pluginId = pluginPkg.name.replace(/^@strapi\/plugin-/i, "");
26
- function getTranslation(id) {
27
- return `${pluginId}.${id}`;
28
- }
29
26
  function Initializer({ setPlugin }) {
30
27
  const ref = react.useRef();
31
28
  ref.current = setPlugin;
@@ -66,8 +63,6 @@ const en = {
66
63
  "whitelist.toggle.enabled": "Enabled",
67
64
  "whitelist.toggle.disabled": "Disabled",
68
65
  "whitelist.email.placeholder": "Email address",
69
- "whitelist.roles.placeholder": "Select specific role(s)",
70
- "whitelist.table.roles": "Role(s)",
71
66
  "whitelist.table.empty": "No email addresses",
72
67
  "whitelist.delete.label": "Delete",
73
68
  "page.title.oidc": "OIDC",
@@ -91,7 +86,6 @@ const en = {
91
86
  "unsaved.description": "You have unsaved changes that will be lost if you leave. Do you want to continue?",
92
87
  "unsaved.confirm": "Leave",
93
88
  "unsaved.cancel": "Stay",
94
- "whitelist.table.roles.default": "(Default)",
95
89
  "auditlog.title": "Audit Logs",
96
90
  "auditlog.export": "Download",
97
91
  "auditlog.table.timestamp": "Timestamp",
@@ -152,7 +146,7 @@ const index = {
152
146
  defaultMessage: "Configuration"
153
147
  },
154
148
  Component: async () => {
155
- return await Promise.resolve().then(() => require("./index-DowwUs07.js"));
149
+ return await Promise.resolve().then(() => require("./index-DUFtPEHD.js"));
156
150
  },
157
151
  permissions: [{ action: "plugin::strapi-plugin-oidc.read", subject: null }]
158
152
  }
@@ -265,7 +259,7 @@ const index = {
265
259
  async registerTrads({ locales }) {
266
260
  const transformKeys = (data) => Object.fromEntries(
267
261
  Object.entries(data).map(([key, value]) => [
268
- key.startsWith("global.") ? key : getTranslation(key),
262
+ key.startsWith("global.") ? key : `${pluginId}.${key}`,
269
263
  value
270
264
  ])
271
265
  );
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const index = require("./index-EAfqxfV4.js");
3
+ const index = require("./index-DVjS4hOr.js");
4
4
  exports.default = index.index;
@@ -1,4 +1,4 @@
1
- import { i } from "./index-AxBC5YLT.mjs";
1
+ import { i } from "./index-D2aMSVmR.mjs";
2
2
  export {
3
3
  i as default
4
4
  };