Commit b040f21f authored by Gergely Ujvari's avatar Gergely Ujvari

Simplify auth service

parent 9e4a3a45
......@@ -11,6 +11,9 @@ class AppController
) ->
{plugins, host, providers} = annotator
$scope.auth = auth
isFirstRun = $location.search().hasOwnProperty('firstrun')
applyUpdates = (action, data) ->
"""Update the application with new data from the websocket."""
return unless data?.length
......@@ -103,24 +106,6 @@ class AppController
annotator.deleteAnnotation first
$timeout -> cleanup rest
onlogin = (assertion) ->
user.login assertion, reset
onlogout = ->
user.logout()
reset()
onready = ->
persona = user.getPersona()
if not user.checkingInProgress() and typeof persona == 'undefined'
# If we're not checking the token and persona is undefined, onlogin
# hasn't run, which means we aren't authenticated.
user.noPersona()
reset()
if isFirstRun
$scope.login()
oncancel = ->
$scope.dialog.visible = false
......@@ -154,7 +139,7 @@ class AppController
$scope.sort = {name, predicate}
$scope.$watch 'store.entities', (entities, oldEntities) ->
return if entities is oldEntities or not entities
return if entities is oldEntities
if entities.length
streamfilter
......@@ -163,14 +148,18 @@ class AppController
streamer.send({filter: streamfilter.getFilter()})
$scope.$watch 'auth.user', (newVal, oldVal) ->
reset() if newVal isnt undefined
$scope.login() if isFirstRun and newVal is null and oldVal is undefined
$scope.login = ->
$scope.dialog.visible = true
auth.login().then(reset, oncancel)
identity.request(oncancel)
$scope.logout = ->
return unless drafts.discard()
$scope.dialog.visible = false
auth.logout().then(reset)
identity.logout()
$scope.loadMore = (number) ->
unless streamfilter.getPastData().hits then return
......@@ -199,11 +188,6 @@ class AppController
$scope.sort = name: 'Location'
$scope.threading = plugins.Threading
auth.getInitialUser().then(reset, ->
reset()
$scope.login()
)
class AnnotationViewerController
this.$inject = [
......
LOGIN_REQUEST = 'login'
LOGOUT_REQUEST = 'logout'
INITIAL_REQUEST = 'initial'
pendingRequests =
login: []
logout: []
initial: []
resolvePendingRequests = (requestType, user) ->
for request in pendingRequests[requestType]
request.resolve user if request.resolve?
pendingRequests[requestType] = []
rejectPendingRequests = (requestType, reason) ->
for request in pendingRequests[requestType]
request.reject reason if request.reject?
pendingRequests[requestType] = []
class Auth
_checkingToken: false
login: null
logout: null
getInitialUser: null
user: null
user: undefined
this.$inject = ['$location', '$q',
this.$inject = ['$location', '$rootScope',
'annotator', 'documentHelpers', 'identity']
constructor: ( $location, $q,
constructor: ( $location, $rootScope,
annotator, documentHelpers, identity) ->
{plugins} = annotator
@login = ->
oncancel = ->
rejectPendingRequests LOGIN_REQUEST, null
deferred = $q.defer()
pendingRequests[LOGIN_REQUEST].push deferred
identity.request({oncancel})
deferred.promise
@logout = ->
deferred = $q.defer()
identity.logout()
pendingRequests[LOGOUT_REQUEST].push deferred
deferred.promise
@getInitialUser = ->
deferred = $q.defer()
pendingRequests[INITIAL_REQUEST].push deferred
deferred.promise
_checkingToken = false
onlogin = (assertion) =>
@_checkingToken = true
_checkingToken = true
# Configure the Auth plugin with the issued assertion as refresh token.
annotator.addPlugin 'Auth',
......@@ -65,13 +18,12 @@ class Auth
# Set the user from the token.
plugins.Auth.withToken (token) =>
@_checkingToken = false
_checkingToken = false
annotator.addPlugin 'Permissions',
user: token.userId
userAuthorize: @permits
@user = token.userId
resolvePendingRequests INITIAL_REQUEST, @user
resolvePendingRequests LOGIN_REQUEST, @user
$rootScope.$apply()
onlogout = =>
plugins.Auth?.element.removeData('annotator:headers')
......@@ -83,13 +35,11 @@ class Auth
delete plugins.Permissions
@user = null
@_checkingToken = false
resolvePendingRequests LOGOUT_REQUEST, @user
_checkingToken = false
onready = =>
if not @_checkingToken
rejectPendingRequests INITIAL_REQUEST, null
if @user is undefined and not _checkingToken
@user = null
identity.watch {onlogin, onlogout, onready}
......
......@@ -7,6 +7,7 @@ describe 'h', ->
fakeLocation = null
fakeParams = null
fakeStreamer = null
fakeIdentity = null
sandbox = null
beforeEach module('h')
......@@ -21,6 +22,11 @@ describe 'h', ->
resolve()
}
fakeIdentity = {
watch: sandbox.spy()
request: sandbox.spy()
}
fakeAnnotator = {
plugins: {
Auth: {withToken: sandbox.spy()}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment