@spica-devkit/identity 0.13.1 → 0.18.2
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.
- package/dist/index.js +132 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +132 -43
- package/dist/index.mjs.map +1 -1
- package/dist/src/identity.d.ts +18 -16
- package/dist/src/interface.d.ts +2 -19
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -42,6 +42,7 @@ class Axios {
|
|
|
42
42
|
}
|
|
43
43
|
setBaseUrl(url) {
|
|
44
44
|
this.instance.defaults.baseURL = url;
|
|
45
|
+
this.baseUrl = url;
|
|
45
46
|
}
|
|
46
47
|
setWriteDefaults(writeDefaults) {
|
|
47
48
|
for (const [header, value] of Object.entries(writeDefaults.headers)) {
|
|
@@ -52,6 +53,9 @@ class Axios {
|
|
|
52
53
|
setAuthorization(authorization) {
|
|
53
54
|
this.instance.defaults.headers["Authorization"] = authorization;
|
|
54
55
|
}
|
|
56
|
+
getAuthorization() {
|
|
57
|
+
return this.instance.defaults.headers["Authorization"].toString();
|
|
58
|
+
}
|
|
55
59
|
get(url, config) {
|
|
56
60
|
return this.instance.get(url, config);
|
|
57
61
|
}
|
|
@@ -149,23 +153,28 @@ function initialize$1(options) {
|
|
|
149
153
|
else if ("identity" in options) {
|
|
150
154
|
authorization = `IDENTITY ${options.identity}`;
|
|
151
155
|
}
|
|
152
|
-
|
|
156
|
+
else if ("user" in options) {
|
|
157
|
+
authorization = `USER ${options.user}`;
|
|
158
|
+
}
|
|
153
159
|
const publicUrl = options.publicUrl || getPublicUrl();
|
|
154
160
|
if (!publicUrl) {
|
|
155
161
|
throw new Error("Public url must be provided.");
|
|
156
162
|
}
|
|
157
163
|
if (!service$1) {
|
|
158
|
-
service$1 = new Axios({
|
|
164
|
+
service$1 = new Axios({});
|
|
159
165
|
}
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
service$1.setBaseUrl(publicUrl);
|
|
167
|
+
if (authorization) {
|
|
162
168
|
service$1.setAuthorization(authorization);
|
|
163
169
|
}
|
|
164
170
|
return { authorization, publicUrl, service: service$1 };
|
|
165
171
|
}
|
|
166
|
-
function checkInitialized(authorization) {
|
|
167
|
-
if (!authorization) {
|
|
168
|
-
throw new Error("You should call initialize method with a valid
|
|
172
|
+
function checkInitialized(authorization, service, options = { skipAuthCheck: false }) {
|
|
173
|
+
if (!authorization && !options.skipAuthCheck) {
|
|
174
|
+
throw new Error("You should call initialize method with a valid credentials.");
|
|
175
|
+
}
|
|
176
|
+
if (!service) {
|
|
177
|
+
throw new Error("You should call initialize method with a valid publicUrl.");
|
|
169
178
|
}
|
|
170
179
|
}
|
|
171
180
|
function getPublicUrl() {
|
|
@@ -175,6 +184,76 @@ function isPlatformBrowser() {
|
|
|
175
184
|
return typeof window !== "undefined";
|
|
176
185
|
}
|
|
177
186
|
|
|
187
|
+
var Batch;
|
|
188
|
+
(function (Batch) {
|
|
189
|
+
function prepareInsertRequest(resources, url, auth, headers) {
|
|
190
|
+
return {
|
|
191
|
+
requests: resources.map((resource, i) => {
|
|
192
|
+
headers = addAuthHeader(headers, auth);
|
|
193
|
+
return {
|
|
194
|
+
id: i.toString(),
|
|
195
|
+
body: resource,
|
|
196
|
+
method: "POST",
|
|
197
|
+
url: url,
|
|
198
|
+
headers: headers
|
|
199
|
+
};
|
|
200
|
+
})
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
Batch.prepareInsertRequest = prepareInsertRequest;
|
|
204
|
+
function prepareRemoveRequest(ids, url, auth, headers) {
|
|
205
|
+
return {
|
|
206
|
+
requests: ids.map((id, i) => {
|
|
207
|
+
headers = addAuthHeader(headers, auth);
|
|
208
|
+
return {
|
|
209
|
+
id: i.toString(),
|
|
210
|
+
body: undefined,
|
|
211
|
+
method: "DELETE",
|
|
212
|
+
url: `${url}/${id}`,
|
|
213
|
+
headers: headers
|
|
214
|
+
};
|
|
215
|
+
})
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
Batch.prepareRemoveRequest = prepareRemoveRequest;
|
|
219
|
+
function handleBatchResponse({ requests }, { responses }) {
|
|
220
|
+
const sortById = (a, b) => Number(a.id) - Number(b.id);
|
|
221
|
+
const successResponses = responses
|
|
222
|
+
.sort(sortById)
|
|
223
|
+
.filter(r => r.status >= 200 && r.status < 300);
|
|
224
|
+
const failureResponses = responses
|
|
225
|
+
.sort(sortById)
|
|
226
|
+
.filter(r => r.status >= 400 && r.status <= 500);
|
|
227
|
+
const successes = successResponses.map(sr => {
|
|
228
|
+
const req = requests.find(r => r.id == sr.id);
|
|
229
|
+
return {
|
|
230
|
+
request: (req.body || req.url),
|
|
231
|
+
response: sr.body
|
|
232
|
+
};
|
|
233
|
+
});
|
|
234
|
+
const failures = failureResponses.map(fr => {
|
|
235
|
+
const req = requests.find(r => r.id == fr.id);
|
|
236
|
+
return {
|
|
237
|
+
request: (req.body || req.url),
|
|
238
|
+
response: {
|
|
239
|
+
error: fr.body["error"],
|
|
240
|
+
message: fr.body["message"]
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
successes,
|
|
246
|
+
failures
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
Batch.handleBatchResponse = handleBatchResponse;
|
|
250
|
+
})(Batch || (Batch = {}));
|
|
251
|
+
function addAuthHeader(headers, auth) {
|
|
252
|
+
headers = headers || {};
|
|
253
|
+
headers["Authorization"] = auth;
|
|
254
|
+
return headers;
|
|
255
|
+
}
|
|
256
|
+
|
|
178
257
|
/******************************************************************************
|
|
179
258
|
Copyright (c) Microsoft Corporation.
|
|
180
259
|
|
|
@@ -746,22 +825,22 @@ function initialize(options) {
|
|
|
746
825
|
}
|
|
747
826
|
});
|
|
748
827
|
}
|
|
749
|
-
function verifyToken(token, baseUrl) {
|
|
828
|
+
function verifyToken(token, baseUrl, headers = {}) {
|
|
750
829
|
const _baseUrl = baseUrl ? baseUrl : service ? service.baseUrl : undefined;
|
|
751
830
|
if (!_baseUrl) {
|
|
752
831
|
throw new Error("You should pass the base url of the server or call the initialize method.");
|
|
753
832
|
}
|
|
754
833
|
const req = new Axios({ baseURL: _baseUrl });
|
|
755
|
-
return req.get(`${identitySegment}/verify`, { headers: { Authorization: token } });
|
|
834
|
+
return req.get(`${identitySegment}/verify`, { headers: { Authorization: token, ...headers } });
|
|
756
835
|
}
|
|
757
|
-
async function login(identifier, password, tokenLifeSpan) {
|
|
758
|
-
checkInitialized(authorization);
|
|
836
|
+
async function login(identifier, password, tokenLifeSpan, headers) {
|
|
837
|
+
checkInitialized(authorization, service);
|
|
759
838
|
return service
|
|
760
839
|
.post("/passport/identify", {
|
|
761
840
|
identifier,
|
|
762
841
|
password,
|
|
763
842
|
expires: tokenLifeSpan
|
|
764
|
-
})
|
|
843
|
+
}, { headers })
|
|
765
844
|
.then(r => {
|
|
766
845
|
if (isTokenScheme(r)) {
|
|
767
846
|
return r.token;
|
|
@@ -777,7 +856,7 @@ function isChallenge(tokenOrChallenge) {
|
|
|
777
856
|
return typeof tokenOrChallenge.show == "function" && typeof tokenOrChallenge.answer == "function";
|
|
778
857
|
}
|
|
779
858
|
async function loginWithStrategy(id) {
|
|
780
|
-
checkInitialized(authorization);
|
|
859
|
+
checkInitialized(authorization, service);
|
|
781
860
|
const { url, state } = await service.get(`/passport/strategy/${id}/url`);
|
|
782
861
|
const token = new Observable(observer => {
|
|
783
862
|
service
|
|
@@ -803,71 +882,81 @@ async function loginWithStrategy(id) {
|
|
|
803
882
|
}
|
|
804
883
|
exports.authfactor = void 0;
|
|
805
884
|
(function (authfactor) {
|
|
806
|
-
function list() {
|
|
807
|
-
return service.get("passport/identity/factors");
|
|
885
|
+
function list(headers) {
|
|
886
|
+
return service.get("passport/identity/factors", { headers });
|
|
808
887
|
}
|
|
809
888
|
authfactor.list = list;
|
|
810
|
-
async function register(identityId, factor) {
|
|
811
|
-
const response = await service.post(`passport/identity/${identityId}/start-factor-verification`, factor);
|
|
889
|
+
async function register(identityId, factor, headers) {
|
|
890
|
+
const response = await service.post(`passport/identity/${identityId}/start-factor-verification`, factor, { headers });
|
|
812
891
|
const challenge = new _Challenge(response, response => response.message);
|
|
813
892
|
return challenge;
|
|
814
893
|
}
|
|
815
894
|
authfactor.register = register;
|
|
816
|
-
function unregister(identityId) {
|
|
817
|
-
return service.delete(`passport/identity/${identityId}/factors
|
|
895
|
+
function unregister(identityId, headers) {
|
|
896
|
+
return service.delete(`passport/identity/${identityId}/factors`, { headers });
|
|
818
897
|
}
|
|
819
898
|
authfactor.unregister = unregister;
|
|
820
899
|
})(exports.authfactor || (exports.authfactor = {}));
|
|
821
|
-
function getStrategies() {
|
|
822
|
-
return service.get("/passport/strategies");
|
|
900
|
+
function getStrategies(headers) {
|
|
901
|
+
return service.get("/passport/strategies", { headers });
|
|
823
902
|
}
|
|
824
|
-
function get(id) {
|
|
825
|
-
checkInitialized(authorization);
|
|
826
|
-
return service.get(`${identitySegment}/${id}
|
|
903
|
+
function get(id, headers) {
|
|
904
|
+
checkInitialized(authorization, service);
|
|
905
|
+
return service.get(`${identitySegment}/${id}`, { headers });
|
|
827
906
|
}
|
|
828
|
-
function getAll(queryParams = {}) {
|
|
829
|
-
checkInitialized(authorization);
|
|
907
|
+
function getAll(queryParams = {}, headers) {
|
|
908
|
+
checkInitialized(authorization, service);
|
|
830
909
|
return service.get(identitySegment, {
|
|
831
|
-
params: queryParams
|
|
910
|
+
params: queryParams,
|
|
911
|
+
headers
|
|
832
912
|
});
|
|
833
913
|
}
|
|
834
|
-
async function insert(identity) {
|
|
835
|
-
checkInitialized(authorization);
|
|
914
|
+
async function insert(identity, headers) {
|
|
915
|
+
checkInitialized(authorization, service);
|
|
836
916
|
identity = deepCopyJSON(identity);
|
|
837
917
|
const desiredPolicies = identity.policies;
|
|
838
918
|
delete identity.policies;
|
|
839
|
-
const insertedIdentity = await service.post(identitySegment, identity);
|
|
919
|
+
const insertedIdentity = await service.post(identitySegment, identity, { headers });
|
|
840
920
|
return exports.policy.attach(insertedIdentity._id, desiredPolicies).then(policies => {
|
|
841
921
|
insertedIdentity.policies = policies;
|
|
842
922
|
return insertedIdentity;
|
|
843
923
|
});
|
|
844
924
|
}
|
|
845
|
-
async function update(id, identity) {
|
|
846
|
-
checkInitialized(authorization);
|
|
925
|
+
async function update(id, identity, headers) {
|
|
926
|
+
checkInitialized(authorization, service);
|
|
847
927
|
const existingIdentity = await service.get(`${identitySegment}/${id}`);
|
|
848
928
|
identity = deepCopyJSON(identity);
|
|
849
929
|
const desiredPolicies = identity.policies;
|
|
850
930
|
delete identity.policies;
|
|
851
931
|
const { onlyInFirst: policiesForDetach, onlyInSecond: policiesForAttach } = getArrayDifferences(existingIdentity.policies, desiredPolicies);
|
|
852
|
-
const updatedIdentity = await service.put(`${identitySegment}/${id}`, identity
|
|
932
|
+
const updatedIdentity = await service.put(`${identitySegment}/${id}`, identity, {
|
|
933
|
+
headers
|
|
934
|
+
});
|
|
853
935
|
updatedIdentity.policies = desiredPolicies;
|
|
854
936
|
await exports.policy.attach(id, policiesForAttach);
|
|
855
937
|
await exports.policy.detach(id, policiesForDetach);
|
|
856
938
|
return updatedIdentity;
|
|
857
939
|
}
|
|
858
|
-
function remove(id) {
|
|
859
|
-
checkInitialized(authorization);
|
|
860
|
-
return service.delete(`${identitySegment}/${id}
|
|
940
|
+
function remove(id, headers) {
|
|
941
|
+
checkInitialized(authorization, service);
|
|
942
|
+
return service.delete(`${identitySegment}/${id}`, { headers });
|
|
943
|
+
}
|
|
944
|
+
function removeMany(ids, headers) {
|
|
945
|
+
checkInitialized(authorization, service);
|
|
946
|
+
const batchReqs = Batch.prepareRemoveRequest(ids, identitySegment, service.getAuthorization(), headers);
|
|
947
|
+
return service
|
|
948
|
+
.post("batch", batchReqs, { headers })
|
|
949
|
+
.then(response => Batch.handleBatchResponse(batchReqs, response));
|
|
861
950
|
}
|
|
862
951
|
exports.policy = void 0;
|
|
863
952
|
(function (policy) {
|
|
864
|
-
function attach(identityId, policyIds = []) {
|
|
865
|
-
checkInitialized(authorization);
|
|
953
|
+
function attach(identityId, policyIds = [], headers) {
|
|
954
|
+
checkInitialized(authorization, service);
|
|
866
955
|
const promises = [];
|
|
867
956
|
const attachedPolicies = new Set();
|
|
868
957
|
for (const policyId of policyIds) {
|
|
869
958
|
const promise = service
|
|
870
|
-
.put(`${identitySegment}/${identityId}/policy/${policyId}`, {})
|
|
959
|
+
.put(`${identitySegment}/${identityId}/policy/${policyId}`, {}, { headers })
|
|
871
960
|
.then(() => attachedPolicies.add(policyId))
|
|
872
961
|
.catch(e => {
|
|
873
962
|
console.error(`Failed to attach policy with id ${policyId}: `, e);
|
|
@@ -878,13 +967,13 @@ exports.policy = void 0;
|
|
|
878
967
|
return Promise.all(promises).then(() => Array.from(attachedPolicies));
|
|
879
968
|
}
|
|
880
969
|
policy.attach = attach;
|
|
881
|
-
function detach(identityId, policyIds = []) {
|
|
882
|
-
checkInitialized(authorization);
|
|
970
|
+
function detach(identityId, policyIds = [], headers) {
|
|
971
|
+
checkInitialized(authorization, service);
|
|
883
972
|
const promises = [];
|
|
884
973
|
const detachedPolicies = new Set();
|
|
885
974
|
for (const policyId of policyIds) {
|
|
886
975
|
const promise = service
|
|
887
|
-
.delete(`${identitySegment}/${identityId}/policy/${policyId}
|
|
976
|
+
.delete(`${identitySegment}/${identityId}/policy/${policyId}`, { headers })
|
|
888
977
|
.then(() => detachedPolicies.add(policyId))
|
|
889
978
|
.catch(e => {
|
|
890
979
|
console.error(`Failed to detach policy with id ${policyId}: `, e);
|
|
@@ -913,6 +1002,7 @@ exports.isChallenge = isChallenge;
|
|
|
913
1002
|
exports.login = login;
|
|
914
1003
|
exports.loginWithStrategy = loginWithStrategy;
|
|
915
1004
|
exports.remove = remove;
|
|
1005
|
+
exports.removeMany = removeMany;
|
|
916
1006
|
exports.update = update;
|
|
917
1007
|
exports.verifyToken = verifyToken;
|
|
918
1008
|
//# sourceMappingURL=index.js.map
|