diff --git a/src/app/_services/auth.service.ts b/src/app/_services/auth.service.ts index 937f2ce..59df582 100644 --- a/src/app/_services/auth.service.ts +++ b/src/app/_services/auth.service.ts @@ -35,7 +35,7 @@ export class AuthService { await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))); } } - + getSessionToken(): string { return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); } @@ -49,84 +49,80 @@ export class AuthService { } getWithToken(): Promise { - const headers = { - Authorization: 'Bearer ' + this.getSessionToken, - 'Content-Type': 'application/json;charset=utf-8', - 'x-cic-automerge': 'none', - }; - const options = { - headers, - }; - return fetch(environment.cicMetaUrl, options).then((response) => { - if (!response.ok) { - this.loggingService.sendErrorLevelMessage('failed to get with auth token.', - this, - { error: "" }); + const headers = { + Authorization: 'Bearer ' + this.getSessionToken, + 'Content-Type': 'application/json;charset=utf-8', + 'x-cic-automerge': 'none', + }; + const options = { + headers, + }; + return fetch(environment.cicMetaUrl, options).then((response) => { + if (!response.ok) { + this.loggingService.sendErrorLevelMessage('failed to get with auth token.', this, { + error: '', + }); - return false; - } - return true; - }); + return false; + } + return true; + }); } // TODO rename to send signed challenge and set session. Also separate these responsibilities sendSignedChallenge(hobaResponseEncoded: any): Promise { - const headers = { - Authorization: 'HOBA ' + hobaResponseEncoded, - 'Content-Type': 'application/json;charset=utf-8', - 'x-cic-automerge': 'none', - }; - const options = { - headers, - }; - return fetch(environment.cicMetaUrl, options) + const headers = { + Authorization: 'HOBA ' + hobaResponseEncoded, + 'Content-Type': 'application/json;charset=utf-8', + 'x-cic-automerge': 'none', + }; + const options = { + headers, + }; + return fetch(environment.cicMetaUrl, options); } getChallenge(): Promise { - return fetch(environment.cicMetaUrl) - .then(response => { - if (response.status === 401) { - const authHeader: string = response.headers.get('WWW-Authenticate'); - return hobaParseChallengeHeader(authHeader); - } - }); + return fetch(environment.cicMetaUrl).then((response) => { + if (response.status === 401) { + const authHeader: string = response.headers.get('WWW-Authenticate'); + return hobaParseChallengeHeader(authHeader); + } + }); } async login(): Promise { if (this.getSessionToken()) { - sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); + sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); } else { - const o = await this.getChallenge(); + const o = await this.getChallenge(); - const r = await signChallenge( - o.challenge, - o.realm, - environment.cicMetaUrl, - this.mutableKeyStore - ); + const r = await signChallenge( + o.challenge, + o.realm, + environment.cicMetaUrl, + this.mutableKeyStore + ); - const tokenResponse = await this.sendSignedChallenge(r) - .then(response => { - const token = response.headers.get('Token') - if (token) { - return token - } - if (response.status === 401) { - let e = new HttpError("You are not authorized to use this system", response.status) - throw e - } - if (!response.ok) { - let e = new HttpError("Unknown error from authentication server", response.status) - throw e - } - }) - - if (tokenResponse) { - this.setSessionToken(tokenResponse); - this.setState('Click button to log in'); - return true + const tokenResponse = await this.sendSignedChallenge(r).then((response) => { + const token = response.headers.get('Token'); + if (token) { + return token; } - return false + if (response.status === 401) { + throw new HttpError('You are not authorized to use this system', response.status); + } + if (!response.ok) { + throw new HttpError('Unknown error from authentication server', response.status); + } + }); + + if (tokenResponse) { + this.setSessionToken(tokenResponse); + this.setState('Click button to log in'); + return true; + } + return false; } } diff --git a/src/app/auth/auth.component.ts b/src/app/auth/auth.component.ts index 328997f..4272cec 100644 --- a/src/app/auth/auth.component.ts +++ b/src/app/auth/auth.component.ts @@ -22,7 +22,7 @@ export class AuthComponent implements OnInit { private authService: AuthService, private formBuilder: FormBuilder, private router: Router, - private errorDialogService: ErrorDialogService, + private errorDialogService: ErrorDialogService ) {} async ngOnInit(): Promise { @@ -49,10 +49,10 @@ export class AuthComponent implements OnInit { async login(): Promise { try { - const loginResult = await this.authService.login() - if (loginResult) { + const loginResult = await this.authService.login(); + if (loginResult) { this.router.navigate(['/home']); - } + } } catch (HttpError) { this.errorDialogService.openDialog({ message: HttpError.message,