refactor app/auth stuff
This commit is contained in:
parent
e647a7527d
commit
d872c6ae5d
1
Gemfile
1
Gemfile
|
@ -9,6 +9,7 @@ gem 'sinatra'
|
|||
gem 'sinatra-contrib'
|
||||
gem 'yard-sinatra', github: 'rkh/yard-sinatra'
|
||||
|
||||
gem 'bunny'
|
||||
gem 'pg', '~> 0.13.2'
|
||||
gem 'newrelic_rpm', '~> 3.3.0'
|
||||
gem 'hubble', git: 'git://github.com/roidrage/hubble'
|
||||
|
|
|
@ -15,14 +15,15 @@ GIT
|
|||
|
||||
GIT
|
||||
remote: git://github.com/roidrage/hubble
|
||||
revision: 5220415d5542a2868d54f7be9f35fc1d66126b8e
|
||||
revision: 8972b940a4f927927d2a4bdb250b3c98c04692a6
|
||||
specs:
|
||||
hubble (0.1.2)
|
||||
faraday
|
||||
json (~> 1.6.5)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/travis-ci/travis-api.git
|
||||
revision: 5d908480c7170a84aebb80b0d0a0398295a207f2
|
||||
revision: d887f01e0b27bb2936bcfd929258eec92b76a9b2
|
||||
branch: sf-use-services
|
||||
specs:
|
||||
travis-api (0.0.1)
|
||||
|
@ -63,7 +64,7 @@ GIT
|
|||
|
||||
GIT
|
||||
remote: git://github.com/travis-ci/travis-support.git
|
||||
revision: 268dc0ff74b5a559e06f637e0814af983e60919e
|
||||
revision: 06844d2db558d88be775ca1cf9cfff8ec36120fb
|
||||
specs:
|
||||
travis-support (0.0.1)
|
||||
|
||||
|
@ -108,6 +109,7 @@ GEM
|
|||
avl_tree (1.1.3)
|
||||
backports (2.6.4)
|
||||
builder (3.0.3)
|
||||
bunny (0.8.0)
|
||||
chunky_png (1.2.6)
|
||||
coffee-script (2.2.0)
|
||||
coffee-script-source
|
||||
|
@ -237,6 +239,7 @@ PLATFORMS
|
|||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
bunny
|
||||
coffee-script
|
||||
compass
|
||||
guard
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'travis' # hrm.
|
||||
require 'auth'
|
||||
require 'controllers'
|
||||
require 'helpers'
|
||||
require 'models'
|
||||
|
@ -11,7 +12,6 @@ require 'views'
|
|||
|
||||
require 'config/locales'
|
||||
require 'data/sponsors'
|
||||
require 'travis/auth'
|
||||
|
||||
# $.mockjaxSettings.log = false
|
||||
# Ember.LOG_BINDINGS = true
|
||||
|
@ -20,6 +20,10 @@ require 'travis/auth'
|
|||
|
||||
Travis.reopen
|
||||
App: Em.Application.extend
|
||||
currentUserBinding: 'auth.user'
|
||||
accessTokenBinding: 'auth.user.accessToken'
|
||||
authStateBinding: 'auth.state'
|
||||
|
||||
init: ->
|
||||
@_super()
|
||||
@connect()
|
||||
|
@ -27,40 +31,18 @@ Travis.reopen
|
|||
@store = Travis.Store.create()
|
||||
@store.loadMany(Travis.Sponsor, Travis.SPONSORS)
|
||||
|
||||
@set('auth', Travis.Auth.create(store: @store, endpoint: Travis.config.api_endpoint))
|
||||
|
||||
@routes = new Travis.Routes()
|
||||
@pusher = new Travis.Pusher()
|
||||
@tailing = new Travis.Tailing()
|
||||
|
||||
@loadUser()
|
||||
|
||||
loadUser: ->
|
||||
user = sessionStorage?.getItem("travisUser")
|
||||
if user
|
||||
@setCurrentUser JSON.parse(user)
|
||||
else if localStorage?.getItem("travisTrySignIn")
|
||||
Travis.Auth.trySignIn()
|
||||
|
||||
signIn: ->
|
||||
@set('signingIn', true)
|
||||
Travis.Auth.signIn()
|
||||
# TODO: this has to mov, no?
|
||||
@render.apply(this, @get('returnTo') || ['home', 'index'])
|
||||
@get('auth').signIn()
|
||||
|
||||
signOut: ->
|
||||
Travis.config.access_token = null
|
||||
localStorage?.clear()
|
||||
sessionStorage?.clear()
|
||||
@setCurrentUser()
|
||||
|
||||
setCurrentUser: (data) ->
|
||||
data = JSON.parse(data) if typeof data == 'string'
|
||||
if data
|
||||
localStorage?.setItem("travisTrySignIn", "true")
|
||||
sessionStorage?.setItem("travisUser", JSON.stringify(data))
|
||||
@store.load(Travis.User, data.user)
|
||||
@store.loadMany(Travis.Account, data.accounts)
|
||||
@set('currentUser', if data then Travis.User.find(data.user.id) else undefined)
|
||||
@set('signingIn', false)
|
||||
@get('auth').signOut()
|
||||
@routes.route('')
|
||||
|
||||
render: (name, action, params) ->
|
||||
layout = @connectLayout(name)
|
||||
|
|
63
assets/javascripts/app/auth.coffee
Normal file
63
assets/javascripts/app/auth.coffee
Normal file
|
@ -0,0 +1,63 @@
|
|||
@Travis.Auth = Ember.Object.extend
|
||||
iframe: $('<iframe id="auth-frame" />').hide()
|
||||
timeout: 5000
|
||||
state: 'signed-out'
|
||||
|
||||
init: ->
|
||||
@iframe.appendTo('body')
|
||||
window.addEventListener('message', (e) => @receiveMessage(e))
|
||||
Ember.run.next(this, @loadUser)
|
||||
|
||||
# if the user is in the session storage, we're using it. if we have a flag
|
||||
# for auto signin then we're trying to sign in.
|
||||
loadUser: ->
|
||||
if user = sessionStorage?.getItem('travis.user')
|
||||
@setUser(user)
|
||||
else if localStorage?.getItem('travis.auto_signin')
|
||||
@trySignIn()
|
||||
|
||||
# try signing in, but check later in case we have a timeout
|
||||
signIn: ->
|
||||
@set('state', 'signing-in')
|
||||
@trySignIn()
|
||||
Ember.run.later(this, @checkSignIn.bind(this), @timeout)
|
||||
|
||||
trySignIn: ->
|
||||
@iframe.attr('src', "#{@endpoint}/auth/post_message")
|
||||
|
||||
checkSignIn: ->
|
||||
@forceSignIn() if @get('state') == 'signing-in'
|
||||
|
||||
forceSignIn: ->
|
||||
localStorage?.setItem('travis.auto_signin', 'true')
|
||||
window.location = "#{@endpoint}/auth/handshake?redirect_uri=#{location}"
|
||||
|
||||
signOut: ->
|
||||
localStorage?.clear()
|
||||
sessionStorage?.clear()
|
||||
@setUser()
|
||||
|
||||
|
||||
setUser: (data) ->
|
||||
data = JSON.parse(data) if typeof data == 'string'
|
||||
user = @storeUser(data) if data
|
||||
@set('state', if user then 'signed-in' else 'signed-out')
|
||||
@set('user', if user then user else undefined)
|
||||
|
||||
storeUser: (data) ->
|
||||
localStorage?.setItem('travis.auto_signin', 'true')
|
||||
sessionStorage?.setItem('travis.user', JSON.stringify(data))
|
||||
data.user.access_token = data.token # TODO why's the access_token not set on the user?
|
||||
@store.load(Travis.User, data.user)
|
||||
@store.loadMany(Travis.Account, data.accounts)
|
||||
Travis.User.find(data.user.id)
|
||||
|
||||
receiveMessage: (event) ->
|
||||
if event.origin == @expectedOrigin()
|
||||
@setUser(event.data)
|
||||
console.log("signed in as #{event.data.user.login}")
|
||||
else
|
||||
console.log("unexpected message #{event.origin}: #{event.data}")
|
||||
|
||||
expectedOrigin: ->
|
||||
if @endpoint[0] == '/' then "#{location.protocol}//#{location.host}" else @endpoint
|
|
@ -1,15 +1,17 @@
|
|||
require 'travis/ajax'
|
||||
require 'travis/model'
|
||||
|
||||
@Travis.User = Travis.Model.extend
|
||||
name: DS.attr('string')
|
||||
email: DS.attr('string')
|
||||
login: DS.attr('string')
|
||||
token: DS.attr('string')
|
||||
locale: DS.attr('string')
|
||||
gravatarId: DS.attr('string')
|
||||
isSyncing: DS.attr('boolean')
|
||||
syncedAt: DS.attr('string')
|
||||
repoCount: DS.attr('number')
|
||||
@Travis.User = Travis.Model.extend Travis.Ajax,
|
||||
name: DS.attr('string')
|
||||
email: DS.attr('string')
|
||||
login: DS.attr('string')
|
||||
token: DS.attr('string')
|
||||
locale: DS.attr('string')
|
||||
gravatarId: DS.attr('string')
|
||||
isSyncing: DS.attr('boolean')
|
||||
syncedAt: DS.attr('string')
|
||||
repoCount: DS.attr('number')
|
||||
accessToken: DS.attr('string')
|
||||
|
||||
init: ->
|
||||
@poll() if @get('isSyncing')
|
||||
|
@ -28,16 +30,15 @@ require 'travis/model'
|
|||
).property()
|
||||
|
||||
sync: ->
|
||||
$.post('/api/profile/sync')
|
||||
@post('/profile/sync')
|
||||
@set('isSyncing', true)
|
||||
@poll()
|
||||
|
||||
poll: ->
|
||||
$.get('/api/profile', ((data) ->
|
||||
@ajax '/profile', 'get', success: (data) =>
|
||||
if data.user.is_syncing
|
||||
Ember.run.later(this, this.poll.bind(this), 3000)
|
||||
else if this.get('isSyncing')
|
||||
# TODO this doesn't seem to work properly
|
||||
Travis.app.store.load(Travis.User, data.user)
|
||||
Travis.app.store.loadMany(Travis.Account, data.accounts)
|
||||
).bind(this))
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
Travis.Routes = ->
|
||||
unless Travis.Routes.initialized
|
||||
Em.routes.set('usesHistory', true)
|
||||
Em.routes.set('wantsHistory', true)
|
||||
Em.routes.set('baseURI', @base_uri)
|
||||
Ember.run.next =>
|
||||
Em.routes.set('usesHistory', true)
|
||||
Em.routes.set('wantsHistory', true)
|
||||
Em.routes.set('baseURI', @base_uri)
|
||||
|
||||
@add(route, target[0], target[1]) for route, target of Travis.ROUTES
|
||||
Travis.Routes.initialized = true
|
||||
@add(route, target[0], target[1]) for route, target of Travis.ROUTES
|
||||
Travis.Routes.initialized = true
|
||||
|
||||
$.extend Travis.Routes.prototype,
|
||||
base_uri: "#{document.location.protocol}//#{document.location.host}"
|
||||
|
@ -14,8 +15,9 @@ $.extend Travis.Routes.prototype,
|
|||
Em.routes.add route, (params) =>
|
||||
@action(layout, action, params)
|
||||
|
||||
route: (event) ->
|
||||
Em.routes.set('location', $(event.target).closest('a')[0].href.replace("#{@base_uri}/", ''))
|
||||
route: (location) ->
|
||||
location = $(event.target).closest('a')[0].href.replace("#{@base_uri}/", '') if typeof location != 'string'
|
||||
Em.routes.set('location', location)
|
||||
|
||||
action: (name, action, params) ->
|
||||
# this needs to be a global reference because Em.routes is global
|
||||
|
@ -23,17 +25,18 @@ $.extend Travis.Routes.prototype,
|
|||
|
||||
before: (name, action, params) ->
|
||||
if @requiresAuth(name, action, params)
|
||||
true
|
||||
else
|
||||
@requireAuth(name, action, params)
|
||||
else
|
||||
true
|
||||
|
||||
signedIn: ->
|
||||
!!Travis.app.get('currentUser')
|
||||
|
||||
requiresAuth: (name, action, params) ->
|
||||
name != 'profile' || @signedIn()
|
||||
name == 'profile' and not @signedIn()
|
||||
|
||||
requireAuth: (name, action, params) ->
|
||||
Travis.app.set('returnTo', [name, action, params])
|
||||
Travis.app.render('auth', 'show')
|
||||
# Travis.app.render('auth', 'show')
|
||||
@route('')
|
||||
false
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
require 'travis/ajax'
|
||||
require 'models'
|
||||
|
||||
jQuery.support.cors = true
|
||||
|
||||
@Travis.RestAdapter = DS.RESTAdapter.extend
|
||||
DEFAULT_OPTIONS:
|
||||
accepts:
|
||||
json: 'application/vnd.travis-ci.2+json'
|
||||
|
||||
@Travis.RestAdapter = DS.RESTAdapter.extend Travis.Ajax,
|
||||
mappings:
|
||||
repositories: Travis.Repository
|
||||
repository: Travis.Repository
|
||||
|
@ -27,10 +22,3 @@ jQuery.support.cors = true
|
|||
branch: 'branches'
|
||||
job: 'jobs'
|
||||
worker: 'workers'
|
||||
|
||||
ajax: (url, method, options) ->
|
||||
endpoint = Travis.config.api_endpoint || ''
|
||||
if Travis.config.access_token
|
||||
options.headers ||= {}
|
||||
options.headers['Authorization'] ||= "token #{Travis.config.access_token}"
|
||||
@_super("#{endpoint}#{url}", method, $.extend(options, @DEFAULT_OPTIONS))
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
</li>
|
||||
<li {{bindAttr class="view.classProfile"}}>
|
||||
<p class="handle">
|
||||
<a class="signed-in" href="#" class="name"><img {{bindAttr src="view.gravatarUrl"}}>{{user.name}}</a>
|
||||
<a class="sign-in" href="#" {{action signIn target="Travis.app"}}>{{t layouts.top.github_login}}</a>
|
||||
<a class="signed-out" href="#" {{action signIn target="Travis.app"}}>{{t layouts.top.github_login}}</a>
|
||||
<a class="signed-in" href="#" class="name"><img {{bindAttr src="view.gravatarUrl"}}>{{user.name}}</a>
|
||||
<span class="signing-in">Signing in</span>
|
||||
</p>
|
||||
<ul>
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
require 'travis/auth'
|
||||
|
||||
@Travis.reopen
|
||||
TopView: Travis.View.extend
|
||||
templateName: 'layouts/top'
|
||||
|
@ -23,14 +21,9 @@ require 'travis/auth'
|
|||
classProfile: (->
|
||||
classes = ['profile']
|
||||
classes.push('active') if @get('tab') == 'profile'
|
||||
if Travis.app.get('currentUser')
|
||||
classes.push('signed-in')
|
||||
else if Travis.app.get('signingIn')
|
||||
classes.push('signing-in')
|
||||
else
|
||||
classes.push('sign-in')
|
||||
classes.push(Travis.app.get('authState'))
|
||||
classes.join(' ')
|
||||
).property('tab', 'Travis.app.currentUser', 'Travis.app.signingIn')
|
||||
).property('tab', 'Travis.app.authState')
|
||||
|
||||
showProfile: ->
|
||||
$('#top .profile ul').show()
|
||||
|
|
28
assets/javascripts/lib/travis/ajax.coffee
Normal file
28
assets/javascripts/lib/travis/ajax.coffee
Normal file
|
@ -0,0 +1,28 @@
|
|||
jQuery.support.cors = true
|
||||
|
||||
@Travis.Ajax = Ember.Mixin.create
|
||||
DEFAULT_OPTIONS:
|
||||
accepts:
|
||||
json: 'application/vnd.travis-ci.2+json'
|
||||
|
||||
post: (url, data, callback) ->
|
||||
@ajax(url, 'post', data: data, success: callback)
|
||||
|
||||
ajax: (url, method, options) ->
|
||||
endpoint = Travis.config.api_endpoint || ''
|
||||
options = options || {}
|
||||
|
||||
if access_token = Travis.app.get('accessToken')
|
||||
options.headers ||= {}
|
||||
options.headers['Authorization'] ||= "token #{access_token}"
|
||||
|
||||
options.url = "#{endpoint}#{url}"
|
||||
options.type = method
|
||||
options.dataType = 'json'
|
||||
options.contentType = 'application/json; charset=utf-8'
|
||||
options.context = this
|
||||
|
||||
if options.data && method != 'GET'
|
||||
options.data = JSON.stringify(options.data)
|
||||
|
||||
$.ajax($.extend(options, @DEFAULT_OPTIONS))
|
|
@ -1,53 +0,0 @@
|
|||
@Travis.Auth = ->
|
||||
$ =>
|
||||
@iframe.appendTo('body')
|
||||
window.addEventListener "message", (e) => @receiveMessage(e)
|
||||
this
|
||||
|
||||
$.extend Travis.Auth,
|
||||
instance: new Travis.Auth()
|
||||
|
||||
signIn: ->
|
||||
@instance.signIn()
|
||||
|
||||
trySignIn: ->
|
||||
@instance.trySignIn()
|
||||
|
||||
$.extend Travis.Auth.prototype,
|
||||
iframe: $('<iframe id="auth-frame" />').hide()
|
||||
timeout: 10000
|
||||
|
||||
expectedOrigin: ->
|
||||
if Travis.config.api_endpoint[0] == '/'
|
||||
window.location.protocol + "//" + window.location.host
|
||||
else
|
||||
Travis.config.api_endpoint
|
||||
|
||||
receiveMessage: (event) ->
|
||||
if event.origin != @expectedOrigin()
|
||||
console.log("unexpected message #{event.origin}: #{event.data}")
|
||||
else
|
||||
Travis.config.access_token = event.data.token
|
||||
Travis.app.setCurrentUser(event.data)
|
||||
console.log("signed in as #{event.data.user.login}")
|
||||
|
||||
trySignIn: ->
|
||||
@iframe.attr('src', "#{Travis.config.api_endpoint}/auth/post_message")
|
||||
|
||||
# # TODO: use views
|
||||
# link = $("#navigation .profile")
|
||||
# html = link.html()
|
||||
# link.html("Signing in with GitHub...")
|
||||
# window.setTimeout((-> link.html(html)), @timeout)
|
||||
|
||||
newUser: ->
|
||||
localStorage?.setItem("travisTrySignIn", "true")
|
||||
url = "#{Travis.config.api_endpoint}/auth/handshake?redirect_uri=#{window.location}"
|
||||
window.location = url
|
||||
|
||||
checkUser: ->
|
||||
@newUser() unless Travis.config.access_token?
|
||||
|
||||
signIn: ->
|
||||
@trySignIn()
|
||||
window.setTimeout((=> @checkUser()), @timeout)
|
|
@ -37,5 +37,3 @@ require 'ext/ember/namespace'
|
|||
console.log "Connecting to #{Travis.config.api_endpoint}"
|
||||
@app = Travis.App.create(attrs || {})
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -43,13 +43,13 @@
|
|||
.handle
|
||||
margin: 0
|
||||
|
||||
.signed-in, .signing-in, .sign-in
|
||||
.signed-out, .signing-in, .signed-in
|
||||
display: none
|
||||
&.signed-in .signed-in
|
||||
&.signed-out .signed-out
|
||||
display: block
|
||||
&.signing-in .signing-in
|
||||
display: inline-block
|
||||
&.sign-in .sign-in
|
||||
&.signed-in .signed-in
|
||||
display: block
|
||||
|
||||
.signed-in
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -6628,11 +6628,11 @@ table.list .red .number a {
|
|||
margin: 0;
|
||||
}
|
||||
/* line 46, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||
#top .profile .signed-in, #top .profile .signing-in, #top .profile .sign-in {
|
||||
#top .profile .signed-out, #top .profile .signing-in, #top .profile .signed-in {
|
||||
display: none;
|
||||
}
|
||||
/* line 48, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||
#top .profile.signed-in .signed-in {
|
||||
#top .profile.signed-out .signed-out {
|
||||
display: block;
|
||||
}
|
||||
/* line 50, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||
|
@ -6640,7 +6640,7 @@ table.list .red .number a {
|
|||
display: inline-block;
|
||||
}
|
||||
/* line 52, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||
#top .profile.sign-in .sign-in {
|
||||
#top .profile.signed-in .signed-in {
|
||||
display: block;
|
||||
}
|
||||
/* line 55, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||
|
|
Loading…
Reference in New Issue
Block a user