Skip to content

Commit 92110a6

Browse files
committed
fix(igx-ts): update auth template
1 parent 7198fc6 commit 92110a6

File tree

7 files changed

+88
-21
lines changed

7 files changed

+88
-21
lines changed

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/providers/base-oidc-provider.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { OidcSecurityService } from 'angular-auth-oidc-client';
22
import { firstValueFrom } from 'rxjs';
33
import { ExternalLogin } from '../models/login';
4+
import { ExternalAuthConfig } from '../services/external-auth-configs';
45
import { AuthProvider } from './auth-provider';
56

67
/** Base provider for OpenID Connect (OIDC) https://openid.net/connect/ */
78
export abstract class BaseOidcProvider implements AuthProvider {
89
constructor(
910
protected oidcSecurityService: OidcSecurityService,
10-
protected configId: string) { }
11+
protected externalStsConfig: ExternalAuthConfig) { }
1112

1213
public login() {
13-
this.oidcSecurityService.authorize(this.configId);
14+
this.oidcSecurityService.authorize(this.externalStsConfig.configId);
1415
}
1516

1617
public async getUserInfo(): Promise<ExternalLogin> {
1718
const result = await firstValueFrom(this.oidcSecurityService.userData$);
1819
const configData = result.allUserData?.find(
19-
d => d.configId === this.configId
20+
d => d.configId === this.externalStsConfig.configId
2021
);
2122
if (configData?.userData) {
2223
return this.formatUserData(configData.userData);
@@ -25,7 +26,7 @@ export abstract class BaseOidcProvider implements AuthProvider {
2526
}
2627

2728
public logout() {
28-
this.oidcSecurityService.logoff(this.configId);
29+
this.oidcSecurityService.logoff(this.externalStsConfig.configId);
2930
}
3031

3132
/** Format received user data per provider claims */

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/providers/google-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class GoogleProvider extends BaseOidcProvider {
99
* https://developers.google.com/+/web/api/rest/openidconnect/getOpenIdConnect
1010
*/
1111
protected async formatUserData(userData: { [key: string]: any; }): Promise<ExternalLogin> {
12-
const token = await firstValueFrom(this.oidcSecurityService.getAccessToken(this.configId));
12+
const token = await firstValueFrom(this.oidcSecurityService.getAccessToken(this.externalStsConfig.configId));
1313
return {
1414
id: userData['sub'],
1515
name: userData['name'],

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/providers/microsoft-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class MicrosoftProvider extends BaseOidcProvider {
1010
* https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens#payload-claims
1111
*/
1212
protected async formatUserData(userData: { [key: string]: any; }): Promise<ExternalLogin> {
13-
const token = await firstValueFrom(this.oidcSecurityService.getAccessToken(this.configId));
13+
const token = await firstValueFrom(this.oidcSecurityService.getAccessToken(this.externalStsConfig.configId));
1414
return {
1515
id: userData['oid'],
1616
name: userData['name'],

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/providers/providers.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ describe('Providers', () => {
130130
describe('Google Provider', () => {
131131

132132
it('Should properly initialize', () => {
133-
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
133+
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
134134
expect(provider).toBeDefined();
135135
});
136136

137137
it('Should properly call login', () => {
138-
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
138+
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
139139
vi.spyOn(MOCK_OIDC_SECURITY, 'authorize');
140140
provider.login();
141141
expect(MOCK_OIDC_SECURITY.authorize).toHaveBeenCalledWith(MOCK_EXTERNAL_AUTH_CONFIG.configId);
142142
});
143143

144144
it(`Should properly call 'formatUserData'`, async () => {
145-
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
145+
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
146146
const mockObj = {
147147
sub: 'test-id',
148148
name: 'test-name',
@@ -165,7 +165,7 @@ describe('Providers', () => {
165165
});
166166

167167
it('Should properly call getUserInfo', async () => {
168-
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
168+
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
169169
const mockUserData = { sub: 'test-id', name: 'test-name', email: 'test@example.com' };
170170
MOCK_OIDC_SECURITY.userData$ = of({
171171
allUserData: [{ configId: MOCK_EXTERNAL_AUTH_CONFIG.configId, userData: mockUserData }]
@@ -177,7 +177,7 @@ describe('Providers', () => {
177177
});
178178

179179
it('Should properly call logout', () => {
180-
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
180+
const provider = new GoogleProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
181181
vi.spyOn(MOCK_OIDC_SECURITY, 'logoff');
182182
provider.logout();
183183
expect(MOCK_OIDC_SECURITY.logoff).toHaveBeenCalledWith(MOCK_EXTERNAL_AUTH_CONFIG.configId);
@@ -187,19 +187,19 @@ describe('Providers', () => {
187187
describe('Microsoft Provider', () => {
188188

189189
it('Should properly initialize', () => {
190-
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
190+
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
191191
expect(provider).toBeDefined();
192192
});
193193

194194
it('Should properly call login', () => {
195-
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
195+
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
196196
vi.spyOn(MOCK_OIDC_SECURITY, 'authorize');
197197
provider.login();
198198
expect(MOCK_OIDC_SECURITY.authorize).toHaveBeenCalledWith(MOCK_EXTERNAL_AUTH_CONFIG.configId);
199199
});
200200

201201
it('Should properly call getUserInfo', async () => {
202-
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
202+
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
203203
const mockUserData = { oid: 'test-id', name: 'test-name', email: 'test@example.com' };
204204
MOCK_OIDC_SECURITY.userData$ = of({
205205
allUserData: [{ configId: MOCK_EXTERNAL_AUTH_CONFIG.configId, userData: mockUserData }]
@@ -211,7 +211,7 @@ describe('Providers', () => {
211211
});
212212

213213
it(`Should properly call 'formatUserData'`, async () => {
214-
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG.configId);
214+
const provider = new MicrosoftProvider(MOCK_OIDC_SECURITY, MOCK_EXTERNAL_AUTH_CONFIG);
215215
const mockObj = {
216216
oid: 'test-id',
217217
name: 'test-name',

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/services/external-auth-configs.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ export enum ExternalAuthProvider {
44
Microsoft = 'Microsoft'
55
}
66

7-
/** Configuration for Facebook-based external authentication. */
87
export interface ExternalAuthConfig {
8+
configId?: string;
9+
stsServer: string;
910
client_id: string;
11+
scope: string;
12+
provider: ExternalAuthProvider;
1013
redirect_url: string;
14+
response_type: string;
15+
post_logout_redirect_uri: string;
16+
post_login_route: string;
17+
auto_userinfo: boolean;
18+
max_id_token_iat_offset_allowed_in_seconds: number;
1119
}

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/services/external-auth.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,31 @@ export class ExternalAuth {
4343
}
4444

4545
public addGoogle(clientID: string) {
46+
const googleConfig: ExternalAuthConfig = {
47+
configId: ExternalAuthProvider.Google,
48+
provider: ExternalAuthProvider.Google,
49+
stsServer: 'https://accounts.google.com',
50+
client_id: clientID,
51+
scope: 'openid email profile',
52+
redirect_url: this.getAbsoluteUrl(ExternalAuthRedirectUrl.Google),
53+
response_type: 'id_token token',
54+
post_logout_redirect_uri: '/',
55+
post_login_route: 'redirect',
56+
auto_userinfo: false,
57+
max_id_token_iat_offset_allowed_in_seconds: 30
58+
};
59+
4660
this.providers.set(
4761
ExternalAuthProvider.Google,
48-
new GoogleProvider(this.oidcSecurityService, clientID)
62+
new GoogleProvider(this.oidcSecurityService, googleConfig)
4963
);
5064
}
5165

5266
public addFacebook(clientID: string) {
5367
const fbConfig: ExternalAuthConfig = {
5468
client_id: clientID,
5569
redirect_url: ExternalAuthRedirectUrl.Facebook
56-
};
70+
} as ExternalAuthConfig;
5771

5872
this.providers.set(
5973
ExternalAuthProvider.Facebook,
@@ -62,9 +76,23 @@ export class ExternalAuth {
6276
}
6377

6478
public addMicrosoft(clientID: string) {
79+
const msConfig: ExternalAuthConfig = {
80+
configId: ExternalAuthProvider.Microsoft,
81+
provider: ExternalAuthProvider.Microsoft,
82+
stsServer: 'https://login.microsoftonline.com/consumers/v2.0/',
83+
client_id: clientID,
84+
scope: 'openid email profile',
85+
redirect_url: this.getAbsoluteUrl(ExternalAuthRedirectUrl.Microsoft),
86+
response_type: 'id_token token',
87+
post_logout_redirect_uri: '/',
88+
post_login_route: '',
89+
auto_userinfo: false,
90+
max_id_token_iat_offset_allowed_in_seconds: 1000
91+
};
92+
6593
this.providers.set(
6694
ExternalAuthProvider.Microsoft,
67-
new MicrosoftProvider(this.oidcSecurityService, clientID)
95+
new MicrosoftProvider(this.oidcSecurityService, msConfig)
6896
);
6997
}
7098

packages/igx-templates/igx-ts/projects/side-nav-auth/files/src/app/authentication/services/services.spec.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,25 @@ describe('Services', () => {
109109

110110
it(`Should properly call 'addGoogle'`, () => {
111111
const providersSpy = vi.spyOn((extAuthServ as any).providers, 'set');
112+
const getAbsoluteUrlSpy = vi.spyOn(extAuthServ as any, 'getAbsoluteUrl').mockReturnValue('testUrl');
113+
const configParams = {
114+
configId: ExternalAuthProvider.Google,
115+
provider: ExternalAuthProvider.Google,
116+
stsServer: 'https://accounts.google.com',
117+
client_id: 'test',
118+
scope: 'openid email profile',
119+
redirect_url: 'testUrl',
120+
response_type: 'id_token token',
121+
post_logout_redirect_uri: '/',
122+
post_login_route: 'redirect',
123+
auto_userinfo: false,
124+
max_id_token_iat_offset_allowed_in_seconds: 30
125+
};
112126
extAuthServ.addGoogle('test');
113127
expect(providersSpy).toHaveBeenCalled();
114128
expect(providersSpy).toHaveBeenCalledWith('Google',
115-
new GoogleProvider(MOCK_OIDC_SECURITY, ExternalAuthProvider.Google));
129+
new GoogleProvider(MOCK_OIDC_SECURITY, configParams));
130+
expect(getAbsoluteUrlSpy).toHaveBeenCalled();
116131
});
117132

118133
it(`Should properly call 'addFacebook'`, () => {
@@ -129,10 +144,25 @@ describe('Services', () => {
129144

130145
it(`Should properly call 'addMicrosoft'`, () => {
131146
const providersSpy = vi.spyOn((extAuthServ as any).providers, 'set');
147+
const getAbsoluteUrlSpy = vi.spyOn(extAuthServ as any, 'getAbsoluteUrl').mockReturnValue('testUrl');
148+
const configParams = {
149+
configId: ExternalAuthProvider.Microsoft,
150+
provider: ExternalAuthProvider.Microsoft,
151+
stsServer: 'https://login.microsoftonline.com/consumers/v2.0/',
152+
client_id: 'test',
153+
scope: 'openid email profile',
154+
redirect_url: 'testUrl',
155+
response_type: 'id_token token',
156+
post_logout_redirect_uri: '/',
157+
post_login_route: '',
158+
auto_userinfo: false,
159+
max_id_token_iat_offset_allowed_in_seconds: 1000
160+
} as any;
132161
extAuthServ.addMicrosoft('test');
133162
expect(providersSpy).toHaveBeenCalled();
134163
expect(providersSpy).toHaveBeenCalledWith('Microsoft',
135-
new MicrosoftProvider(MOCK_OIDC_SECURITY, ExternalAuthProvider.Microsoft));
164+
new MicrosoftProvider(MOCK_OIDC_SECURITY, configParams));
165+
expect(getAbsoluteUrlSpy).toHaveBeenCalled();
136166
});
137167

138168
it(`Should properly call 'getUserInfo'`, async () => {

0 commit comments

Comments
 (0)