@niledatabase/server 3.0.0-alpha.16 → 3.0.0-alpha.18

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.
@@ -2,7 +2,12 @@ import { Config } from '../utils/Config';
2
2
  /**
3
3
  * a helper function to log in server side.
4
4
  */
5
- export default function login(config: Config): ({ email, password, }: {
5
+ export default function serverAuth(config: Config, handlers: {
6
+ GET: (req: Request) => Promise<void | Response>;
7
+ POST: (req: Request) => Promise<void | Response>;
8
+ DELETE: (req: Request) => Promise<void | Response>;
9
+ PUT: (req: Request) => Promise<void | Response>;
10
+ }): ({ email, password, }: {
6
11
  email: string;
7
12
  password: string;
8
13
  }) => Promise<Headers>;
@@ -1399,13 +1399,11 @@ function auth(_x, _x2) {
1399
1399
  }
1400
1400
  function _auth() {
1401
1401
  _auth = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
1402
- var _Logger, info, sessionUrl, res;
1402
+ var _Logger, info, error, sessionUrl, res;
1403
1403
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1404
1404
  while (1) switch (_context.prev = _context.next) {
1405
1405
  case 0:
1406
- _Logger = Logger(_extends({}, config, {
1407
- debug: config.debug
1408
- }), '[nileauth]'), info = _Logger.info;
1406
+ _Logger = Logger(config, '[nileauth]'), info = _Logger.info, error = _Logger.error;
1409
1407
  info('checking auth');
1410
1408
  sessionUrl = config.api.basePath + "/auth/session";
1411
1409
  info('using session', sessionUrl);
@@ -1425,15 +1423,21 @@ function _auth() {
1425
1423
  return _context.abrupt("return", undefined);
1426
1424
  case 11:
1427
1425
  info('session active');
1428
- _context.next = 14;
1426
+ _context.prev = 12;
1427
+ _context.next = 15;
1429
1428
  return new Response(res.body).json();
1430
- case 14:
1431
- return _context.abrupt("return", _context.sent);
1432
1429
  case 15:
1430
+ return _context.abrupt("return", _context.sent);
1431
+ case 18:
1432
+ _context.prev = 18;
1433
+ _context.t0 = _context["catch"](12);
1434
+ error(_context.t0);
1435
+ return _context.abrupt("return", undefined);
1436
+ case 22:
1433
1437
  case "end":
1434
1438
  return _context.stop();
1435
1439
  }
1436
- }, _callee);
1440
+ }, _callee, null, [[12, 18]]);
1437
1441
  }));
1438
1442
  return _auth.apply(this, arguments);
1439
1443
  }
@@ -2797,81 +2801,96 @@ function matches(configRoutes, request) {
2797
2801
  }
2798
2802
 
2799
2803
  function GETTER(configRoutes, config) {
2804
+ var _Logger = Logger(config, '[GET MATCHER]'),
2805
+ info = _Logger.info,
2806
+ warn = _Logger.warn;
2800
2807
  return /*#__PURE__*/function () {
2801
2808
  var _GET = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2802
2809
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2803
2810
  while (1) switch (_context.prev = _context.next) {
2804
2811
  case 0:
2805
2812
  if (!matches$a(configRoutes, req)) {
2806
- _context.next = 2;
2813
+ _context.next = 3;
2807
2814
  break;
2808
2815
  }
2816
+ info('matches me');
2809
2817
  return _context.abrupt("return", route$a(req, config));
2810
- case 2:
2811
- if (!matches$9(configRoutes, req)) {
2812
- _context.next = 4;
2813
- break;
2814
- }
2815
- return _context.abrupt("return", route$9(req, config));
2816
- case 4:
2818
+ case 3:
2817
2819
  if (!matches$8(configRoutes, req)) {
2818
2820
  _context.next = 6;
2819
2821
  break;
2820
2822
  }
2823
+ info('matches tenant users');
2821
2824
  return _context.abrupt("return", route$8(req, config));
2822
2825
  case 6:
2826
+ if (!matches$9(configRoutes, req)) {
2827
+ _context.next = 9;
2828
+ break;
2829
+ }
2830
+ info('matches users');
2831
+ return _context.abrupt("return", route$9(req, config));
2832
+ case 9:
2823
2833
  if (!matches$7(configRoutes, req)) {
2824
- _context.next = 8;
2834
+ _context.next = 12;
2825
2835
  break;
2826
2836
  }
2837
+ info('matches tenants');
2827
2838
  return _context.abrupt("return", route$7(req, config));
2828
- case 8:
2839
+ case 12:
2829
2840
  if (!matches$5(configRoutes, req)) {
2830
- _context.next = 10;
2841
+ _context.next = 15;
2831
2842
  break;
2832
2843
  }
2844
+ info('matches session');
2833
2845
  return _context.abrupt("return", route$5(req, config));
2834
- case 10:
2846
+ case 15:
2835
2847
  if (!matches$6(configRoutes, req)) {
2836
- _context.next = 12;
2848
+ _context.next = 18;
2837
2849
  break;
2838
2850
  }
2851
+ info('matches signin');
2839
2852
  return _context.abrupt("return", route$6(req, config));
2840
- case 12:
2853
+ case 18:
2841
2854
  if (!matches$4(configRoutes, req)) {
2842
- _context.next = 14;
2855
+ _context.next = 21;
2843
2856
  break;
2844
2857
  }
2858
+ info('matches providers');
2845
2859
  return _context.abrupt("return", route$4(req, config));
2846
- case 14:
2860
+ case 21:
2847
2861
  if (!matches$3(configRoutes, req)) {
2848
- _context.next = 16;
2862
+ _context.next = 24;
2849
2863
  break;
2850
2864
  }
2865
+ info('matches csrf');
2851
2866
  return _context.abrupt("return", route$3(req, config));
2852
- case 16:
2867
+ case 24:
2853
2868
  if (!matches$2(configRoutes, req)) {
2854
- _context.next = 18;
2869
+ _context.next = 27;
2855
2870
  break;
2856
2871
  }
2872
+ info('matches callback');
2857
2873
  return _context.abrupt("return", route$2(req, config));
2858
- case 18:
2874
+ case 27:
2859
2875
  if (!matches$1(configRoutes, req)) {
2860
- _context.next = 20;
2876
+ _context.next = 30;
2861
2877
  break;
2862
2878
  }
2879
+ info('matches signout');
2863
2880
  return _context.abrupt("return", route$1(req, config));
2864
- case 20:
2881
+ case 30:
2865
2882
  if (!matches(configRoutes, req)) {
2866
- _context.next = 22;
2883
+ _context.next = 33;
2867
2884
  break;
2868
2885
  }
2886
+ info('matches error');
2869
2887
  return _context.abrupt("return", route(req, config));
2870
- case 22:
2888
+ case 33:
2889
+ warn('No GET routes matched');
2871
2890
  return _context.abrupt("return", new Response(null, {
2872
2891
  status: 404
2873
2892
  }));
2874
- case 23:
2893
+ case 35:
2875
2894
  case "end":
2876
2895
  return _context.stop();
2877
2896
  }
@@ -2885,69 +2904,82 @@ function GETTER(configRoutes, config) {
2885
2904
  }
2886
2905
 
2887
2906
  function POSTER(configRoutes, config) {
2907
+ var _Logger = Logger(config, '[POST MATCHER]'),
2908
+ info = _Logger.info,
2909
+ warn = _Logger.warn;
2888
2910
  return /*#__PURE__*/function () {
2889
2911
  var _POST = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2890
2912
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2891
2913
  while (1) switch (_context.prev = _context.next) {
2892
2914
  case 0:
2893
2915
  if (!matches$8(configRoutes, req)) {
2894
- _context.next = 2;
2916
+ _context.next = 3;
2895
2917
  break;
2896
2918
  }
2919
+ info('matches tenant users');
2897
2920
  return _context.abrupt("return", route$8(req, config));
2898
- case 2:
2921
+ case 3:
2899
2922
  if (!matches$9(configRoutes, req)) {
2900
- _context.next = 4;
2923
+ _context.next = 6;
2901
2924
  break;
2902
2925
  }
2926
+ info('matches users');
2903
2927
  return _context.abrupt("return", route$9(req, config));
2904
- case 4:
2928
+ case 6:
2905
2929
  if (!matches$7(configRoutes, req)) {
2906
- _context.next = 6;
2930
+ _context.next = 9;
2907
2931
  break;
2908
2932
  }
2933
+ info('matches tenants');
2909
2934
  return _context.abrupt("return", route$7(req, config));
2910
- case 6:
2935
+ case 9:
2911
2936
  if (!matches$5(configRoutes, req)) {
2912
- _context.next = 8;
2937
+ _context.next = 12;
2913
2938
  break;
2914
2939
  }
2940
+ info('matches session');
2915
2941
  return _context.abrupt("return", route$5(req, config));
2916
- case 8:
2942
+ case 12:
2917
2943
  if (!matches$6(configRoutes, req)) {
2918
- _context.next = 10;
2944
+ _context.next = 15;
2919
2945
  break;
2920
2946
  }
2947
+ info('matches signin');
2921
2948
  return _context.abrupt("return", route$6(req, config));
2922
- case 10:
2949
+ case 15:
2923
2950
  if (!matches$4(configRoutes, req)) {
2924
- _context.next = 12;
2951
+ _context.next = 18;
2925
2952
  break;
2926
2953
  }
2954
+ info('matches providers');
2927
2955
  return _context.abrupt("return", route$4(req, config));
2928
- case 12:
2956
+ case 18:
2929
2957
  if (!matches$3(configRoutes, req)) {
2930
- _context.next = 14;
2958
+ _context.next = 21;
2931
2959
  break;
2932
2960
  }
2961
+ info('matches csrf');
2933
2962
  return _context.abrupt("return", route$3(req, config));
2934
- case 14:
2963
+ case 21:
2935
2964
  if (!matches$2(configRoutes, req)) {
2936
- _context.next = 16;
2965
+ _context.next = 24;
2937
2966
  break;
2938
2967
  }
2968
+ info('matches callback');
2939
2969
  return _context.abrupt("return", route$2(req, config));
2940
- case 16:
2970
+ case 24:
2941
2971
  if (!matches$1(configRoutes, req)) {
2942
- _context.next = 18;
2972
+ _context.next = 27;
2943
2973
  break;
2944
2974
  }
2975
+ info('matches signout');
2945
2976
  return _context.abrupt("return", route$1(req, config));
2946
- case 18:
2977
+ case 27:
2978
+ warn('No POST routes matched');
2947
2979
  return _context.abrupt("return", new Response(null, {
2948
2980
  status: 404
2949
2981
  }));
2950
- case 19:
2982
+ case 29:
2951
2983
  case "end":
2952
2984
  return _context.stop();
2953
2985
  }
@@ -2961,27 +2993,33 @@ function POSTER(configRoutes, config) {
2961
2993
  }
2962
2994
 
2963
2995
  function DELETER(configRoutes, config) {
2996
+ var _Logger = Logger(config, '[DELETE MATCHER]'),
2997
+ info = _Logger.info,
2998
+ warn = _Logger.warn;
2964
2999
  return /*#__PURE__*/function () {
2965
3000
  var _DELETE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2966
3001
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2967
3002
  while (1) switch (_context.prev = _context.next) {
2968
3003
  case 0:
2969
3004
  if (!matches$8(configRoutes, req)) {
2970
- _context.next = 2;
3005
+ _context.next = 3;
2971
3006
  break;
2972
3007
  }
3008
+ info('matches tenant users');
2973
3009
  return _context.abrupt("return", route$8(req, config));
2974
- case 2:
3010
+ case 3:
2975
3011
  if (!matches$7(configRoutes, req)) {
2976
- _context.next = 4;
3012
+ _context.next = 6;
2977
3013
  break;
2978
3014
  }
3015
+ info('matches tenants');
2979
3016
  return _context.abrupt("return", route$7(req, config));
2980
- case 4:
3017
+ case 6:
3018
+ warn('No DELETE routes matched');
2981
3019
  return _context.abrupt("return", new Response(null, {
2982
3020
  status: 404
2983
3021
  }));
2984
- case 5:
3022
+ case 8:
2985
3023
  case "end":
2986
3024
  return _context.stop();
2987
3025
  }
@@ -2995,33 +3033,40 @@ function DELETER(configRoutes, config) {
2995
3033
  }
2996
3034
 
2997
3035
  function PUTER(configRoutes, config) {
3036
+ var _Logger = Logger(config, '[PUT MATCHER]'),
3037
+ info = _Logger.info,
3038
+ warn = _Logger.warn;
2998
3039
  return /*#__PURE__*/function () {
2999
3040
  var _PUT = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3000
3041
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3001
3042
  while (1) switch (_context.prev = _context.next) {
3002
3043
  case 0:
3003
3044
  if (!matches$8(configRoutes, req)) {
3004
- _context.next = 2;
3045
+ _context.next = 3;
3005
3046
  break;
3006
3047
  }
3048
+ info('matches tenant users');
3007
3049
  return _context.abrupt("return", route$8(req, config));
3008
- case 2:
3050
+ case 3:
3009
3051
  if (!matches$9(configRoutes, req)) {
3010
- _context.next = 4;
3052
+ _context.next = 6;
3011
3053
  break;
3012
3054
  }
3055
+ info('matches users');
3013
3056
  return _context.abrupt("return", route$9(req, config));
3014
- case 4:
3057
+ case 6:
3015
3058
  if (!matches$7(configRoutes, req)) {
3016
- _context.next = 6;
3059
+ _context.next = 9;
3017
3060
  break;
3018
3061
  }
3062
+ info('matches tenants');
3019
3063
  return _context.abrupt("return", route$7(req, config));
3020
- case 6:
3064
+ case 9:
3065
+ warn('No PUT routes matched');
3021
3066
  return _context.abrupt("return", new Response(null, {
3022
3067
  status: 404
3023
3068
  }));
3024
- case 7:
3069
+ case 11:
3025
3070
  case "end":
3026
3071
  return _context.stop();
3027
3072
  }
@@ -3068,14 +3113,16 @@ var appRoutes = function appRoutes(prefix) {
3068
3113
  };
3069
3114
  };
3070
3115
 
3116
+ // url host does not matter, we only match on the 1st leg by path
3117
+ var ORIGIN = 'http://localhost';
3071
3118
  /**
3072
3119
  * a helper function to log in server side.
3073
3120
  */
3074
- function login(config) {
3121
+ function serverAuth(config, handlers) {
3075
3122
  var _Logger = Logger(config, '[server side login]'),
3076
3123
  info = _Logger.info,
3077
3124
  error = _Logger.error;
3078
- var routes = proxyRoutes(config);
3125
+ var routes = appRoutes(config.routePrefix);
3079
3126
  return /*#__PURE__*/function () {
3080
3127
  var _login = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
3081
3128
  var _providers, _exec;
@@ -3091,7 +3138,7 @@ function login(config) {
3091
3138
  throw new Error('Server side login requires a user email and password.');
3092
3139
  case 3:
3093
3140
  info('Obtaining providers for', email);
3094
- sessionUrl = new URL(routes.PROVIDERS);
3141
+ sessionUrl = new URL("" + ORIGIN + routes.PROVIDERS);
3095
3142
  sessionReq = new Request(sessionUrl, {
3096
3143
  method: 'GET',
3097
3144
  headers: new Headers({
@@ -3099,75 +3146,81 @@ function login(config) {
3099
3146
  })
3100
3147
  });
3101
3148
  _context.next = 8;
3102
- return fetch(sessionReq);
3149
+ return handlers.POST(sessionReq);
3103
3150
  case 8:
3104
3151
  sessionRes = _context.sent;
3105
- _context.prev = 9;
3106
- _context.next = 12;
3152
+ if (!((sessionRes == null ? void 0 : sessionRes.status) === 404)) {
3153
+ _context.next = 11;
3154
+ break;
3155
+ }
3156
+ throw new Error('Unable to login, cannot find region api.');
3157
+ case 11:
3158
+ _context.prev = 11;
3159
+ _context.next = 14;
3107
3160
  return sessionRes == null ? void 0 : sessionRes.json();
3108
- case 12:
3161
+ case 14:
3109
3162
  providers = _context.sent;
3110
- _context.next = 19;
3163
+ _context.next = 21;
3111
3164
  break;
3112
- case 15:
3113
- _context.prev = 15;
3114
- _context.t0 = _context["catch"](9);
3165
+ case 17:
3166
+ _context.prev = 17;
3167
+ _context.t0 = _context["catch"](11);
3115
3168
  info(sessionUrl, sessionRes);
3116
3169
  error(_context.t0);
3117
- case 19:
3170
+ case 21:
3118
3171
  info('Obtaining csrf');
3119
- csrf = new URL(routes.CSRF);
3172
+ csrf = new URL("" + ORIGIN + routes.CSRF);
3120
3173
  csrfReq = new Request(csrf, {
3121
3174
  method: 'GET',
3122
3175
  headers: new Headers({
3123
3176
  host: sessionUrl.host
3124
3177
  })
3125
3178
  });
3126
- _context.next = 24;
3127
- return fetch(csrfReq);
3128
- case 24:
3179
+ _context.next = 26;
3180
+ return handlers.POST(csrfReq);
3181
+ case 26:
3129
3182
  csrfRes = _context.sent;
3130
- _context.prev = 25;
3131
- _context.next = 28;
3183
+ _context.prev = 27;
3184
+ _context.next = 30;
3132
3185
  return csrfRes == null ? void 0 : csrfRes.json();
3133
- case 28:
3186
+ case 30:
3134
3187
  _context.t1 = _yield$csrfRes$json = _context.sent;
3135
3188
  if (!(_context.t1 != null)) {
3136
- _context.next = 33;
3189
+ _context.next = 35;
3137
3190
  break;
3138
3191
  }
3139
3192
  _context.t2 = _yield$csrfRes$json;
3140
- _context.next = 34;
3193
+ _context.next = 36;
3141
3194
  break;
3142
- case 33:
3195
+ case 35:
3143
3196
  _context.t2 = {};
3144
- case 34:
3197
+ case 36:
3145
3198
  json = _context.t2;
3146
3199
  csrfToken = json == null ? void 0 : json.csrfToken;
3147
- _context.next = 42;
3200
+ _context.next = 44;
3148
3201
  break;
3149
- case 38:
3150
- _context.prev = 38;
3151
- _context.t3 = _context["catch"](25);
3202
+ case 40:
3203
+ _context.prev = 40;
3204
+ _context.t3 = _context["catch"](27);
3152
3205
  info(sessionUrl, csrfRes);
3153
3206
  error(_context.t3, csrfRes);
3154
- case 42:
3207
+ case 44:
3155
3208
  _ref2 = (_providers = providers) != null ? _providers : {}, credentials = _ref2.credentials;
3156
3209
  csrfCookie = csrfRes == null ? void 0 : csrfRes.headers.get('set-cookie');
3157
3210
  if (credentials) {
3158
- _context.next = 46;
3211
+ _context.next = 48;
3159
3212
  break;
3160
3213
  }
3161
3214
  throw new Error('Unable to obtain credential provider. Aborting server side login.');
3162
- case 46:
3163
- signInUrl = new URL(routes.SIGNIN);
3215
+ case 48:
3216
+ signInUrl = new URL(credentials.callbackUrl);
3164
3217
  if (csrfCookie) {
3165
- _context.next = 49;
3218
+ _context.next = 51;
3166
3219
  break;
3167
3220
  }
3168
3221
  throw new Error('Unable to authenticate REST');
3169
- case 49:
3170
- info('Attempting sign in via proxy', signInUrl.href, 'with email', email);
3222
+ case 51:
3223
+ info('Attempting sign in with email', email);
3171
3224
  postReq = new Request(signInUrl, {
3172
3225
  method: 'POST',
3173
3226
  headers: new Headers({
@@ -3181,27 +3234,33 @@ function login(config) {
3181
3234
  callbackUrl: credentials.callbackUrl
3182
3235
  })
3183
3236
  });
3184
- _context.next = 53;
3185
- return fetch(postReq);
3186
- case 53:
3237
+ _context.next = 55;
3238
+ return handlers.POST(postReq);
3239
+ case 55:
3187
3240
  loginRes = _context.sent;
3188
3241
  authCookie = loginRes == null ? void 0 : loginRes.headers.get('set-cookie');
3189
3242
  if (authCookie) {
3190
- _context.next = 57;
3243
+ _context.next = 59;
3191
3244
  break;
3192
3245
  }
3193
3246
  throw new Error('authentication failed');
3194
- case 57:
3247
+ case 59:
3195
3248
  _ref3 = (_exec = /(nile\.session-token=.+?);/.exec(authCookie)) != null ? _exec : [], token = _ref3[1];
3249
+ if (token) {
3250
+ _context.next = 62;
3251
+ break;
3252
+ }
3253
+ throw new Error('Server login failed');
3254
+ case 62:
3196
3255
  info('Server login successful', authCookie, csrfCookie);
3197
3256
  return _context.abrupt("return", new Headers({
3198
3257
  cookie: [token, csrfCookie].join('; ')
3199
3258
  }));
3200
- case 60:
3259
+ case 64:
3201
3260
  case "end":
3202
3261
  return _context.stop();
3203
3262
  }
3204
- }, _callee, null, [[9, 15], [25, 38]]);
3263
+ }, _callee, null, [[11, 17], [27, 40]]);
3205
3264
  }));
3206
3265
  function login(_x) {
3207
3266
  return _login.apply(this, arguments);
@@ -3807,7 +3866,7 @@ var Api = /*#__PURE__*/function () {
3807
3866
  while (1) switch (_context.prev = _context.next) {
3808
3867
  case 0:
3809
3868
  _context.next = 2;
3810
- return login(this.config)(payload);
3869
+ return serverAuth(this.config, this.handlers)(payload);
3811
3870
  case 2:
3812
3871
  this.headers = _context.sent;
3813
3872
  case 3:
@@ -3816,10 +3875,10 @@ var Api = /*#__PURE__*/function () {
3816
3875
  }
3817
3876
  }, _callee, this);
3818
3877
  }));
3819
- function login$1(_x) {
3878
+ function login(_x) {
3820
3879
  return _login.apply(this, arguments);
3821
3880
  }
3822
- return login$1;
3881
+ return login;
3823
3882
  }();
3824
3883
  return _createClass(Api, [{
3825
3884
  key: "headers",