Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Zendesk SSO #2

Merged
merged 5 commits into from
Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions app/app-config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,42 @@ config = (
title : 'Home'
controller : 'HomeController as vm'
template : require('./views/home')()


# State parameters
# app : tc|connect|etc..
# retUrl : URL to redirect after authentication
# handle : direct login with handle/password
# password : direct login with handle/password
# return_to: URL of Zendesk to redirect after authentication. This is handed by Zendesk.
#
# Connect example:
# /login?app=connect&retUrl=https%3A%2F%2Fconnect.topcoder.com
# Direct login example:
# /login?app=connect&handle=jdoe&password=xxxxxx&retUrl=https%3A%2F%2Fconnect.topcoder.com
# Zendesk example:
# /login?app=zendesk&return_to=https%3A%2F%2Ftopcoder.zendesk.com
states['login'] =
url: '/login?app&retUrl&handle&password'
url: '/login?app&retUrl&handle&password&return_to'
title: 'Login'
controller : 'LoginController as vm'
template: require('./views/login')()
public: true

states['logout'] =
url: '/logout'
url: '/logout?retUrl'
title: 'Logout'
controller : 'LogoutController as vm'
template: require('./views/logout')()
public: true

states['MEMBER_LOGIN'] =
url: '/tc?retUrl&handle&password'
url: '/tc?retUrl&handle&password&return_to'
controller : 'TCLoginController as vm'
template: require('./views/tc/login')()
public: true

states['SOCIAL_CALLBACK'] =
url: '/social-callback?retUrl&userJWTToken&tcjwt&tcsso&status&message'
url: '/social-callback?retUrl&userJWTToken&status&message'
template : require('./views/tc/social-callback')()
controller : 'SSOCallbackController as vm'
public: true
Expand Down
1 change: 1 addition & 0 deletions app/app.module.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ angular.module 'accounts', dependencies
constants =
ENV : process.env.ENV
DOMAIN : process.env.DOMAIN
ZENDESK_DOMAIN : process.env.ZENDESK_DOMAIN

angular.module('accounts').constant 'Constants', constants
61 changes: 22 additions & 39 deletions app/scripts/connect/login.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
'use strict'

{ login } = require '../../../core/auth.js'
{ TC_JWT } = require '../../../core/constants.js'
{ TC_JWT } = require '../../../core/constants.js'
{ login } = require '../../../core/auth.js'
{ getToken } = require '../../../core/token.js'

ConnectLoginController = (
$log
$rootScope
$location
$window
$scope
$state
$stateParams
$timeout
AuthService
TokenService
Utils
Constants) ->

Expand All @@ -23,24 +19,25 @@ ConnectLoginController = (
vm.loading = false
vm.init = false
vm.$stateParams = $stateParams
vm.retUrl = decodeURIComponent($stateParams.retUrl)

vm.registrationUrl = 'https://connect.' + Constants.DOMAIN + '/registration'
vm.forgotPasswordUrl = 'https://connect.' + Constants.DOMAIN + '/forgot-password'
vm.baseUrl = "https://connect.#{Constants.DOMAIN}"
vm.registrationUrl = vm.baseUrl + '/registration'
vm.forgotPasswordUrl = vm.baseUrl + '/forgot-password'
vm.retUrl = if $stateParams.retUrl then decodeURIComponent($stateParams.retUrl) else vm.baseUrl

vm.submit = ->
vm.error = false
vm.loading = true

# Auth0 connection
# handle: "LDAP", email: "TC-User-Database"
conn = if Utils.isEmail(vm.username) then 'TC-User-Database' else 'LDAP'
conn = Utils.getLoginConnection vm.username

options =
connection: conn
username: vm.username
username: vm.username
password: vm.password

login(options).then(loginSuccess, loginFailure)

loginFailure = (error) ->
Expand All @@ -54,34 +51,25 @@ ConnectLoginController = (
unless jwt
vm.error = true
else if vm.retUrl
redirectUrl = Utils.generateReturnUrl vm.retUrl
$log.info 'redirect back to ' + redirectUrl
$window.location = redirectUrl
Utils.redirectTo Utils.generateReturnUrl(vm.retUrl)
else
$state.go 'home'

# vm.socialLogin = (provider) ->
# callbackUrl = $state.href 'home', {}, { absolute: true }
# authUrl = AuthService.generateSSOUrl provider, callbackUrl
# $log.info "auth with: "+authUrl
# $window.location = authUrl


init = ->
jwt = localStorage.getItem(TC_JWT)

jwt = getToken(TC_JWT)
if jwt && vm.retUrl
redirectUrl = Utils.generateReturnUrl vm.retUrl
$log.info 'redirect back to ' + redirectUrl
$window.location = redirectUrl
Utils.redirectTo Utils.generateReturnUrl(vm.retUrl)
else if ($stateParams.handle || $stateParams.email) && $stateParams.password
id = $stateParams.handle || $stateParams.email
pass = $stateParams.password
loginOptions =
options =
connection: Utils.getLoginConnection id
username: id
password: pass
error : loginFailure
success : loginSuccess
AuthService.login loginOptions

login(options)
.then(loginSuccess)
.catch(loginFailure)
else
vm.init = true
vm
Expand All @@ -91,14 +79,9 @@ ConnectLoginController = (

ConnectLoginController.$inject = [
'$log'
'$rootScope'
'$location'
'$window'
'$scope'
'$state'
'$stateParams'
'$timeout'
'AuthService'
'TokenService'
'Utils'
'Constants'
]
Expand Down
25 changes: 7 additions & 18 deletions app/scripts/connect/sso-callback.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

{ TC_JWT } = require '../../../core/constants.js'
{ login } = require '../../../core/auth.js'
{ setToken } = require '../../../core/token.js'

SSOCallbackController = (
$log
$state
$stateParams
$window
$cookies
$http
API_URL
TokenService
AuthService
Utils) ->

vm = this
Expand All @@ -35,16 +34,11 @@ SSOCallbackController = (
$log.warn status + ', ' + message
return vm

TokenService.setAppirioJWT $stateParams.userJWTToken
TokenService.getAuth0Token $stateParams.tcjwt || ''
TokenService.setSSOToken $stateParams.tcsso || ''
setToken(TC_JWT, $stateParams.userJWTToken)

redirectUrl = Utils.generateReturnUrl $stateParams.retUrl
unless redirectUrl
error = Utils.redirectTo Utils.generateReturnUrl($stateParams.retUrl)
if error
vm.error = 'Invalid URL is assigned to the return-URL.'
else
$log.info 'redirect back to ' + redirectUrl
$window.location = redirectUrl
vm

init()
Expand All @@ -53,12 +47,7 @@ SSOCallbackController.$inject = [
'$log'
'$state'
'$stateParams'
'$window'
'$cookies'
'$http'
'API_URL'
'TokenService'
'AuthService'
'Utils'
]

Expand Down
14 changes: 6 additions & 8 deletions app/scripts/connect/sso-login.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict'

{ getSSOProvider, generateSSOUrl } = require '../../../core/auth.js'

SSOLoginController = (
$log
$state
$stateParams
$window
$authService
AuthService
TokenService
Utils) ->

vm = this
Expand All @@ -34,15 +33,17 @@ SSOLoginController = (
vm.loading = false
vm.error = err.message

AuthService.getSSOProvider(vm.emailOrHandle).then(success).catch(failure)
getSSOProvider(vm.emailOrHandle)
.then(success)
.catch(failure)

go = ->
state = vm.retUrl
unless state
# TODO: home?
state = $state.href 'home', {}, { absolute: true }
callbackUrl = $state.href 'SSO_CALLBACK', {retUrl : state}, { absolute: true }
authUrl = AuthService.generateSSOUrl vm.org, callbackUrl
authUrl = generateSSOUrl vm.org, callbackUrl
$log.info 'redirecting to ' + authUrl
$window.location.href = authUrl;

Expand All @@ -55,9 +56,6 @@ SSOLoginController.$inject = [
'$state'
'$stateParams'
'$window'
'$authService'
'AuthService'
'TokenService'
'Utils'
]

Expand Down
30 changes: 10 additions & 20 deletions app/scripts/home.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
'use strict'

{ TC_JWT } = require '../../core/constants.js'
{ decodeToken } = require '../../core/token.js'
{ isLoggedIn } = require '../../core/auth.js'
{ TC_JWT } = require '../../core/constants.js'
{ getToken } = require '../../core/token.js'

HomeController = (
$log
$state
AuthService
TokenService) ->
$window
Constants) ->

vm = this
vm.title = 'Home'
vm.account = null
vm.loading = false

vm.logout = ->
$state.go 'logout'

vm.isLoggedIn = ->
isLoggedIn()

init = ->
jwt = localStorage.getItem(TC_JWT)

unless jwt
unless getToken(TC_JWT)
$state.go 'MEMBER_LOGIN'
else
vm.account = decodeToken(jwt).handle

$window.location = 'https://www.' + Constants.DOMAIN + '/'
vm

init()


HomeController.$inject = [
'$log'
'$state'
'AuthService'
'TokenService'
'$window'
'Constants'
]

angular.module('accounts').controller 'HomeController', HomeController
6 changes: 3 additions & 3 deletions app/scripts/login.controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

LoginController = (
$log
$window
$state
$stateParams
Constants
Utils) ->

vm = this
Expand All @@ -29,7 +29,7 @@ LoginController = (
init = ->
if isConnectLogin()
$state.go 'CONNECT_LOGIN', Utils.encodeParams $stateParams
else
else
$state.go 'MEMBER_LOGIN', Utils.encodeParams $stateParams
vm

Expand All @@ -38,9 +38,9 @@ LoginController = (

LoginController.$inject = [
'$log'
'$window'
'$state'
'$stateParams'
'Constants'
'Utils'
]

Expand Down
Loading