Commit b040f21f authored by Gergely Ujvari's avatar Gergely Ujvari

Simplify auth service

parent 9e4a3a45
...@@ -11,6 +11,9 @@ class AppController ...@@ -11,6 +11,9 @@ class AppController
) -> ) ->
{plugins, host, providers} = annotator {plugins, host, providers} = annotator
$scope.auth = auth
isFirstRun = $location.search().hasOwnProperty('firstrun')
applyUpdates = (action, data) -> applyUpdates = (action, data) ->
"""Update the application with new data from the websocket.""" """Update the application with new data from the websocket."""
return unless data?.length return unless data?.length
...@@ -103,24 +106,6 @@ class AppController ...@@ -103,24 +106,6 @@ class AppController
annotator.deleteAnnotation first annotator.deleteAnnotation first
$timeout -> cleanup rest $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 = -> oncancel = ->
$scope.dialog.visible = false $scope.dialog.visible = false
...@@ -154,7 +139,7 @@ class AppController ...@@ -154,7 +139,7 @@ class AppController
$scope.sort = {name, predicate} $scope.sort = {name, predicate}
$scope.$watch 'store.entities', (entities, oldEntities) -> $scope.$watch 'store.entities', (entities, oldEntities) ->
return if entities is oldEntities or not entities return if entities is oldEntities
if entities.length if entities.length
streamfilter streamfilter
...@@ -163,14 +148,18 @@ class AppController ...@@ -163,14 +148,18 @@ class AppController
streamer.send({filter: streamfilter.getFilter()}) 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.login = ->
$scope.dialog.visible = true $scope.dialog.visible = true
auth.login().then(reset, oncancel) identity.request(oncancel)
$scope.logout = -> $scope.logout = ->
return unless drafts.discard() return unless drafts.discard()
$scope.dialog.visible = false $scope.dialog.visible = false
auth.logout().then(reset) identity.logout()
$scope.loadMore = (number) -> $scope.loadMore = (number) ->
unless streamfilter.getPastData().hits then return unless streamfilter.getPastData().hits then return
...@@ -199,11 +188,6 @@ class AppController ...@@ -199,11 +188,6 @@ class AppController
$scope.sort = name: 'Location' $scope.sort = name: 'Location'
$scope.threading = plugins.Threading $scope.threading = plugins.Threading
auth.getInitialUser().then(reset, ->
reset()
$scope.login()
)
class AnnotationViewerController class AnnotationViewerController
this.$inject = [ 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 class Auth
_checkingToken: false user: undefined
login: null
logout: null
getInitialUser: null
user: null
this.$inject = ['$location', '$q', this.$inject = ['$location', '$rootScope',
'annotator', 'documentHelpers', 'identity'] 'annotator', 'documentHelpers', 'identity']
constructor: ( $location, $q, constructor: ( $location, $rootScope,
annotator, documentHelpers, identity) -> annotator, documentHelpers, identity) ->
{plugins} = annotator {plugins} = annotator
_checkingToken = false
@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
onlogin = (assertion) => onlogin = (assertion) =>
@_checkingToken = true _checkingToken = true
# Configure the Auth plugin with the issued assertion as refresh token. # Configure the Auth plugin with the issued assertion as refresh token.
annotator.addPlugin 'Auth', annotator.addPlugin 'Auth',
...@@ -65,13 +18,12 @@ class Auth ...@@ -65,13 +18,12 @@ class Auth
# Set the user from the token. # Set the user from the token.
plugins.Auth.withToken (token) => plugins.Auth.withToken (token) =>
@_checkingToken = false _checkingToken = false
annotator.addPlugin 'Permissions', annotator.addPlugin 'Permissions',
user: token.userId user: token.userId
userAuthorize: @permits userAuthorize: @permits
@user = token.userId @user = token.userId
resolvePendingRequests INITIAL_REQUEST, @user $rootScope.$apply()
resolvePendingRequests LOGIN_REQUEST, @user
onlogout = => onlogout = =>
plugins.Auth?.element.removeData('annotator:headers') plugins.Auth?.element.removeData('annotator:headers')
...@@ -83,13 +35,11 @@ class Auth ...@@ -83,13 +35,11 @@ class Auth
delete plugins.Permissions delete plugins.Permissions
@user = null @user = null
@_checkingToken = false _checkingToken = false
resolvePendingRequests LOGOUT_REQUEST, @user
onready = => onready = =>
if not @_checkingToken if @user is undefined and not _checkingToken
rejectPendingRequests INITIAL_REQUEST, null @user = null
identity.watch {onlogin, onlogout, onready} identity.watch {onlogin, onlogout, onready}
......
...@@ -7,6 +7,7 @@ describe 'h', -> ...@@ -7,6 +7,7 @@ describe 'h', ->
fakeLocation = null fakeLocation = null
fakeParams = null fakeParams = null
fakeStreamer = null fakeStreamer = null
fakeIdentity = null
sandbox = null sandbox = null
beforeEach module('h') beforeEach module('h')
...@@ -21,6 +22,11 @@ describe 'h', -> ...@@ -21,6 +22,11 @@ describe 'h', ->
resolve() resolve()
} }
fakeIdentity = {
watch: sandbox.spy()
request: sandbox.spy()
}
fakeAnnotator = { fakeAnnotator = {
plugins: { plugins: {
Auth: {withToken: sandbox.spy()} 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