Small controller for generating an auth token, if you are an authable user

This commit is contained in:
Raphael Sofaer 2011-05-19 18:29:53 -07:00
parent ed61c53e09
commit 93be5497a7
11 changed files with 78 additions and 36 deletions

View file

@ -29,12 +29,6 @@ class AdminsController < ApplicationController
redirect_to :back, :notice => notice, :user => {:id => u.id} redirect_to :back, :notice => notice, :user => {:id => u.id}
end end
def generate_new_token
current_user.reset_authentication_token!
current_user.authentication_token
redirect_to user_search_path, :notice => "auth token reset"
end
def admin_inviter def admin_inviter
opts = {:service => 'email', :identifier => params[:identifier]} opts = {:service => 'email', :identifier => params[:identifier]}
existing_user = Invitation.find_existing_user('email', params[:identifier]) existing_user = Invitation.find_existing_user('email', params[:identifier])

View file

@ -0,0 +1,12 @@
class TokensController < ApplicationController
before_filter :redirect_unless_tokenable
def redirect_unless_tokenable
redirect_to root_url unless current_user.auth_tokenable?
end
def create
current_user.reset_authentication_token!
current_user.authentication_token
redirect_to token_path, :notice => "Authentication token reset."
end
end

View file

@ -326,6 +326,10 @@ class User < ActiveRecord::Base
AppConfig[:admins].present? && AppConfig[:admins].include?(self.username) AppConfig[:admins].present? && AppConfig[:admins].include?(self.username)
end end
def auth_tokenable?
admin? || (AppConfig[:auth_tokenable].present? && AppConfig[:auth_tokenable].include?(self.username))
end
protected protected
def remove_person def remove_person

View file

@ -41,9 +41,6 @@
%br %br
%br %br
%br %br
%h3 your auth token
%h2= current_user.authentication_token
= link_to "reset auth token", new_auth_token_path
%br %br
= javascript_include_tag 'apiconsole' = javascript_include_tag 'apiconsole'
#query #query

View file

@ -22,6 +22,8 @@
%h4.section.invite_friends %h4.section.invite_friends
!= t('bookmarklet.explanation', :link => link_to(t('bookmarklet.explanation_link_text'), bookmarklet)) != t('bookmarklet.explanation', :link => link_to(t('bookmarklet.explanation_link_text'), bookmarklet))
- if current_user.auth_tokenable?
%h4.section.invite_friends= link_to "Generate an authentication token for Cubbi.es", token_path
- if @invites > 0 - if @invites > 0
.section.invite_friends .section.invite_friends
%h4= t('shared.invitations.invite_your_friends') %h4= t('shared.invitations.invite_your_friends')

View file

@ -0,0 +1,16 @@
%h3
This is a temporary hack while we develop a more general application framework.
%div
- if current_user.authentication_token
%h4= current_user.authentication_token
- else
%h4 No authentication token set.
%div
= form_tag(token_path) do
=submit_tag "Generate new authentication token"
%br
%div
%h4
Click settings on
= link_to "Cubbi.es", 'http://cubbi.es'
to share your internet folder with the internet!

View file

@ -87,6 +87,11 @@ default:
admins: admins:
- 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo' - 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo'
#List of users who can generate auth tokens
#Temporary so we can work on apps while oauth is being developed
auth_tokenable:
- 'iknowthatthismanualauthtokenthingisnoteasyorsecure'
#s3 config, if set, carrierwave will store your photos on s3 #s3 config, if set, carrierwave will store your photos on s3
#s3_key: 'key' #s3_key: 'key'
#s3_secret: 'secret' #s3_secret: 'secret'

View file

@ -67,6 +67,8 @@ Diaspora::Application.routes.draw do
resources :photos, :controller => "photos", :only => [:create, :show, :destroy] resources :photos, :controller => "photos", :only => [:create, :show, :destroy]
end end
#Temporary token_authenticable route
resource :token, :only => [:show, :create]
get 'login' => redirect('/users/sign_in') get 'login' => redirect('/users/sign_in')
@ -74,7 +76,6 @@ Diaspora::Application.routes.draw do
match 'user_search' => :user_search match 'user_search' => :user_search
get 'admin_inviter' => :admin_inviter get 'admin_inviter' => :admin_inviter
get 'add_invites' => :add_invites, :as => 'add_invites' get 'add_invites' => :add_invites, :as => 'add_invites'
get 'generate_new_token' => :generate_new_token, :as => 'new_auth_token'
end end
resource :profile resource :profile

View file

@ -23,7 +23,7 @@ class AppConfig
generate_pod_uri generate_pod_uri
normalize_pod_url normalize_pod_url
check_pod_uri check_pod_uri
downcase_admins downcase_usernames
end end
def self.load_config_for_environment(env) def self.load_config_for_environment(env)
@ -77,9 +77,11 @@ class AppConfig
end end
def self.downcase_admins def self.downcase_usernames
self.config_vars[:admins] ||= [] [:admins, :auth_tokenable].each do |key|
self.config_vars[:admins].collect! { |admin| admin.downcase } self.config_vars[key] ||= []
self.config_vars[key].collect! { |username| username.downcase }
end
end end
def self.load_config_yaml filename def self.load_config_yaml filename

View file

@ -59,24 +59,6 @@ describe AdminsController do
end end
end end
describe '#generate_new_token' do
before do
AppConfig[:admins] = [@user.username]
end
it 'generates a new token for the current user' do
lambda {
get 'generate_new_token'
}.should change{ @user.reload.authentication_token }
end
it 'displays a token' do
get 'generate_new_token'
get :user_search
response.body.should include(@user.reload.authentication_token)
end
end
describe '#admin_inviter' do describe '#admin_inviter' do
context 'admin signed in' do context 'admin signed in' do
before do before do

View file

@ -0,0 +1,27 @@
describe TokensController do
before do
AppConfig[:admins] = [bob.username]
AppConfig[:auth_tokenable] = [eve.username]
end
describe '#create' do
it 'generates a new token for the current user' do
sign_in bob
lambda {
get :create
}.should change{ bob.reload.authentication_token }
end
it 'redirects normal users away' do
sign_in alice
get :create
response.should redirect_to root_url
end
end
describe '#edit' do
it 'displays a token' do
sign_in bob
get :create
get :show
response.body.should include(bob.reload.authentication_token)
end
end
end