@qikdev/sdk 0.1.16 → 0.1.20
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/package.json +2 -2
- package/src/api/qik.access.js +407 -53
- package/src/api/qik.api.js +22 -63
- package/src/api/qik.auth.js +143 -105
- package/src/api/qik.content.js +538 -8
- package/src/api/qik.core.js +46 -3
- package/src/api/qik.files.js +88 -0
- package/src/api/qik.filter.js +255 -0
- package/src/api/qik.system.js +42 -0
- package/src/api/qik.utils.js +653 -91
- package/src/index.js +3 -2
- package/src/version.js +1 -1
package/src/api/qik.auth.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
import _ from 'lodash';
|
|
3
2
|
import { EventDispatcher } from './qik.utils.js';
|
|
4
3
|
|
|
5
4
|
///////////////////////////////////////////////////
|
|
@@ -25,12 +24,13 @@ var QikAuth = function(qik) {
|
|
|
25
24
|
|
|
26
25
|
var defaultStore = {};
|
|
27
26
|
var store = defaultStore;
|
|
27
|
+
const tokenBufferSeconds = 10;
|
|
28
28
|
|
|
29
29
|
///////////////////////////////////////////////////
|
|
30
30
|
///////////////////////////////////////////////////
|
|
31
31
|
|
|
32
32
|
var service = {
|
|
33
|
-
debug:
|
|
33
|
+
debug: false,
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
Object.defineProperty(service, 'store', {
|
|
@@ -61,13 +61,6 @@ var QikAuth = function(qik) {
|
|
|
61
61
|
dispatcher.dispatch('change', user, parameters);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
///////////////////////////////////////////////////
|
|
65
|
-
|
|
66
|
-
function log(message) {
|
|
67
|
-
if (service.debug) {
|
|
68
|
-
console.log(message);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
64
|
|
|
72
65
|
///////////////////////////////////////////////////
|
|
73
66
|
|
|
@@ -85,7 +78,7 @@ var QikAuth = function(qik) {
|
|
|
85
78
|
|
|
86
79
|
store.user = user;
|
|
87
80
|
|
|
88
|
-
|
|
81
|
+
|
|
89
82
|
return dispatch(parameters)
|
|
90
83
|
}
|
|
91
84
|
|
|
@@ -112,7 +105,7 @@ var QikAuth = function(qik) {
|
|
|
112
105
|
|
|
113
106
|
|
|
114
107
|
|
|
115
|
-
|
|
108
|
+
|
|
116
109
|
|
|
117
110
|
|
|
118
111
|
if (qik.withCredentials) {
|
|
@@ -322,7 +315,7 @@ var QikAuth = function(qik) {
|
|
|
322
315
|
|
|
323
316
|
if (autoAuthenticate) {
|
|
324
317
|
store.user = res.data;
|
|
325
|
-
|
|
318
|
+
|
|
326
319
|
dispatch();
|
|
327
320
|
// if (service.onChange) {
|
|
328
321
|
// service.onChange(store.user);
|
|
@@ -651,6 +644,49 @@ var QikAuth = function(qik) {
|
|
|
651
644
|
}
|
|
652
645
|
|
|
653
646
|
|
|
647
|
+
///////////////////////////////////////////////////
|
|
648
|
+
|
|
649
|
+
service.ensureValidToken = async function() {
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
var currentUser = service.getCurrentUser();
|
|
654
|
+
if (!currentUser) {
|
|
655
|
+
|
|
656
|
+
return Promise.reject('No user');
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
var { token } = currentUser;
|
|
660
|
+
if (!token) {
|
|
661
|
+
|
|
662
|
+
return Promise.reject('No token');
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/////////////////////////////////////////////////////
|
|
666
|
+
|
|
667
|
+
//Check our date
|
|
668
|
+
var now = new Date();
|
|
669
|
+
|
|
670
|
+
//Give us a bit of buffer so that the backend doesn't beat us to
|
|
671
|
+
//retiring the token
|
|
672
|
+
now.setSeconds(now.getSeconds() + tokenBufferSeconds);
|
|
673
|
+
|
|
674
|
+
/////////////////////////////////////////////////////
|
|
675
|
+
|
|
676
|
+
var expires = new Date(token.expires);
|
|
677
|
+
|
|
678
|
+
//If the token is still fresh
|
|
679
|
+
if (now < expires) {
|
|
680
|
+
|
|
681
|
+
return token;
|
|
682
|
+
} else {
|
|
683
|
+
|
|
684
|
+
return await service.refreshAccessToken(token.refreshToken);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
}
|
|
689
|
+
|
|
654
690
|
///////////////////////////////////////////////////
|
|
655
691
|
|
|
656
692
|
/**
|
|
@@ -664,33 +700,32 @@ var QikAuth = function(qik) {
|
|
|
664
700
|
|
|
665
701
|
const refreshContext = {};
|
|
666
702
|
|
|
667
|
-
|
|
668
|
-
service.refreshAccessToken = function(refreshToken) {
|
|
703
|
+
service.refreshAccessToken = async function(refreshToken) {
|
|
669
704
|
|
|
670
705
|
// /////////////////////////////////////////////
|
|
671
706
|
|
|
672
707
|
// if (appContext) {
|
|
673
|
-
|
|
708
|
+
|
|
674
709
|
// } else {
|
|
675
|
-
|
|
710
|
+
|
|
676
711
|
// }
|
|
677
712
|
|
|
678
713
|
// /////////////////////////////////////////////
|
|
679
714
|
|
|
680
715
|
//If there is already a request in progress
|
|
681
716
|
if (refreshContext.inflightRefreshRequest) {
|
|
682
|
-
|
|
717
|
+
|
|
683
718
|
return refreshContext.inflightRefreshRequest;
|
|
684
719
|
}
|
|
685
720
|
|
|
686
721
|
/////////////////////////////////////////////////////
|
|
687
722
|
|
|
688
723
|
//Create an refresh request
|
|
689
|
-
|
|
724
|
+
|
|
690
725
|
refreshContext.inflightRefreshRequest = new Promise(function(resolve, reject) {
|
|
691
726
|
|
|
692
727
|
|
|
693
|
-
|
|
728
|
+
|
|
694
729
|
|
|
695
730
|
//Bypass the interceptor on all token refresh calls
|
|
696
731
|
//Because we don't need to add the access token etc onto it
|
|
@@ -699,21 +734,21 @@ var QikAuth = function(qik) {
|
|
|
699
734
|
refreshToken: refreshToken
|
|
700
735
|
}, {
|
|
701
736
|
bypassInterceptor: true,
|
|
702
|
-
withoutToken:true,
|
|
737
|
+
withoutToken: true,
|
|
703
738
|
})
|
|
704
739
|
.then(function tokenRefreshComplete(res) {
|
|
705
740
|
|
|
706
741
|
//Update the user with any changes
|
|
707
742
|
//returned back from the refresh request
|
|
708
743
|
if (!res) {
|
|
709
|
-
|
|
744
|
+
|
|
710
745
|
refreshContext.inflightRefreshRequest = null;
|
|
711
746
|
return reject();
|
|
712
747
|
|
|
713
|
-
} else {
|
|
748
|
+
} else {
|
|
714
749
|
//Update with our new session
|
|
715
750
|
store.user = res.data;
|
|
716
|
-
|
|
751
|
+
|
|
717
752
|
|
|
718
753
|
dispatch();
|
|
719
754
|
}
|
|
@@ -729,11 +764,11 @@ var QikAuth = function(qik) {
|
|
|
729
764
|
|
|
730
765
|
})
|
|
731
766
|
.catch(function(err) {
|
|
732
|
-
|
|
767
|
+
|
|
733
768
|
|
|
734
769
|
//TODO Check if invalid_refresh_token
|
|
735
770
|
|
|
736
|
-
|
|
771
|
+
|
|
737
772
|
setTimeout(function() {
|
|
738
773
|
refreshContext.inflightRefreshRequest = null;
|
|
739
774
|
});
|
|
@@ -777,8 +812,8 @@ var QikAuth = function(qik) {
|
|
|
777
812
|
} else {
|
|
778
813
|
store.user = null;
|
|
779
814
|
}
|
|
815
|
+
|
|
780
816
|
|
|
781
|
-
log('qik.auth > server session synced');
|
|
782
817
|
retryCount = 0;
|
|
783
818
|
|
|
784
819
|
dispatch();
|
|
@@ -790,7 +825,7 @@ var QikAuth = function(qik) {
|
|
|
790
825
|
retryCount = 0;
|
|
791
826
|
dispatch();
|
|
792
827
|
// } else {
|
|
793
|
-
|
|
828
|
+
|
|
794
829
|
// retryCount++;
|
|
795
830
|
// service.sync();
|
|
796
831
|
// }
|
|
@@ -811,7 +846,7 @@ var QikAuth = function(qik) {
|
|
|
811
846
|
}
|
|
812
847
|
|
|
813
848
|
|
|
814
|
-
service.getCurrentToken = function() {
|
|
849
|
+
service.getCurrentToken = function(ensureFresh) {
|
|
815
850
|
|
|
816
851
|
var details = service.getCurrentUser();
|
|
817
852
|
|
|
@@ -832,105 +867,108 @@ var QikAuth = function(qik) {
|
|
|
832
867
|
|
|
833
868
|
qik.api.interceptors.request.use(async function(config) {
|
|
834
869
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
870
|
+
//If we want to bypass the interceptor
|
|
871
|
+
//then just return the request
|
|
872
|
+
if (config.bypassInterceptor) {
|
|
873
|
+
return config;
|
|
874
|
+
}
|
|
840
875
|
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
876
|
+
//////////////////////////////
|
|
877
|
+
//////////////////////////////
|
|
878
|
+
//////////////////////////////
|
|
879
|
+
//////////////////////////////
|
|
845
880
|
|
|
846
|
-
|
|
847
|
-
|
|
881
|
+
//Get the original request
|
|
882
|
+
var originalRequest = config;
|
|
848
883
|
|
|
849
|
-
|
|
884
|
+
//////////////////////////////
|
|
850
885
|
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
886
|
+
var userDetails = await service.getCurrentUser();
|
|
887
|
+
var accessToken;
|
|
888
|
+
var refreshToken;
|
|
889
|
+
var expiryDate;
|
|
855
890
|
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
891
|
+
if (userDetails) {
|
|
892
|
+
var { token } = userDetails;
|
|
893
|
+
if (token) {
|
|
894
|
+
accessToken = token.accessToken;
|
|
895
|
+
refreshToken = token.refreshToken;
|
|
896
|
+
expiryDate = token.expires;
|
|
897
|
+
}
|
|
862
898
|
}
|
|
863
|
-
}
|
|
864
899
|
|
|
865
|
-
|
|
900
|
+
//////////////////////////////
|
|
866
901
|
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
902
|
+
//If there is a user token
|
|
903
|
+
if (accessToken) {
|
|
904
|
+
//Set the token of the request as the user's access token
|
|
905
|
+
originalRequest.headers['Authorization'] = `Bearer ${accessToken}`;
|
|
906
|
+
|
|
872
907
|
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
908
|
+
} else {
|
|
909
|
+
//Return the original request without a token
|
|
910
|
+
|
|
911
|
+
return originalRequest;
|
|
912
|
+
}
|
|
878
913
|
|
|
879
|
-
|
|
914
|
+
/////////////////////////////////////////////////////
|
|
880
915
|
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
916
|
+
//If no refresh token
|
|
917
|
+
if (!refreshToken) {
|
|
918
|
+
|
|
884
919
|
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
920
|
+
//Continue with the original request
|
|
921
|
+
return originalRequest;
|
|
922
|
+
}
|
|
888
923
|
|
|
889
|
-
|
|
924
|
+
/////////////////////////////////////////////////////
|
|
890
925
|
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
926
|
+
//We have a refresh token so we need to check
|
|
927
|
+
//whether our access token is stale and needs to be refreshed
|
|
928
|
+
var now = new Date();
|
|
894
929
|
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
930
|
+
//Give us a bit of buffer so that the backend doesn't beat us to
|
|
931
|
+
//retiring the token
|
|
932
|
+
now.setSeconds(now.getSeconds() + tokenBufferSeconds);
|
|
898
933
|
|
|
899
|
-
|
|
934
|
+
/////////////////////////////////////////////////////
|
|
900
935
|
|
|
901
|
-
|
|
936
|
+
var expires = new Date(expiryDate);
|
|
902
937
|
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
938
|
+
//If the token is still fresh
|
|
939
|
+
if (now < expires) {
|
|
940
|
+
//Return the original request
|
|
941
|
+
return originalRequest;
|
|
942
|
+
}
|
|
908
943
|
|
|
909
|
-
|
|
944
|
+
/////////////////////////////////////////////////////
|
|
910
945
|
|
|
911
|
-
|
|
946
|
+
|
|
912
947
|
|
|
913
|
-
|
|
948
|
+
return new Promise(async function(resolve, reject) {
|
|
914
949
|
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
950
|
+
//Refresh the token
|
|
951
|
+
await service.refreshAccessToken(refreshToken)
|
|
952
|
+
.then(function(newToken) {
|
|
953
|
+
|
|
954
|
+
//Update the original request with our new token
|
|
955
|
+
originalRequest.headers['Authorization'] = `Bearer ${newToken}`;
|
|
956
|
+
//And continue onward
|
|
957
|
+
return resolve(originalRequest);
|
|
958
|
+
})
|
|
959
|
+
.catch(function(err) {
|
|
960
|
+
|
|
961
|
+
return reject(err);
|
|
962
|
+
});
|
|
963
|
+
});
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
},
|
|
967
|
+
function(error) {
|
|
968
|
+
return Promise.reject(error);
|
|
969
|
+
})
|
|
929
970
|
|
|
930
971
|
|
|
931
|
-
}, function(error) {
|
|
932
|
-
return Promise.reject(error);
|
|
933
|
-
})
|
|
934
972
|
|
|
935
973
|
/////////////////////////////////////////////////////
|
|
936
974
|
|
|
@@ -941,9 +979,9 @@ var QikAuth = function(qik) {
|
|
|
941
979
|
//////////////////////////////
|
|
942
980
|
|
|
943
981
|
//Get the response status
|
|
944
|
-
var status =
|
|
982
|
+
var status = (err && err.response && err.response.status) || err.status;
|
|
945
983
|
|
|
946
|
-
|
|
984
|
+
|
|
947
985
|
switch (status) {
|
|
948
986
|
case 401:
|
|
949
987
|
service.logout();
|