diff --git a/docs/core/user-info.component.md b/docs/core/user-info.component.md index 421c737e95..9db2214241 100644 --- a/docs/core/user-info.component.md +++ b/docs/core/user-info.component.md @@ -32,3 +32,4 @@ Shows user information. The component shows a round icon for the user and will show extra information about the user when clicked. If user is logged in with both ACS and APS, the ACS image will be shown. +In case of SSO authentication, the information related to the user like firstname, lastname will be fetched using the Keycloak Api diff --git a/lib/core/mock/bpm-user.service.mock.ts b/lib/core/mock/bpm-user.service.mock.ts index 796260bab9..cbbc6236ca 100644 --- a/lib/core/mock/bpm-user.service.mock.ts +++ b/lib/core/mock/bpm-user.service.mock.ts @@ -24,7 +24,6 @@ export let fakeBpmUserNoImage = { externalId: 'fake-external-id', firstName: 'fake-first-name', lastName: 'fake-last-name', - fullname: 'fake-full-name', groups: [], id: 'fake-id', lastUpdate: 'fake-update-date', @@ -47,7 +46,6 @@ export let fakeBpmUser = { externalId: 'fake-external-id', firstName: 'fake-bpm-first-name', lastName: 'fake-bpm-last-name', - fullname: 'fake-bpm-full-name', groups: [], id: 'fake-id', lastUpdate: 'fake-update-date', @@ -70,7 +68,6 @@ export let fakeBpmEditedUser = { externalId: 'fake-external-id', firstName: 'fake-first-name', lastName: 'fake-last-name', - fullname: 'fake-full-name', groups: [], id: 'fake-id', lastUpdate: 'fake-update-date', diff --git a/lib/core/mock/jwt-helper.service.spec.ts b/lib/core/mock/jwt-helper.service.spec.ts new file mode 100644 index 0000000000..9329ed525e --- /dev/null +++ b/lib/core/mock/jwt-helper.service.spec.ts @@ -0,0 +1,22 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export let mockToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.' + + 'eyJzdWIiOiIxMjM0NTY3ODkwIiwiZW1haWwiOiJqb2huRG9lQGdtYWlsLmNvbSI' + + 'sImdpdmVuX25hbWUiOiJKb2huIERvZSIsImp0aSI6IjY1ZGMzZTEyLWJhNGUtNDQ' + + '2Mi1iZjAyLTBlZGQ2MTYwM2M2NCIsImlhdCI6MTU0MjcyMTYxMywiZXhwIjoxNTQyNzI3NzY5fQ' + + '.cUxMzfiJeLwh9Er2CBn_y8ehQgSm_s2-NHehx-SRZKg'; diff --git a/lib/core/mock/public-api.ts b/lib/core/mock/public-api.ts index 3f9e626ad4..1b943355a3 100644 --- a/lib/core/mock/public-api.ts +++ b/lib/core/mock/public-api.ts @@ -36,3 +36,4 @@ export * from './form/formDefinitionVisibility.mock'; export * from './form/start-form.component.mock'; export * from './form/form.service.mock'; export * from './form/widget-visibility.service.mock'; +export * from './jwt-helper.service.spec'; diff --git a/lib/core/services/jwt-helper.service.spec.ts b/lib/core/services/jwt-helper.service.spec.ts new file mode 100644 index 0000000000..ac66526235 --- /dev/null +++ b/lib/core/services/jwt-helper.service.spec.ts @@ -0,0 +1,45 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { TestBed } from '@angular/core/testing'; +import { JwtHelperService } from './jwt-helper.service'; +import { mockToken } from './../mock/jwt-helper.service.spec'; + +describe('JwtHelperService', () => { + + let jwtHelperService: JwtHelperService; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [JwtHelperService] + }); + jwtHelperService = TestBed.get(JwtHelperService); + }); + + it('should be able to create the service', () => { + expect(jwtHelperService).not.toBeNull(); + expect(jwtHelperService).toBeDefined(); + }); + + it('Should decode the Jwt token', () => { + const result = jwtHelperService.decodeToken(mockToken); + expect(result).toBeDefined(); + expect(result).not.toBeNull(''); + expect(result['given_name']).toBe('John Doe'); + expect(result['email']).toBe('johnDoe@gmail.com'); + }); +}); diff --git a/lib/core/services/jwt-helper.service.ts b/lib/core/services/jwt-helper.service.ts new file mode 100644 index 0000000000..192299b935 --- /dev/null +++ b/lib/core/services/jwt-helper.service.ts @@ -0,0 +1,62 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class JwtHelperService { + + constructor() {} + + decodeToken(token): Object { + let parts = token.split('.'); + + if (parts.length !== 3) { + throw new Error('JWT must have 3 parts'); + } + + let decoded = this.urlBase64Decode(parts[1]); + if (!decoded) { + throw new Error('Cannot decode the token'); + } + + return JSON.parse(decoded); + } + + private urlBase64Decode(token): string { + let output = token.replace(/-/g, '+').replace(/_/g, '/'); + switch (output.length % 4) { + case 0: { + break; + } + case 2: { + output += '=='; + break; + } + case 3: { + output += '='; + break; + } + default: { + throw new Error('Illegal base64url string!'); + } + } + return decodeURIComponent(escape(window.atob(output))); + } +} diff --git a/lib/core/services/public-api.ts b/lib/core/services/public-api.ts index 2e7c97b866..78059ea9c2 100644 --- a/lib/core/services/public-api.ts +++ b/lib/core/services/public-api.ts @@ -50,3 +50,4 @@ export * from './search-configuration.service'; export * from './comment-content.service'; export * from './login-dialog.service'; export * from './external-alfresco-api.service'; +export * from './jwt-helper.service'; diff --git a/lib/core/userinfo/components/user-info.component.html b/lib/core/userinfo/components/user-info.component.html index be3a6d7143..5303a54eb5 100644 --- a/lib/core/userinfo/components/user-info.component.html +++ b/lib/core/userinfo/components/user-info.component.html @@ -1,48 +1,68 @@ -