Add check for private key in localstorage.
This commit is contained in:
		
							parent
							
								
									060c47f840
								
							
						
					
					
						commit
						a9f007573f
					
				@ -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<boolean> {
 | 
			
		||||
      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<any> {
 | 
			
		||||
      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<any> {
 | 
			
		||||
      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<boolean> {
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -145,7 +141,7 @@ export class AuthService {
 | 
			
		||||
      // TODO leaving this out for now.
 | 
			
		||||
      // const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
 | 
			
		||||
      // if (!isEncryptedKeyCheck) {
 | 
			
		||||
      //   throw Error('The private key doesn\'t have a password!');
 | 
			
		||||
      //   throw Error('The private key does not have a password!');
 | 
			
		||||
      // }
 | 
			
		||||
      const key = await this.mutableKeyStore.importPrivateKey(privateKeyArmored);
 | 
			
		||||
      localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,7 @@ import { TokenRegistry } from '@app/_eth';
 | 
			
		||||
import { HttpClient } from '@angular/common/http';
 | 
			
		||||
import { RegistryService } from '@app/_services/registry.service';
 | 
			
		||||
import { Token } from '@app/_models';
 | 
			
		||||
import {BehaviorSubject, Observable, Subject} from 'rxjs';
 | 
			
		||||
import { BehaviorSubject, Observable, Subject } from 'rxjs';
 | 
			
		||||
 | 
			
		||||
@Injectable({
 | 
			
		||||
  providedIn: 'root',
 | 
			
		||||
@ -14,7 +14,9 @@ export class TokenService {
 | 
			
		||||
  tokenRegistry: TokenRegistry;
 | 
			
		||||
  onload: (status: boolean) => void;
 | 
			
		||||
  tokens: Array<Token> = [];
 | 
			
		||||
  private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(this.tokens);
 | 
			
		||||
  private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
 | 
			
		||||
    this.tokens
 | 
			
		||||
  );
 | 
			
		||||
  tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
 | 
			
		||||
 | 
			
		||||
  constructor(private httpClient: HttpClient) {}
 | 
			
		||||
 | 
			
		||||
@ -8,7 +8,7 @@
 | 
			
		||||
            <h1 class="text-white">CICADA</h1>
 | 
			
		||||
          </a>
 | 
			
		||||
        </mat-card-title>
 | 
			
		||||
        <div  id="one" style="display: block" class="card-body p-4">
 | 
			
		||||
        <div id="one" style="display: block" class="card-body p-4">
 | 
			
		||||
 | 
			
		||||
          <div class="text-center w-75 m-auto">
 | 
			
		||||
            <h4 class="text-dark-50 text-center font-weight-bold">Add Private Key</h4>
 | 
			
		||||
@ -32,7 +32,7 @@
 | 
			
		||||
 | 
			
		||||
          </form>
 | 
			
		||||
        </div>
 | 
			
		||||
        <div  id="two" style="display: none" class="card-body p-4 align-items-center">
 | 
			
		||||
        <div id="two" style="display: none" class="card-body p-4 align-items-center">
 | 
			
		||||
 | 
			
		||||
          <div class="text-center w-75 m-auto">
 | 
			
		||||
            <h4 id="state" class="text-dark-50 text-center font-weight-bold"></h4>
 | 
			
		||||
 | 
			
		||||
@ -22,13 +22,16 @@ export class AuthComponent implements OnInit {
 | 
			
		||||
    private authService: AuthService,
 | 
			
		||||
    private formBuilder: FormBuilder,
 | 
			
		||||
    private router: Router,
 | 
			
		||||
    private errorDialogService: ErrorDialogService,
 | 
			
		||||
    private errorDialogService: ErrorDialogService
 | 
			
		||||
  ) {}
 | 
			
		||||
 | 
			
		||||
  async ngOnInit(): Promise<void> {
 | 
			
		||||
    this.keyForm = this.formBuilder.group({
 | 
			
		||||
      key: ['', Validators.required],
 | 
			
		||||
    });
 | 
			
		||||
    if (this.authService.getPrivateKey()) {
 | 
			
		||||
      this.authService.loginView();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  get keyFormStub(): any {
 | 
			
		||||
@ -49,10 +52,10 @@ export class AuthComponent implements OnInit {
 | 
			
		||||
 | 
			
		||||
  async login(): Promise<void> {
 | 
			
		||||
    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,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user