Authentication support for cdt
[appc/cdt.git] / src / app / vnfs / userlogin-form / userlogin-form.component.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import {Component, OnInit} from '@angular/core';
23
24 import { ActivatedRoute } from '@angular/router';
25 import {EmitterService} from '../../shared/services/emitter.service';
26 import { environment } from '../../../environments/environment';
27 import { HttpUtilService } from '../../shared/services/httpUtil/http-util.service';
28 import {Router} from '@angular/router';
29 import { NgProgress } from 'ngx-progressbar';
30 import {UtilityService} from '../../shared/services/utilityService/utility.service';
31 import { Http, Response, Headers, RequestOptions } from '@angular/http';
32
33 @Component({selector: 'app-mvnfs-form', templateUrl: './userlogin-form.component.html', styleUrls: ['./userlogin-form.component.css']})
34 export class userloginFormComponent implements OnInit {
35
36     userId: string = '';
37     password: string = '';
38     returnUrl:string
39     invalid = true;
40     errorMessage = '';
41
42     constructor(private router: Router, private utiltiy: UtilityService, private route: ActivatedRoute,
43             private http: Http, private ngProgress: NgProgress) {
44     }
45
46     ngOnInit() {
47            this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/home';
48     }
49
50
51     getData() {
52         this.ngProgress.start();
53         var getHeader = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
54         var authStr = btoa(this.userId + ':' + this.password);
55         const options = {
56                 headers: new Headers({
57                     'Content-Type': 'application/json',
58                     'Authorization': 'Basic ' + authStr
59                 }),
60                 observe: 'response'
61         };
62         const data = {
63                 'input': {
64                     'design-request': {
65                         'request-id': '1',
66                         'action': 'getDesigns',
67                         'payload': '{"userID": "","filter":"reference"}'
68                     }
69                 }
70             };
71             const x = JSON.parse(data.input['design-request']['payload']);
72             x.userID = this.userId;
73             data.input['design-request']['payload'] = JSON.stringify(x);
74         console.log("auth " + btoa(this.userId + ':' + this.password));
75         this.http.post(environment.getDesigns,data,options).subscribe(resp => {
76                 console.log("status " + resp.status);
77                 if(resp.status == 200){
78                     console.log('logged in');
79                     sessionStorage['userId'] = this.userId;
80                     sessionStorage['apiToken'] = this.utiltiy.randomId();
81                     sessionStorage['auth'] = authStr;
82                     EmitterService
83                         .get('userLogin')
84                         .emit(this.userId);
85                     this.router.navigateByUrl(this.returnUrl);
86                 } else {
87                     console.log("Invalid user or password");
88                     this.invalid = true;
89                     this.errorMessage = 'Invalid user or password';
90                 }
91             }, error => {
92                 console.log(error);
93                 if(error.status == 401){
94                     this.invalid = true;
95                     this.errorMessage = 'Invalid user or password';
96                 } else {
97                     this.invalid = true;
98                     this.errorMessage = 'Incorrect login or connection error.';
99                 }
100             });
101         console.log("After");
102         
103     }
104
105     validateUserName(){
106         if (!this.userId.trim() || this.userId.length < 1) {
107             this.errorMessage = '';
108             this.invalid = true;
109         }else if(this.userId.startsWith(' ') || this.userId.endsWith(' ')){
110             this.errorMessage = 'Leading and trailing space is not allowed in username';
111             this.invalid = true;
112         } else if(this.userId.includes('  ')){
113             this.errorMessage = 'More than one space is not allowed in username';
114              this.invalid = true;
115         } else if(this.userId.length > 50) {
116             this.errorMessage = 'Username should be of minimum one character and maximum 50 character';
117              this.invalid = true;
118         }else {
119             this.invalid = false;
120             this.errorMessage = '';
121         }
122     }
123     
124     validatePassword(){
125         if (!this.password.trim() || this.password.length < 1) {
126             this.errorMessage = '';
127             this.invalid = true;
128         }else if(this.password.startsWith(' ') || this.password.endsWith(' ')){
129             this.errorMessage = 'Leading and trailing space is not allowed in password';
130             this.invalid = true;
131         } else if(this.password.includes('  ')){
132             this.errorMessage = 'More than one space is not allowed in password';
133              this.invalid = true;
134         } else if(this.password.length > 50) {
135             this.errorMessage = 'Password should be of minimum one character and maximum 50 character';
136              this.invalid = true;
137         }else {
138             this.invalid = false;
139             this.errorMessage = '';
140         }
141     }
142
143 }