Skip to content

Commit 41acf3e

Browse files
committed
feat(header): Added feature to get the user the access token is issued for
1 parent 61d7c39 commit 41acf3e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/ws-header/ws-header.js

+15
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ export class WSHeader extends Component {
8383
this.authorization.unauthorize();
8484
}
8585

86+
/**
87+
* Get abbreviation for the user the access token is issued for
88+
* @returns {string|null}
89+
*/
90+
static getUserAbbreviation() {
91+
try {
92+
const json = JSON.parse(atob(this.getAccessToken()));
93+
// Find key which contains the name
94+
const nameKey = Object.keys(json).find(key => key.includes('managed-id'));
95+
return json[nameKey];
96+
} catch (e) {
97+
return null;
98+
}
99+
}
100+
86101
/**
87102
* Retrieve the persisted locale
88103
* @returns {string}

tests/ws-header/ws-header.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ describe('A WSHeader', () => {
3131
expect(WSHeader.getAccessToken()).toBe(tokenName);
3232
});
3333

34+
it('get\'s the user abbreviation from access token', () => {
35+
const tokenName = btoa('{"test": "asd", "asdasdmanaged-id": "supercooluser"}');
36+
// Only test the basic function, the general authorization ist tested elsewhere
37+
WSHeader.storage.set('access_token', tokenName);
38+
expect(WSHeader.getUserAbbreviation()).toBe('supercooluser');
39+
expect(WSHeader.getAccessToken()).toBe(tokenName);
40+
});
41+
3442
it('doesn\'t get an access token', () => {
3543
const tokenName = 'asd';
3644
// Only test the basic function, the general authorization ist tested elsewhere

0 commit comments

Comments
 (0)