Add basic Persona functionality

1. No session integration yet, so automatic login/logout events are suppressed.

2. Popup blockers must be disabled: submits form to target="_blank"
This commit is contained in:
Dan Callahan 2013-03-01 09:23:21 -06:00
parent 81c545539e
commit ef8cf2f734
4 changed files with 64 additions and 0 deletions

View File

@ -109,6 +109,10 @@ Discourse.LoginView = Discourse.ModalBodyView.extend({
return window.open("/auth/github", "_blank", "menubar=no,status=no,height=400,width=800,left=" + left + ",top=" + top); return window.open("/auth/github", "_blank", "menubar=no,status=no,height=400,width=800,left=" + left + ",top=" + top);
}, },
personaLogin: function() {
navigator.id.request();
},
authenticationComplete: function(options) { authenticationComplete: function(options) {
if (options.awaiting_approval) { if (options.awaiting_approval) {
this.flash(Em.String.i18n('login.awaiting_approval'), 'success'); this.flash(Em.String.i18n('login.awaiting_approval'), 'success');

View File

@ -21,6 +21,8 @@ class Users::OmniauthCallbacksController < ApplicationController
create_or_sign_on_user_using_openid(auth_token) create_or_sign_on_user_using_openid(auth_token)
when "github" when "github"
create_or_sign_on_user_using_github(auth_token) create_or_sign_on_user_using_github(auth_token)
when "persona"
create_or_sign_on_user_using_persona(auth_token)
end end
end end
@ -169,6 +171,7 @@ class Users::OmniauthCallbacksController < ApplicationController
openid_url: identity_url openid_url: identity_url
} }
end end
end end
def create_or_sign_on_user_using_github(auth_token) def create_or_sign_on_user_using_github(auth_token)
@ -200,6 +203,37 @@ class Users::OmniauthCallbacksController < ApplicationController
else else
@data[:name] = screen_name @data[:name] = screen_name
end end
end
def create_or_sign_on_user_using_persona(auth_token)
email = auth_token[:info][:email]
user = User.find_by_email(email)
if user
if SiteSetting.must_approve_users? and !user.approved?
@data = {awaiting_approval: true}
else
log_on_user(user)
@data = {authenticated: true}
end
else
@data = {
email: email,
email_valid: true,
name: User.suggest_name(email),
username: User.suggest_username(email),
auth_provider: params[:provider].try(:capitalize)
}
session[:authentication] = {
email: email,
email_valid: true,
}
end
end end
end end

View File

@ -0,0 +1,25 @@
<script src="https://login.persona.org/include.js"></script>
<form id="persona_assertion_form" action="/auth/persona/callback" method="post" target="_blank">
<input type='hidden' name='assertion'/>
</form>
<script>
(function() {
var readyCalled = false;
navigator.id.watch({
onlogin: function(assertion) {
if (readyCalled) {
$('#persona_assertion_form input[name=assertion]').val(assertion);
$('#persona_assertion_form').submit();
}
},
onlogout: function() {
if (readyCalled) {
Discourse.logout();
}
},
onready: function() {
readyCalled = true;
}
});
}());
</script>

View File

@ -58,6 +58,7 @@
<footer id='bottom'></footer> <footer id='bottom'></footer>
<%= render :partial => "common/discourse_javascript" %> <%= render :partial => "common/discourse_javascript" %>
<%= render :partial => "common/persona_javascript" %>
<%= render_google_analytics_code %> <%= render_google_analytics_code %>
<!-- Discourse Version: <%= Discourse::VERSION::STRING %> --> <!-- Discourse Version: <%= Discourse::VERSION::STRING %> -->