-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathslice.ts
More file actions
146 lines (127 loc) · 3.79 KB
/
Copy pathslice.ts
File metadata and controls
146 lines (127 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { createStorage, hashArray } from "@theme/ApiExplorer/storage-utils";
import type {
SecurityRequirementObject,
SecuritySchemeObject,
} from "docusaurus-plugin-openapi-docs/src/openapi/types";
/* eslint-disable import/no-extraneous-dependencies*/
import { ThemeConfig } from "docusaurus-theme-openapi-docs/src/types";
import { getAuthDataKeys } from "./auth-types";
// The global definitions
// "securitySchemes": {
// "BearerAuth": { "type": "http", "scheme": "BeAreR" },
// "BasicAuth": { "type": "http", "scheme": "basic" }
// },
// The operation level requirements
// "security": [
// { "BearerAuth": [] },
// { "BearerAuth": [], "BasicAuth": [] }
// ],
// SLICE_STATE
// data:
// BearerAuth:
// token=xxx
// BasicAuth:
// username=xxx
// password=xxx
//
// options:
// "BearerAuth": [{ key: "BearerAuth", scopes: [], ...rest }]
// "BearerAuth and BasicAuth": [{ key: "BearerAuth", scopes: [], ...rest }, { key: "BasicAuth", scopes: [], ...rest }]
//
// selected: "BearerAuth and BasicAuth"
// LOCAL_STORAGE
// hash(SLICE_STATE.options) -> "BearerAuth and BasicAuth"
// BearerAuth -> { token: xxx }
// BasicAuth -> { username: xxx, password: xxx }
export function createAuth({
security,
securitySchemes,
options: opts,
}: {
security?: SecurityRequirementObject[];
securitySchemes?: {
[key: string]: SecuritySchemeObject;
};
options?: ThemeConfig["api"];
}): AuthState {
const storage = createStorage(opts?.authPersistence ?? "sessionStorage");
let data: AuthState["data"] = {};
let options: AuthState["options"] = {};
for (const option of security ?? []) {
const id = Object.keys(option).join(" and ");
for (const [schemeID, scopes] of Object.entries(option)) {
const scheme = securitySchemes?.[schemeID];
if (scheme) {
if (options[id] === undefined) {
options[id] = [];
}
const dataKeys = getAuthDataKeys(scheme);
for (const key of dataKeys) {
if (data[schemeID] === undefined) {
data[schemeID] = {};
}
let persisted = undefined;
try {
persisted = JSON.parse(storage.getItem(schemeID) ?? "")[key];
} catch {}
data[schemeID][key] = persisted;
}
options[id].push({
...scheme,
key: schemeID,
scopes,
});
}
}
}
let persisted = undefined;
try {
persisted = storage.getItem(hashArray(Object.keys(options))) ?? undefined;
} catch {}
return {
data,
options,
selected: persisted ?? Object.keys(options)[0],
};
}
export type Scheme = {
key: string;
scopes: string[];
} & SecuritySchemeObject;
export interface AuthState {
data: {
[scheme: string]: {
[key: string]: string | undefined;
};
};
options: {
[key: string]: Scheme[];
};
selected?: string;
}
const initialState: AuthState = {} as any;
export const slice = createSlice({
name: "auth",
initialState,
reducers: {
setAuthData: (
state,
action: PayloadAction<{ scheme: string; key: string; value?: string }>
) => {
const { scheme, key, value } = action.payload;
state.data[scheme][key] = value;
},
setSelectedAuth: (state, action: PayloadAction<string>) => {
state.selected = action.payload;
},
},
});
export const { setAuthData, setSelectedAuth } = slice.actions;
export default slice.reducer;