|
|
@@ -1,14 +1,14 @@
|
|
1
|
1
|
import { Injectable } from "@angular/core";
|
|
2
|
2
|
import {RPCSocket} from 'rpclibrary/js/src/Frontend'
|
|
3
|
3
|
|
|
4
|
|
-import { Token, Auth, User, _Rank, _Class, FrontcraftFeatureIfc, LoginManagerIfc, } from '../../../../backend/Types/Types'
|
|
|
4
|
+import { Token, Auth, User, _Class, FrontcraftFeatureIfc, LoginManagerIfc, SomeOf, } from '../../../../backend/Types/Types'
|
|
5
|
5
|
import { CookieService } from 'ngx-cookie-service';
|
|
6
|
6
|
|
|
7
|
7
|
@Injectable()
|
|
8
|
8
|
export class LoginApiService{
|
|
9
|
9
|
private socket:RPCSocket & LoginManagerIfc;
|
|
10
|
10
|
private auth:Auth
|
|
11
|
|
- private privSocket: RPCSocket & FrontcraftFeatureIfc
|
|
|
11
|
+ private privSocket: RPCSocket & SomeOf<FrontcraftFeatureIfc>
|
|
12
|
12
|
|
|
13
|
13
|
constructor(
|
|
14
|
14
|
private cookieSvc : CookieService
|
|
|
@@ -16,10 +16,21 @@ export class LoginApiService{
|
|
16
|
16
|
|
|
17
|
17
|
getUnprivilegedSocket = () : RPCSocket & LoginManagerIfc => this.socket
|
|
18
|
18
|
|
|
19
|
|
- private getPrivilegedSocket = async (auth:Auth) : Promise<RPCSocket & FrontcraftFeatureIfc> => {
|
|
|
19
|
+ private getPrivilegedSocket = async (auth:Auth) : Promise<RPCSocket & SomeOf<FrontcraftFeatureIfc>> => {
|
|
20
|
20
|
if(this.privSocket) return this.privSocket
|
|
|
21
|
+ if(auth.user.rank === 'Guest'){
|
|
|
22
|
+ return await RPCSocket.makeSocket<SomeOf<FrontcraftFeatureIfc>>(
|
|
|
23
|
+ 20001,
|
|
|
24
|
+ window.location.hostname
|
|
|
25
|
+ )
|
|
|
26
|
+ }
|
|
21
|
27
|
try{
|
|
22
|
|
- const sock: RPCSocket & FrontcraftFeatureIfc = await new RPCSocket(auth.port, window.location.hostname).connect(auth.token.value)
|
|
|
28
|
+ const sock = await RPCSocket.makeSocket<SomeOf<FrontcraftFeatureIfc>>(
|
|
|
29
|
+ auth.port,
|
|
|
30
|
+ window.location.hostname,
|
|
|
31
|
+ auth.token.value
|
|
|
32
|
+ )
|
|
|
33
|
+
|
|
23
|
34
|
//login success
|
|
24
|
35
|
this.auth = auth
|
|
25
|
36
|
this.privSocket = sock
|
|
|
@@ -29,7 +40,12 @@ export class LoginApiService{
|
|
29
|
40
|
//login failed
|
|
30
|
41
|
throw new Error('login failed')
|
|
31
|
42
|
}
|
|
32
|
|
- }
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ getFeature = async <K extends keyof FrontcraftFeatureIfc>(feature : K) : Promise<void | FrontcraftFeatureIfc[K]> => {
|
|
|
46
|
+ const sock = await this.getPrivilegedSocket(this.auth)
|
|
|
47
|
+ if(sock[feature]) return <FrontcraftFeatureIfc[K]> sock[feature]
|
|
|
48
|
+ }
|
|
33
|
49
|
|
|
34
|
50
|
getCurrentUser = () : User => this.auth
|
|
35
|
51
|
? this.auth.user
|
|
|
@@ -40,12 +56,12 @@ export class LoginApiService{
|
|
40
|
56
|
rank: 'Guest'
|
|
41
|
57
|
}
|
|
42
|
58
|
|
|
43
|
|
- authenticate = async (token : Token | string) : Promise<RPCSocket & FrontcraftFeatureIfc> => {
|
|
|
59
|
+ authenticate = async (token : Token | string) : Promise<RPCSocket & SomeOf<FrontcraftFeatureIfc>> => {
|
|
44
|
60
|
const auth = await this.socket.Authenticator.authenticate(token)
|
|
45
|
61
|
return await this.getPrivilegedSocket(auth)
|
|
46
|
62
|
}
|
|
47
|
63
|
|
|
48
|
|
- login = async (username, password) => {
|
|
|
64
|
+ login = async (username: string, password: string) => {
|
|
49
|
65
|
const buf = str2arraybuf(password)
|
|
50
|
66
|
const pwHash = await crypto.subtle.digest('SHA-256', buf);
|
|
51
|
67
|
const token = await this.socket.Authenticator.login(username, buf2hex(pwHash))
|
|
|
@@ -69,12 +85,16 @@ export class LoginApiService{
|
|
69
|
85
|
}
|
|
70
|
86
|
|
|
71
|
87
|
initialize = async () : Promise<RPCSocket> => {
|
|
72
|
|
- const sock : RPCSocket & LoginManagerIfc = await new RPCSocket(20000, window.location.hostname).connect()
|
|
|
88
|
+ const sock = await RPCSocket.makeSocket<LoginManagerIfc>(20000, window.location.hostname)
|
|
73
|
89
|
this.socket = sock
|
|
74
|
90
|
|
|
75
|
91
|
const cookie = this.getCookie()
|
|
76
|
92
|
if(cookie) {
|
|
77
|
|
- return await this.authenticate(cookie)
|
|
|
93
|
+ try{
|
|
|
94
|
+ return await this.authenticate(cookie)
|
|
|
95
|
+ }catch(e){
|
|
|
96
|
+ this.logout()
|
|
|
97
|
+ }
|
|
78
|
98
|
}
|
|
79
|
99
|
|
|
80
|
100
|
return sock
|
|
|
@@ -89,7 +109,7 @@ function buf2hex(buffer) { // buffer is an ArrayBuffer
|
|
89
|
109
|
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
|
|
90
|
110
|
}
|
|
91
|
111
|
|
|
92
|
|
-
|
|
93
|
|
-export function initializeLoginSvc(svc: LoginApiService): () => Promise<RPCSocket> {
|
|
|
112
|
+//angular depenency manager requires this
|
|
|
113
|
+export function initializeLoginSvc(svc: LoginApiService): () => Promise<any> {
|
|
94
|
114
|
return svc.initialize
|
|
95
|
115
|
}
|