From 757dd9785ce299c4e891437c9cc38764d62c3267 Mon Sep 17 00:00:00 2001 From: Anna Shumilova Date: Thu, 12 Oct 2017 12:44:55 +0300 Subject: [PATCH] che-6689: fix oauth login through github in UD Signed-off-by: Anna Shumilova --- .../import-github-project.controller.ts | 29 ++++++++++++++++++- .../src/components/github/github-service.ts | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/dashboard/src/app/workspaces/create-workspace/project-source-selector/add-import-project/import-github-project/import-github-project.controller.ts b/dashboard/src/app/workspaces/create-workspace/project-source-selector/add-import-project/import-github-project/import-github-project.controller.ts index 0682267e0df..5346e403687 100644 --- a/dashboard/src/app/workspaces/create-workspace/project-source-selector/add-import-project/import-github-project/import-github-project.controller.ts +++ b/dashboard/src/app/workspaces/create-workspace/project-source-selector/add-import-project/import-github-project/import-github-project.controller.ts @@ -108,12 +108,19 @@ export class ImportGithubProjectController { * The list of selected repositories. */ private selectedRepositories: Array; + /** + * Keycloak auth service. + */ + private keycloakAuth: any; /** * Default constructor that is using resource * @ngInject for Dependency injection */ - constructor ($q: ng.IQService, $mdDialog: ng.material.IDialogService, $location: ng.ILocationService, $browser: ng.IBrowserService, $scope: ng.IScope, githubPopup: any, cheBranding: CheBranding, githubOrganizationNameResolver: any, importGithubProjectService: ImportGithubProjectService, cheListHelperFactory: che.widget.ICheListHelperFactory, addImportProjectService: AddImportProjectService) { + constructor ($q: ng.IQService, $mdDialog: ng.material.IDialogService, $location: ng.ILocationService, + $browser: ng.IBrowserService, $scope: ng.IScope, githubPopup: any, cheBranding: CheBranding, + githubOrganizationNameResolver: any, importGithubProjectService: ImportGithubProjectService, + cheListHelperFactory: che.widget.ICheListHelperFactory, addImportProjectService: AddImportProjectService, keycloakAuth: any) { this.$q = $q; this.$mdDialog = $mdDialog; this.$location = $location; @@ -123,6 +130,7 @@ export class ImportGithubProjectController { this.githubOrganizationNameResolver = githubOrganizationNameResolver; this.resolveOrganizationName = this.githubOrganizationNameResolver.resolve; this.addImportProjectService = addImportProjectService; + this.keycloakAuth = keycloakAuth; this.importGithubProjectService = importGithubProjectService; this.productName = cheBranding.getName(); @@ -209,6 +217,24 @@ export class ImportGithubProjectController { return; } + if (this.keycloakAuth.isPresent) { + this.keycloakAuth.keycloak.updateToken(5).success(() => { + let token = '&token=' + this.keycloakAuth.keycloak.token; + this.openGithubPopup(token); + }).error(() => { + this.keycloakAuth.keycloak.login(); + }); + } else { + this.openGithubPopup(''); + } + } + + /** + * Opens Github popup. + * + * @param {string} token + */ + openGithubPopup(token: string): void { const redirectUrl = this.$location.protocol() + '://' + this.$location.host() + ':' + this.$location.port() @@ -218,6 +244,7 @@ export class ImportGithubProjectController { + '?oauth_provider=github' + '&scope=' + ['user', 'repo', 'write:public_key'].join(',') + '&userId=' + this.importGithubProjectService.getCurrentUserId() + + token + '&redirect_after_login=' + redirectUrl, { diff --git a/dashboard/src/components/github/github-service.ts b/dashboard/src/components/github/github-service.ts index 52eea7b731f..30681d44c0c 100644 --- a/dashboard/src/components/github/github-service.ts +++ b/dashboard/src/components/github/github-service.ts @@ -82,7 +82,7 @@ export class GitHubService { } catch (error) { } - if (popupWindow.closed) { + if (popupWindow && popupWindow.closed) { $interval.cancel(polling); deferred.reject({data: 'Authorization Failed'}); }