Repository navigation
Expand file tree
/
Copy pathauth.service.ts
More file actions
61 lines (53 loc) · 1.82 KB
/
Copy pathauth.service.ts
File metadata and controls
61 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Injectable } from '@angular/core';
import { KJUR } from 'jsrsasign';
@Injectable()
export class AuthService {
constructor() { }
/**
* Method to create a JWT using dummy auth settings and incorporating a nonce. THis will be passed into Comapi as part of the auth flow.
* The jsrsasign library is used to perform this task
* This is a basic example with no authentication mechanism, Usually this would be linked to your own security system.
* @param {profileId} profileId
* @param {profileId} nonce
* @returns {Promise<string>} - returns a jwt via a promise (using promises as a real app will probably do this asynchronously)
*/
public createJwt(profileId: string, nonce: string): Promise<string> {
// Header
let oHeader = { alg: 'HS256', typ: 'JWT' };
// Payload
let tNow = KJUR.jws.IntDate.get('now');
let tEnd = KJUR.jws.IntDate.get('now + 1day');
let oPayload = {
sub: profileId,
nonce: nonce,
iss: "local",
aud: "local",
iat: tNow,
exp: tEnd,
};
let sHeader = JSON.stringify(oHeader);
let sPayload = JSON.stringify(oPayload);
let sJWT = KJUR.jws.JWS.sign("HS256", sHeader, sPayload, {utf8: "secret"});
return Promise.resolve(sJWT);
}
/**
* Method to retrieve the current profileId from local storage
* @returns {string} - returns a profileId
*/
public getProfileId(): string {
return localStorage.getItem("compapiChat.profileId");
}
/**
* Method to set the current profileId in local storage
* @param {profileId} profileId
*/
public setProfileId(profileId): void {
localStorage.setItem("compapiChat.profileId", profileId);
}
/**
* Method to clear the current profileId from local storage
*/
public clearProfileId(): void {
localStorage.removeItem("compapiChat.profileId");
}
}