This commit is contained in:
peter
2019-11-09 11:53:36 +01:00
parent 92824d67a5
commit a7a5a85033
10 changed files with 79 additions and 32 deletions
+3 -3
View File
@@ -14269,9 +14269,9 @@
"integrity": "sha512-ZYzRkETgBrdEGzL5JSKimvjI2CX7ioyZCkX2BpcfyjqI+079W0wHAyj5W4rIZMcDSOHgLZtgz1IdDi/vU77KEQ=="
},
"rpclibrary": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/rpclibrary/-/rpclibrary-1.4.0.tgz",
"integrity": "sha512-JUU7+HymV2ZTI+NrIVOiIr5LGFoRsPq4k/ffdUonqIpd9fjOHBQ2/PFLVXx9z94+dKxdqN8/D9ZXPwU03ovZrg==",
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/rpclibrary/-/rpclibrary-1.4.2.tgz",
"integrity": "sha512-IFigD+65a9MM+1AgEH5nurUzCyMg9hawqtnOA67/PtTGP5GsnWxaFESKl03k8L/PQ8ptZkWcub/63bfxkrwyMw==",
"requires": {
"bsock": "^0.1.9",
"http": "0.0.0",
+1 -1
View File
@@ -69,7 +69,7 @@
"normalize.css": "6.0.0",
"pace-js": "1.0.2",
"roboto-fontface": "0.8.0",
"rpclibrary": "^1.4.0",
"rpclibrary": "^1.4.2",
"rxjs": "6.5.2",
"rxjs-compat": "6.3.0",
"socicon": "3.0.5",
+3
View File
@@ -19,6 +19,9 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.analytics.trackPageViews();
this.loginSvc.getFeature('createUser').then( (f) => {
})
window['s'] = this.loginSvc
}
}
+31 -11
View File
@@ -1,14 +1,14 @@
import { Injectable } from "@angular/core";
import {RPCSocket} from 'rpclibrary/js/src/Frontend'
import { Token, Auth, User, _Rank, _Class, FrontcraftFeatureIfc, LoginManagerIfc, } from '../../../../backend/Types/Types'
import { Token, Auth, User, _Class, FrontcraftFeatureIfc, LoginManagerIfc, SomeOf, } from '../../../../backend/Types/Types'
import { CookieService } from 'ngx-cookie-service';
@Injectable()
export class LoginApiService{
private socket:RPCSocket & LoginManagerIfc;
private auth:Auth
private privSocket: RPCSocket & FrontcraftFeatureIfc
private privSocket: RPCSocket & SomeOf<FrontcraftFeatureIfc>
constructor(
private cookieSvc : CookieService
@@ -16,10 +16,21 @@ export class LoginApiService{
getUnprivilegedSocket = () : RPCSocket & LoginManagerIfc => this.socket
private getPrivilegedSocket = async (auth:Auth) : Promise<RPCSocket & FrontcraftFeatureIfc> => {
private getPrivilegedSocket = async (auth:Auth) : Promise<RPCSocket & SomeOf<FrontcraftFeatureIfc>> => {
if(this.privSocket) return this.privSocket
if(auth.user.rank === 'Guest'){
return await RPCSocket.makeSocket<SomeOf<FrontcraftFeatureIfc>>(
20001,
window.location.hostname
)
}
try{
const sock: RPCSocket & FrontcraftFeatureIfc = await new RPCSocket(auth.port, window.location.hostname).connect(auth.token.value)
const sock = await RPCSocket.makeSocket<SomeOf<FrontcraftFeatureIfc>>(
auth.port,
window.location.hostname,
auth.token.value
)
//login success
this.auth = auth
this.privSocket = sock
@@ -29,7 +40,12 @@ export class LoginApiService{
//login failed
throw new Error('login failed')
}
}
}
getFeature = async <K extends keyof FrontcraftFeatureIfc>(feature : K) : Promise<void | FrontcraftFeatureIfc[K]> => {
const sock = await this.getPrivilegedSocket(this.auth)
if(sock[feature]) return <FrontcraftFeatureIfc[K]> sock[feature]
}
getCurrentUser = () : User => this.auth
? this.auth.user
@@ -40,12 +56,12 @@ export class LoginApiService{
rank: 'Guest'
}
authenticate = async (token : Token | string) : Promise<RPCSocket & FrontcraftFeatureIfc> => {
authenticate = async (token : Token | string) : Promise<RPCSocket & SomeOf<FrontcraftFeatureIfc>> => {
const auth = await this.socket.Authenticator.authenticate(token)
return await this.getPrivilegedSocket(auth)
}
login = async (username, password) => {
login = async (username: string, password: string) => {
const buf = str2arraybuf(password)
const pwHash = await crypto.subtle.digest('SHA-256', buf);
const token = await this.socket.Authenticator.login(username, buf2hex(pwHash))
@@ -69,12 +85,16 @@ export class LoginApiService{
}
initialize = async () : Promise<RPCSocket> => {
const sock : RPCSocket & LoginManagerIfc = await new RPCSocket(20000, window.location.hostname).connect()
const sock = await RPCSocket.makeSocket<LoginManagerIfc>(20000, window.location.hostname)
this.socket = sock
const cookie = this.getCookie()
if(cookie) {
return await this.authenticate(cookie)
try{
return await this.authenticate(cookie)
}catch(e){
this.logout()
}
}
return sock
@@ -89,7 +109,7 @@ function buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
export function initializeLoginSvc(svc: LoginApiService): () => Promise<RPCSocket> {
//angular depenency manager requires this
export function initializeLoginSvc(svc: LoginApiService): () => Promise<any> {
return svc.initialize
}