@feathersjs/authentication-client 5.0.0-pre.3 → 5.0.0-pre.30

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/src/core.ts CHANGED
@@ -1,200 +1,200 @@
1
- import { NotAuthenticated, FeathersError } from '@feathersjs/errors';
2
- import { Application, Params } from '@feathersjs/feathers';
3
- import { AuthenticationRequest, AuthenticationResult } from '@feathersjs/authentication';
4
- import { Storage, StorageWrapper } from './storage';
1
+ import { NotAuthenticated, FeathersError } from '@feathersjs/errors'
2
+ import { Application, Params } from '@feathersjs/feathers'
3
+ import { AuthenticationRequest, AuthenticationResult } from '@feathersjs/authentication'
4
+ import { Storage, StorageWrapper } from './storage'
5
5
 
6
6
  class OauthError extends FeathersError {
7
- constructor (message: string, data?: any) {
8
- super(message, 'OauthError', 401, 'oauth-error', data);
7
+ constructor(message: string, data?: any) {
8
+ super(message, 'OauthError', 401, 'oauth-error', data)
9
9
  }
10
10
  }
11
11
 
12
- const getMatch = (location: Location, key: string): [ string, RegExp ] => {
13
- const regex = new RegExp(`(?:\&?)${key}=([^&]*)`);
14
- const match = location.hash ? location.hash.match(regex) : null;
12
+ const getMatch = (location: Location, key: string): [string, RegExp] => {
13
+ const regex = new RegExp(`(?:\&?)${key}=([^&]*)`)
14
+ const match = location.hash ? location.hash.match(regex) : null
15
15
 
16
16
  if (match !== null) {
17
- const [ , value ] = match;
17
+ const [, value] = match
18
18
 
19
- return [ value, regex ];
19
+ return [value, regex]
20
20
  }
21
21
 
22
- return [ null, regex ];
23
- };
22
+ return [null, regex]
23
+ }
24
24
 
25
- export type ClientConstructor = new (app: Application, options: AuthenticationClientOptions)
26
- => AuthenticationClient;
25
+ export type ClientConstructor = new (
26
+ app: Application,
27
+ options: AuthenticationClientOptions
28
+ ) => AuthenticationClient
27
29
 
28
30
  export interface AuthenticationClientOptions {
29
- storage: Storage;
30
- header: string;
31
- scheme: string;
32
- storageKey: string;
33
- locationKey: string;
34
- locationErrorKey: string;
35
- jwtStrategy: string;
36
- path: string;
37
- Authentication: ClientConstructor;
31
+ storage: Storage
32
+ header: string
33
+ scheme: string
34
+ storageKey: string
35
+ locationKey: string
36
+ locationErrorKey: string
37
+ jwtStrategy: string
38
+ path: string
39
+ Authentication: ClientConstructor
38
40
  }
39
41
 
40
42
  export class AuthenticationClient {
41
- app: Application;
42
- authenticated: boolean;
43
- options: AuthenticationClientOptions;
43
+ app: Application
44
+ authenticated: boolean
45
+ options: AuthenticationClientOptions
44
46
 
45
- constructor (app: Application, options: AuthenticationClientOptions) {
46
- const socket = app.io;
47
- const storage = new StorageWrapper(app.get('storage') || options.storage);
47
+ constructor(app: Application, options: AuthenticationClientOptions) {
48
+ const socket = app.io
49
+ const storage = new StorageWrapper(app.get('storage') || options.storage)
48
50
 
49
- this.app = app;
50
- this.options = options;
51
- this.authenticated = false;
52
- this.app.set('storage', storage);
51
+ this.app = app
52
+ this.options = options
53
+ this.authenticated = false
54
+ this.app.set('storage', storage)
53
55
 
54
56
  if (socket) {
55
- this.handleSocket(socket);
57
+ this.handleSocket(socket)
56
58
  }
57
59
  }
58
60
 
59
- get service () {
60
- return this.app.service(this.options.path);
61
+ get service() {
62
+ return this.app.service(this.options.path)
61
63
  }
62
64
 
63
- get storage () {
64
- return this.app.get('storage') as Storage;
65
+ get storage() {
66
+ return this.app.get('storage') as Storage
65
67
  }
66
68
 
67
- handleSocket (socket: any) {
69
+ handleSocket(socket: any) {
68
70
  // Connection events happen on every reconnect
69
- const connected = this.app.io ? 'connect' : 'open';
70
- const disconnected = this.app.io ? 'disconnect' : 'disconnection';
71
+ const connected = this.app.io ? 'connect' : 'open'
72
+ const disconnected = this.app.io ? 'disconnect' : 'disconnection'
71
73
 
72
74
  socket.on(disconnected, () => {
73
- const authPromise = new Promise(resolve =>
74
- socket.once(connected, (data: any) => resolve(data))
75
- )
76
- // Only reconnect when `reAuthenticate()` or `authenticate()`
77
- // has been called explicitly first
78
- // Force reauthentication with the server
79
- .then(() => this.authenticated ? this.reAuthenticate(true) : null);
80
-
81
- this.app.set('authentication', authPromise);
82
- });
75
+ const authPromise = new Promise((resolve) => socket.once(connected, (data: any) => resolve(data)))
76
+ // Only reconnect when `reAuthenticate()` or `authenticate()`
77
+ // has been called explicitly first
78
+ // Force reauthentication with the server
79
+ .then(() => (this.authenticated ? this.reAuthenticate(true) : null))
80
+
81
+ this.app.set('authentication', authPromise)
82
+ })
83
83
  }
84
84
 
85
- getFromLocation (location: Location) {
86
- const [ accessToken, tokenRegex ] = getMatch(location, this.options.locationKey);
85
+ getFromLocation(location: Location) {
86
+ const [accessToken, tokenRegex] = getMatch(location, this.options.locationKey)
87
87
 
88
88
  if (accessToken !== null) {
89
- location.hash = location.hash.replace(tokenRegex, '');
89
+ location.hash = location.hash.replace(tokenRegex, '')
90
90
 
91
- return Promise.resolve(accessToken);
91
+ return Promise.resolve(accessToken)
92
92
  }
93
93
 
94
- const [ message, errorRegex ] = getMatch(location, this.options.locationErrorKey);
94
+ const [message, errorRegex] = getMatch(location, this.options.locationErrorKey)
95
95
 
96
96
  if (message !== null) {
97
- location.hash = location.hash.replace(errorRegex, '');
97
+ location.hash = location.hash.replace(errorRegex, '')
98
98
 
99
- return Promise.reject(new OauthError(decodeURIComponent(message)));
99
+ return Promise.reject(new OauthError(decodeURIComponent(message)))
100
100
  }
101
101
 
102
- return Promise.resolve(null);
102
+ return Promise.resolve(null)
103
103
  }
104
104
 
105
- setAccessToken (accessToken: string) {
106
- return this.storage.setItem(this.options.storageKey, accessToken);
105
+ setAccessToken(accessToken: string) {
106
+ return this.storage.setItem(this.options.storageKey, accessToken)
107
107
  }
108
108
 
109
- getAccessToken (): Promise<string|null> {
110
- return this.storage.getItem(this.options.storageKey)
111
- .then((accessToken: string) => {
112
- if (!accessToken && typeof window !== 'undefined' && window.location) {
113
- return this.getFromLocation(window.location);
114
- }
109
+ getAccessToken(): Promise<string | null> {
110
+ return this.storage.getItem(this.options.storageKey).then((accessToken: string) => {
111
+ if (!accessToken && typeof window !== 'undefined' && window.location) {
112
+ return this.getFromLocation(window.location)
113
+ }
115
114
 
116
- return accessToken || null;
117
- });
115
+ return accessToken || null
116
+ })
118
117
  }
119
118
 
120
- removeAccessToken () {
121
- return this.storage.removeItem(this.options.storageKey);
119
+ removeAccessToken() {
120
+ return this.storage.removeItem(this.options.storageKey)
122
121
  }
123
122
 
124
- reset () {
125
- this.app.set('authentication', null);
126
- this.authenticated = false;
123
+ reset() {
124
+ this.app.set('authentication', null)
125
+ this.authenticated = false
127
126
 
128
- return Promise.resolve(null);
127
+ return Promise.resolve(null)
129
128
  }
130
129
 
131
- handleError (error: FeathersError, type: 'authenticate'|'logout') {
130
+ handleError(error: FeathersError, type: 'authenticate' | 'logout') {
132
131
  if (error.code === 401 || error.code === 403) {
133
- const promise = this.removeAccessToken().then(() => this.reset());
132
+ const promise = this.removeAccessToken().then(() => this.reset())
134
133
 
135
- return type === 'logout' ? promise : promise.then(() => Promise.reject(error));
134
+ return type === 'logout' ? promise : promise.then(() => Promise.reject(error))
136
135
  }
137
136
 
138
- return Promise.reject(error);
137
+ return Promise.reject(error)
139
138
  }
140
139
 
141
- reAuthenticate (force = false, strategy?: string): Promise<AuthenticationResult> {
140
+ reAuthenticate(force = false, strategy?: string): Promise<AuthenticationResult> {
142
141
  // Either returns the authentication state or
143
142
  // tries to re-authenticate with the stored JWT and strategy
144
- const authPromise = this.app.get('authentication');
143
+ let authPromise = this.app.get('authentication')
145
144
 
146
145
  if (!authPromise || force === true) {
147
- return this.getAccessToken().then(accessToken => {
146
+ authPromise = this.getAccessToken().then((accessToken) => {
148
147
  if (!accessToken) {
149
- throw new NotAuthenticated('No accessToken found in storage');
148
+ return this.handleError(new NotAuthenticated('No accessToken found in storage'), 'authenticate')
150
149
  }
151
150
 
152
151
  return this.authenticate({
153
152
  strategy: strategy || this.options.jwtStrategy,
154
153
  accessToken
155
- });
156
- });
154
+ })
155
+ })
156
+ this.app.set('authentication', authPromise)
157
157
  }
158
158
 
159
- return authPromise;
159
+ return authPromise
160
160
  }
161
161
 
162
- authenticate (authentication?: AuthenticationRequest, params?: Params): Promise<AuthenticationResult> {
162
+ authenticate(authentication?: AuthenticationRequest, params?: Params): Promise<AuthenticationResult> {
163
163
  if (!authentication) {
164
- return this.reAuthenticate();
164
+ return this.reAuthenticate()
165
165
  }
166
166
 
167
- const promise = this.service.create(authentication, params)
167
+ const promise = this.service
168
+ .create(authentication, params)
168
169
  .then((authResult: AuthenticationResult) => {
169
- const { accessToken } = authResult;
170
+ const { accessToken } = authResult
170
171
 
171
- this.authenticated = true;
172
- this.app.emit('login', authResult);
173
- this.app.emit('authenticated', authResult);
172
+ this.authenticated = true
173
+ this.app.emit('login', authResult)
174
+ this.app.emit('authenticated', authResult)
174
175
 
175
- return this.setAccessToken(accessToken).then(() => authResult);
176
- }).catch((error: FeathersError) =>
177
- this.handleError(error, 'authenticate')
178
- );
176
+ return this.setAccessToken(accessToken).then(() => authResult)
177
+ })
178
+ .catch((error: FeathersError) => this.handleError(error, 'authenticate'))
179
179
 
180
- this.app.set('authentication', promise);
180
+ this.app.set('authentication', promise)
181
181
 
182
- return promise;
182
+ return promise
183
183
  }
184
184
 
185
- logout (): Promise<AuthenticationResult | null> {
185
+ logout(): Promise<AuthenticationResult | null> {
186
186
  return Promise.resolve(this.app.get('authentication'))
187
- .then(() => this.service.remove(null)
188
- .then((authResult: AuthenticationResult) => this.removeAccessToken()
189
- .then(() => this.reset())
190
- .then(() => {
191
- this.app.emit('logout', authResult);
192
-
193
- return authResult;
194
- })
195
- ))
196
- .catch((error: FeathersError) =>
197
- this.handleError(error, 'logout')
198
- );
187
+ .then(() =>
188
+ this.service.remove(null).then((authResult: AuthenticationResult) =>
189
+ this.removeAccessToken()
190
+ .then(() => this.reset())
191
+ .then(() => {
192
+ this.app.emit('logout', authResult)
193
+
194
+ return authResult
195
+ })
196
+ )
197
+ )
198
+ .catch((error: FeathersError) => this.handleError(error, 'logout'))
199
199
  }
200
200
  }
@@ -1,20 +1,26 @@
1
- import { HookContext } from '@feathersjs/feathers';
2
- import { stripSlashes } from '@feathersjs/commons';
1
+ import { HookContext, NextFunction } from '@feathersjs/feathers'
2
+ import { stripSlashes } from '@feathersjs/commons'
3
3
 
4
4
  export const authentication = () => {
5
- return (context: HookContext) => {
6
- const { app, params, path, method, app: { authentication: service } } = context;
5
+ return (context: HookContext, next: NextFunction) => {
6
+ const {
7
+ app,
8
+ params,
9
+ path,
10
+ method,
11
+ app: { authentication: service }
12
+ } = context
7
13
 
8
14
  if (stripSlashes(service.options.path) === path && method === 'create') {
9
- return context;
15
+ return next()
10
16
  }
11
17
 
12
- return Promise.resolve(app.get('authentication')).then(authResult => {
13
- if (authResult) {
14
- context.params = Object.assign({}, authResult, params);
15
- }
16
-
17
- return context;
18
- });
19
- };
20
- };
18
+ return Promise.resolve(app.get('authentication'))
19
+ .then((authResult) => {
20
+ if (authResult) {
21
+ context.params = Object.assign({}, authResult, params)
22
+ }
23
+ })
24
+ .then(next)
25
+ }
26
+ }
@@ -1,2 +1,2 @@
1
- export { authentication } from './authentication';
2
- export { populateHeader } from './populate-header';
1
+ export { authentication } from './authentication'
2
+ export { populateHeader } from './populate-header'
@@ -1,20 +1,27 @@
1
- import { HookContext } from '@feathersjs/feathers';
1
+ import { HookContext, NextFunction } from '@feathersjs/feathers'
2
2
 
3
3
  export const populateHeader = () => {
4
- return (context: HookContext) => {
5
- const { app, params: { accessToken } } = context;
6
- const authentication = app.authentication;
4
+ return (context: HookContext, next: NextFunction) => {
5
+ const {
6
+ app,
7
+ params: { accessToken }
8
+ } = context
9
+ const authentication = app.authentication
7
10
 
8
11
  // Set REST header if necessary
9
12
  if (app.rest && accessToken) {
10
- const { scheme, header } = authentication.options;
11
- const authHeader = `${scheme} ${accessToken}`;
13
+ const { scheme, header } = authentication.options
14
+ const authHeader = `${scheme} ${accessToken}`
12
15
 
13
- context.params.headers = Object.assign({}, {
14
- [header]: authHeader
15
- }, context.params.headers);
16
+ context.params.headers = Object.assign(
17
+ {},
18
+ {
19
+ [header]: authHeader
20
+ },
21
+ context.params.headers
22
+ )
16
23
  }
17
24
 
18
- return context;
19
- };
20
- };
25
+ return next()
26
+ }
27
+ }
package/src/index.ts CHANGED
@@ -1,32 +1,37 @@
1
- import { AuthenticationClient, AuthenticationClientOptions } from './core';
2
- import * as hooks from './hooks';
3
- import { Application } from '@feathersjs/feathers';
4
- import { Storage, MemoryStorage, StorageWrapper } from './storage';
1
+ import { AuthenticationClient, AuthenticationClientOptions } from './core'
2
+ import * as hooks from './hooks'
3
+ import { Application } from '@feathersjs/feathers'
4
+ import { Storage, MemoryStorage, StorageWrapper } from './storage'
5
5
 
6
6
  declare module '@feathersjs/feathers/lib/declarations' {
7
- interface Application<ServiceTypes, AppSettings> { // eslint-disable-line
8
- io?: any;
9
- rest?: any;
10
- authentication: AuthenticationClient;
11
- authenticate: AuthenticationClient['authenticate'];
12
- reAuthenticate: AuthenticationClient['reAuthenticate'];
13
- logout: AuthenticationClient['logout'];
7
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
8
+ interface Application<Services, Settings> {
9
+ // eslint-disable-line
10
+ io?: any
11
+ rest?: any
12
+ authentication: AuthenticationClient
13
+ authenticate: AuthenticationClient['authenticate']
14
+ reAuthenticate: AuthenticationClient['reAuthenticate']
15
+ logout: AuthenticationClient['logout']
14
16
  }
15
17
  }
16
18
 
17
19
  export const getDefaultStorage = () => {
18
20
  try {
19
- return new StorageWrapper(window.localStorage);
20
- } catch (error) {}
21
+ return new StorageWrapper(window.localStorage)
22
+ } catch (error: any) {}
21
23
 
22
- return new MemoryStorage();
23
- };
24
+ return new MemoryStorage()
25
+ }
24
26
 
25
- export { AuthenticationClient, AuthenticationClientOptions, Storage, MemoryStorage, hooks };
27
+ export { AuthenticationClient, AuthenticationClientOptions, Storage, MemoryStorage, hooks }
26
28
 
27
- export type ClientConstructor = new (app: Application, options: AuthenticationClientOptions) => AuthenticationClient;
29
+ export type ClientConstructor = new (
30
+ app: Application,
31
+ options: AuthenticationClientOptions
32
+ ) => AuthenticationClient
28
33
 
29
- export const defaultStorage: Storage = getDefaultStorage();
34
+ export const defaultStorage: Storage = getDefaultStorage()
30
35
 
31
36
  export const defaults: AuthenticationClientOptions = {
32
37
  header: 'Authorization',
@@ -38,33 +43,26 @@ export const defaults: AuthenticationClientOptions = {
38
43
  path: '/authentication',
39
44
  Authentication: AuthenticationClient,
40
45
  storage: defaultStorage
41
- };
46
+ }
42
47
 
43
48
  const init = (_options: Partial<AuthenticationClientOptions> = {}) => {
44
- const options: AuthenticationClientOptions = Object.assign({}, defaults, _options);
45
- const { Authentication } = options;
49
+ const options: AuthenticationClientOptions = Object.assign({}, defaults, _options)
50
+ const { Authentication } = options
46
51
 
47
52
  return (app: Application) => {
48
- const authentication = new Authentication(app, options);
53
+ const authentication = new Authentication(app, options)
49
54
 
50
- app.authentication = authentication;
51
- app.authenticate = authentication.authenticate.bind(authentication);
52
- app.reAuthenticate = authentication.reAuthenticate.bind(authentication);
53
- app.logout = authentication.logout.bind(authentication);
55
+ app.authentication = authentication
56
+ app.authenticate = authentication.authenticate.bind(authentication)
57
+ app.reAuthenticate = authentication.reAuthenticate.bind(authentication)
58
+ app.logout = authentication.logout.bind(authentication)
54
59
 
55
- app.hooks({
56
- before: {
57
- all: [
58
- hooks.authentication(),
59
- hooks.populateHeader()
60
- ]
61
- }
62
- });
63
- };
64
- };
60
+ app.hooks([hooks.authentication(), hooks.populateHeader()])
61
+ }
62
+ }
65
63
 
66
- export default init;
64
+ export default init
67
65
 
68
66
  if (typeof module !== 'undefined') {
69
- module.exports = Object.assign(init, module.exports);
67
+ module.exports = Object.assign(init, module.exports)
70
68
  }
package/src/storage.ts CHANGED
@@ -1,49 +1,49 @@
1
1
  export interface Storage {
2
- getItem (key: string): any;
3
- setItem? (key: string, value: any): any;
4
- removeItem? (key: string): any;
2
+ getItem(key: string): any
3
+ setItem?(key: string, value: any): any
4
+ removeItem?(key: string): any
5
5
  }
6
6
 
7
7
  export class MemoryStorage implements Storage {
8
- store: { [key: string]: any };
8
+ store: { [key: string]: any }
9
9
 
10
- constructor () {
11
- this.store = {};
10
+ constructor() {
11
+ this.store = {}
12
12
  }
13
13
 
14
- getItem (key: string) {
15
- return Promise.resolve(this.store[key]);
14
+ getItem(key: string) {
15
+ return Promise.resolve(this.store[key])
16
16
  }
17
17
 
18
- setItem (key: string, value: any) {
19
- return Promise.resolve(this.store[key] = value);
18
+ setItem(key: string, value: any) {
19
+ return Promise.resolve((this.store[key] = value))
20
20
  }
21
21
 
22
- removeItem (key: string) {
23
- const value = this.store[key];
22
+ removeItem(key: string) {
23
+ const value = this.store[key]
24
24
 
25
- delete this.store[key];
25
+ delete this.store[key]
26
26
 
27
- return Promise.resolve(value);
27
+ return Promise.resolve(value)
28
28
  }
29
29
  }
30
30
 
31
31
  export class StorageWrapper implements Storage {
32
- storage: any;
32
+ storage: any
33
33
 
34
- constructor (storage: any) {
35
- this.storage = storage;
34
+ constructor(storage: any) {
35
+ this.storage = storage
36
36
  }
37
37
 
38
- getItem (key: string) {
39
- return Promise.resolve(this.storage.getItem(key));
38
+ getItem(key: string) {
39
+ return Promise.resolve(this.storage.getItem(key))
40
40
  }
41
41
 
42
- setItem (key: string, value: any) {
43
- return Promise.resolve(this.storage.setItem(key, value));
42
+ setItem(key: string, value: any) {
43
+ return Promise.resolve(this.storage.setItem(key, value))
44
44
  }
45
45
 
46
- removeItem (key: string) {
47
- return Promise.resolve(this.storage.removeItem(key));
46
+ removeItem(key: string) {
47
+ return Promise.resolve(this.storage.removeItem(key))
48
48
  }
49
49
  }