Skip to content

Commit 19854d3

Browse files
vonagamdaffl
authored andcommitted
fix: Authentication type improvements and timeout fix (#1605)
1 parent 4e4d10a commit 19854d3

File tree

7 files changed

+26
-21
lines changed

7 files changed

+26
-21
lines changed

packages/authentication-client/src/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class AuthenticationClient {
149149
return authPromise;
150150
}
151151

152-
authenticate (authentication: AuthenticationRequest, params?: Params): Promise<AuthenticationResult> {
152+
authenticate (authentication?: AuthenticationRequest, params?: Params): Promise<AuthenticationResult> {
153153
if (!authentication) {
154154
return this.reAuthenticate();
155155
}
@@ -172,7 +172,7 @@ export class AuthenticationClient {
172172
return promise;
173173
}
174174

175-
logout () {
175+
logout (): Promise<AuthenticationResult | null> {
176176
return Promise.resolve(this.app.get('authentication'))
177177
.then(() => this.service.remove(null)
178178
.then((authResult: AuthenticationResult) => this.removeAccessToken()

packages/authentication-client/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AuthenticationClient, AuthenticationClientOptions } from './core';
22
import * as hooks from './hooks';
33
import { Application } from '@feathersjs/feathers';
4-
import { AuthenticationResult, AuthenticationRequest } from '@feathersjs/authentication';
54
import { Storage, MemoryStorage, StorageWrapper } from './storage';
65

76
declare module '@feathersjs/feathers' {
@@ -10,9 +9,9 @@ declare module '@feathersjs/feathers' {
109
rest?: any;
1110
primus?: any;
1211
authentication: AuthenticationClient;
13-
authenticate (authentication?: AuthenticationRequest): Promise<AuthenticationResult>;
14-
reAuthenticate (force: boolean): Promise<AuthenticationResult>;
15-
logout (): Promise<AuthenticationResult>;
12+
authenticate: AuthenticationClient['authenticate'];
13+
reAuthenticate: AuthenticationClient['reAuthenticate'];
14+
logout: AuthenticationClient['logout'];
1615
}
1716
}
1817

packages/authentication/src/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface AuthenticationRequest {
2121
[key: string]: any;
2222
}
2323

24-
export type ConnectionEvent = 'login'|'logout'|'disconnect';
24+
export type ConnectionEvent = 'login' | 'logout' | 'disconnect';
2525

2626
export interface AuthenticationStrategy {
2727
/**
@@ -63,11 +63,11 @@ export interface AuthenticationStrategy {
6363
* @param req The HTTP request
6464
* @param res The HTTP response
6565
*/
66-
parse? (req: IncomingMessage, res: ServerResponse): Promise<AuthenticationRequest|null>;
66+
parse? (req: IncomingMessage, res: ServerResponse): Promise<AuthenticationRequest | null>;
6767
}
6868

6969
export interface JwtVerifyOptions extends VerifyOptions {
70-
algorithm?: string|string[];
70+
algorithm?: string | string[];
7171
}
7272

7373
/**

packages/authentication/src/hooks/authenticate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface AuthenticateHookSettings {
1010
strategies: string[];
1111
}
1212

13-
export default (originalSettings: string|AuthenticateHookSettings, ...originalStrategies: string[]) => {
13+
export default (originalSettings: string | AuthenticateHookSettings, ...originalStrategies: string[]) => {
1414
const settings = typeof originalSettings === 'string'
1515
? { strategies: flatten([ originalSettings, ...originalStrategies ]) }
1616
: originalSettings;
@@ -54,7 +54,7 @@ export default (originalSettings: string|AuthenticateHookSettings, ...originalSt
5454
context.params = merge({}, params, omit(authResult, 'accessToken'), { authenticated: true });
5555

5656
return context;
57-
} else if (!authentication && provider) {
57+
} else if (provider) {
5858
throw new NotAuthenticated('Not authenticated');
5959
}
6060

packages/authentication/src/jwt.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
4444
const timer = lt.setTimeout(() => this.app.emit('disconnect', connection), duration);
4545

4646
debug(`Registering connection expiration timer for ${duration}ms`);
47+
lt.clearTimeout(this.expirationTimers.get(connection));
4748
this.expirationTimers.set(connection, timer);
4849

4950
debug('Adding authentication information to connection');
@@ -56,6 +57,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
5657

5758
delete connection.authentication;
5859
lt.clearTimeout(this.expirationTimers.get(connection));
60+
this.expirationTimers.delete(connection);
5961
}
6062
}
6163

@@ -128,8 +130,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
128130
}
129131

130132
async parse (req: IncomingMessage) {
131-
const result = { strategy: this.name };
132-
const { header, schemes }: { header: any, schemes: string[] } = this.configuration;
133+
const { header, schemes }: { header: string, schemes: string[] } = this.configuration;
133134
const headerValue = req.headers && req.headers[header.toLowerCase()];
134135

135136
if (!headerValue || typeof headerValue !== 'string') {
@@ -138,7 +139,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
138139

139140
debug('Found parsed header value');
140141

141-
const [ , scheme = null, schemeValue = null ] = headerValue.match(SPLIT_HEADER) || [];
142+
const [ , scheme, schemeValue ] = headerValue.match(SPLIT_HEADER) || [];
142143
const hasScheme = scheme && schemes.some(
143144
current => new RegExp(current, 'i').test(scheme)
144145
);
@@ -148,7 +149,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
148149
}
149150

150151
return {
151-
...result,
152+
strategy: this.name,
152153
accessToken: hasScheme ? schemeValue : headerValue
153154
};
154155
}

packages/authentication/src/service.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class AuthenticationService extends AuthenticationBase implements Partial
114114
* @param id The JWT to remove or null
115115
* @param params Service call parameters
116116
*/
117-
async remove (id: null|string, params: Params) {
117+
async remove (id: string | null, params: Params) {
118118
const { authentication } = params;
119119
const { authStrategies } = this.configuration;
120120

@@ -131,11 +131,10 @@ export class AuthenticationService extends AuthenticationBase implements Partial
131131
/**
132132
* Validates the service configuration.
133133
*/
134-
setup () {
134+
setup (this: AuthenticationService & ServiceAddons<AuthenticationResult>) {
135135
// The setup method checks for valid settings and registers the
136136
// connection and event (login, logout) hooks
137137
const { secret, service, entity, entityId } = this.configuration;
138-
const self = this as unknown as ServiceAddons<AuthenticationResult>;
139138

140139
if (typeof secret !== 'string') {
141140
throw new Error(`A 'secret' must be provided in your authentication configuration`);
@@ -155,15 +154,19 @@ export class AuthenticationService extends AuthenticationBase implements Partial
155154
}
156155
}
157156

158-
self.hooks({
157+
this.hooks({
159158
after: {
160159
create: [ connection('login'), event('login') ],
161160
remove: [ connection('logout'), event('logout') ]
162161
}
163162
});
164163

165-
if (typeof self.publish === 'function') {
166-
self.publish(() => null);
164+
this.app.on('disconnect', async (connection) => {
165+
await this.handleConnection('disconnect', connection);
166+
});
167+
168+
if (typeof this.publish === 'function') {
169+
this.publish(() => null);
167170
}
168171
}
169172
}

packages/authentication/test/jwt.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ describe('authentication/jwt', () => {
127127
const disconnection = await new Promise(resolve => app.once('disconnect', resolve));
128128

129129
assert.strictEqual(disconnection, connection);
130+
131+
assert.ok(!connection.authentication);
130132
});
131133

132134
it('deletes authentication information on remove', async () => {

0 commit comments

Comments
 (0)