This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.component.ts
170 lines (148 loc) · 5.31 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {Component, ChangeDetectionStrategy, AfterViewInit, OnInit, ViewEncapsulation} from "@angular/core";
import {OAuthService} from "angular2-oauth2/oauth-service";
import {OAuthConfigStore} from "./kubernetes/store/oauth-config-store";
import {Observable} from "rxjs";
import {OnLogin} from "./shared/onlogin.service";
import {ActivatedRoute, Router} from '@angular/router';
import {LoginService} from './shared/login.service';
// import { jquery as $ } from 'jquery';
@Component({
host:{
'class':'app app-component flex-container in-column-direction flex-grow-1'
},
selector: 'f8-app',
encapsulation: ViewEncapsulation.None,
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements OnInit, AfterViewInit {
name = 'Fabric8 Console';
title = 'Fabric8 Console';
url = 'https://www.twitter.com/fabric8io';
constructor(private oauthService: OAuthService,
private oauthConfigStore: OAuthConfigStore,
private onLogin: OnLogin,
private activatedRoute: ActivatedRoute,
private loginService: LoginService,
private router: Router) {
// change this to false to use platform authentication directly instead of the custom openshift oauth
this.loginService.useCustomAuth = true;
// set the scope for the permissions the client should request
this.oauthService.scope = "user:full";
// set to true, to receive also an id_token via OpenId Connect (OIDC) in addition to the
// OAuth2-based access_token
// setting to true doesn't work with openshift oauth
this.oauthService.oidc = false;
// Use setStorage to use sessionStorage or another implementation of the TS-type Storage
// instead of localStorage
this.oauthService.setStorage(localStorage);
// this.oauthService.setStorage(sessionStorage);
this.oauthService.redirectUri = window.location.origin;
this.oauthService.clientId = "fabric8";
}
ngOnInit(): void {
}
protected configureOAuth() {
}
ngAfterViewInit() {
if(!this.loginService.useCustomAuth) {
this.activatedRoute.params.subscribe(() => {
this.loginService.login();
});
} else {
var token = "";
if (this.oauthConfigStore.config.authorizeUri) {
token = this.checkLoggedIn();
}
if (!token) {
this.oauthConfigStore.resource.subscribe(config => {
var authorizeUri = config.authorizeUri;
if (authorizeUri) {
//console.log("OAuthConfig loaded with URI: " + authorizeUri);
this.checkLoggedIn();
}
});
}
}
}
protected checkLoggedIn() {
let config = this.oauthConfigStore.config;
let issuer = config.issuer;
let clientId = config.clientId || "fabric8";
let scope = config.scope || "user:full";
this.oauthService.scope = scope;
this.oauthService.loginUrl = config.authorizeUri;
this.oauthService.issuer = issuer;
this.oauthService.clientId = clientId;
//this.oauthService.logoutUrl = config.logoutUri;
console.log("Logging in with client " + clientId + " scope " + scope + " and issuer " + issuer);
let token = this.oauthService.getAccessToken();
if (this.oauthService.hasValidAccessToken()) {
console.log("has valid OAuth token from auth URI: " + config.authorizeUri);
this.onLogin.onLogin(token);
} else {
//console.log("not yet got a valid OAuth token at auth URI: " + config.authorizeUri);
token = null;
this.onLogin.onLogin("");
}
if (token) {
// lets setup a timer for when the token expires
// TODO when this code is merged:
// https://github.com/manfredsteyer/angular2-oauth2/issues/25
// lets use:
//
//let expiresAt = this.oauthService.expiresAt;
let expires = expiresAt();
if (expires) {
console.log("The token expires at " + expires);
Observable.timer(expires).subscribe(() => {
console.log("OAuth token has expired so lets login again");
if (!this.oauthService.tryLogin({
onTokenReceived: context => {
if (context) {
token = context.accessToken;
if (this.oauthService.hasValidAccessToken()) {
console.log("has valid OAuth token!");
this.onLogin.onLogin(token);
}
}
}
})) {
this.oauthService.initImplicitFlow();
}
});
}
}
if (!token) {
if (config.authorizeUri) {
if (!this.oauthService.tryLogin({
onTokenReceived: context => {
if (context) {
token = context.accessToken;
if (this.oauthService.hasValidAccessToken()) {
console.log("has valid OAuth token!");
this.onLogin.onLogin(token);
}
}
}
})) {
this.oauthService.initImplicitFlow();
}
} else {
console.log("No OAuth URI!");
}
}
return token;
}
}
function expiresAt(): Date {
var expiresAt = localStorage.getItem("expires_at");
if (expiresAt) {
var i = parseInt(expiresAt);
if (i) {
return new Date(i);
}
}
return null;
}