Skip to content

Commit d4481ae

Browse files
author
Nicolas MACHEREY
committed
[BC] CreateClientCommand is no accepting grants and the 'noSecret' to prevent secret generation
1 parent f6cce65 commit d4481ae

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

lib/app/command/create-client.command.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export class CreateClientCommand {
66
public readonly name: string,
77
public readonly scope: string,
88
public readonly clientId ?: string,
9+
public readonly grants ?: string[],
10+
public readonly noSecret ?: boolean,
911
public readonly accessTokenLifetime?: number,
1012
public readonly refreshTokenLifetime?: number,
1113
) {}

lib/app/command/create-client.handler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ export class CreateClientHandler implements ICommandHandler<CreateClientCommand>
2828
const client = new ClientEntity();
2929
client.name = command.name;
3030
client.clientId = command.clientId || uuid();
31-
client.clientSecret = crypto.randomBytes(32).toString('hex');
31+
if (!command.noSecret) {
32+
client.clientSecret = crypto.randomBytes(32).toString('hex');
33+
}
34+
3235
client.scope = command.scope;
3336
client.accessTokenLifetime = command.accessTokenLifetime || 3600;
3437
client.refreshTokenLifetime = command.refreshTokenLifetime || 7200;
38+
client.grants = command.grants || ['client_credentials', 'refresh_token'];
3539

3640
// generate keys
3741
const attrs = [{ name: 'commonName', value: command.name }];

test/unit/app/command/create-client.handler.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ describe('Create Client Command Handler', () => {
8080
'client-1',
8181
'["app-1", "app-2"]',
8282
'client-id',
83+
['client_credentials', 'refresh_token'],
84+
false,
8385
1000,
8486
3600
8587
));
@@ -89,6 +91,32 @@ describe('Create Client Command Handler', () => {
8991
scope: '["app-1", "app-2"]',
9092
clientId: 'client-id',
9193
clientSecret: expect.any(String),
94+
grants: ['client_credentials', 'refresh_token'],
95+
accessTokenLifetime: 1000,
96+
refreshTokenLifetime: 3600,
97+
}));
98+
99+
serviceSpy.mockRestore();
100+
});
101+
102+
it('"CreateClientHandler::execute": should create the Client with no secret when asked', async () => {
103+
const serviceSpy = jest.spyOn(clientRepositoryMock, 'create');
104+
105+
await handler.execute(new CreateClientCommand(
106+
'client-1',
107+
'["app-1", "app-2"]',
108+
'client-id',
109+
['password_grant', 'refresh_token'],
110+
true,
111+
1000,
112+
3600
113+
));
114+
115+
expect(serviceSpy).toBeCalledWith(expect.objectContaining({
116+
name: 'client-1',
117+
scope: '["app-1", "app-2"]',
118+
clientId: 'client-id',
119+
grants: ['password_grant', 'refresh_token'],
92120
accessTokenLifetime: 1000,
93121
refreshTokenLifetime: 3600,
94122
}));

0 commit comments

Comments
 (0)